iscsi_tcp.c 27 KB

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