iscsi_tcp.c 27 KB

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