iscsi_tcp.c 27 KB

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