iscsi_tcp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. return r;
  220. }
  221. copied += r;
  222. }
  223. return copied;
  224. }
  225. /**
  226. * iscsi_sw_tcp_xmit - TCP transmit
  227. **/
  228. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  229. {
  230. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  231. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  232. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  233. unsigned int consumed = 0;
  234. int rc = 0;
  235. while (1) {
  236. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  237. /*
  238. * We may not have been able to send data because the conn
  239. * is getting stopped. libiscsi will know so propogate err
  240. * for it to do the right thing.
  241. */
  242. if (rc == -EAGAIN)
  243. return rc;
  244. else if (rc < 0) {
  245. rc = ISCSI_ERR_XMIT_FAILED;
  246. goto error;
  247. } else if (rc == 0)
  248. break;
  249. consumed += rc;
  250. if (segment->total_copied >= segment->total_size) {
  251. if (segment->done != NULL) {
  252. rc = segment->done(tcp_conn, segment);
  253. if (rc != 0)
  254. goto error;
  255. }
  256. }
  257. }
  258. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  259. conn->txdata_octets += consumed;
  260. return consumed;
  261. error:
  262. /* Transmit error. We could initiate error recovery
  263. * here. */
  264. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  265. iscsi_conn_failure(conn, rc);
  266. return -EIO;
  267. }
  268. /**
  269. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  270. */
  271. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  272. {
  273. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  274. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  275. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  276. return segment->total_copied - segment->total_size;
  277. }
  278. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  279. {
  280. struct iscsi_conn *conn = task->conn;
  281. int rc;
  282. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  283. rc = iscsi_sw_tcp_xmit(conn);
  284. if (rc == 0)
  285. return -EAGAIN;
  286. if (rc < 0)
  287. return rc;
  288. }
  289. return 0;
  290. }
  291. /*
  292. * This is called when we're done sending the header.
  293. * Simply copy the data_segment to the send segment, and return.
  294. */
  295. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  296. struct iscsi_segment *segment)
  297. {
  298. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  299. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  300. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  301. "Header done. Next segment size %u total_size %u\n",
  302. tcp_sw_conn->out.segment.size,
  303. tcp_sw_conn->out.segment.total_size);
  304. return 0;
  305. }
  306. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  307. size_t hdrlen)
  308. {
  309. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  310. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  311. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  312. "digest enabled" : "digest disabled");
  313. /* Clear the data segment - needs to be filled in by the
  314. * caller using iscsi_tcp_send_data_prep() */
  315. memset(&tcp_sw_conn->out.data_segment, 0,
  316. sizeof(struct iscsi_segment));
  317. /* If header digest is enabled, compute the CRC and
  318. * place the digest into the same buffer. We make
  319. * sure that both iscsi_tcp_task and mtask have
  320. * sufficient room.
  321. */
  322. if (conn->hdrdgst_en) {
  323. iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
  324. hdr + hdrlen);
  325. hdrlen += ISCSI_DIGEST_SIZE;
  326. }
  327. /* Remember header pointer for later, when we need
  328. * to decide whether there's a payload to go along
  329. * with the header. */
  330. tcp_sw_conn->out.hdr = hdr;
  331. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  332. iscsi_sw_tcp_send_hdr_done, NULL);
  333. }
  334. /*
  335. * Prepare the send buffer for the payload data.
  336. * Padding and checksumming will all be taken care
  337. * of by the iscsi_segment routines.
  338. */
  339. static int
  340. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  341. unsigned int count, unsigned int offset,
  342. unsigned int len)
  343. {
  344. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  345. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  346. struct hash_desc *tx_hash = NULL;
  347. unsigned int hdr_spec_len;
  348. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  349. conn->datadgst_en ?
  350. "digest enabled" : "digest disabled");
  351. /* Make sure the datalen matches what the caller
  352. said he would send. */
  353. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  354. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  355. if (conn->datadgst_en)
  356. tx_hash = &tcp_sw_conn->tx_hash;
  357. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  358. sg, count, offset, len,
  359. NULL, tx_hash);
  360. }
  361. static void
  362. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  363. size_t len)
  364. {
  365. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  366. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  367. struct hash_desc *tx_hash = NULL;
  368. unsigned int hdr_spec_len;
  369. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  370. "digest enabled" : "digest disabled");
  371. /* Make sure the datalen matches what the caller
  372. said he would send. */
  373. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  374. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  375. if (conn->datadgst_en)
  376. tx_hash = &tcp_sw_conn->tx_hash;
  377. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  378. data, len, NULL, tx_hash);
  379. }
  380. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  381. unsigned int offset, unsigned int count)
  382. {
  383. struct iscsi_conn *conn = task->conn;
  384. int err = 0;
  385. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  386. if (!count)
  387. return 0;
  388. if (!task->sc)
  389. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  390. else {
  391. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  392. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  393. sdb->table.nents, offset,
  394. count);
  395. }
  396. if (err) {
  397. /* got invalid offset/len */
  398. return -EIO;
  399. }
  400. return 0;
  401. }
  402. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  403. {
  404. struct iscsi_tcp_task *tcp_task = task->dd_data;
  405. task->hdr = task->dd_data + sizeof(*tcp_task);
  406. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  407. return 0;
  408. }
  409. static struct iscsi_cls_conn *
  410. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  411. uint32_t conn_idx)
  412. {
  413. struct iscsi_conn *conn;
  414. struct iscsi_cls_conn *cls_conn;
  415. struct iscsi_tcp_conn *tcp_conn;
  416. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  417. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  418. conn_idx);
  419. if (!cls_conn)
  420. return NULL;
  421. conn = cls_conn->dd_data;
  422. tcp_conn = conn->dd_data;
  423. tcp_sw_conn = tcp_conn->dd_data;
  424. tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  425. CRYPTO_ALG_ASYNC);
  426. tcp_sw_conn->tx_hash.flags = 0;
  427. if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
  428. goto free_conn;
  429. tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  430. CRYPTO_ALG_ASYNC);
  431. tcp_sw_conn->rx_hash.flags = 0;
  432. if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
  433. goto free_tx_tfm;
  434. tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
  435. return cls_conn;
  436. free_tx_tfm:
  437. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  438. free_conn:
  439. iscsi_conn_printk(KERN_ERR, conn,
  440. "Could not create connection due to crc32c "
  441. "loading error. Make sure the crc32c "
  442. "module is built as a module or into the "
  443. "kernel\n");
  444. iscsi_tcp_conn_teardown(cls_conn);
  445. return NULL;
  446. }
  447. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  448. {
  449. struct iscsi_session *session = conn->session;
  450. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  451. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  452. struct socket *sock = tcp_sw_conn->sock;
  453. if (!sock)
  454. return;
  455. sock_hold(sock->sk);
  456. iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
  457. sock_put(sock->sk);
  458. spin_lock_bh(&session->lock);
  459. tcp_sw_conn->sock = NULL;
  460. spin_unlock_bh(&session->lock);
  461. sockfd_put(sock);
  462. }
  463. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  464. {
  465. struct iscsi_conn *conn = cls_conn->dd_data;
  466. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  467. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  468. iscsi_sw_tcp_release_conn(conn);
  469. if (tcp_sw_conn->tx_hash.tfm)
  470. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  471. if (tcp_sw_conn->rx_hash.tfm)
  472. crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
  473. iscsi_tcp_conn_teardown(cls_conn);
  474. }
  475. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  476. {
  477. struct iscsi_conn *conn = cls_conn->dd_data;
  478. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  479. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  480. /* userspace may have goofed up and not bound us */
  481. if (!tcp_sw_conn->sock)
  482. return;
  483. /*
  484. * Make sure our recv side is stopped.
  485. * Older tools called conn stop before ep_disconnect
  486. * so IO could still be coming in.
  487. */
  488. write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
  489. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  490. write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
  491. iscsi_conn_stop(cls_conn, flag);
  492. iscsi_sw_tcp_release_conn(conn);
  493. }
  494. static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  495. char *buf, int *port,
  496. int (*getname)(struct socket *,
  497. struct sockaddr *,
  498. int *addrlen))
  499. {
  500. struct sockaddr_storage *addr;
  501. struct sockaddr_in6 *sin6;
  502. struct sockaddr_in *sin;
  503. int rc = 0, len;
  504. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  505. if (!addr)
  506. return -ENOMEM;
  507. if (getname(sock, (struct sockaddr *) addr, &len)) {
  508. rc = -ENODEV;
  509. goto free_addr;
  510. }
  511. switch (addr->ss_family) {
  512. case AF_INET:
  513. sin = (struct sockaddr_in *)addr;
  514. spin_lock_bh(&conn->session->lock);
  515. sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
  516. *port = be16_to_cpu(sin->sin_port);
  517. spin_unlock_bh(&conn->session->lock);
  518. break;
  519. case AF_INET6:
  520. sin6 = (struct sockaddr_in6 *)addr;
  521. spin_lock_bh(&conn->session->lock);
  522. sprintf(buf, "%pI6", &sin6->sin6_addr);
  523. *port = be16_to_cpu(sin6->sin6_port);
  524. spin_unlock_bh(&conn->session->lock);
  525. break;
  526. }
  527. free_addr:
  528. kfree(addr);
  529. return rc;
  530. }
  531. static int
  532. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  533. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  534. int is_leading)
  535. {
  536. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  537. struct iscsi_host *ihost = shost_priv(shost);
  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. /*
  552. * copy these values now because if we drop the session
  553. * userspace may still want to query the values since we will
  554. * be using them for the reconnect
  555. */
  556. err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
  557. &conn->portal_port, kernel_getpeername);
  558. if (err)
  559. goto free_socket;
  560. err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
  561. &ihost->local_port, kernel_getsockname);
  562. if (err)
  563. goto free_socket;
  564. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  565. if (err)
  566. goto free_socket;
  567. /* bind iSCSI connection and socket */
  568. tcp_sw_conn->sock = sock;
  569. /* setup Socket parameters */
  570. sk = sock->sk;
  571. sk->sk_reuse = 1;
  572. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  573. sk->sk_allocation = GFP_ATOMIC;
  574. iscsi_sw_tcp_conn_set_callbacks(conn);
  575. tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
  576. /*
  577. * set receive state machine into initial state
  578. */
  579. iscsi_tcp_hdr_recv_prep(tcp_conn);
  580. return 0;
  581. free_socket:
  582. sockfd_put(sock);
  583. return err;
  584. }
  585. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  586. enum iscsi_param param, char *buf,
  587. int buflen)
  588. {
  589. struct iscsi_conn *conn = cls_conn->dd_data;
  590. struct iscsi_session *session = conn->session;
  591. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  592. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  593. int value;
  594. switch(param) {
  595. case ISCSI_PARAM_HDRDGST_EN:
  596. iscsi_set_param(cls_conn, param, buf, buflen);
  597. break;
  598. case ISCSI_PARAM_DATADGST_EN:
  599. iscsi_set_param(cls_conn, param, buf, buflen);
  600. tcp_sw_conn->sendpage = conn->datadgst_en ?
  601. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  602. break;
  603. case ISCSI_PARAM_MAX_R2T:
  604. sscanf(buf, "%d", &value);
  605. if (value <= 0 || !is_power_of_2(value))
  606. return -EINVAL;
  607. if (session->max_r2t == value)
  608. break;
  609. iscsi_tcp_r2tpool_free(session);
  610. iscsi_set_param(cls_conn, param, buf, buflen);
  611. if (iscsi_tcp_r2tpool_alloc(session))
  612. return -ENOMEM;
  613. break;
  614. default:
  615. return iscsi_set_param(cls_conn, param, buf, buflen);
  616. }
  617. return 0;
  618. }
  619. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  620. enum iscsi_param param, char *buf)
  621. {
  622. struct iscsi_conn *conn = cls_conn->dd_data;
  623. int len;
  624. switch(param) {
  625. case ISCSI_PARAM_CONN_PORT:
  626. spin_lock_bh(&conn->session->lock);
  627. len = sprintf(buf, "%hu\n", conn->portal_port);
  628. spin_unlock_bh(&conn->session->lock);
  629. break;
  630. case ISCSI_PARAM_CONN_ADDRESS:
  631. spin_lock_bh(&conn->session->lock);
  632. len = sprintf(buf, "%s\n", conn->portal_address);
  633. spin_unlock_bh(&conn->session->lock);
  634. break;
  635. default:
  636. return iscsi_conn_get_param(cls_conn, param, buf);
  637. }
  638. return len;
  639. }
  640. static void
  641. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  642. struct iscsi_stats *stats)
  643. {
  644. struct iscsi_conn *conn = cls_conn->dd_data;
  645. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  646. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  647. stats->custom_length = 3;
  648. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  649. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  650. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  651. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  652. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  653. stats->custom[2].value = conn->eh_abort_cnt;
  654. iscsi_tcp_conn_get_stats(cls_conn, stats);
  655. }
  656. static struct iscsi_cls_session *
  657. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  658. uint16_t qdepth, uint32_t initial_cmdsn)
  659. {
  660. struct iscsi_cls_session *cls_session;
  661. struct iscsi_session *session;
  662. struct Scsi_Host *shost;
  663. if (ep) {
  664. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  665. return NULL;
  666. }
  667. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
  668. if (!shost)
  669. return NULL;
  670. shost->transportt = iscsi_sw_tcp_scsi_transport;
  671. shost->cmd_per_lun = qdepth;
  672. shost->max_lun = iscsi_max_lun;
  673. shost->max_id = 0;
  674. shost->max_channel = 0;
  675. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  676. if (iscsi_host_add(shost, NULL))
  677. goto free_host;
  678. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  679. cmds_max,
  680. sizeof(struct iscsi_tcp_task) +
  681. sizeof(struct iscsi_sw_tcp_hdrbuf),
  682. initial_cmdsn, 0);
  683. if (!cls_session)
  684. goto remove_host;
  685. session = cls_session->dd_data;
  686. shost->can_queue = session->scsi_cmds_max;
  687. if (iscsi_tcp_r2tpool_alloc(session))
  688. goto remove_session;
  689. return cls_session;
  690. remove_session:
  691. iscsi_session_teardown(cls_session);
  692. remove_host:
  693. iscsi_host_remove(shost);
  694. free_host:
  695. iscsi_host_free(shost);
  696. return NULL;
  697. }
  698. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  699. {
  700. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  701. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  702. iscsi_session_teardown(cls_session);
  703. iscsi_host_remove(shost);
  704. iscsi_host_free(shost);
  705. }
  706. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  707. {
  708. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  709. return 0;
  710. }
  711. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  712. {
  713. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  714. blk_queue_dma_alignment(sdev->request_queue, 0);
  715. return 0;
  716. }
  717. static struct scsi_host_template iscsi_sw_tcp_sht = {
  718. .module = THIS_MODULE,
  719. .name = "iSCSI Initiator over TCP/IP",
  720. .queuecommand = iscsi_queuecommand,
  721. .change_queue_depth = iscsi_change_queue_depth,
  722. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  723. .sg_tablesize = 4096,
  724. .max_sectors = 0xFFFF,
  725. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  726. .eh_abort_handler = iscsi_eh_abort,
  727. .eh_device_reset_handler= iscsi_eh_device_reset,
  728. .eh_target_reset_handler= iscsi_eh_target_reset,
  729. .use_clustering = DISABLE_CLUSTERING,
  730. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  731. .slave_configure = iscsi_sw_tcp_slave_configure,
  732. .target_alloc = iscsi_target_alloc,
  733. .proc_name = "iscsi_tcp",
  734. .this_id = -1,
  735. };
  736. static struct iscsi_transport iscsi_sw_tcp_transport = {
  737. .owner = THIS_MODULE,
  738. .name = "tcp",
  739. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  740. | CAP_DATADGST,
  741. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  742. ISCSI_MAX_XMIT_DLENGTH |
  743. ISCSI_HDRDGST_EN |
  744. ISCSI_DATADGST_EN |
  745. ISCSI_INITIAL_R2T_EN |
  746. ISCSI_MAX_R2T |
  747. ISCSI_IMM_DATA_EN |
  748. ISCSI_FIRST_BURST |
  749. ISCSI_MAX_BURST |
  750. ISCSI_PDU_INORDER_EN |
  751. ISCSI_DATASEQ_INORDER_EN |
  752. ISCSI_ERL |
  753. ISCSI_CONN_PORT |
  754. ISCSI_CONN_ADDRESS |
  755. ISCSI_EXP_STATSN |
  756. ISCSI_PERSISTENT_PORT |
  757. ISCSI_PERSISTENT_ADDRESS |
  758. ISCSI_TARGET_NAME | ISCSI_TPGT |
  759. ISCSI_USERNAME | ISCSI_PASSWORD |
  760. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  761. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  762. ISCSI_LU_RESET_TMO |
  763. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  764. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  765. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  766. ISCSI_HOST_INITIATOR_NAME |
  767. ISCSI_HOST_NETDEV_NAME,
  768. /* session management */
  769. .create_session = iscsi_sw_tcp_session_create,
  770. .destroy_session = iscsi_sw_tcp_session_destroy,
  771. /* connection management */
  772. .create_conn = iscsi_sw_tcp_conn_create,
  773. .bind_conn = iscsi_sw_tcp_conn_bind,
  774. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  775. .set_param = iscsi_sw_tcp_conn_set_param,
  776. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  777. .get_session_param = iscsi_session_get_param,
  778. .start_conn = iscsi_conn_start,
  779. .stop_conn = iscsi_sw_tcp_conn_stop,
  780. /* iscsi host params */
  781. .get_host_param = iscsi_host_get_param,
  782. .set_host_param = iscsi_host_set_param,
  783. /* IO */
  784. .send_pdu = iscsi_conn_send_pdu,
  785. .get_stats = iscsi_sw_tcp_conn_get_stats,
  786. /* iscsi task/cmd helpers */
  787. .init_task = iscsi_tcp_task_init,
  788. .xmit_task = iscsi_tcp_task_xmit,
  789. .cleanup_task = iscsi_tcp_cleanup_task,
  790. /* low level pdu helpers */
  791. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  792. .init_pdu = iscsi_sw_tcp_pdu_init,
  793. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  794. /* recovery */
  795. .session_recovery_timedout = iscsi_session_recovery_timedout,
  796. };
  797. static int __init iscsi_sw_tcp_init(void)
  798. {
  799. if (iscsi_max_lun < 1) {
  800. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  801. iscsi_max_lun);
  802. return -EINVAL;
  803. }
  804. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  805. &iscsi_sw_tcp_transport);
  806. if (!iscsi_sw_tcp_scsi_transport)
  807. return -ENODEV;
  808. return 0;
  809. }
  810. static void __exit iscsi_sw_tcp_exit(void)
  811. {
  812. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  813. }
  814. module_init(iscsi_sw_tcp_init);
  815. module_exit(iscsi_sw_tcp_exit);