18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * iSCSI over TCP/IP Data-Path lib
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004 Dmitry Yusupov
68c2ecf20Sopenharmony_ci * Copyright (C) 2004 Alex Aizman
78c2ecf20Sopenharmony_ci * Copyright (C) 2005 - 2006 Mike Christie
88c2ecf20Sopenharmony_ci * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
98c2ecf20Sopenharmony_ci * maintained by open-iscsi@googlegroups.com
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Credits:
128c2ecf20Sopenharmony_ci *	Christoph Hellwig
138c2ecf20Sopenharmony_ci *	FUJITA Tomonori
148c2ecf20Sopenharmony_ci *	Arne Redlich
158c2ecf20Sopenharmony_ci *	Zhenyu Wang
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <crypto/hash.h>
198c2ecf20Sopenharmony_ci#include <linux/types.h>
208c2ecf20Sopenharmony_ci#include <linux/list.h>
218c2ecf20Sopenharmony_ci#include <linux/inet.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <linux/file.h>
248c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
258c2ecf20Sopenharmony_ci#include <linux/delay.h>
268c2ecf20Sopenharmony_ci#include <linux/kfifo.h>
278c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
288c2ecf20Sopenharmony_ci#include <linux/module.h>
298c2ecf20Sopenharmony_ci#include <net/tcp.h>
308c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
318c2ecf20Sopenharmony_ci#include <scsi/scsi_device.h>
328c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
338c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
348c2ecf20Sopenharmony_ci#include <scsi/scsi_transport_iscsi.h>
358c2ecf20Sopenharmony_ci#include <trace/events/iscsi.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "iscsi_tcp.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
408c2ecf20Sopenharmony_ci	      "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
418c2ecf20Sopenharmony_ci	      "Alex Aizman <itn780@yahoo.com>");
428c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("iSCSI/TCP data-path");
438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int iscsi_dbg_libtcp;
468c2ecf20Sopenharmony_cimodule_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
478c2ecf20Sopenharmony_ci		   S_IRUGO | S_IWUSR);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
498c2ecf20Sopenharmony_ci		 "module. Set to 1 to turn on, and zero to turn off. Default "
508c2ecf20Sopenharmony_ci		 "is off.");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...)			\
538c2ecf20Sopenharmony_ci	do {							\
548c2ecf20Sopenharmony_ci		if (iscsi_dbg_libtcp)				\
558c2ecf20Sopenharmony_ci			iscsi_conn_printk(KERN_INFO, _conn,	\
568c2ecf20Sopenharmony_ci					     "%s " dbg_fmt,	\
578c2ecf20Sopenharmony_ci					     __func__, ##arg);	\
588c2ecf20Sopenharmony_ci		iscsi_dbg_trace(trace_iscsi_dbg_tcp,		\
598c2ecf20Sopenharmony_ci				&(_conn)->cls_conn->dev,	\
608c2ecf20Sopenharmony_ci				"%s " dbg_fmt, __func__, ##arg);\
618c2ecf20Sopenharmony_ci	} while (0);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
648c2ecf20Sopenharmony_ci				   struct iscsi_segment *segment);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Scatterlist handling: inside the iscsi_segment, we
688c2ecf20Sopenharmony_ci * remember an index into the scatterlist, and set data/size
698c2ecf20Sopenharmony_ci * to the current scatterlist entry. For highmem pages, we
708c2ecf20Sopenharmony_ci * kmap as needed.
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * Note that the page is unmapped when we return from
738c2ecf20Sopenharmony_ci * TCP's data_ready handler, so we may end up mapping and
748c2ecf20Sopenharmony_ci * unmapping the same page repeatedly. The whole reason
758c2ecf20Sopenharmony_ci * for this is that we shouldn't keep the page mapped
768c2ecf20Sopenharmony_ci * outside the softirq.
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/**
808c2ecf20Sopenharmony_ci * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
818c2ecf20Sopenharmony_ci * @segment: the buffer object
828c2ecf20Sopenharmony_ci * @sg: scatterlist
838c2ecf20Sopenharmony_ci * @offset: byte offset into that sg entry
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci * This function sets up the segment so that subsequent
868c2ecf20Sopenharmony_ci * data is copied to the indicated sg entry, at the given
878c2ecf20Sopenharmony_ci * offset.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_cistatic inline void
908c2ecf20Sopenharmony_ciiscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
918c2ecf20Sopenharmony_ci			  struct scatterlist *sg, unsigned int offset)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	segment->sg = sg;
948c2ecf20Sopenharmony_ci	segment->sg_offset = offset;
958c2ecf20Sopenharmony_ci	segment->size = min(sg->length - offset,
968c2ecf20Sopenharmony_ci			    segment->total_size - segment->total_copied);
978c2ecf20Sopenharmony_ci	segment->data = NULL;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/**
1018c2ecf20Sopenharmony_ci * iscsi_tcp_segment_map - map the current S/G page
1028c2ecf20Sopenharmony_ci * @segment: iscsi_segment
1038c2ecf20Sopenharmony_ci * @recv: 1 if called from recv path
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * We only need to possibly kmap data if scatter lists are being used,
1068c2ecf20Sopenharmony_ci * because the iscsi passthrough and internal IO paths will never use high
1078c2ecf20Sopenharmony_ci * mem pages.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistatic void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct scatterlist *sg;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (segment->data != NULL || !segment->sg)
1148c2ecf20Sopenharmony_ci		return;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	sg = segment->sg;
1178c2ecf20Sopenharmony_ci	BUG_ON(segment->sg_mapped);
1188c2ecf20Sopenharmony_ci	BUG_ON(sg->length == 0);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/*
1218c2ecf20Sopenharmony_ci	 * We always map for the recv path.
1228c2ecf20Sopenharmony_ci	 *
1238c2ecf20Sopenharmony_ci	 * If the page count is greater than one it is ok to send
1248c2ecf20Sopenharmony_ci	 * to the network layer's zero copy send path. If not we
1258c2ecf20Sopenharmony_ci	 * have to go the slow sendmsg path.
1268c2ecf20Sopenharmony_ci	 *
1278c2ecf20Sopenharmony_ci	 * Same goes for slab pages: skb_can_coalesce() allows
1288c2ecf20Sopenharmony_ci	 * coalescing neighboring slab objects into a single frag which
1298c2ecf20Sopenharmony_ci	 * triggers one of hardened usercopy checks.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	if (!recv && sendpage_ok(sg_page(sg)))
1328c2ecf20Sopenharmony_ci		return;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (recv) {
1358c2ecf20Sopenharmony_ci		segment->atomic_mapped = true;
1368c2ecf20Sopenharmony_ci		segment->sg_mapped = kmap_atomic(sg_page(sg));
1378c2ecf20Sopenharmony_ci	} else {
1388c2ecf20Sopenharmony_ci		segment->atomic_mapped = false;
1398c2ecf20Sopenharmony_ci		/* the xmit path can sleep with the page mapped so use kmap */
1408c2ecf20Sopenharmony_ci		segment->sg_mapped = kmap(sg_page(sg));
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_civoid iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	if (segment->sg_mapped) {
1498c2ecf20Sopenharmony_ci		if (segment->atomic_mapped)
1508c2ecf20Sopenharmony_ci			kunmap_atomic(segment->sg_mapped);
1518c2ecf20Sopenharmony_ci		else
1528c2ecf20Sopenharmony_ci			kunmap(sg_page(segment->sg));
1538c2ecf20Sopenharmony_ci		segment->sg_mapped = NULL;
1548c2ecf20Sopenharmony_ci		segment->data = NULL;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * Splice the digest buffer into the buffer
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_cistatic inline void
1638c2ecf20Sopenharmony_ciiscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	segment->data = digest;
1668c2ecf20Sopenharmony_ci	segment->digest_len = ISCSI_DIGEST_SIZE;
1678c2ecf20Sopenharmony_ci	segment->total_size += ISCSI_DIGEST_SIZE;
1688c2ecf20Sopenharmony_ci	segment->size = ISCSI_DIGEST_SIZE;
1698c2ecf20Sopenharmony_ci	segment->copied = 0;
1708c2ecf20Sopenharmony_ci	segment->sg = NULL;
1718c2ecf20Sopenharmony_ci	segment->hash = NULL;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/**
1758c2ecf20Sopenharmony_ci * iscsi_tcp_segment_done - check whether the segment is complete
1768c2ecf20Sopenharmony_ci * @tcp_conn: iscsi tcp connection
1778c2ecf20Sopenharmony_ci * @segment: iscsi segment to check
1788c2ecf20Sopenharmony_ci * @recv: set to one of this is called from the recv path
1798c2ecf20Sopenharmony_ci * @copied: number of bytes copied
1808c2ecf20Sopenharmony_ci *
1818c2ecf20Sopenharmony_ci * Check if we're done receiving this segment. If the receive
1828c2ecf20Sopenharmony_ci * buffer is full but we expect more data, move on to the
1838c2ecf20Sopenharmony_ci * next entry in the scatterlist.
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * If the amount of data we received isn't a multiple of 4,
1868c2ecf20Sopenharmony_ci * we will transparently receive the pad bytes, too.
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * This function must be re-entrant.
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_ciint iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
1918c2ecf20Sopenharmony_ci			   struct iscsi_segment *segment, int recv,
1928c2ecf20Sopenharmony_ci			   unsigned copied)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct scatterlist sg;
1958c2ecf20Sopenharmony_ci	unsigned int pad;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
1988c2ecf20Sopenharmony_ci		      segment->copied, copied, segment->size,
1998c2ecf20Sopenharmony_ci		      recv ? "recv" : "xmit");
2008c2ecf20Sopenharmony_ci	if (segment->hash && copied) {
2018c2ecf20Sopenharmony_ci		/*
2028c2ecf20Sopenharmony_ci		 * If a segment is kmapd we must unmap it before sending
2038c2ecf20Sopenharmony_ci		 * to the crypto layer since that will try to kmap it again.
2048c2ecf20Sopenharmony_ci		 */
2058c2ecf20Sopenharmony_ci		iscsi_tcp_segment_unmap(segment);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		if (!segment->data) {
2088c2ecf20Sopenharmony_ci			sg_init_table(&sg, 1);
2098c2ecf20Sopenharmony_ci			sg_set_page(&sg, sg_page(segment->sg), copied,
2108c2ecf20Sopenharmony_ci				    segment->copied + segment->sg_offset +
2118c2ecf20Sopenharmony_ci							segment->sg->offset);
2128c2ecf20Sopenharmony_ci		} else
2138c2ecf20Sopenharmony_ci			sg_init_one(&sg, segment->data + segment->copied,
2148c2ecf20Sopenharmony_ci				    copied);
2158c2ecf20Sopenharmony_ci		ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
2168c2ecf20Sopenharmony_ci		crypto_ahash_update(segment->hash);
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	segment->copied += copied;
2208c2ecf20Sopenharmony_ci	if (segment->copied < segment->size) {
2218c2ecf20Sopenharmony_ci		iscsi_tcp_segment_map(segment, recv);
2228c2ecf20Sopenharmony_ci		return 0;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	segment->total_copied += segment->copied;
2268c2ecf20Sopenharmony_ci	segment->copied = 0;
2278c2ecf20Sopenharmony_ci	segment->size = 0;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Unmap the current scatterlist page, if there is one. */
2308c2ecf20Sopenharmony_ci	iscsi_tcp_segment_unmap(segment);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* Do we have more scatterlist entries? */
2338c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
2348c2ecf20Sopenharmony_ci		      segment->total_copied, segment->total_size);
2358c2ecf20Sopenharmony_ci	if (segment->total_copied < segment->total_size) {
2368c2ecf20Sopenharmony_ci		/* Proceed to the next entry in the scatterlist. */
2378c2ecf20Sopenharmony_ci		iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
2388c2ecf20Sopenharmony_ci					  0);
2398c2ecf20Sopenharmony_ci		iscsi_tcp_segment_map(segment, recv);
2408c2ecf20Sopenharmony_ci		BUG_ON(segment->size == 0);
2418c2ecf20Sopenharmony_ci		return 0;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* Do we need to handle padding? */
2458c2ecf20Sopenharmony_ci	if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
2468c2ecf20Sopenharmony_ci		pad = iscsi_padding(segment->total_copied);
2478c2ecf20Sopenharmony_ci		if (pad != 0) {
2488c2ecf20Sopenharmony_ci			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
2498c2ecf20Sopenharmony_ci				      "consume %d pad bytes\n", pad);
2508c2ecf20Sopenharmony_ci			segment->total_size += pad;
2518c2ecf20Sopenharmony_ci			segment->size = pad;
2528c2ecf20Sopenharmony_ci			segment->data = segment->padbuf;
2538c2ecf20Sopenharmony_ci			return 0;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/*
2588c2ecf20Sopenharmony_ci	 * Set us up for transferring the data digest. hdr digest
2598c2ecf20Sopenharmony_ci	 * is completely handled in hdr done function.
2608c2ecf20Sopenharmony_ci	 */
2618c2ecf20Sopenharmony_ci	if (segment->hash) {
2628c2ecf20Sopenharmony_ci		ahash_request_set_crypt(segment->hash, NULL,
2638c2ecf20Sopenharmony_ci					segment->digest, 0);
2648c2ecf20Sopenharmony_ci		crypto_ahash_final(segment->hash);
2658c2ecf20Sopenharmony_ci		iscsi_tcp_segment_splice_digest(segment,
2668c2ecf20Sopenharmony_ci				 recv ? segment->recv_digest : segment->digest);
2678c2ecf20Sopenharmony_ci		return 0;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return 1;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/**
2758c2ecf20Sopenharmony_ci * iscsi_tcp_segment_recv - copy data to segment
2768c2ecf20Sopenharmony_ci * @tcp_conn: the iSCSI TCP connection
2778c2ecf20Sopenharmony_ci * @segment: the buffer to copy to
2788c2ecf20Sopenharmony_ci * @ptr: data pointer
2798c2ecf20Sopenharmony_ci * @len: amount of data available
2808c2ecf20Sopenharmony_ci *
2818c2ecf20Sopenharmony_ci * This function copies up to @len bytes to the
2828c2ecf20Sopenharmony_ci * given buffer, and returns the number of bytes
2838c2ecf20Sopenharmony_ci * consumed, which can actually be less than @len.
2848c2ecf20Sopenharmony_ci *
2858c2ecf20Sopenharmony_ci * If hash digest is enabled, the function will update the
2868c2ecf20Sopenharmony_ci * hash while copying.
2878c2ecf20Sopenharmony_ci * Combining these two operations doesn't buy us a lot (yet),
2888c2ecf20Sopenharmony_ci * but in the future we could implement combined copy+crc,
2898c2ecf20Sopenharmony_ci * just way we do for network layer checksums.
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_cistatic int
2928c2ecf20Sopenharmony_ciiscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
2938c2ecf20Sopenharmony_ci		       struct iscsi_segment *segment, const void *ptr,
2948c2ecf20Sopenharmony_ci		       unsigned int len)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	unsigned int copy = 0, copied = 0;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
2998c2ecf20Sopenharmony_ci		if (copied == len) {
3008c2ecf20Sopenharmony_ci			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
3018c2ecf20Sopenharmony_ci				      "copied %d bytes\n", len);
3028c2ecf20Sopenharmony_ci			break;
3038c2ecf20Sopenharmony_ci		}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		copy = min(len - copied, segment->size - segment->copied);
3068c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
3078c2ecf20Sopenharmony_ci		memcpy(segment->data + segment->copied, ptr + copied, copy);
3088c2ecf20Sopenharmony_ci		copied += copy;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci	return copied;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ciinline void
3148c2ecf20Sopenharmony_ciiscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
3158c2ecf20Sopenharmony_ci		      size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct scatterlist sg;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	sg_init_one(&sg, hdr, hdrlen);
3208c2ecf20Sopenharmony_ci	ahash_request_set_crypt(hash, &sg, digest, hdrlen);
3218c2ecf20Sopenharmony_ci	crypto_ahash_digest(hash);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic inline int
3268c2ecf20Sopenharmony_ciiscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
3278c2ecf20Sopenharmony_ci		      struct iscsi_segment *segment)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	if (!segment->digest_len)
3308c2ecf20Sopenharmony_ci		return 1;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (memcmp(segment->recv_digest, segment->digest,
3338c2ecf20Sopenharmony_ci		   segment->digest_len)) {
3348c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
3358c2ecf20Sopenharmony_ci		return 0;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	return 1;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/*
3428c2ecf20Sopenharmony_ci * Helper function to set up segment buffer
3438c2ecf20Sopenharmony_ci */
3448c2ecf20Sopenharmony_cistatic inline void
3458c2ecf20Sopenharmony_ci__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
3468c2ecf20Sopenharmony_ci		     iscsi_segment_done_fn_t *done, struct ahash_request *hash)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	memset(segment, 0, sizeof(*segment));
3498c2ecf20Sopenharmony_ci	segment->total_size = size;
3508c2ecf20Sopenharmony_ci	segment->done = done;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (hash) {
3538c2ecf20Sopenharmony_ci		segment->hash = hash;
3548c2ecf20Sopenharmony_ci		crypto_ahash_init(hash);
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ciinline void
3598c2ecf20Sopenharmony_ciiscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
3608c2ecf20Sopenharmony_ci			  size_t size, iscsi_segment_done_fn_t *done,
3618c2ecf20Sopenharmony_ci			  struct ahash_request *hash)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	__iscsi_segment_init(segment, size, done, hash);
3648c2ecf20Sopenharmony_ci	segment->data = data;
3658c2ecf20Sopenharmony_ci	segment->size = size;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ciinline int
3708c2ecf20Sopenharmony_ciiscsi_segment_seek_sg(struct iscsi_segment *segment,
3718c2ecf20Sopenharmony_ci		      struct scatterlist *sg_list, unsigned int sg_count,
3728c2ecf20Sopenharmony_ci		      unsigned int offset, size_t size,
3738c2ecf20Sopenharmony_ci		      iscsi_segment_done_fn_t *done,
3748c2ecf20Sopenharmony_ci		      struct ahash_request *hash)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct scatterlist *sg;
3778c2ecf20Sopenharmony_ci	unsigned int i;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	__iscsi_segment_init(segment, size, done, hash);
3808c2ecf20Sopenharmony_ci	for_each_sg(sg_list, sg, sg_count, i) {
3818c2ecf20Sopenharmony_ci		if (offset < sg->length) {
3828c2ecf20Sopenharmony_ci			iscsi_tcp_segment_init_sg(segment, sg, offset);
3838c2ecf20Sopenharmony_ci			return 0;
3848c2ecf20Sopenharmony_ci		}
3858c2ecf20Sopenharmony_ci		offset -= sg->length;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return ISCSI_ERR_DATA_OFFSET;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci/**
3938c2ecf20Sopenharmony_ci * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
3948c2ecf20Sopenharmony_ci * @tcp_conn: iscsi connection to prep for
3958c2ecf20Sopenharmony_ci *
3968c2ecf20Sopenharmony_ci * This function always passes NULL for the hash argument, because when this
3978c2ecf20Sopenharmony_ci * function is called we do not yet know the final size of the header and want
3988c2ecf20Sopenharmony_ci * to delay the digest processing until we know that.
3998c2ecf20Sopenharmony_ci */
4008c2ecf20Sopenharmony_civoid iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
4038c2ecf20Sopenharmony_ci		      "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
4048c2ecf20Sopenharmony_ci		      "digest enabled" : "digest disabled");
4058c2ecf20Sopenharmony_ci	iscsi_segment_init_linear(&tcp_conn->in.segment,
4068c2ecf20Sopenharmony_ci				tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
4078c2ecf20Sopenharmony_ci				iscsi_tcp_hdr_recv_done, NULL);
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci/*
4128c2ecf20Sopenharmony_ci * Handle incoming reply to any other type of command
4138c2ecf20Sopenharmony_ci */
4148c2ecf20Sopenharmony_cistatic int
4158c2ecf20Sopenharmony_ciiscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
4168c2ecf20Sopenharmony_ci			 struct iscsi_segment *segment)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
4198c2ecf20Sopenharmony_ci	int rc = 0;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
4228c2ecf20Sopenharmony_ci		return ISCSI_ERR_DATA_DGST;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
4258c2ecf20Sopenharmony_ci			conn->data, tcp_conn->in.datalen);
4268c2ecf20Sopenharmony_ci	if (rc)
4278c2ecf20Sopenharmony_ci		return rc;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	iscsi_tcp_hdr_recv_prep(tcp_conn);
4308c2ecf20Sopenharmony_ci	return 0;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic void
4348c2ecf20Sopenharmony_ciiscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
4378c2ecf20Sopenharmony_ci	struct ahash_request *rx_hash = NULL;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (conn->datadgst_en &&
4408c2ecf20Sopenharmony_ci	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
4418c2ecf20Sopenharmony_ci		rx_hash = tcp_conn->rx_hash;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	iscsi_segment_init_linear(&tcp_conn->in.segment,
4448c2ecf20Sopenharmony_ci				conn->data, tcp_conn->in.datalen,
4458c2ecf20Sopenharmony_ci				iscsi_tcp_data_recv_done, rx_hash);
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci/**
4498c2ecf20Sopenharmony_ci * iscsi_tcp_cleanup_task - free tcp_task resources
4508c2ecf20Sopenharmony_ci * @task: iscsi task
4518c2ecf20Sopenharmony_ci *
4528c2ecf20Sopenharmony_ci * must be called with session back_lock
4538c2ecf20Sopenharmony_ci */
4548c2ecf20Sopenharmony_civoid iscsi_tcp_cleanup_task(struct iscsi_task *task)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	struct iscsi_tcp_task *tcp_task = task->dd_data;
4578c2ecf20Sopenharmony_ci	struct iscsi_r2t_info *r2t;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* nothing to do for mgmt */
4608c2ecf20Sopenharmony_ci	if (!task->sc)
4618c2ecf20Sopenharmony_ci		return;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	spin_lock_bh(&tcp_task->queue2pool);
4648c2ecf20Sopenharmony_ci	/* flush task's r2t queues */
4658c2ecf20Sopenharmony_ci	while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
4668c2ecf20Sopenharmony_ci		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
4678c2ecf20Sopenharmony_ci			    sizeof(void*));
4688c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	r2t = tcp_task->r2t;
4728c2ecf20Sopenharmony_ci	if (r2t != NULL) {
4738c2ecf20Sopenharmony_ci		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
4748c2ecf20Sopenharmony_ci			    sizeof(void*));
4758c2ecf20Sopenharmony_ci		tcp_task->r2t = NULL;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci	spin_unlock_bh(&tcp_task->queue2pool);
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci/**
4828c2ecf20Sopenharmony_ci * iscsi_tcp_data_in - SCSI Data-In Response processing
4838c2ecf20Sopenharmony_ci * @conn: iscsi connection
4848c2ecf20Sopenharmony_ci * @task: scsi command task
4858c2ecf20Sopenharmony_ci */
4868c2ecf20Sopenharmony_cistatic int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
4898c2ecf20Sopenharmony_ci	struct iscsi_tcp_task *tcp_task = task->dd_data;
4908c2ecf20Sopenharmony_ci	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
4918c2ecf20Sopenharmony_ci	int datasn = be32_to_cpu(rhdr->datasn);
4928c2ecf20Sopenharmony_ci	unsigned total_in_length = task->sc->sdb.length;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	/*
4958c2ecf20Sopenharmony_ci	 * lib iscsi will update this in the completion handling if there
4968c2ecf20Sopenharmony_ci	 * is status.
4978c2ecf20Sopenharmony_ci	 */
4988c2ecf20Sopenharmony_ci	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
4998c2ecf20Sopenharmony_ci		iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (tcp_conn->in.datalen == 0)
5028c2ecf20Sopenharmony_ci		return 0;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (tcp_task->exp_datasn != datasn) {
5058c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
5068c2ecf20Sopenharmony_ci			      "\n", tcp_task->exp_datasn, datasn);
5078c2ecf20Sopenharmony_ci		return ISCSI_ERR_DATASN;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	tcp_task->exp_datasn++;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	tcp_task->data_offset = be32_to_cpu(rhdr->offset);
5138c2ecf20Sopenharmony_ci	if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
5148c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
5158c2ecf20Sopenharmony_ci			      "total_length_in(%d)\n", tcp_task->data_offset,
5168c2ecf20Sopenharmony_ci			      tcp_conn->in.datalen, total_in_length);
5178c2ecf20Sopenharmony_ci		return ISCSI_ERR_DATA_OFFSET;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	conn->datain_pdus_cnt++;
5218c2ecf20Sopenharmony_ci	return 0;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/**
5258c2ecf20Sopenharmony_ci * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
5268c2ecf20Sopenharmony_ci * @conn: iscsi connection
5278c2ecf20Sopenharmony_ci * @hdr: PDU header
5288c2ecf20Sopenharmony_ci */
5298c2ecf20Sopenharmony_cistatic int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct iscsi_session *session = conn->session;
5328c2ecf20Sopenharmony_ci	struct iscsi_tcp_task *tcp_task;
5338c2ecf20Sopenharmony_ci	struct iscsi_tcp_conn *tcp_conn;
5348c2ecf20Sopenharmony_ci	struct iscsi_r2t_rsp *rhdr;
5358c2ecf20Sopenharmony_ci	struct iscsi_r2t_info *r2t;
5368c2ecf20Sopenharmony_ci	struct iscsi_task *task;
5378c2ecf20Sopenharmony_ci	u32 data_length;
5388c2ecf20Sopenharmony_ci	u32 data_offset;
5398c2ecf20Sopenharmony_ci	int r2tsn;
5408c2ecf20Sopenharmony_ci	int rc;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	spin_lock(&session->back_lock);
5438c2ecf20Sopenharmony_ci	task = iscsi_itt_to_ctask(conn, hdr->itt);
5448c2ecf20Sopenharmony_ci	if (!task) {
5458c2ecf20Sopenharmony_ci		spin_unlock(&session->back_lock);
5468c2ecf20Sopenharmony_ci		return ISCSI_ERR_BAD_ITT;
5478c2ecf20Sopenharmony_ci	} else if (task->sc->sc_data_direction != DMA_TO_DEVICE) {
5488c2ecf20Sopenharmony_ci		spin_unlock(&session->back_lock);
5498c2ecf20Sopenharmony_ci		return ISCSI_ERR_PROTO;
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci	/*
5528c2ecf20Sopenharmony_ci	 * A bad target might complete the cmd before we have handled R2Ts
5538c2ecf20Sopenharmony_ci	 * so get a ref to the task that will be dropped in the xmit path.
5548c2ecf20Sopenharmony_ci	 */
5558c2ecf20Sopenharmony_ci	if (task->state != ISCSI_TASK_RUNNING) {
5568c2ecf20Sopenharmony_ci		spin_unlock(&session->back_lock);
5578c2ecf20Sopenharmony_ci		/* Let the path that got the early rsp complete it */
5588c2ecf20Sopenharmony_ci		return 0;
5598c2ecf20Sopenharmony_ci	}
5608c2ecf20Sopenharmony_ci	task->last_xfer = jiffies;
5618c2ecf20Sopenharmony_ci	__iscsi_get_task(task);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	tcp_conn = conn->dd_data;
5648c2ecf20Sopenharmony_ci	rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
5658c2ecf20Sopenharmony_ci	/* fill-in new R2T associated with the task */
5668c2ecf20Sopenharmony_ci	iscsi_update_cmdsn(session, (struct iscsi_nopin *)rhdr);
5678c2ecf20Sopenharmony_ci	spin_unlock(&session->back_lock);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	if (tcp_conn->in.datalen) {
5708c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_ERR, conn,
5718c2ecf20Sopenharmony_ci				  "invalid R2t with datalen %d\n",
5728c2ecf20Sopenharmony_ci				  tcp_conn->in.datalen);
5738c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_DATALEN;
5748c2ecf20Sopenharmony_ci		goto put_task;
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	tcp_task = task->dd_data;
5788c2ecf20Sopenharmony_ci	r2tsn = be32_to_cpu(rhdr->r2tsn);
5798c2ecf20Sopenharmony_ci	if (tcp_task->exp_datasn != r2tsn){
5808c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
5818c2ecf20Sopenharmony_ci			      tcp_task->exp_datasn, r2tsn);
5828c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_R2TSN;
5838c2ecf20Sopenharmony_ci		goto put_task;
5848c2ecf20Sopenharmony_ci	}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (session->state != ISCSI_STATE_LOGGED_IN) {
5878c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_INFO, conn,
5888c2ecf20Sopenharmony_ci				  "dropping R2T itt %d in recovery.\n",
5898c2ecf20Sopenharmony_ci				  task->itt);
5908c2ecf20Sopenharmony_ci		rc = 0;
5918c2ecf20Sopenharmony_ci		goto put_task;
5928c2ecf20Sopenharmony_ci	}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	data_length = be32_to_cpu(rhdr->data_length);
5958c2ecf20Sopenharmony_ci	if (data_length == 0) {
5968c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_ERR, conn,
5978c2ecf20Sopenharmony_ci				  "invalid R2T with zero data len\n");
5988c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_DATALEN;
5998c2ecf20Sopenharmony_ci		goto put_task;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (data_length > session->max_burst)
6038c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
6048c2ecf20Sopenharmony_ci			      "burst %u. Attempting to execute request.\n",
6058c2ecf20Sopenharmony_ci			      data_length, session->max_burst);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	data_offset = be32_to_cpu(rhdr->data_offset);
6088c2ecf20Sopenharmony_ci	if (data_offset + data_length > task->sc->sdb.length) {
6098c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_ERR, conn,
6108c2ecf20Sopenharmony_ci				  "invalid R2T with data len %u at offset %u "
6118c2ecf20Sopenharmony_ci				  "and total length %d\n", data_length,
6128c2ecf20Sopenharmony_ci				  data_offset, task->sc->sdb.length);
6138c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_DATALEN;
6148c2ecf20Sopenharmony_ci		goto put_task;
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	spin_lock(&tcp_task->pool2queue);
6188c2ecf20Sopenharmony_ci	rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
6198c2ecf20Sopenharmony_ci	if (!rc) {
6208c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
6218c2ecf20Sopenharmony_ci				  "Target has sent more R2Ts than it "
6228c2ecf20Sopenharmony_ci				  "negotiated for or driver has leaked.\n");
6238c2ecf20Sopenharmony_ci		spin_unlock(&tcp_task->pool2queue);
6248c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_PROTO;
6258c2ecf20Sopenharmony_ci		goto put_task;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	r2t->exp_statsn = rhdr->statsn;
6298c2ecf20Sopenharmony_ci	r2t->data_length = data_length;
6308c2ecf20Sopenharmony_ci	r2t->data_offset = data_offset;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	r2t->ttt = rhdr->ttt; /* no flip */
6338c2ecf20Sopenharmony_ci	r2t->datasn = 0;
6348c2ecf20Sopenharmony_ci	r2t->sent = 0;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	tcp_task->exp_datasn = r2tsn + 1;
6378c2ecf20Sopenharmony_ci	kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
6388c2ecf20Sopenharmony_ci	conn->r2t_pdus_cnt++;
6398c2ecf20Sopenharmony_ci	spin_unlock(&tcp_task->pool2queue);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	iscsi_requeue_task(task);
6428c2ecf20Sopenharmony_ci	return 0;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ciput_task:
6458c2ecf20Sopenharmony_ci	iscsi_put_task(task);
6468c2ecf20Sopenharmony_ci	return rc;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci/*
6508c2ecf20Sopenharmony_ci * Handle incoming reply to DataIn command
6518c2ecf20Sopenharmony_ci */
6528c2ecf20Sopenharmony_cistatic int
6538c2ecf20Sopenharmony_ciiscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
6548c2ecf20Sopenharmony_ci			  struct iscsi_segment *segment)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
6578c2ecf20Sopenharmony_ci	struct iscsi_hdr *hdr = tcp_conn->in.hdr;
6588c2ecf20Sopenharmony_ci	int rc;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
6618c2ecf20Sopenharmony_ci		return ISCSI_ERR_DATA_DGST;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/* check for non-exceptional status */
6648c2ecf20Sopenharmony_ci	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
6658c2ecf20Sopenharmony_ci		rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
6668c2ecf20Sopenharmony_ci		if (rc)
6678c2ecf20Sopenharmony_ci			return rc;
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	iscsi_tcp_hdr_recv_prep(tcp_conn);
6718c2ecf20Sopenharmony_ci	return 0;
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci/**
6758c2ecf20Sopenharmony_ci * iscsi_tcp_hdr_dissect - process PDU header
6768c2ecf20Sopenharmony_ci * @conn: iSCSI connection
6778c2ecf20Sopenharmony_ci * @hdr: PDU header
6788c2ecf20Sopenharmony_ci *
6798c2ecf20Sopenharmony_ci * This function analyzes the header of the PDU received,
6808c2ecf20Sopenharmony_ci * and performs several sanity checks. If the PDU is accompanied
6818c2ecf20Sopenharmony_ci * by data, the receive buffer is set up to copy the incoming data
6828c2ecf20Sopenharmony_ci * to the correct location.
6838c2ecf20Sopenharmony_ci */
6848c2ecf20Sopenharmony_cistatic int
6858c2ecf20Sopenharmony_ciiscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	int rc = 0, opcode, ahslen;
6888c2ecf20Sopenharmony_ci	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
6898c2ecf20Sopenharmony_ci	struct iscsi_task *task;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	/* verify PDU length */
6928c2ecf20Sopenharmony_ci	tcp_conn->in.datalen = ntoh24(hdr->dlength);
6938c2ecf20Sopenharmony_ci	if (tcp_conn->in.datalen > conn->max_recv_dlength) {
6948c2ecf20Sopenharmony_ci		iscsi_conn_printk(KERN_ERR, conn,
6958c2ecf20Sopenharmony_ci				  "iscsi_tcp: datalen %d > %d\n",
6968c2ecf20Sopenharmony_ci				  tcp_conn->in.datalen, conn->max_recv_dlength);
6978c2ecf20Sopenharmony_ci		return ISCSI_ERR_DATALEN;
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	/* Additional header segments. So far, we don't
7018c2ecf20Sopenharmony_ci	 * process additional headers.
7028c2ecf20Sopenharmony_ci	 */
7038c2ecf20Sopenharmony_ci	ahslen = hdr->hlength << 2;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	opcode = hdr->opcode & ISCSI_OPCODE_MASK;
7068c2ecf20Sopenharmony_ci	/* verify itt (itt encoding: age+cid+itt) */
7078c2ecf20Sopenharmony_ci	rc = iscsi_verify_itt(conn, hdr->itt);
7088c2ecf20Sopenharmony_ci	if (rc)
7098c2ecf20Sopenharmony_ci		return rc;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
7128c2ecf20Sopenharmony_ci		      opcode, ahslen, tcp_conn->in.datalen);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	switch(opcode) {
7158c2ecf20Sopenharmony_ci	case ISCSI_OP_SCSI_DATA_IN:
7168c2ecf20Sopenharmony_ci		spin_lock(&conn->session->back_lock);
7178c2ecf20Sopenharmony_ci		task = iscsi_itt_to_ctask(conn, hdr->itt);
7188c2ecf20Sopenharmony_ci		if (!task)
7198c2ecf20Sopenharmony_ci			rc = ISCSI_ERR_BAD_ITT;
7208c2ecf20Sopenharmony_ci		else
7218c2ecf20Sopenharmony_ci			rc = iscsi_tcp_data_in(conn, task);
7228c2ecf20Sopenharmony_ci		if (rc) {
7238c2ecf20Sopenharmony_ci			spin_unlock(&conn->session->back_lock);
7248c2ecf20Sopenharmony_ci			break;
7258c2ecf20Sopenharmony_ci		}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci		if (tcp_conn->in.datalen) {
7288c2ecf20Sopenharmony_ci			struct iscsi_tcp_task *tcp_task = task->dd_data;
7298c2ecf20Sopenharmony_ci			struct ahash_request *rx_hash = NULL;
7308c2ecf20Sopenharmony_ci			struct scsi_data_buffer *sdb = &task->sc->sdb;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci			/*
7338c2ecf20Sopenharmony_ci			 * Setup copy of Data-In into the struct scsi_cmnd
7348c2ecf20Sopenharmony_ci			 * Scatterlist case:
7358c2ecf20Sopenharmony_ci			 * We set up the iscsi_segment to point to the next
7368c2ecf20Sopenharmony_ci			 * scatterlist entry to copy to. As we go along,
7378c2ecf20Sopenharmony_ci			 * we move on to the next scatterlist entry and
7388c2ecf20Sopenharmony_ci			 * update the digest per-entry.
7398c2ecf20Sopenharmony_ci			 */
7408c2ecf20Sopenharmony_ci			if (conn->datadgst_en &&
7418c2ecf20Sopenharmony_ci			    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
7428c2ecf20Sopenharmony_ci				rx_hash = tcp_conn->rx_hash;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci			ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
7458c2ecf20Sopenharmony_ci				     "offset=%d, datalen=%d)\n",
7468c2ecf20Sopenharmony_ci				      tcp_task->data_offset,
7478c2ecf20Sopenharmony_ci				      tcp_conn->in.datalen);
7488c2ecf20Sopenharmony_ci			task->last_xfer = jiffies;
7498c2ecf20Sopenharmony_ci			rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
7508c2ecf20Sopenharmony_ci						   sdb->table.sgl,
7518c2ecf20Sopenharmony_ci						   sdb->table.nents,
7528c2ecf20Sopenharmony_ci						   tcp_task->data_offset,
7538c2ecf20Sopenharmony_ci						   tcp_conn->in.datalen,
7548c2ecf20Sopenharmony_ci						   iscsi_tcp_process_data_in,
7558c2ecf20Sopenharmony_ci						   rx_hash);
7568c2ecf20Sopenharmony_ci			spin_unlock(&conn->session->back_lock);
7578c2ecf20Sopenharmony_ci			return rc;
7588c2ecf20Sopenharmony_ci		}
7598c2ecf20Sopenharmony_ci		rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
7608c2ecf20Sopenharmony_ci		spin_unlock(&conn->session->back_lock);
7618c2ecf20Sopenharmony_ci		break;
7628c2ecf20Sopenharmony_ci	case ISCSI_OP_SCSI_CMD_RSP:
7638c2ecf20Sopenharmony_ci		if (tcp_conn->in.datalen) {
7648c2ecf20Sopenharmony_ci			iscsi_tcp_data_recv_prep(tcp_conn);
7658c2ecf20Sopenharmony_ci			return 0;
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
7688c2ecf20Sopenharmony_ci		break;
7698c2ecf20Sopenharmony_ci	case ISCSI_OP_R2T:
7708c2ecf20Sopenharmony_ci		if (ahslen) {
7718c2ecf20Sopenharmony_ci			rc = ISCSI_ERR_AHSLEN;
7728c2ecf20Sopenharmony_ci			break;
7738c2ecf20Sopenharmony_ci		}
7748c2ecf20Sopenharmony_ci		rc = iscsi_tcp_r2t_rsp(conn, hdr);
7758c2ecf20Sopenharmony_ci		break;
7768c2ecf20Sopenharmony_ci	case ISCSI_OP_LOGIN_RSP:
7778c2ecf20Sopenharmony_ci	case ISCSI_OP_TEXT_RSP:
7788c2ecf20Sopenharmony_ci	case ISCSI_OP_REJECT:
7798c2ecf20Sopenharmony_ci	case ISCSI_OP_ASYNC_EVENT:
7808c2ecf20Sopenharmony_ci		/*
7818c2ecf20Sopenharmony_ci		 * It is possible that we could get a PDU with a buffer larger
7828c2ecf20Sopenharmony_ci		 * than 8K, but there are no targets that currently do this.
7838c2ecf20Sopenharmony_ci		 * For now we fail until we find a vendor that needs it
7848c2ecf20Sopenharmony_ci		 */
7858c2ecf20Sopenharmony_ci		if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
7868c2ecf20Sopenharmony_ci			iscsi_conn_printk(KERN_ERR, conn,
7878c2ecf20Sopenharmony_ci					  "iscsi_tcp: received buffer of "
7888c2ecf20Sopenharmony_ci					  "len %u but conn buffer is only %u "
7898c2ecf20Sopenharmony_ci					  "(opcode %0x)\n",
7908c2ecf20Sopenharmony_ci					  tcp_conn->in.datalen,
7918c2ecf20Sopenharmony_ci					  ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
7928c2ecf20Sopenharmony_ci			rc = ISCSI_ERR_PROTO;
7938c2ecf20Sopenharmony_ci			break;
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci		/* If there's data coming in with the response,
7978c2ecf20Sopenharmony_ci		 * receive it to the connection's buffer.
7988c2ecf20Sopenharmony_ci		 */
7998c2ecf20Sopenharmony_ci		if (tcp_conn->in.datalen) {
8008c2ecf20Sopenharmony_ci			iscsi_tcp_data_recv_prep(tcp_conn);
8018c2ecf20Sopenharmony_ci			return 0;
8028c2ecf20Sopenharmony_ci		}
8038c2ecf20Sopenharmony_ci		fallthrough;
8048c2ecf20Sopenharmony_ci	case ISCSI_OP_LOGOUT_RSP:
8058c2ecf20Sopenharmony_ci	case ISCSI_OP_NOOP_IN:
8068c2ecf20Sopenharmony_ci	case ISCSI_OP_SCSI_TMFUNC_RSP:
8078c2ecf20Sopenharmony_ci		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
8088c2ecf20Sopenharmony_ci		break;
8098c2ecf20Sopenharmony_ci	default:
8108c2ecf20Sopenharmony_ci		rc = ISCSI_ERR_BAD_OPCODE;
8118c2ecf20Sopenharmony_ci		break;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	if (rc == 0) {
8158c2ecf20Sopenharmony_ci		/* Anything that comes with data should have
8168c2ecf20Sopenharmony_ci		 * been handled above. */
8178c2ecf20Sopenharmony_ci		if (tcp_conn->in.datalen)
8188c2ecf20Sopenharmony_ci			return ISCSI_ERR_PROTO;
8198c2ecf20Sopenharmony_ci		iscsi_tcp_hdr_recv_prep(tcp_conn);
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	return rc;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci/**
8268c2ecf20Sopenharmony_ci * iscsi_tcp_hdr_recv_done - process PDU header
8278c2ecf20Sopenharmony_ci * @tcp_conn: iSCSI TCP connection
8288c2ecf20Sopenharmony_ci * @segment: the buffer segment being processed
8298c2ecf20Sopenharmony_ci *
8308c2ecf20Sopenharmony_ci * This is the callback invoked when the PDU header has
8318c2ecf20Sopenharmony_ci * been received. If the header is followed by additional
8328c2ecf20Sopenharmony_ci * header segments, we go back for more data.
8338c2ecf20Sopenharmony_ci */
8348c2ecf20Sopenharmony_cistatic int
8358c2ecf20Sopenharmony_ciiscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
8368c2ecf20Sopenharmony_ci			struct iscsi_segment *segment)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
8398c2ecf20Sopenharmony_ci	struct iscsi_hdr *hdr;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	/* Check if there are additional header segments
8428c2ecf20Sopenharmony_ci	 * *prior* to computing the digest, because we
8438c2ecf20Sopenharmony_ci	 * may need to go back to the caller for more.
8448c2ecf20Sopenharmony_ci	 */
8458c2ecf20Sopenharmony_ci	hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
8468c2ecf20Sopenharmony_ci	if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
8478c2ecf20Sopenharmony_ci		/* Bump the header length - the caller will
8488c2ecf20Sopenharmony_ci		 * just loop around and get the AHS for us, and
8498c2ecf20Sopenharmony_ci		 * call again. */
8508c2ecf20Sopenharmony_ci		unsigned int ahslen = hdr->hlength << 2;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		/* Make sure we don't overflow */
8538c2ecf20Sopenharmony_ci		if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
8548c2ecf20Sopenharmony_ci			return ISCSI_ERR_AHSLEN;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci		segment->total_size += ahslen;
8578c2ecf20Sopenharmony_ci		segment->size += ahslen;
8588c2ecf20Sopenharmony_ci		return 0;
8598c2ecf20Sopenharmony_ci	}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	/* We're done processing the header. See if we're doing
8628c2ecf20Sopenharmony_ci	 * header digests; if so, set up the recv_digest buffer
8638c2ecf20Sopenharmony_ci	 * and go back for more. */
8648c2ecf20Sopenharmony_ci	if (conn->hdrdgst_en &&
8658c2ecf20Sopenharmony_ci	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
8668c2ecf20Sopenharmony_ci		if (segment->digest_len == 0) {
8678c2ecf20Sopenharmony_ci			/*
8688c2ecf20Sopenharmony_ci			 * Even if we offload the digest processing we
8698c2ecf20Sopenharmony_ci			 * splice it in so we can increment the skb/segment
8708c2ecf20Sopenharmony_ci			 * counters in preparation for the data segment.
8718c2ecf20Sopenharmony_ci			 */
8728c2ecf20Sopenharmony_ci			iscsi_tcp_segment_splice_digest(segment,
8738c2ecf20Sopenharmony_ci							segment->recv_digest);
8748c2ecf20Sopenharmony_ci			return 0;
8758c2ecf20Sopenharmony_ci		}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci		iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
8788c2ecf20Sopenharmony_ci				      segment->total_copied - ISCSI_DIGEST_SIZE,
8798c2ecf20Sopenharmony_ci				      segment->digest);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci		if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
8828c2ecf20Sopenharmony_ci			return ISCSI_ERR_HDR_DGST;
8838c2ecf20Sopenharmony_ci	}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	tcp_conn->in.hdr = hdr;
8868c2ecf20Sopenharmony_ci	return iscsi_tcp_hdr_dissect(conn, hdr);
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci/**
8908c2ecf20Sopenharmony_ci * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
8918c2ecf20Sopenharmony_ci * @tcp_conn: iscsi tcp conn
8928c2ecf20Sopenharmony_ci *
8938c2ecf20Sopenharmony_ci * returns non zero if we are currently processing or setup to process
8948c2ecf20Sopenharmony_ci * a header.
8958c2ecf20Sopenharmony_ci */
8968c2ecf20Sopenharmony_ciinline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
8978c2ecf20Sopenharmony_ci{
8988c2ecf20Sopenharmony_ci	return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci/**
9038c2ecf20Sopenharmony_ci * iscsi_tcp_recv_skb - Process skb
9048c2ecf20Sopenharmony_ci * @conn: iscsi connection
9058c2ecf20Sopenharmony_ci * @skb: network buffer with header and/or data segment
9068c2ecf20Sopenharmony_ci * @offset: offset in skb
9078c2ecf20Sopenharmony_ci * @offloaded: bool indicating if transfer was offloaded
9088c2ecf20Sopenharmony_ci * @status: iscsi TCP status result
9098c2ecf20Sopenharmony_ci *
9108c2ecf20Sopenharmony_ci * Will return status of transfer in @status. And will return
9118c2ecf20Sopenharmony_ci * number of bytes copied.
9128c2ecf20Sopenharmony_ci */
9138c2ecf20Sopenharmony_ciint iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
9148c2ecf20Sopenharmony_ci		       unsigned int offset, bool offloaded, int *status)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
9178c2ecf20Sopenharmony_ci	struct iscsi_segment *segment = &tcp_conn->in.segment;
9188c2ecf20Sopenharmony_ci	struct skb_seq_state seq;
9198c2ecf20Sopenharmony_ci	unsigned int consumed = 0;
9208c2ecf20Sopenharmony_ci	int rc = 0;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
9238c2ecf20Sopenharmony_ci	/*
9248c2ecf20Sopenharmony_ci	 * Update for each skb instead of pdu, because over slow networks a
9258c2ecf20Sopenharmony_ci	 * data_in's data could take a while to read in. We also want to
9268c2ecf20Sopenharmony_ci	 * account for r2ts.
9278c2ecf20Sopenharmony_ci	 */
9288c2ecf20Sopenharmony_ci	conn->last_recv = jiffies;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	if (unlikely(conn->suspend_rx)) {
9318c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "Rx suspended!\n");
9328c2ecf20Sopenharmony_ci		*status = ISCSI_TCP_SUSPENDED;
9338c2ecf20Sopenharmony_ci		return 0;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (offloaded) {
9378c2ecf20Sopenharmony_ci		segment->total_copied = segment->total_size;
9388c2ecf20Sopenharmony_ci		goto segment_done;
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	skb_prepare_seq_read(skb, offset, skb->len, &seq);
9428c2ecf20Sopenharmony_ci	while (1) {
9438c2ecf20Sopenharmony_ci		unsigned int avail;
9448c2ecf20Sopenharmony_ci		const u8 *ptr;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		avail = skb_seq_read(consumed, &ptr, &seq);
9478c2ecf20Sopenharmony_ci		if (avail == 0) {
9488c2ecf20Sopenharmony_ci			ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
9498c2ecf20Sopenharmony_ci				      consumed);
9508c2ecf20Sopenharmony_ci			*status = ISCSI_TCP_SKB_DONE;
9518c2ecf20Sopenharmony_ci			goto skb_done;
9528c2ecf20Sopenharmony_ci		}
9538c2ecf20Sopenharmony_ci		BUG_ON(segment->copied >= segment->size);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
9568c2ecf20Sopenharmony_ci			      avail);
9578c2ecf20Sopenharmony_ci		rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
9588c2ecf20Sopenharmony_ci		BUG_ON(rc == 0);
9598c2ecf20Sopenharmony_ci		consumed += rc;
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci		if (segment->total_copied >= segment->total_size) {
9628c2ecf20Sopenharmony_ci			skb_abort_seq_read(&seq);
9638c2ecf20Sopenharmony_ci			goto segment_done;
9648c2ecf20Sopenharmony_ci		}
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cisegment_done:
9688c2ecf20Sopenharmony_ci	*status = ISCSI_TCP_SEGMENT_DONE;
9698c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(conn, "segment done\n");
9708c2ecf20Sopenharmony_ci	rc = segment->done(tcp_conn, segment);
9718c2ecf20Sopenharmony_ci	if (rc != 0) {
9728c2ecf20Sopenharmony_ci		*status = ISCSI_TCP_CONN_ERR;
9738c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
9748c2ecf20Sopenharmony_ci		iscsi_conn_failure(conn, rc);
9758c2ecf20Sopenharmony_ci		return 0;
9768c2ecf20Sopenharmony_ci	}
9778c2ecf20Sopenharmony_ci	/* The done() functions sets up the next segment. */
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ciskb_done:
9808c2ecf20Sopenharmony_ci	conn->rxdata_octets += consumed;
9818c2ecf20Sopenharmony_ci	return consumed;
9828c2ecf20Sopenharmony_ci}
9838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci/**
9868c2ecf20Sopenharmony_ci * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
9878c2ecf20Sopenharmony_ci * @task: scsi command task
9888c2ecf20Sopenharmony_ci */
9898c2ecf20Sopenharmony_ciint iscsi_tcp_task_init(struct iscsi_task *task)
9908c2ecf20Sopenharmony_ci{
9918c2ecf20Sopenharmony_ci	struct iscsi_tcp_task *tcp_task = task->dd_data;
9928c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = task->conn;
9938c2ecf20Sopenharmony_ci	struct scsi_cmnd *sc = task->sc;
9948c2ecf20Sopenharmony_ci	int err;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	if (!sc) {
9978c2ecf20Sopenharmony_ci		/*
9988c2ecf20Sopenharmony_ci		 * mgmt tasks do not have a scatterlist since they come
9998c2ecf20Sopenharmony_ci		 * in from the iscsi interface.
10008c2ecf20Sopenharmony_ci		 */
10018c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci		return conn->session->tt->init_pdu(task, 0, task->data_count);
10048c2ecf20Sopenharmony_ci	}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	BUG_ON(kfifo_len(&tcp_task->r2tqueue));
10078c2ecf20Sopenharmony_ci	tcp_task->exp_datasn = 0;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	/* Prepare PDU, optionally w/ immediate data */
10108c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
10118c2ecf20Sopenharmony_ci		      task->itt, task->imm_count, task->unsol_r2t.data_length);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	err = conn->session->tt->init_pdu(task, 0, task->imm_count);
10148c2ecf20Sopenharmony_ci	if (err)
10158c2ecf20Sopenharmony_ci		return err;
10168c2ecf20Sopenharmony_ci	task->imm_count = 0;
10178c2ecf20Sopenharmony_ci	return 0;
10188c2ecf20Sopenharmony_ci}
10198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_cistatic struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
10228c2ecf20Sopenharmony_ci{
10238c2ecf20Sopenharmony_ci	struct iscsi_tcp_task *tcp_task = task->dd_data;
10248c2ecf20Sopenharmony_ci	struct iscsi_r2t_info *r2t = NULL;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	if (iscsi_task_has_unsol_data(task))
10278c2ecf20Sopenharmony_ci		r2t = &task->unsol_r2t;
10288c2ecf20Sopenharmony_ci	else {
10298c2ecf20Sopenharmony_ci		spin_lock_bh(&tcp_task->queue2pool);
10308c2ecf20Sopenharmony_ci		if (tcp_task->r2t) {
10318c2ecf20Sopenharmony_ci			r2t = tcp_task->r2t;
10328c2ecf20Sopenharmony_ci			/* Continue with this R2T? */
10338c2ecf20Sopenharmony_ci			if (r2t->data_length <= r2t->sent) {
10348c2ecf20Sopenharmony_ci				ISCSI_DBG_TCP(task->conn,
10358c2ecf20Sopenharmony_ci					      "  done with r2t %p\n", r2t);
10368c2ecf20Sopenharmony_ci				kfifo_in(&tcp_task->r2tpool.queue,
10378c2ecf20Sopenharmony_ci					    (void *)&tcp_task->r2t,
10388c2ecf20Sopenharmony_ci					    sizeof(void *));
10398c2ecf20Sopenharmony_ci				tcp_task->r2t = r2t = NULL;
10408c2ecf20Sopenharmony_ci			}
10418c2ecf20Sopenharmony_ci		}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci		if (r2t == NULL) {
10448c2ecf20Sopenharmony_ci			if (kfifo_out(&tcp_task->r2tqueue,
10458c2ecf20Sopenharmony_ci			    (void *)&tcp_task->r2t, sizeof(void *)) !=
10468c2ecf20Sopenharmony_ci			    sizeof(void *))
10478c2ecf20Sopenharmony_ci				r2t = NULL;
10488c2ecf20Sopenharmony_ci			else
10498c2ecf20Sopenharmony_ci				r2t = tcp_task->r2t;
10508c2ecf20Sopenharmony_ci		}
10518c2ecf20Sopenharmony_ci		spin_unlock_bh(&tcp_task->queue2pool);
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	return r2t;
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci/**
10588c2ecf20Sopenharmony_ci * iscsi_tcp_task_xmit - xmit normal PDU task
10598c2ecf20Sopenharmony_ci * @task: iscsi command task
10608c2ecf20Sopenharmony_ci *
10618c2ecf20Sopenharmony_ci * We're expected to return 0 when everything was transmitted successfully,
10628c2ecf20Sopenharmony_ci * -EAGAIN if there's still data in the queue, or != 0 for any other kind
10638c2ecf20Sopenharmony_ci * of error.
10648c2ecf20Sopenharmony_ci */
10658c2ecf20Sopenharmony_ciint iscsi_tcp_task_xmit(struct iscsi_task *task)
10668c2ecf20Sopenharmony_ci{
10678c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = task->conn;
10688c2ecf20Sopenharmony_ci	struct iscsi_session *session = conn->session;
10698c2ecf20Sopenharmony_ci	struct iscsi_r2t_info *r2t;
10708c2ecf20Sopenharmony_ci	int rc = 0;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ciflush:
10738c2ecf20Sopenharmony_ci	/* Flush any pending data first. */
10748c2ecf20Sopenharmony_ci	rc = session->tt->xmit_pdu(task);
10758c2ecf20Sopenharmony_ci	if (rc < 0)
10768c2ecf20Sopenharmony_ci		return rc;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	/* mgmt command */
10798c2ecf20Sopenharmony_ci	if (!task->sc) {
10808c2ecf20Sopenharmony_ci		if (task->hdr->itt == RESERVED_ITT)
10818c2ecf20Sopenharmony_ci			iscsi_put_task(task);
10828c2ecf20Sopenharmony_ci		return 0;
10838c2ecf20Sopenharmony_ci	}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	/* Are we done already? */
10868c2ecf20Sopenharmony_ci	if (task->sc->sc_data_direction != DMA_TO_DEVICE)
10878c2ecf20Sopenharmony_ci		return 0;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	r2t = iscsi_tcp_get_curr_r2t(task);
10908c2ecf20Sopenharmony_ci	if (r2t == NULL) {
10918c2ecf20Sopenharmony_ci		/* Waiting for more R2Ts to arrive. */
10928c2ecf20Sopenharmony_ci		ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
10938c2ecf20Sopenharmony_ci		return 0;
10948c2ecf20Sopenharmony_ci	}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
10978c2ecf20Sopenharmony_ci	if (rc)
10988c2ecf20Sopenharmony_ci		return rc;
10998c2ecf20Sopenharmony_ci	iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
11028c2ecf20Sopenharmony_ci		      r2t, r2t->datasn - 1, task->hdr->itt,
11038c2ecf20Sopenharmony_ci		      r2t->data_offset + r2t->sent, r2t->data_count);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
11068c2ecf20Sopenharmony_ci					 r2t->data_count);
11078c2ecf20Sopenharmony_ci	if (rc) {
11088c2ecf20Sopenharmony_ci		iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
11098c2ecf20Sopenharmony_ci		return rc;
11108c2ecf20Sopenharmony_ci	}
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	r2t->sent += r2t->data_count;
11138c2ecf20Sopenharmony_ci	goto flush;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_cistruct iscsi_cls_conn *
11188c2ecf20Sopenharmony_ciiscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
11198c2ecf20Sopenharmony_ci		      uint32_t conn_idx)
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci{
11228c2ecf20Sopenharmony_ci	struct iscsi_conn *conn;
11238c2ecf20Sopenharmony_ci	struct iscsi_cls_conn *cls_conn;
11248c2ecf20Sopenharmony_ci	struct iscsi_tcp_conn *tcp_conn;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	cls_conn = iscsi_conn_setup(cls_session,
11278c2ecf20Sopenharmony_ci				    sizeof(*tcp_conn) + dd_data_size, conn_idx);
11288c2ecf20Sopenharmony_ci	if (!cls_conn)
11298c2ecf20Sopenharmony_ci		return NULL;
11308c2ecf20Sopenharmony_ci	conn = cls_conn->dd_data;
11318c2ecf20Sopenharmony_ci	/*
11328c2ecf20Sopenharmony_ci	 * due to strange issues with iser these are not set
11338c2ecf20Sopenharmony_ci	 * in iscsi_conn_setup
11348c2ecf20Sopenharmony_ci	 */
11358c2ecf20Sopenharmony_ci	conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	tcp_conn = conn->dd_data;
11388c2ecf20Sopenharmony_ci	tcp_conn->iscsi_conn = conn;
11398c2ecf20Sopenharmony_ci	tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
11408c2ecf20Sopenharmony_ci	return cls_conn;
11418c2ecf20Sopenharmony_ci}
11428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_civoid iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
11458c2ecf20Sopenharmony_ci{
11468c2ecf20Sopenharmony_ci	iscsi_conn_teardown(cls_conn);
11478c2ecf20Sopenharmony_ci}
11488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ciint iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
11518c2ecf20Sopenharmony_ci{
11528c2ecf20Sopenharmony_ci	int i;
11538c2ecf20Sopenharmony_ci	int cmd_i;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	/*
11568c2ecf20Sopenharmony_ci	 * initialize per-task: R2T pool and xmit queue
11578c2ecf20Sopenharmony_ci	 */
11588c2ecf20Sopenharmony_ci	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
11598c2ecf20Sopenharmony_ci	        struct iscsi_task *task = session->cmds[cmd_i];
11608c2ecf20Sopenharmony_ci		struct iscsi_tcp_task *tcp_task = task->dd_data;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci		/*
11638c2ecf20Sopenharmony_ci		 * pre-allocated x2 as much r2ts to handle race when
11648c2ecf20Sopenharmony_ci		 * target acks DataOut faster than we data_xmit() queues
11658c2ecf20Sopenharmony_ci		 * could replenish r2tqueue.
11668c2ecf20Sopenharmony_ci		 */
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci		/* R2T pool */
11698c2ecf20Sopenharmony_ci		if (iscsi_pool_init(&tcp_task->r2tpool,
11708c2ecf20Sopenharmony_ci				    session->max_r2t * 2, NULL,
11718c2ecf20Sopenharmony_ci				    sizeof(struct iscsi_r2t_info))) {
11728c2ecf20Sopenharmony_ci			goto r2t_alloc_fail;
11738c2ecf20Sopenharmony_ci		}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci		/* R2T xmit queue */
11768c2ecf20Sopenharmony_ci		if (kfifo_alloc(&tcp_task->r2tqueue,
11778c2ecf20Sopenharmony_ci		      session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
11788c2ecf20Sopenharmony_ci			iscsi_pool_free(&tcp_task->r2tpool);
11798c2ecf20Sopenharmony_ci			goto r2t_alloc_fail;
11808c2ecf20Sopenharmony_ci		}
11818c2ecf20Sopenharmony_ci		spin_lock_init(&tcp_task->pool2queue);
11828c2ecf20Sopenharmony_ci		spin_lock_init(&tcp_task->queue2pool);
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	return 0;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_cir2t_alloc_fail:
11888c2ecf20Sopenharmony_ci	for (i = 0; i < cmd_i; i++) {
11898c2ecf20Sopenharmony_ci		struct iscsi_task *task = session->cmds[i];
11908c2ecf20Sopenharmony_ci		struct iscsi_tcp_task *tcp_task = task->dd_data;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci		kfifo_free(&tcp_task->r2tqueue);
11938c2ecf20Sopenharmony_ci		iscsi_pool_free(&tcp_task->r2tpool);
11948c2ecf20Sopenharmony_ci	}
11958c2ecf20Sopenharmony_ci	return -ENOMEM;
11968c2ecf20Sopenharmony_ci}
11978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_civoid iscsi_tcp_r2tpool_free(struct iscsi_session *session)
12008c2ecf20Sopenharmony_ci{
12018c2ecf20Sopenharmony_ci	int i;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	for (i = 0; i < session->cmds_max; i++) {
12048c2ecf20Sopenharmony_ci		struct iscsi_task *task = session->cmds[i];
12058c2ecf20Sopenharmony_ci		struct iscsi_tcp_task *tcp_task = task->dd_data;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci		kfifo_free(&tcp_task->r2tqueue);
12088c2ecf20Sopenharmony_ci		iscsi_pool_free(&tcp_task->r2tpool);
12098c2ecf20Sopenharmony_ci	}
12108c2ecf20Sopenharmony_ci}
12118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ciint iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
12148c2ecf20Sopenharmony_ci{
12158c2ecf20Sopenharmony_ci	struct iscsi_session *session = conn->session;
12168c2ecf20Sopenharmony_ci	unsigned short r2ts = 0;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	sscanf(buf, "%hu", &r2ts);
12198c2ecf20Sopenharmony_ci	if (session->max_r2t == r2ts)
12208c2ecf20Sopenharmony_ci		return 0;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	if (!r2ts || !is_power_of_2(r2ts))
12238c2ecf20Sopenharmony_ci		return -EINVAL;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	session->max_r2t = r2ts;
12268c2ecf20Sopenharmony_ci	iscsi_tcp_r2tpool_free(session);
12278c2ecf20Sopenharmony_ci	return iscsi_tcp_r2tpool_alloc(session);
12288c2ecf20Sopenharmony_ci}
12298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_civoid iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
12328c2ecf20Sopenharmony_ci			      struct iscsi_stats *stats)
12338c2ecf20Sopenharmony_ci{
12348c2ecf20Sopenharmony_ci	struct iscsi_conn *conn = cls_conn->dd_data;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	stats->txdata_octets = conn->txdata_octets;
12378c2ecf20Sopenharmony_ci	stats->rxdata_octets = conn->rxdata_octets;
12388c2ecf20Sopenharmony_ci	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
12398c2ecf20Sopenharmony_ci	stats->dataout_pdus = conn->dataout_pdus_cnt;
12408c2ecf20Sopenharmony_ci	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
12418c2ecf20Sopenharmony_ci	stats->datain_pdus = conn->datain_pdus_cnt;
12428c2ecf20Sopenharmony_ci	stats->r2t_pdus = conn->r2t_pdus_cnt;
12438c2ecf20Sopenharmony_ci	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
12448c2ecf20Sopenharmony_ci	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
12458c2ecf20Sopenharmony_ci}
12468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);
1247