iscsi_tcp.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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_session *session = conn->session;
  581. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  582. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  583. int value;
  584. switch(param) {
  585. case ISCSI_PARAM_HDRDGST_EN:
  586. iscsi_set_param(cls_conn, param, buf, buflen);
  587. break;
  588. case ISCSI_PARAM_DATADGST_EN:
  589. iscsi_set_param(cls_conn, param, buf, buflen);
  590. tcp_sw_conn->sendpage = conn->datadgst_en ?
  591. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  592. break;
  593. case ISCSI_PARAM_MAX_R2T:
  594. sscanf(buf, "%d", &value);
  595. if (value <= 0 || !is_power_of_2(value))
  596. return -EINVAL;
  597. if (session->max_r2t == value)
  598. break;
  599. iscsi_tcp_r2tpool_free(session);
  600. iscsi_set_param(cls_conn, param, buf, buflen);
  601. if (iscsi_tcp_r2tpool_alloc(session))
  602. return -ENOMEM;
  603. break;
  604. default:
  605. return iscsi_set_param(cls_conn, param, buf, buflen);
  606. }
  607. return 0;
  608. }
  609. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  610. enum iscsi_param param, char *buf)
  611. {
  612. struct iscsi_conn *conn = cls_conn->dd_data;
  613. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  614. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  615. struct sockaddr_in6 addr;
  616. int rc, len;
  617. switch(param) {
  618. case ISCSI_PARAM_CONN_PORT:
  619. case ISCSI_PARAM_CONN_ADDRESS:
  620. spin_lock_bh(&conn->session->lock);
  621. if (!tcp_sw_conn || !tcp_sw_conn->sock) {
  622. spin_unlock_bh(&conn->session->lock);
  623. return -ENOTCONN;
  624. }
  625. rc = kernel_getpeername(tcp_sw_conn->sock,
  626. (struct sockaddr *)&addr, &len);
  627. spin_unlock_bh(&conn->session->lock);
  628. if (rc)
  629. return rc;
  630. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  631. &addr, param, buf);
  632. default:
  633. return iscsi_conn_get_param(cls_conn, param, buf);
  634. }
  635. return 0;
  636. }
  637. static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
  638. enum iscsi_host_param param, char *buf)
  639. {
  640. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
  641. struct iscsi_session *session = tcp_sw_host->session;
  642. struct iscsi_conn *conn;
  643. struct iscsi_tcp_conn *tcp_conn;
  644. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  645. struct sockaddr_in6 addr;
  646. int rc, len;
  647. switch (param) {
  648. case ISCSI_HOST_PARAM_IPADDRESS:
  649. spin_lock_bh(&session->lock);
  650. conn = session->leadconn;
  651. if (!conn) {
  652. spin_unlock_bh(&session->lock);
  653. return -ENOTCONN;
  654. }
  655. tcp_conn = conn->dd_data;
  656. tcp_sw_conn = tcp_conn->dd_data;
  657. if (!tcp_sw_conn->sock) {
  658. spin_unlock_bh(&session->lock);
  659. return -ENOTCONN;
  660. }
  661. rc = kernel_getsockname(tcp_sw_conn->sock,
  662. (struct sockaddr *)&addr, &len);
  663. spin_unlock_bh(&session->lock);
  664. if (rc)
  665. return rc;
  666. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  667. &addr, param, buf);
  668. default:
  669. return iscsi_host_get_param(shost, param, buf);
  670. }
  671. return 0;
  672. }
  673. static void
  674. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  675. struct iscsi_stats *stats)
  676. {
  677. struct iscsi_conn *conn = cls_conn->dd_data;
  678. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  679. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  680. stats->custom_length = 3;
  681. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  682. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  683. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  684. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  685. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  686. stats->custom[2].value = conn->eh_abort_cnt;
  687. iscsi_tcp_conn_get_stats(cls_conn, stats);
  688. }
  689. static struct iscsi_cls_session *
  690. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  691. uint16_t qdepth, uint32_t initial_cmdsn)
  692. {
  693. struct iscsi_cls_session *cls_session;
  694. struct iscsi_session *session;
  695. struct iscsi_sw_tcp_host *tcp_sw_host;
  696. struct Scsi_Host *shost;
  697. if (ep) {
  698. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  699. return NULL;
  700. }
  701. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
  702. sizeof(struct iscsi_sw_tcp_host), 1);
  703. if (!shost)
  704. return NULL;
  705. shost->transportt = iscsi_sw_tcp_scsi_transport;
  706. shost->cmd_per_lun = qdepth;
  707. shost->max_lun = iscsi_max_lun;
  708. shost->max_id = 0;
  709. shost->max_channel = 0;
  710. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  711. if (iscsi_host_add(shost, NULL))
  712. goto free_host;
  713. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  714. cmds_max, 0,
  715. sizeof(struct iscsi_tcp_task) +
  716. sizeof(struct iscsi_sw_tcp_hdrbuf),
  717. initial_cmdsn, 0);
  718. if (!cls_session)
  719. goto remove_host;
  720. session = cls_session->dd_data;
  721. tcp_sw_host = iscsi_host_priv(shost);
  722. tcp_sw_host->session = session;
  723. shost->can_queue = session->scsi_cmds_max;
  724. if (iscsi_tcp_r2tpool_alloc(session))
  725. goto remove_session;
  726. return cls_session;
  727. remove_session:
  728. iscsi_session_teardown(cls_session);
  729. remove_host:
  730. iscsi_host_remove(shost);
  731. free_host:
  732. iscsi_host_free(shost);
  733. return NULL;
  734. }
  735. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  736. {
  737. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  738. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  739. iscsi_session_teardown(cls_session);
  740. iscsi_host_remove(shost);
  741. iscsi_host_free(shost);
  742. }
  743. static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
  744. {
  745. switch (param_type) {
  746. case ISCSI_HOST_PARAM:
  747. switch (param) {
  748. case ISCSI_HOST_PARAM_NETDEV_NAME:
  749. case ISCSI_HOST_PARAM_HWADDRESS:
  750. case ISCSI_HOST_PARAM_IPADDRESS:
  751. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  752. return S_IRUGO;
  753. default:
  754. return 0;
  755. }
  756. case ISCSI_PARAM:
  757. switch (param) {
  758. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  759. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  760. case ISCSI_PARAM_HDRDGST_EN:
  761. case ISCSI_PARAM_DATADGST_EN:
  762. case ISCSI_PARAM_CONN_ADDRESS:
  763. case ISCSI_PARAM_CONN_PORT:
  764. case ISCSI_PARAM_EXP_STATSN:
  765. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  766. case ISCSI_PARAM_PERSISTENT_PORT:
  767. case ISCSI_PARAM_PING_TMO:
  768. case ISCSI_PARAM_RECV_TMO:
  769. case ISCSI_PARAM_INITIAL_R2T_EN:
  770. case ISCSI_PARAM_MAX_R2T:
  771. case ISCSI_PARAM_IMM_DATA_EN:
  772. case ISCSI_PARAM_FIRST_BURST:
  773. case ISCSI_PARAM_MAX_BURST:
  774. case ISCSI_PARAM_PDU_INORDER_EN:
  775. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  776. case ISCSI_PARAM_ERL:
  777. case ISCSI_PARAM_TARGET_NAME:
  778. case ISCSI_PARAM_TPGT:
  779. case ISCSI_PARAM_USERNAME:
  780. case ISCSI_PARAM_PASSWORD:
  781. case ISCSI_PARAM_USERNAME_IN:
  782. case ISCSI_PARAM_PASSWORD_IN:
  783. case ISCSI_PARAM_FAST_ABORT:
  784. case ISCSI_PARAM_ABORT_TMO:
  785. case ISCSI_PARAM_LU_RESET_TMO:
  786. case ISCSI_PARAM_TGT_RESET_TMO:
  787. case ISCSI_PARAM_IFACE_NAME:
  788. case ISCSI_PARAM_INITIATOR_NAME:
  789. return S_IRUGO;
  790. default:
  791. return 0;
  792. }
  793. }
  794. return 0;
  795. }
  796. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  797. {
  798. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  799. return 0;
  800. }
  801. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  802. {
  803. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  804. blk_queue_dma_alignment(sdev->request_queue, 0);
  805. return 0;
  806. }
  807. static struct scsi_host_template iscsi_sw_tcp_sht = {
  808. .module = THIS_MODULE,
  809. .name = "iSCSI Initiator over TCP/IP",
  810. .queuecommand = iscsi_queuecommand,
  811. .change_queue_depth = iscsi_change_queue_depth,
  812. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  813. .sg_tablesize = 4096,
  814. .max_sectors = 0xFFFF,
  815. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  816. .eh_abort_handler = iscsi_eh_abort,
  817. .eh_device_reset_handler= iscsi_eh_device_reset,
  818. .eh_target_reset_handler = iscsi_eh_recover_target,
  819. .use_clustering = DISABLE_CLUSTERING,
  820. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  821. .slave_configure = iscsi_sw_tcp_slave_configure,
  822. .target_alloc = iscsi_target_alloc,
  823. .proc_name = "iscsi_tcp",
  824. .this_id = -1,
  825. };
  826. static struct iscsi_transport iscsi_sw_tcp_transport = {
  827. .owner = THIS_MODULE,
  828. .name = "tcp",
  829. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  830. | CAP_DATADGST,
  831. /* session management */
  832. .create_session = iscsi_sw_tcp_session_create,
  833. .destroy_session = iscsi_sw_tcp_session_destroy,
  834. /* connection management */
  835. .create_conn = iscsi_sw_tcp_conn_create,
  836. .bind_conn = iscsi_sw_tcp_conn_bind,
  837. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  838. .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
  839. .set_param = iscsi_sw_tcp_conn_set_param,
  840. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  841. .get_session_param = iscsi_session_get_param,
  842. .start_conn = iscsi_conn_start,
  843. .stop_conn = iscsi_sw_tcp_conn_stop,
  844. /* iscsi host params */
  845. .get_host_param = iscsi_sw_tcp_host_get_param,
  846. .set_host_param = iscsi_host_set_param,
  847. /* IO */
  848. .send_pdu = iscsi_conn_send_pdu,
  849. .get_stats = iscsi_sw_tcp_conn_get_stats,
  850. /* iscsi task/cmd helpers */
  851. .init_task = iscsi_tcp_task_init,
  852. .xmit_task = iscsi_tcp_task_xmit,
  853. .cleanup_task = iscsi_tcp_cleanup_task,
  854. /* low level pdu helpers */
  855. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  856. .init_pdu = iscsi_sw_tcp_pdu_init,
  857. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  858. /* recovery */
  859. .session_recovery_timedout = iscsi_session_recovery_timedout,
  860. };
  861. static int __init iscsi_sw_tcp_init(void)
  862. {
  863. if (iscsi_max_lun < 1) {
  864. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  865. iscsi_max_lun);
  866. return -EINVAL;
  867. }
  868. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  869. &iscsi_sw_tcp_transport);
  870. if (!iscsi_sw_tcp_scsi_transport)
  871. return -ENODEV;
  872. return 0;
  873. }
  874. static void __exit iscsi_sw_tcp_exit(void)
  875. {
  876. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  877. }
  878. module_init(iscsi_sw_tcp_init);
  879. module_exit(iscsi_sw_tcp_exit);