iscsi_tcp.c 27 KB

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