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