iscsi_tcp.c 28 KB

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