Linux Audio

Check our new training course

Loading...
v3.15
   1/*
   2 * iSCSI over TCP/IP Data-Path lib
   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 <linux/types.h>
  30#include <linux/list.h>
  31#include <linux/inet.h>
  32#include <linux/slab.h>
  33#include <linux/file.h>
  34#include <linux/blkdev.h>
  35#include <linux/crypto.h>
  36#include <linux/delay.h>
  37#include <linux/kfifo.h>
  38#include <linux/scatterlist.h>
  39#include <linux/module.h>
  40#include <net/tcp.h>
  41#include <scsi/scsi_cmnd.h>
  42#include <scsi/scsi_device.h>
  43#include <scsi/scsi_host.h>
  44#include <scsi/scsi.h>
  45#include <scsi/scsi_transport_iscsi.h>
  46
  47#include "iscsi_tcp.h"
  48
  49MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  50	      "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  51	      "Alex Aizman <itn780@yahoo.com>");
  52MODULE_DESCRIPTION("iSCSI/TCP data-path");
  53MODULE_LICENSE("GPL");
  54
  55static int iscsi_dbg_libtcp;
  56module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
  57		   S_IRUGO | S_IWUSR);
  58MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
  59		 "module. Set to 1 to turn on, and zero to turn off. Default "
  60		 "is off.");
  61
  62#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...)			\
  63	do {							\
  64		if (iscsi_dbg_libtcp)				\
  65			iscsi_conn_printk(KERN_INFO, _conn,	\
  66					     "%s " dbg_fmt,	\
  67					     __func__, ##arg);	\
  68	} while (0);
  69
  70static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  71				   struct iscsi_segment *segment);
  72
  73/*
  74 * Scatterlist handling: inside the iscsi_segment, we
  75 * remember an index into the scatterlist, and set data/size
  76 * to the current scatterlist entry. For highmem pages, we
  77 * kmap as needed.
  78 *
  79 * Note that the page is unmapped when we return from
  80 * TCP's data_ready handler, so we may end up mapping and
  81 * unmapping the same page repeatedly. The whole reason
  82 * for this is that we shouldn't keep the page mapped
  83 * outside the softirq.
  84 */
  85
  86/**
  87 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
  88 * @segment: the buffer object
  89 * @sg: scatterlist
  90 * @offset: byte offset into that sg entry
  91 *
  92 * This function sets up the segment so that subsequent
  93 * data is copied to the indicated sg entry, at the given
  94 * offset.
  95 */
  96static inline void
  97iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
  98			  struct scatterlist *sg, unsigned int offset)
  99{
 100	segment->sg = sg;
 101	segment->sg_offset = offset;
 102	segment->size = min(sg->length - offset,
 103			    segment->total_size - segment->total_copied);
 104	segment->data = NULL;
 105}
 106
 107/**
 108 * iscsi_tcp_segment_map - map the current S/G page
 109 * @segment: iscsi_segment
 110 * @recv: 1 if called from recv path
 111 *
 112 * We only need to possibly kmap data if scatter lists are being used,
 113 * because the iscsi passthrough and internal IO paths will never use high
 114 * mem pages.
 115 */
 116static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
 117{
 118	struct scatterlist *sg;
 119
 120	if (segment->data != NULL || !segment->sg)
 121		return;
 122
 123	sg = segment->sg;
 124	BUG_ON(segment->sg_mapped);
 125	BUG_ON(sg->length == 0);
 126
 127	/*
 128	 * If the page count is greater than one it is ok to send
 129	 * to the network layer's zero copy send path. If not we
 130	 * have to go the slow sendmsg path. We always map for the
 131	 * recv path.
 132	 */
 133	if (page_count(sg_page(sg)) >= 1 && !recv)
 134		return;
 135
 136	if (recv) {
 137		segment->atomic_mapped = true;
 138		segment->sg_mapped = kmap_atomic(sg_page(sg));
 139	} else {
 140		segment->atomic_mapped = false;
 141		/* the xmit path can sleep with the page mapped so use kmap */
 142		segment->sg_mapped = kmap(sg_page(sg));
 143	}
 144
 145	segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
 146}
 147
 148void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
 149{
 150	if (segment->sg_mapped) {
 151		if (segment->atomic_mapped)
 152			kunmap_atomic(segment->sg_mapped);
 153		else
 154			kunmap(sg_page(segment->sg));
 155		segment->sg_mapped = NULL;
 156		segment->data = NULL;
 157	}
 158}
 159EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
 160
 161/*
 162 * Splice the digest buffer into the buffer
 163 */
 164static inline void
 165iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
 166{
 167	segment->data = digest;
 168	segment->digest_len = ISCSI_DIGEST_SIZE;
 169	segment->total_size += ISCSI_DIGEST_SIZE;
 170	segment->size = ISCSI_DIGEST_SIZE;
 171	segment->copied = 0;
 172	segment->sg = NULL;
 173	segment->hash = NULL;
 174}
 175
 176/**
 177 * iscsi_tcp_segment_done - check whether the segment is complete
 178 * @tcp_conn: iscsi tcp connection
 179 * @segment: iscsi segment to check
 180 * @recv: set to one of this is called from the recv path
 181 * @copied: number of bytes copied
 182 *
 183 * Check if we're done receiving this segment. If the receive
 184 * buffer is full but we expect more data, move on to the
 185 * next entry in the scatterlist.
 186 *
 187 * If the amount of data we received isn't a multiple of 4,
 188 * we will transparently receive the pad bytes, too.
 189 *
 190 * This function must be re-entrant.
 191 */
 192int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
 193			   struct iscsi_segment *segment, int recv,
 194			   unsigned copied)
 195{
 196	struct scatterlist sg;
 197	unsigned int pad;
 198
 199	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
 200		      segment->copied, copied, segment->size,
 201		      recv ? "recv" : "xmit");
 202	if (segment->hash && copied) {
 203		/*
 204		 * If a segment is kmapd we must unmap it before sending
 205		 * to the crypto layer since that will try to kmap it again.
 206		 */
 207		iscsi_tcp_segment_unmap(segment);
 208
 209		if (!segment->data) {
 210			sg_init_table(&sg, 1);
 211			sg_set_page(&sg, sg_page(segment->sg), copied,
 212				    segment->copied + segment->sg_offset +
 213							segment->sg->offset);
 214		} else
 215			sg_init_one(&sg, segment->data + segment->copied,
 216				    copied);
 217		crypto_hash_update(segment->hash, &sg, copied);
 
 218	}
 219
 220	segment->copied += copied;
 221	if (segment->copied < segment->size) {
 222		iscsi_tcp_segment_map(segment, recv);
 223		return 0;
 224	}
 225
 226	segment->total_copied += segment->copied;
 227	segment->copied = 0;
 228	segment->size = 0;
 229
 230	/* Unmap the current scatterlist page, if there is one. */
 231	iscsi_tcp_segment_unmap(segment);
 232
 233	/* Do we have more scatterlist entries? */
 234	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
 235		      segment->total_copied, segment->total_size);
 236	if (segment->total_copied < segment->total_size) {
 237		/* Proceed to the next entry in the scatterlist. */
 238		iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
 239					  0);
 240		iscsi_tcp_segment_map(segment, recv);
 241		BUG_ON(segment->size == 0);
 242		return 0;
 243	}
 244
 245	/* Do we need to handle padding? */
 246	if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
 247		pad = iscsi_padding(segment->total_copied);
 248		if (pad != 0) {
 249			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 250				      "consume %d pad bytes\n", pad);
 251			segment->total_size += pad;
 252			segment->size = pad;
 253			segment->data = segment->padbuf;
 254			return 0;
 255		}
 256	}
 257
 258	/*
 259	 * Set us up for transferring the data digest. hdr digest
 260	 * is completely handled in hdr done function.
 261	 */
 262	if (segment->hash) {
 263		crypto_hash_final(segment->hash, segment->digest);
 
 
 264		iscsi_tcp_segment_splice_digest(segment,
 265				 recv ? segment->recv_digest : segment->digest);
 266		return 0;
 267	}
 268
 269	return 1;
 270}
 271EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
 272
 273/**
 274 * iscsi_tcp_segment_recv - copy data to segment
 275 * @tcp_conn: the iSCSI TCP connection
 276 * @segment: the buffer to copy to
 277 * @ptr: data pointer
 278 * @len: amount of data available
 279 *
 280 * This function copies up to @len bytes to the
 281 * given buffer, and returns the number of bytes
 282 * consumed, which can actually be less than @len.
 283 *
 284 * If hash digest is enabled, the function will update the
 285 * hash while copying.
 286 * Combining these two operations doesn't buy us a lot (yet),
 287 * but in the future we could implement combined copy+crc,
 288 * just way we do for network layer checksums.
 289 */
 290static int
 291iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
 292		       struct iscsi_segment *segment, const void *ptr,
 293		       unsigned int len)
 294{
 295	unsigned int copy = 0, copied = 0;
 296
 297	while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
 298		if (copied == len) {
 299			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 300				      "copied %d bytes\n", len);
 301			break;
 302		}
 303
 304		copy = min(len - copied, segment->size - segment->copied);
 305		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
 306		memcpy(segment->data + segment->copied, ptr + copied, copy);
 307		copied += copy;
 308	}
 309	return copied;
 310}
 311
 312inline void
 313iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
 314		      unsigned char digest[ISCSI_DIGEST_SIZE])
 315{
 316	struct scatterlist sg;
 317
 318	sg_init_one(&sg, hdr, hdrlen);
 319	crypto_hash_digest(hash, &sg, hdrlen, digest);
 
 320}
 321EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
 322
 323static inline int
 324iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
 325		      struct iscsi_segment *segment)
 326{
 327	if (!segment->digest_len)
 328		return 1;
 329
 330	if (memcmp(segment->recv_digest, segment->digest,
 331		   segment->digest_len)) {
 332		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
 333		return 0;
 334	}
 335
 336	return 1;
 337}
 338
 339/*
 340 * Helper function to set up segment buffer
 341 */
 342static inline void
 343__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
 344		     iscsi_segment_done_fn_t *done, struct hash_desc *hash)
 345{
 346	memset(segment, 0, sizeof(*segment));
 347	segment->total_size = size;
 348	segment->done = done;
 349
 350	if (hash) {
 351		segment->hash = hash;
 352		crypto_hash_init(hash);
 353	}
 354}
 355
 356inline void
 357iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
 358			  size_t size, iscsi_segment_done_fn_t *done,
 359			  struct hash_desc *hash)
 360{
 361	__iscsi_segment_init(segment, size, done, hash);
 362	segment->data = data;
 363	segment->size = size;
 364}
 365EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
 366
 367inline int
 368iscsi_segment_seek_sg(struct iscsi_segment *segment,
 369		      struct scatterlist *sg_list, unsigned int sg_count,
 370		      unsigned int offset, size_t size,
 371		      iscsi_segment_done_fn_t *done, struct hash_desc *hash)
 
 372{
 373	struct scatterlist *sg;
 374	unsigned int i;
 375
 376	__iscsi_segment_init(segment, size, done, hash);
 377	for_each_sg(sg_list, sg, sg_count, i) {
 378		if (offset < sg->length) {
 379			iscsi_tcp_segment_init_sg(segment, sg, offset);
 380			return 0;
 381		}
 382		offset -= sg->length;
 383	}
 384
 385	return ISCSI_ERR_DATA_OFFSET;
 386}
 387EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
 388
 389/**
 390 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
 391 * @tcp_conn: iscsi connection to prep for
 392 *
 393 * This function always passes NULL for the hash argument, because when this
 394 * function is called we do not yet know the final size of the header and want
 395 * to delay the digest processing until we know that.
 396 */
 397void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
 398{
 399	ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 400		      "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
 401		      "digest enabled" : "digest disabled");
 402	iscsi_segment_init_linear(&tcp_conn->in.segment,
 403				tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
 404				iscsi_tcp_hdr_recv_done, NULL);
 405}
 406EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
 407
 408/*
 409 * Handle incoming reply to any other type of command
 410 */
 411static int
 412iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
 413			 struct iscsi_segment *segment)
 414{
 415	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 416	int rc = 0;
 417
 418	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 419		return ISCSI_ERR_DATA_DGST;
 420
 421	rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
 422			conn->data, tcp_conn->in.datalen);
 423	if (rc)
 424		return rc;
 425
 426	iscsi_tcp_hdr_recv_prep(tcp_conn);
 427	return 0;
 428}
 429
 430static void
 431iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
 432{
 433	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 434	struct hash_desc *rx_hash = NULL;
 435
 436	if (conn->datadgst_en &&
 437	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
 438		rx_hash = tcp_conn->rx_hash;
 439
 440	iscsi_segment_init_linear(&tcp_conn->in.segment,
 441				conn->data, tcp_conn->in.datalen,
 442				iscsi_tcp_data_recv_done, rx_hash);
 443}
 444
 445/**
 446 * iscsi_tcp_cleanup_task - free tcp_task resources
 447 * @task: iscsi task
 448 *
 449 * must be called with session back_lock
 450 */
 451void iscsi_tcp_cleanup_task(struct iscsi_task *task)
 452{
 453	struct iscsi_tcp_task *tcp_task = task->dd_data;
 454	struct iscsi_r2t_info *r2t;
 455
 456	/* nothing to do for mgmt */
 457	if (!task->sc)
 458		return;
 459
 460	spin_lock_bh(&tcp_task->queue2pool);
 461	/* flush task's r2t queues */
 462	while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
 463		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
 464			    sizeof(void*));
 465		ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
 466	}
 467
 468	r2t = tcp_task->r2t;
 469	if (r2t != NULL) {
 470		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
 471			    sizeof(void*));
 472		tcp_task->r2t = NULL;
 473	}
 474	spin_unlock_bh(&tcp_task->queue2pool);
 475}
 476EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
 477
 478/**
 479 * iscsi_tcp_data_in - SCSI Data-In Response processing
 480 * @conn: iscsi connection
 481 * @task: scsi command task
 482 */
 483static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
 484{
 485	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 486	struct iscsi_tcp_task *tcp_task = task->dd_data;
 487	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
 488	int datasn = be32_to_cpu(rhdr->datasn);
 489	unsigned total_in_length = scsi_in(task->sc)->length;
 490
 491	/*
 492	 * lib iscsi will update this in the completion handling if there
 493	 * is status.
 494	 */
 495	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
 496		iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
 497
 498	if (tcp_conn->in.datalen == 0)
 499		return 0;
 500
 501	if (tcp_task->exp_datasn != datasn) {
 502		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
 503			      "\n", tcp_task->exp_datasn, datasn);
 504		return ISCSI_ERR_DATASN;
 505	}
 506
 507	tcp_task->exp_datasn++;
 508
 509	tcp_task->data_offset = be32_to_cpu(rhdr->offset);
 510	if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
 511		ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
 512			      "total_length_in(%d)\n", tcp_task->data_offset,
 513			      tcp_conn->in.datalen, total_in_length);
 514		return ISCSI_ERR_DATA_OFFSET;
 515	}
 516
 517	conn->datain_pdus_cnt++;
 518	return 0;
 519}
 520
 521/**
 522 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
 523 * @conn: iscsi connection
 524 * @task: scsi command task
 525 */
 526static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
 527{
 528	struct iscsi_session *session = conn->session;
 529	struct iscsi_tcp_task *tcp_task = task->dd_data;
 530	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 531	struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
 532	struct iscsi_r2t_info *r2t;
 533	int r2tsn = be32_to_cpu(rhdr->r2tsn);
 534	u32 data_length;
 535	u32 data_offset;
 536	int rc;
 537
 538	if (tcp_conn->in.datalen) {
 539		iscsi_conn_printk(KERN_ERR, conn,
 540				  "invalid R2t with datalen %d\n",
 541				  tcp_conn->in.datalen);
 542		return ISCSI_ERR_DATALEN;
 543	}
 544
 545	if (tcp_task->exp_datasn != r2tsn){
 546		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
 547			      tcp_task->exp_datasn, r2tsn);
 548		return ISCSI_ERR_R2TSN;
 549	}
 550
 551	/* fill-in new R2T associated with the task */
 552	iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
 553
 554	if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
 555		iscsi_conn_printk(KERN_INFO, conn,
 556				  "dropping R2T itt %d in recovery.\n",
 557				  task->itt);
 558		return 0;
 559	}
 560
 561	data_length = be32_to_cpu(rhdr->data_length);
 562	if (data_length == 0) {
 563		iscsi_conn_printk(KERN_ERR, conn,
 564				  "invalid R2T with zero data len\n");
 565		return ISCSI_ERR_DATALEN;
 566	}
 567
 568	if (data_length > session->max_burst)
 569		ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
 570			      "burst %u. Attempting to execute request.\n",
 571			      data_length, session->max_burst);
 572
 573	data_offset = be32_to_cpu(rhdr->data_offset);
 574	if (data_offset + data_length > scsi_out(task->sc)->length) {
 575		iscsi_conn_printk(KERN_ERR, conn,
 576				  "invalid R2T with data len %u at offset %u "
 577				  "and total length %d\n", data_length,
 578				  data_offset, scsi_out(task->sc)->length);
 579		return ISCSI_ERR_DATALEN;
 580	}
 581
 582	spin_lock(&tcp_task->pool2queue);
 583	rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
 584	if (!rc) {
 585		iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
 586				  "Target has sent more R2Ts than it "
 587				  "negotiated for or driver has leaked.\n");
 588		spin_unlock(&tcp_task->pool2queue);
 589		return ISCSI_ERR_PROTO;
 590	}
 591
 592	r2t->exp_statsn = rhdr->statsn;
 593	r2t->data_length = data_length;
 594	r2t->data_offset = data_offset;
 595
 596	r2t->ttt = rhdr->ttt; /* no flip */
 597	r2t->datasn = 0;
 598	r2t->sent = 0;
 599
 600	tcp_task->exp_datasn = r2tsn + 1;
 601	kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
 602	conn->r2t_pdus_cnt++;
 603	spin_unlock(&tcp_task->pool2queue);
 604
 605	iscsi_requeue_task(task);
 606	return 0;
 607}
 608
 609/*
 610 * Handle incoming reply to DataIn command
 611 */
 612static int
 613iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
 614			  struct iscsi_segment *segment)
 615{
 616	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 617	struct iscsi_hdr *hdr = tcp_conn->in.hdr;
 618	int rc;
 619
 620	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 621		return ISCSI_ERR_DATA_DGST;
 622
 623	/* check for non-exceptional status */
 624	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
 625		rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
 626		if (rc)
 627			return rc;
 628	}
 629
 630	iscsi_tcp_hdr_recv_prep(tcp_conn);
 631	return 0;
 632}
 633
 634/**
 635 * iscsi_tcp_hdr_dissect - process PDU header
 636 * @conn: iSCSI connection
 637 * @hdr: PDU header
 638 *
 639 * This function analyzes the header of the PDU received,
 640 * and performs several sanity checks. If the PDU is accompanied
 641 * by data, the receive buffer is set up to copy the incoming data
 642 * to the correct location.
 643 */
 644static int
 645iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
 646{
 647	int rc = 0, opcode, ahslen;
 648	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 649	struct iscsi_task *task;
 650
 651	/* verify PDU length */
 652	tcp_conn->in.datalen = ntoh24(hdr->dlength);
 653	if (tcp_conn->in.datalen > conn->max_recv_dlength) {
 654		iscsi_conn_printk(KERN_ERR, conn,
 655				  "iscsi_tcp: datalen %d > %d\n",
 656				  tcp_conn->in.datalen, conn->max_recv_dlength);
 657		return ISCSI_ERR_DATALEN;
 658	}
 659
 660	/* Additional header segments. So far, we don't
 661	 * process additional headers.
 662	 */
 663	ahslen = hdr->hlength << 2;
 664
 665	opcode = hdr->opcode & ISCSI_OPCODE_MASK;
 666	/* verify itt (itt encoding: age+cid+itt) */
 667	rc = iscsi_verify_itt(conn, hdr->itt);
 668	if (rc)
 669		return rc;
 670
 671	ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
 672		      opcode, ahslen, tcp_conn->in.datalen);
 673
 674	switch(opcode) {
 675	case ISCSI_OP_SCSI_DATA_IN:
 676		spin_lock(&conn->session->back_lock);
 677		task = iscsi_itt_to_ctask(conn, hdr->itt);
 678		if (!task)
 679			rc = ISCSI_ERR_BAD_ITT;
 680		else
 681			rc = iscsi_tcp_data_in(conn, task);
 682		if (rc) {
 683			spin_unlock(&conn->session->back_lock);
 684			break;
 685		}
 686
 687		if (tcp_conn->in.datalen) {
 688			struct iscsi_tcp_task *tcp_task = task->dd_data;
 689			struct hash_desc *rx_hash = NULL;
 690			struct scsi_data_buffer *sdb = scsi_in(task->sc);
 691
 692			/*
 693			 * Setup copy of Data-In into the Scsi_Cmnd
 694			 * Scatterlist case:
 695			 * We set up the iscsi_segment to point to the next
 696			 * scatterlist entry to copy to. As we go along,
 697			 * we move on to the next scatterlist entry and
 698			 * update the digest per-entry.
 699			 */
 700			if (conn->datadgst_en &&
 701			    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
 702				rx_hash = tcp_conn->rx_hash;
 703
 704			ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
 705				     "offset=%d, datalen=%d)\n",
 706				      tcp_task->data_offset,
 707				      tcp_conn->in.datalen);
 708			task->last_xfer = jiffies;
 709			rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
 710						   sdb->table.sgl,
 711						   sdb->table.nents,
 712						   tcp_task->data_offset,
 713						   tcp_conn->in.datalen,
 714						   iscsi_tcp_process_data_in,
 715						   rx_hash);
 716			spin_unlock(&conn->session->back_lock);
 717			return rc;
 718		}
 719		rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
 720		spin_unlock(&conn->session->back_lock);
 721		break;
 722	case ISCSI_OP_SCSI_CMD_RSP:
 723		if (tcp_conn->in.datalen) {
 724			iscsi_tcp_data_recv_prep(tcp_conn);
 725			return 0;
 726		}
 727		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
 728		break;
 729	case ISCSI_OP_R2T:
 730		spin_lock(&conn->session->back_lock);
 731		task = iscsi_itt_to_ctask(conn, hdr->itt);
 732		spin_unlock(&conn->session->back_lock);
 733		if (!task)
 734			rc = ISCSI_ERR_BAD_ITT;
 735		else if (ahslen)
 736			rc = ISCSI_ERR_AHSLEN;
 737		else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
 738			task->last_xfer = jiffies;
 739			spin_lock(&conn->session->frwd_lock);
 740			rc = iscsi_tcp_r2t_rsp(conn, task);
 741			spin_unlock(&conn->session->frwd_lock);
 742		} else
 743			rc = ISCSI_ERR_PROTO;
 744		break;
 745	case ISCSI_OP_LOGIN_RSP:
 746	case ISCSI_OP_TEXT_RSP:
 747	case ISCSI_OP_REJECT:
 748	case ISCSI_OP_ASYNC_EVENT:
 749		/*
 750		 * It is possible that we could get a PDU with a buffer larger
 751		 * than 8K, but there are no targets that currently do this.
 752		 * For now we fail until we find a vendor that needs it
 753		 */
 754		if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
 755			iscsi_conn_printk(KERN_ERR, conn,
 756					  "iscsi_tcp: received buffer of "
 757					  "len %u but conn buffer is only %u "
 758					  "(opcode %0x)\n",
 759					  tcp_conn->in.datalen,
 760					  ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
 761			rc = ISCSI_ERR_PROTO;
 762			break;
 763		}
 764
 765		/* If there's data coming in with the response,
 766		 * receive it to the connection's buffer.
 767		 */
 768		if (tcp_conn->in.datalen) {
 769			iscsi_tcp_data_recv_prep(tcp_conn);
 770			return 0;
 771		}
 772	/* fall through */
 773	case ISCSI_OP_LOGOUT_RSP:
 774	case ISCSI_OP_NOOP_IN:
 775	case ISCSI_OP_SCSI_TMFUNC_RSP:
 776		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
 777		break;
 778	default:
 779		rc = ISCSI_ERR_BAD_OPCODE;
 780		break;
 781	}
 782
 783	if (rc == 0) {
 784		/* Anything that comes with data should have
 785		 * been handled above. */
 786		if (tcp_conn->in.datalen)
 787			return ISCSI_ERR_PROTO;
 788		iscsi_tcp_hdr_recv_prep(tcp_conn);
 789	}
 790
 791	return rc;
 792}
 793
 794/**
 795 * iscsi_tcp_hdr_recv_done - process PDU header
 796 *
 797 * This is the callback invoked when the PDU header has
 798 * been received. If the header is followed by additional
 799 * header segments, we go back for more data.
 800 */
 801static int
 802iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
 803			struct iscsi_segment *segment)
 804{
 805	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 806	struct iscsi_hdr *hdr;
 807
 808	/* Check if there are additional header segments
 809	 * *prior* to computing the digest, because we
 810	 * may need to go back to the caller for more.
 811	 */
 812	hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
 813	if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
 814		/* Bump the header length - the caller will
 815		 * just loop around and get the AHS for us, and
 816		 * call again. */
 817		unsigned int ahslen = hdr->hlength << 2;
 818
 819		/* Make sure we don't overflow */
 820		if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
 821			return ISCSI_ERR_AHSLEN;
 822
 823		segment->total_size += ahslen;
 824		segment->size += ahslen;
 825		return 0;
 826	}
 827
 828	/* We're done processing the header. See if we're doing
 829	 * header digests; if so, set up the recv_digest buffer
 830	 * and go back for more. */
 831	if (conn->hdrdgst_en &&
 832	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
 833		if (segment->digest_len == 0) {
 834			/*
 835			 * Even if we offload the digest processing we
 836			 * splice it in so we can increment the skb/segment
 837			 * counters in preparation for the data segment.
 838			 */
 839			iscsi_tcp_segment_splice_digest(segment,
 840							segment->recv_digest);
 841			return 0;
 842		}
 843
 844		iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
 845				      segment->total_copied - ISCSI_DIGEST_SIZE,
 846				      segment->digest);
 847
 848		if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 849			return ISCSI_ERR_HDR_DGST;
 850	}
 851
 852	tcp_conn->in.hdr = hdr;
 853	return iscsi_tcp_hdr_dissect(conn, hdr);
 854}
 855
 856/**
 857 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
 858 * @tcp_conn: iscsi tcp conn
 859 *
 860 * returns non zero if we are currently processing or setup to process
 861 * a header.
 862 */
 863inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
 864{
 865	return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
 866}
 867EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
 868
 869/**
 870 * iscsi_tcp_recv_skb - Process skb
 871 * @conn: iscsi connection
 872 * @skb: network buffer with header and/or data segment
 873 * @offset: offset in skb
 874 * @offload: bool indicating if transfer was offloaded
 875 *
 876 * Will return status of transfer in status. And will return
 877 * number of bytes copied.
 878 */
 879int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
 880		       unsigned int offset, bool offloaded, int *status)
 881{
 882	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 883	struct iscsi_segment *segment = &tcp_conn->in.segment;
 884	struct skb_seq_state seq;
 885	unsigned int consumed = 0;
 886	int rc = 0;
 887
 888	ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
 889	/*
 890	 * Update for each skb instead of pdu, because over slow networks a
 891	 * data_in's data could take a while to read in. We also want to
 892	 * account for r2ts.
 893	 */
 894	conn->last_recv = jiffies;
 895
 896	if (unlikely(conn->suspend_rx)) {
 897		ISCSI_DBG_TCP(conn, "Rx suspended!\n");
 898		*status = ISCSI_TCP_SUSPENDED;
 899		return 0;
 900	}
 901
 902	if (offloaded) {
 903		segment->total_copied = segment->total_size;
 904		goto segment_done;
 905	}
 906
 907	skb_prepare_seq_read(skb, offset, skb->len, &seq);
 908	while (1) {
 909		unsigned int avail;
 910		const u8 *ptr;
 911
 912		avail = skb_seq_read(consumed, &ptr, &seq);
 913		if (avail == 0) {
 914			ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
 915				      consumed);
 916			*status = ISCSI_TCP_SKB_DONE;
 917			goto skb_done;
 918		}
 919		BUG_ON(segment->copied >= segment->size);
 920
 921		ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
 922			      avail);
 923		rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
 924		BUG_ON(rc == 0);
 925		consumed += rc;
 926
 927		if (segment->total_copied >= segment->total_size) {
 928			skb_abort_seq_read(&seq);
 929			goto segment_done;
 930		}
 931	}
 932
 933segment_done:
 934	*status = ISCSI_TCP_SEGMENT_DONE;
 935	ISCSI_DBG_TCP(conn, "segment done\n");
 936	rc = segment->done(tcp_conn, segment);
 937	if (rc != 0) {
 938		*status = ISCSI_TCP_CONN_ERR;
 939		ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
 940		iscsi_conn_failure(conn, rc);
 941		return 0;
 942	}
 943	/* The done() functions sets up the next segment. */
 944
 945skb_done:
 946	conn->rxdata_octets += consumed;
 947	return consumed;
 948}
 949EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
 950
 951/**
 952 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
 953 * @conn: iscsi connection
 954 * @task: scsi command task
 955 * @sc: scsi command
 956 */
 957int iscsi_tcp_task_init(struct iscsi_task *task)
 958{
 959	struct iscsi_tcp_task *tcp_task = task->dd_data;
 960	struct iscsi_conn *conn = task->conn;
 961	struct scsi_cmnd *sc = task->sc;
 962	int err;
 963
 964	if (!sc) {
 965		/*
 966		 * mgmt tasks do not have a scatterlist since they come
 967		 * in from the iscsi interface.
 968		 */
 969		ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
 970
 971		return conn->session->tt->init_pdu(task, 0, task->data_count);
 972	}
 973
 974	BUG_ON(kfifo_len(&tcp_task->r2tqueue));
 975	tcp_task->exp_datasn = 0;
 976
 977	/* Prepare PDU, optionally w/ immediate data */
 978	ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
 979		      task->itt, task->imm_count, task->unsol_r2t.data_length);
 980
 981	err = conn->session->tt->init_pdu(task, 0, task->imm_count);
 982	if (err)
 983		return err;
 984	task->imm_count = 0;
 985	return 0;
 986}
 987EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
 988
 989static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
 990{
 991	struct iscsi_tcp_task *tcp_task = task->dd_data;
 992	struct iscsi_r2t_info *r2t = NULL;
 993
 994	if (iscsi_task_has_unsol_data(task))
 995		r2t = &task->unsol_r2t;
 996	else {
 997		spin_lock_bh(&tcp_task->queue2pool);
 998		if (tcp_task->r2t) {
 999			r2t = tcp_task->r2t;
1000			/* Continue with this R2T? */
1001			if (r2t->data_length <= r2t->sent) {
1002				ISCSI_DBG_TCP(task->conn,
1003					      "  done with r2t %p\n", r2t);
1004				kfifo_in(&tcp_task->r2tpool.queue,
1005					    (void *)&tcp_task->r2t,
1006					    sizeof(void *));
1007				tcp_task->r2t = r2t = NULL;
1008			}
1009		}
1010
1011		if (r2t == NULL) {
1012			if (kfifo_out(&tcp_task->r2tqueue,
1013			    (void *)&tcp_task->r2t, sizeof(void *)) !=
1014			    sizeof(void *))
1015				r2t = NULL;
1016			else
1017				r2t = tcp_task->r2t;
1018		}
1019		spin_unlock_bh(&tcp_task->queue2pool);
1020	}
1021
1022	return r2t;
1023}
1024
1025/**
1026 * iscsi_tcp_task_xmit - xmit normal PDU task
1027 * @task: iscsi command task
1028 *
1029 * We're expected to return 0 when everything was transmitted successfully,
1030 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1031 * of error.
1032 */
1033int iscsi_tcp_task_xmit(struct iscsi_task *task)
1034{
1035	struct iscsi_conn *conn = task->conn;
1036	struct iscsi_session *session = conn->session;
1037	struct iscsi_r2t_info *r2t;
1038	int rc = 0;
1039
1040flush:
1041	/* Flush any pending data first. */
1042	rc = session->tt->xmit_pdu(task);
1043	if (rc < 0)
1044		return rc;
1045
1046	/* mgmt command */
1047	if (!task->sc) {
1048		if (task->hdr->itt == RESERVED_ITT)
1049			iscsi_put_task(task);
1050		return 0;
1051	}
1052
1053	/* Are we done already? */
1054	if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1055		return 0;
1056
1057	r2t = iscsi_tcp_get_curr_r2t(task);
1058	if (r2t == NULL) {
1059		/* Waiting for more R2Ts to arrive. */
1060		ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
1061		return 0;
1062	}
1063
1064	rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1065	if (rc)
1066		return rc;
1067	iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1068
1069	ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1070		      r2t, r2t->datasn - 1, task->hdr->itt,
1071		      r2t->data_offset + r2t->sent, r2t->data_count);
1072
1073	rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1074					 r2t->data_count);
1075	if (rc) {
1076		iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
1077		return rc;
1078	}
1079
1080	r2t->sent += r2t->data_count;
1081	goto flush;
1082}
1083EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1084
1085struct iscsi_cls_conn *
1086iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1087		      uint32_t conn_idx)
1088
1089{
1090	struct iscsi_conn *conn;
1091	struct iscsi_cls_conn *cls_conn;
1092	struct iscsi_tcp_conn *tcp_conn;
1093
1094	cls_conn = iscsi_conn_setup(cls_session,
1095				    sizeof(*tcp_conn) + dd_data_size, conn_idx);
1096	if (!cls_conn)
1097		return NULL;
1098	conn = cls_conn->dd_data;
1099	/*
1100	 * due to strange issues with iser these are not set
1101	 * in iscsi_conn_setup
1102	 */
1103	conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1104
1105	tcp_conn = conn->dd_data;
1106	tcp_conn->iscsi_conn = conn;
1107	tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
1108	return cls_conn;
1109}
1110EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1111
1112void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1113{
1114	iscsi_conn_teardown(cls_conn);
1115}
1116EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1117
1118int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1119{
1120	int i;
1121	int cmd_i;
1122
1123	/*
1124	 * initialize per-task: R2T pool and xmit queue
1125	 */
1126	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1127	        struct iscsi_task *task = session->cmds[cmd_i];
1128		struct iscsi_tcp_task *tcp_task = task->dd_data;
1129
1130		/*
1131		 * pre-allocated x2 as much r2ts to handle race when
1132		 * target acks DataOut faster than we data_xmit() queues
1133		 * could replenish r2tqueue.
1134		 */
1135
1136		/* R2T pool */
1137		if (iscsi_pool_init(&tcp_task->r2tpool,
1138				    session->max_r2t * 2, NULL,
1139				    sizeof(struct iscsi_r2t_info))) {
1140			goto r2t_alloc_fail;
1141		}
1142
1143		/* R2T xmit queue */
1144		if (kfifo_alloc(&tcp_task->r2tqueue,
1145		      session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
1146			iscsi_pool_free(&tcp_task->r2tpool);
1147			goto r2t_alloc_fail;
1148		}
1149		spin_lock_init(&tcp_task->pool2queue);
1150		spin_lock_init(&tcp_task->queue2pool);
1151	}
1152
1153	return 0;
1154
1155r2t_alloc_fail:
1156	for (i = 0; i < cmd_i; i++) {
1157		struct iscsi_task *task = session->cmds[i];
1158		struct iscsi_tcp_task *tcp_task = task->dd_data;
1159
1160		kfifo_free(&tcp_task->r2tqueue);
1161		iscsi_pool_free(&tcp_task->r2tpool);
1162	}
1163	return -ENOMEM;
1164}
1165EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1166
1167void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1168{
1169	int i;
1170
1171	for (i = 0; i < session->cmds_max; i++) {
1172		struct iscsi_task *task = session->cmds[i];
1173		struct iscsi_tcp_task *tcp_task = task->dd_data;
1174
1175		kfifo_free(&tcp_task->r2tqueue);
1176		iscsi_pool_free(&tcp_task->r2tpool);
1177	}
1178}
1179EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1180
1181int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1182{
1183	struct iscsi_session *session = conn->session;
1184	unsigned short r2ts = 0;
1185
1186	sscanf(buf, "%hu", &r2ts);
1187	if (session->max_r2t == r2ts)
1188		return 0;
1189
1190	if (!r2ts || !is_power_of_2(r2ts))
1191		return -EINVAL;
1192
1193	session->max_r2t = r2ts;
1194	iscsi_tcp_r2tpool_free(session);
1195	return iscsi_tcp_r2tpool_alloc(session);
1196}
1197EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1198
1199void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1200			      struct iscsi_stats *stats)
1201{
1202	struct iscsi_conn *conn = cls_conn->dd_data;
1203
1204	stats->txdata_octets = conn->txdata_octets;
1205	stats->rxdata_octets = conn->rxdata_octets;
1206	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1207	stats->dataout_pdus = conn->dataout_pdus_cnt;
1208	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1209	stats->datain_pdus = conn->datain_pdus_cnt;
1210	stats->r2t_pdus = conn->r2t_pdus_cnt;
1211	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1212	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1213}
1214EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);
v4.6
   1/*
   2 * iSCSI over TCP/IP Data-Path lib
   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/list.h>
  32#include <linux/inet.h>
  33#include <linux/slab.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 <net/tcp.h>
  41#include <scsi/scsi_cmnd.h>
  42#include <scsi/scsi_device.h>
  43#include <scsi/scsi_host.h>
  44#include <scsi/scsi.h>
  45#include <scsi/scsi_transport_iscsi.h>
  46
  47#include "iscsi_tcp.h"
  48
  49MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  50	      "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  51	      "Alex Aizman <itn780@yahoo.com>");
  52MODULE_DESCRIPTION("iSCSI/TCP data-path");
  53MODULE_LICENSE("GPL");
  54
  55static int iscsi_dbg_libtcp;
  56module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
  57		   S_IRUGO | S_IWUSR);
  58MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
  59		 "module. Set to 1 to turn on, and zero to turn off. Default "
  60		 "is off.");
  61
  62#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...)			\
  63	do {							\
  64		if (iscsi_dbg_libtcp)				\
  65			iscsi_conn_printk(KERN_INFO, _conn,	\
  66					     "%s " dbg_fmt,	\
  67					     __func__, ##arg);	\
  68	} while (0);
  69
  70static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  71				   struct iscsi_segment *segment);
  72
  73/*
  74 * Scatterlist handling: inside the iscsi_segment, we
  75 * remember an index into the scatterlist, and set data/size
  76 * to the current scatterlist entry. For highmem pages, we
  77 * kmap as needed.
  78 *
  79 * Note that the page is unmapped when we return from
  80 * TCP's data_ready handler, so we may end up mapping and
  81 * unmapping the same page repeatedly. The whole reason
  82 * for this is that we shouldn't keep the page mapped
  83 * outside the softirq.
  84 */
  85
  86/**
  87 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
  88 * @segment: the buffer object
  89 * @sg: scatterlist
  90 * @offset: byte offset into that sg entry
  91 *
  92 * This function sets up the segment so that subsequent
  93 * data is copied to the indicated sg entry, at the given
  94 * offset.
  95 */
  96static inline void
  97iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
  98			  struct scatterlist *sg, unsigned int offset)
  99{
 100	segment->sg = sg;
 101	segment->sg_offset = offset;
 102	segment->size = min(sg->length - offset,
 103			    segment->total_size - segment->total_copied);
 104	segment->data = NULL;
 105}
 106
 107/**
 108 * iscsi_tcp_segment_map - map the current S/G page
 109 * @segment: iscsi_segment
 110 * @recv: 1 if called from recv path
 111 *
 112 * We only need to possibly kmap data if scatter lists are being used,
 113 * because the iscsi passthrough and internal IO paths will never use high
 114 * mem pages.
 115 */
 116static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
 117{
 118	struct scatterlist *sg;
 119
 120	if (segment->data != NULL || !segment->sg)
 121		return;
 122
 123	sg = segment->sg;
 124	BUG_ON(segment->sg_mapped);
 125	BUG_ON(sg->length == 0);
 126
 127	/*
 128	 * If the page count is greater than one it is ok to send
 129	 * to the network layer's zero copy send path. If not we
 130	 * have to go the slow sendmsg path. We always map for the
 131	 * recv path.
 132	 */
 133	if (page_count(sg_page(sg)) >= 1 && !recv)
 134		return;
 135
 136	if (recv) {
 137		segment->atomic_mapped = true;
 138		segment->sg_mapped = kmap_atomic(sg_page(sg));
 139	} else {
 140		segment->atomic_mapped = false;
 141		/* the xmit path can sleep with the page mapped so use kmap */
 142		segment->sg_mapped = kmap(sg_page(sg));
 143	}
 144
 145	segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
 146}
 147
 148void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
 149{
 150	if (segment->sg_mapped) {
 151		if (segment->atomic_mapped)
 152			kunmap_atomic(segment->sg_mapped);
 153		else
 154			kunmap(sg_page(segment->sg));
 155		segment->sg_mapped = NULL;
 156		segment->data = NULL;
 157	}
 158}
 159EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
 160
 161/*
 162 * Splice the digest buffer into the buffer
 163 */
 164static inline void
 165iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
 166{
 167	segment->data = digest;
 168	segment->digest_len = ISCSI_DIGEST_SIZE;
 169	segment->total_size += ISCSI_DIGEST_SIZE;
 170	segment->size = ISCSI_DIGEST_SIZE;
 171	segment->copied = 0;
 172	segment->sg = NULL;
 173	segment->hash = NULL;
 174}
 175
 176/**
 177 * iscsi_tcp_segment_done - check whether the segment is complete
 178 * @tcp_conn: iscsi tcp connection
 179 * @segment: iscsi segment to check
 180 * @recv: set to one of this is called from the recv path
 181 * @copied: number of bytes copied
 182 *
 183 * Check if we're done receiving this segment. If the receive
 184 * buffer is full but we expect more data, move on to the
 185 * next entry in the scatterlist.
 186 *
 187 * If the amount of data we received isn't a multiple of 4,
 188 * we will transparently receive the pad bytes, too.
 189 *
 190 * This function must be re-entrant.
 191 */
 192int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
 193			   struct iscsi_segment *segment, int recv,
 194			   unsigned copied)
 195{
 196	struct scatterlist sg;
 197	unsigned int pad;
 198
 199	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
 200		      segment->copied, copied, segment->size,
 201		      recv ? "recv" : "xmit");
 202	if (segment->hash && copied) {
 203		/*
 204		 * If a segment is kmapd we must unmap it before sending
 205		 * to the crypto layer since that will try to kmap it again.
 206		 */
 207		iscsi_tcp_segment_unmap(segment);
 208
 209		if (!segment->data) {
 210			sg_init_table(&sg, 1);
 211			sg_set_page(&sg, sg_page(segment->sg), copied,
 212				    segment->copied + segment->sg_offset +
 213							segment->sg->offset);
 214		} else
 215			sg_init_one(&sg, segment->data + segment->copied,
 216				    copied);
 217		ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
 218		crypto_ahash_update(segment->hash);
 219	}
 220
 221	segment->copied += copied;
 222	if (segment->copied < segment->size) {
 223		iscsi_tcp_segment_map(segment, recv);
 224		return 0;
 225	}
 226
 227	segment->total_copied += segment->copied;
 228	segment->copied = 0;
 229	segment->size = 0;
 230
 231	/* Unmap the current scatterlist page, if there is one. */
 232	iscsi_tcp_segment_unmap(segment);
 233
 234	/* Do we have more scatterlist entries? */
 235	ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
 236		      segment->total_copied, segment->total_size);
 237	if (segment->total_copied < segment->total_size) {
 238		/* Proceed to the next entry in the scatterlist. */
 239		iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
 240					  0);
 241		iscsi_tcp_segment_map(segment, recv);
 242		BUG_ON(segment->size == 0);
 243		return 0;
 244	}
 245
 246	/* Do we need to handle padding? */
 247	if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
 248		pad = iscsi_padding(segment->total_copied);
 249		if (pad != 0) {
 250			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 251				      "consume %d pad bytes\n", pad);
 252			segment->total_size += pad;
 253			segment->size = pad;
 254			segment->data = segment->padbuf;
 255			return 0;
 256		}
 257	}
 258
 259	/*
 260	 * Set us up for transferring the data digest. hdr digest
 261	 * is completely handled in hdr done function.
 262	 */
 263	if (segment->hash) {
 264		ahash_request_set_crypt(segment->hash, NULL,
 265					segment->digest, 0);
 266		crypto_ahash_final(segment->hash);
 267		iscsi_tcp_segment_splice_digest(segment,
 268				 recv ? segment->recv_digest : segment->digest);
 269		return 0;
 270	}
 271
 272	return 1;
 273}
 274EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
 275
 276/**
 277 * iscsi_tcp_segment_recv - copy data to segment
 278 * @tcp_conn: the iSCSI TCP connection
 279 * @segment: the buffer to copy to
 280 * @ptr: data pointer
 281 * @len: amount of data available
 282 *
 283 * This function copies up to @len bytes to the
 284 * given buffer, and returns the number of bytes
 285 * consumed, which can actually be less than @len.
 286 *
 287 * If hash digest is enabled, the function will update the
 288 * hash while copying.
 289 * Combining these two operations doesn't buy us a lot (yet),
 290 * but in the future we could implement combined copy+crc,
 291 * just way we do for network layer checksums.
 292 */
 293static int
 294iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
 295		       struct iscsi_segment *segment, const void *ptr,
 296		       unsigned int len)
 297{
 298	unsigned int copy = 0, copied = 0;
 299
 300	while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
 301		if (copied == len) {
 302			ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 303				      "copied %d bytes\n", len);
 304			break;
 305		}
 306
 307		copy = min(len - copied, segment->size - segment->copied);
 308		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
 309		memcpy(segment->data + segment->copied, ptr + copied, copy);
 310		copied += copy;
 311	}
 312	return copied;
 313}
 314
 315inline void
 316iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
 317		      size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
 318{
 319	struct scatterlist sg;
 320
 321	sg_init_one(&sg, hdr, hdrlen);
 322	ahash_request_set_crypt(hash, &sg, digest, hdrlen);
 323	crypto_ahash_digest(hash);
 324}
 325EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
 326
 327static inline int
 328iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
 329		      struct iscsi_segment *segment)
 330{
 331	if (!segment->digest_len)
 332		return 1;
 333
 334	if (memcmp(segment->recv_digest, segment->digest,
 335		   segment->digest_len)) {
 336		ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
 337		return 0;
 338	}
 339
 340	return 1;
 341}
 342
 343/*
 344 * Helper function to set up segment buffer
 345 */
 346static inline void
 347__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
 348		     iscsi_segment_done_fn_t *done, struct ahash_request *hash)
 349{
 350	memset(segment, 0, sizeof(*segment));
 351	segment->total_size = size;
 352	segment->done = done;
 353
 354	if (hash) {
 355		segment->hash = hash;
 356		crypto_ahash_init(hash);
 357	}
 358}
 359
 360inline void
 361iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
 362			  size_t size, iscsi_segment_done_fn_t *done,
 363			  struct ahash_request *hash)
 364{
 365	__iscsi_segment_init(segment, size, done, hash);
 366	segment->data = data;
 367	segment->size = size;
 368}
 369EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
 370
 371inline int
 372iscsi_segment_seek_sg(struct iscsi_segment *segment,
 373		      struct scatterlist *sg_list, unsigned int sg_count,
 374		      unsigned int offset, size_t size,
 375		      iscsi_segment_done_fn_t *done,
 376		      struct ahash_request *hash)
 377{
 378	struct scatterlist *sg;
 379	unsigned int i;
 380
 381	__iscsi_segment_init(segment, size, done, hash);
 382	for_each_sg(sg_list, sg, sg_count, i) {
 383		if (offset < sg->length) {
 384			iscsi_tcp_segment_init_sg(segment, sg, offset);
 385			return 0;
 386		}
 387		offset -= sg->length;
 388	}
 389
 390	return ISCSI_ERR_DATA_OFFSET;
 391}
 392EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
 393
 394/**
 395 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
 396 * @tcp_conn: iscsi connection to prep for
 397 *
 398 * This function always passes NULL for the hash argument, because when this
 399 * function is called we do not yet know the final size of the header and want
 400 * to delay the digest processing until we know that.
 401 */
 402void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
 403{
 404	ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
 405		      "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
 406		      "digest enabled" : "digest disabled");
 407	iscsi_segment_init_linear(&tcp_conn->in.segment,
 408				tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
 409				iscsi_tcp_hdr_recv_done, NULL);
 410}
 411EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
 412
 413/*
 414 * Handle incoming reply to any other type of command
 415 */
 416static int
 417iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
 418			 struct iscsi_segment *segment)
 419{
 420	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 421	int rc = 0;
 422
 423	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 424		return ISCSI_ERR_DATA_DGST;
 425
 426	rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
 427			conn->data, tcp_conn->in.datalen);
 428	if (rc)
 429		return rc;
 430
 431	iscsi_tcp_hdr_recv_prep(tcp_conn);
 432	return 0;
 433}
 434
 435static void
 436iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
 437{
 438	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 439	struct ahash_request *rx_hash = NULL;
 440
 441	if (conn->datadgst_en &&
 442	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
 443		rx_hash = tcp_conn->rx_hash;
 444
 445	iscsi_segment_init_linear(&tcp_conn->in.segment,
 446				conn->data, tcp_conn->in.datalen,
 447				iscsi_tcp_data_recv_done, rx_hash);
 448}
 449
 450/**
 451 * iscsi_tcp_cleanup_task - free tcp_task resources
 452 * @task: iscsi task
 453 *
 454 * must be called with session back_lock
 455 */
 456void iscsi_tcp_cleanup_task(struct iscsi_task *task)
 457{
 458	struct iscsi_tcp_task *tcp_task = task->dd_data;
 459	struct iscsi_r2t_info *r2t;
 460
 461	/* nothing to do for mgmt */
 462	if (!task->sc)
 463		return;
 464
 465	spin_lock_bh(&tcp_task->queue2pool);
 466	/* flush task's r2t queues */
 467	while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
 468		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
 469			    sizeof(void*));
 470		ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
 471	}
 472
 473	r2t = tcp_task->r2t;
 474	if (r2t != NULL) {
 475		kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
 476			    sizeof(void*));
 477		tcp_task->r2t = NULL;
 478	}
 479	spin_unlock_bh(&tcp_task->queue2pool);
 480}
 481EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
 482
 483/**
 484 * iscsi_tcp_data_in - SCSI Data-In Response processing
 485 * @conn: iscsi connection
 486 * @task: scsi command task
 487 */
 488static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
 489{
 490	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 491	struct iscsi_tcp_task *tcp_task = task->dd_data;
 492	struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
 493	int datasn = be32_to_cpu(rhdr->datasn);
 494	unsigned total_in_length = scsi_in(task->sc)->length;
 495
 496	/*
 497	 * lib iscsi will update this in the completion handling if there
 498	 * is status.
 499	 */
 500	if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
 501		iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
 502
 503	if (tcp_conn->in.datalen == 0)
 504		return 0;
 505
 506	if (tcp_task->exp_datasn != datasn) {
 507		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
 508			      "\n", tcp_task->exp_datasn, datasn);
 509		return ISCSI_ERR_DATASN;
 510	}
 511
 512	tcp_task->exp_datasn++;
 513
 514	tcp_task->data_offset = be32_to_cpu(rhdr->offset);
 515	if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
 516		ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
 517			      "total_length_in(%d)\n", tcp_task->data_offset,
 518			      tcp_conn->in.datalen, total_in_length);
 519		return ISCSI_ERR_DATA_OFFSET;
 520	}
 521
 522	conn->datain_pdus_cnt++;
 523	return 0;
 524}
 525
 526/**
 527 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
 528 * @conn: iscsi connection
 529 * @task: scsi command task
 530 */
 531static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
 532{
 533	struct iscsi_session *session = conn->session;
 534	struct iscsi_tcp_task *tcp_task = task->dd_data;
 535	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 536	struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
 537	struct iscsi_r2t_info *r2t;
 538	int r2tsn = be32_to_cpu(rhdr->r2tsn);
 539	u32 data_length;
 540	u32 data_offset;
 541	int rc;
 542
 543	if (tcp_conn->in.datalen) {
 544		iscsi_conn_printk(KERN_ERR, conn,
 545				  "invalid R2t with datalen %d\n",
 546				  tcp_conn->in.datalen);
 547		return ISCSI_ERR_DATALEN;
 548	}
 549
 550	if (tcp_task->exp_datasn != r2tsn){
 551		ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
 552			      tcp_task->exp_datasn, r2tsn);
 553		return ISCSI_ERR_R2TSN;
 554	}
 555
 556	/* fill-in new R2T associated with the task */
 557	iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
 558
 559	if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
 560		iscsi_conn_printk(KERN_INFO, conn,
 561				  "dropping R2T itt %d in recovery.\n",
 562				  task->itt);
 563		return 0;
 564	}
 565
 566	data_length = be32_to_cpu(rhdr->data_length);
 567	if (data_length == 0) {
 568		iscsi_conn_printk(KERN_ERR, conn,
 569				  "invalid R2T with zero data len\n");
 570		return ISCSI_ERR_DATALEN;
 571	}
 572
 573	if (data_length > session->max_burst)
 574		ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
 575			      "burst %u. Attempting to execute request.\n",
 576			      data_length, session->max_burst);
 577
 578	data_offset = be32_to_cpu(rhdr->data_offset);
 579	if (data_offset + data_length > scsi_out(task->sc)->length) {
 580		iscsi_conn_printk(KERN_ERR, conn,
 581				  "invalid R2T with data len %u at offset %u "
 582				  "and total length %d\n", data_length,
 583				  data_offset, scsi_out(task->sc)->length);
 584		return ISCSI_ERR_DATALEN;
 585	}
 586
 587	spin_lock(&tcp_task->pool2queue);
 588	rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
 589	if (!rc) {
 590		iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
 591				  "Target has sent more R2Ts than it "
 592				  "negotiated for or driver has leaked.\n");
 593		spin_unlock(&tcp_task->pool2queue);
 594		return ISCSI_ERR_PROTO;
 595	}
 596
 597	r2t->exp_statsn = rhdr->statsn;
 598	r2t->data_length = data_length;
 599	r2t->data_offset = data_offset;
 600
 601	r2t->ttt = rhdr->ttt; /* no flip */
 602	r2t->datasn = 0;
 603	r2t->sent = 0;
 604
 605	tcp_task->exp_datasn = r2tsn + 1;
 606	kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
 607	conn->r2t_pdus_cnt++;
 608	spin_unlock(&tcp_task->pool2queue);
 609
 610	iscsi_requeue_task(task);
 611	return 0;
 612}
 613
 614/*
 615 * Handle incoming reply to DataIn command
 616 */
 617static int
 618iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
 619			  struct iscsi_segment *segment)
 620{
 621	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 622	struct iscsi_hdr *hdr = tcp_conn->in.hdr;
 623	int rc;
 624
 625	if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 626		return ISCSI_ERR_DATA_DGST;
 627
 628	/* check for non-exceptional status */
 629	if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
 630		rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
 631		if (rc)
 632			return rc;
 633	}
 634
 635	iscsi_tcp_hdr_recv_prep(tcp_conn);
 636	return 0;
 637}
 638
 639/**
 640 * iscsi_tcp_hdr_dissect - process PDU header
 641 * @conn: iSCSI connection
 642 * @hdr: PDU header
 643 *
 644 * This function analyzes the header of the PDU received,
 645 * and performs several sanity checks. If the PDU is accompanied
 646 * by data, the receive buffer is set up to copy the incoming data
 647 * to the correct location.
 648 */
 649static int
 650iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
 651{
 652	int rc = 0, opcode, ahslen;
 653	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 654	struct iscsi_task *task;
 655
 656	/* verify PDU length */
 657	tcp_conn->in.datalen = ntoh24(hdr->dlength);
 658	if (tcp_conn->in.datalen > conn->max_recv_dlength) {
 659		iscsi_conn_printk(KERN_ERR, conn,
 660				  "iscsi_tcp: datalen %d > %d\n",
 661				  tcp_conn->in.datalen, conn->max_recv_dlength);
 662		return ISCSI_ERR_DATALEN;
 663	}
 664
 665	/* Additional header segments. So far, we don't
 666	 * process additional headers.
 667	 */
 668	ahslen = hdr->hlength << 2;
 669
 670	opcode = hdr->opcode & ISCSI_OPCODE_MASK;
 671	/* verify itt (itt encoding: age+cid+itt) */
 672	rc = iscsi_verify_itt(conn, hdr->itt);
 673	if (rc)
 674		return rc;
 675
 676	ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
 677		      opcode, ahslen, tcp_conn->in.datalen);
 678
 679	switch(opcode) {
 680	case ISCSI_OP_SCSI_DATA_IN:
 681		spin_lock(&conn->session->back_lock);
 682		task = iscsi_itt_to_ctask(conn, hdr->itt);
 683		if (!task)
 684			rc = ISCSI_ERR_BAD_ITT;
 685		else
 686			rc = iscsi_tcp_data_in(conn, task);
 687		if (rc) {
 688			spin_unlock(&conn->session->back_lock);
 689			break;
 690		}
 691
 692		if (tcp_conn->in.datalen) {
 693			struct iscsi_tcp_task *tcp_task = task->dd_data;
 694			struct ahash_request *rx_hash = NULL;
 695			struct scsi_data_buffer *sdb = scsi_in(task->sc);
 696
 697			/*
 698			 * Setup copy of Data-In into the Scsi_Cmnd
 699			 * Scatterlist case:
 700			 * We set up the iscsi_segment to point to the next
 701			 * scatterlist entry to copy to. As we go along,
 702			 * we move on to the next scatterlist entry and
 703			 * update the digest per-entry.
 704			 */
 705			if (conn->datadgst_en &&
 706			    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
 707				rx_hash = tcp_conn->rx_hash;
 708
 709			ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
 710				     "offset=%d, datalen=%d)\n",
 711				      tcp_task->data_offset,
 712				      tcp_conn->in.datalen);
 713			task->last_xfer = jiffies;
 714			rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
 715						   sdb->table.sgl,
 716						   sdb->table.nents,
 717						   tcp_task->data_offset,
 718						   tcp_conn->in.datalen,
 719						   iscsi_tcp_process_data_in,
 720						   rx_hash);
 721			spin_unlock(&conn->session->back_lock);
 722			return rc;
 723		}
 724		rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
 725		spin_unlock(&conn->session->back_lock);
 726		break;
 727	case ISCSI_OP_SCSI_CMD_RSP:
 728		if (tcp_conn->in.datalen) {
 729			iscsi_tcp_data_recv_prep(tcp_conn);
 730			return 0;
 731		}
 732		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
 733		break;
 734	case ISCSI_OP_R2T:
 735		spin_lock(&conn->session->back_lock);
 736		task = iscsi_itt_to_ctask(conn, hdr->itt);
 737		spin_unlock(&conn->session->back_lock);
 738		if (!task)
 739			rc = ISCSI_ERR_BAD_ITT;
 740		else if (ahslen)
 741			rc = ISCSI_ERR_AHSLEN;
 742		else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
 743			task->last_xfer = jiffies;
 744			spin_lock(&conn->session->frwd_lock);
 745			rc = iscsi_tcp_r2t_rsp(conn, task);
 746			spin_unlock(&conn->session->frwd_lock);
 747		} else
 748			rc = ISCSI_ERR_PROTO;
 749		break;
 750	case ISCSI_OP_LOGIN_RSP:
 751	case ISCSI_OP_TEXT_RSP:
 752	case ISCSI_OP_REJECT:
 753	case ISCSI_OP_ASYNC_EVENT:
 754		/*
 755		 * It is possible that we could get a PDU with a buffer larger
 756		 * than 8K, but there are no targets that currently do this.
 757		 * For now we fail until we find a vendor that needs it
 758		 */
 759		if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
 760			iscsi_conn_printk(KERN_ERR, conn,
 761					  "iscsi_tcp: received buffer of "
 762					  "len %u but conn buffer is only %u "
 763					  "(opcode %0x)\n",
 764					  tcp_conn->in.datalen,
 765					  ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
 766			rc = ISCSI_ERR_PROTO;
 767			break;
 768		}
 769
 770		/* If there's data coming in with the response,
 771		 * receive it to the connection's buffer.
 772		 */
 773		if (tcp_conn->in.datalen) {
 774			iscsi_tcp_data_recv_prep(tcp_conn);
 775			return 0;
 776		}
 777	/* fall through */
 778	case ISCSI_OP_LOGOUT_RSP:
 779	case ISCSI_OP_NOOP_IN:
 780	case ISCSI_OP_SCSI_TMFUNC_RSP:
 781		rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
 782		break;
 783	default:
 784		rc = ISCSI_ERR_BAD_OPCODE;
 785		break;
 786	}
 787
 788	if (rc == 0) {
 789		/* Anything that comes with data should have
 790		 * been handled above. */
 791		if (tcp_conn->in.datalen)
 792			return ISCSI_ERR_PROTO;
 793		iscsi_tcp_hdr_recv_prep(tcp_conn);
 794	}
 795
 796	return rc;
 797}
 798
 799/**
 800 * iscsi_tcp_hdr_recv_done - process PDU header
 801 *
 802 * This is the callback invoked when the PDU header has
 803 * been received. If the header is followed by additional
 804 * header segments, we go back for more data.
 805 */
 806static int
 807iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
 808			struct iscsi_segment *segment)
 809{
 810	struct iscsi_conn *conn = tcp_conn->iscsi_conn;
 811	struct iscsi_hdr *hdr;
 812
 813	/* Check if there are additional header segments
 814	 * *prior* to computing the digest, because we
 815	 * may need to go back to the caller for more.
 816	 */
 817	hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
 818	if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
 819		/* Bump the header length - the caller will
 820		 * just loop around and get the AHS for us, and
 821		 * call again. */
 822		unsigned int ahslen = hdr->hlength << 2;
 823
 824		/* Make sure we don't overflow */
 825		if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
 826			return ISCSI_ERR_AHSLEN;
 827
 828		segment->total_size += ahslen;
 829		segment->size += ahslen;
 830		return 0;
 831	}
 832
 833	/* We're done processing the header. See if we're doing
 834	 * header digests; if so, set up the recv_digest buffer
 835	 * and go back for more. */
 836	if (conn->hdrdgst_en &&
 837	    !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
 838		if (segment->digest_len == 0) {
 839			/*
 840			 * Even if we offload the digest processing we
 841			 * splice it in so we can increment the skb/segment
 842			 * counters in preparation for the data segment.
 843			 */
 844			iscsi_tcp_segment_splice_digest(segment,
 845							segment->recv_digest);
 846			return 0;
 847		}
 848
 849		iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
 850				      segment->total_copied - ISCSI_DIGEST_SIZE,
 851				      segment->digest);
 852
 853		if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
 854			return ISCSI_ERR_HDR_DGST;
 855	}
 856
 857	tcp_conn->in.hdr = hdr;
 858	return iscsi_tcp_hdr_dissect(conn, hdr);
 859}
 860
 861/**
 862 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
 863 * @tcp_conn: iscsi tcp conn
 864 *
 865 * returns non zero if we are currently processing or setup to process
 866 * a header.
 867 */
 868inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
 869{
 870	return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
 871}
 872EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
 873
 874/**
 875 * iscsi_tcp_recv_skb - Process skb
 876 * @conn: iscsi connection
 877 * @skb: network buffer with header and/or data segment
 878 * @offset: offset in skb
 879 * @offload: bool indicating if transfer was offloaded
 880 *
 881 * Will return status of transfer in status. And will return
 882 * number of bytes copied.
 883 */
 884int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
 885		       unsigned int offset, bool offloaded, int *status)
 886{
 887	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
 888	struct iscsi_segment *segment = &tcp_conn->in.segment;
 889	struct skb_seq_state seq;
 890	unsigned int consumed = 0;
 891	int rc = 0;
 892
 893	ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
 894	/*
 895	 * Update for each skb instead of pdu, because over slow networks a
 896	 * data_in's data could take a while to read in. We also want to
 897	 * account for r2ts.
 898	 */
 899	conn->last_recv = jiffies;
 900
 901	if (unlikely(conn->suspend_rx)) {
 902		ISCSI_DBG_TCP(conn, "Rx suspended!\n");
 903		*status = ISCSI_TCP_SUSPENDED;
 904		return 0;
 905	}
 906
 907	if (offloaded) {
 908		segment->total_copied = segment->total_size;
 909		goto segment_done;
 910	}
 911
 912	skb_prepare_seq_read(skb, offset, skb->len, &seq);
 913	while (1) {
 914		unsigned int avail;
 915		const u8 *ptr;
 916
 917		avail = skb_seq_read(consumed, &ptr, &seq);
 918		if (avail == 0) {
 919			ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
 920				      consumed);
 921			*status = ISCSI_TCP_SKB_DONE;
 922			goto skb_done;
 923		}
 924		BUG_ON(segment->copied >= segment->size);
 925
 926		ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
 927			      avail);
 928		rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
 929		BUG_ON(rc == 0);
 930		consumed += rc;
 931
 932		if (segment->total_copied >= segment->total_size) {
 933			skb_abort_seq_read(&seq);
 934			goto segment_done;
 935		}
 936	}
 937
 938segment_done:
 939	*status = ISCSI_TCP_SEGMENT_DONE;
 940	ISCSI_DBG_TCP(conn, "segment done\n");
 941	rc = segment->done(tcp_conn, segment);
 942	if (rc != 0) {
 943		*status = ISCSI_TCP_CONN_ERR;
 944		ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
 945		iscsi_conn_failure(conn, rc);
 946		return 0;
 947	}
 948	/* The done() functions sets up the next segment. */
 949
 950skb_done:
 951	conn->rxdata_octets += consumed;
 952	return consumed;
 953}
 954EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
 955
 956/**
 957 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
 958 * @conn: iscsi connection
 959 * @task: scsi command task
 960 * @sc: scsi command
 961 */
 962int iscsi_tcp_task_init(struct iscsi_task *task)
 963{
 964	struct iscsi_tcp_task *tcp_task = task->dd_data;
 965	struct iscsi_conn *conn = task->conn;
 966	struct scsi_cmnd *sc = task->sc;
 967	int err;
 968
 969	if (!sc) {
 970		/*
 971		 * mgmt tasks do not have a scatterlist since they come
 972		 * in from the iscsi interface.
 973		 */
 974		ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
 975
 976		return conn->session->tt->init_pdu(task, 0, task->data_count);
 977	}
 978
 979	BUG_ON(kfifo_len(&tcp_task->r2tqueue));
 980	tcp_task->exp_datasn = 0;
 981
 982	/* Prepare PDU, optionally w/ immediate data */
 983	ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
 984		      task->itt, task->imm_count, task->unsol_r2t.data_length);
 985
 986	err = conn->session->tt->init_pdu(task, 0, task->imm_count);
 987	if (err)
 988		return err;
 989	task->imm_count = 0;
 990	return 0;
 991}
 992EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
 993
 994static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
 995{
 996	struct iscsi_tcp_task *tcp_task = task->dd_data;
 997	struct iscsi_r2t_info *r2t = NULL;
 998
 999	if (iscsi_task_has_unsol_data(task))
1000		r2t = &task->unsol_r2t;
1001	else {
1002		spin_lock_bh(&tcp_task->queue2pool);
1003		if (tcp_task->r2t) {
1004			r2t = tcp_task->r2t;
1005			/* Continue with this R2T? */
1006			if (r2t->data_length <= r2t->sent) {
1007				ISCSI_DBG_TCP(task->conn,
1008					      "  done with r2t %p\n", r2t);
1009				kfifo_in(&tcp_task->r2tpool.queue,
1010					    (void *)&tcp_task->r2t,
1011					    sizeof(void *));
1012				tcp_task->r2t = r2t = NULL;
1013			}
1014		}
1015
1016		if (r2t == NULL) {
1017			if (kfifo_out(&tcp_task->r2tqueue,
1018			    (void *)&tcp_task->r2t, sizeof(void *)) !=
1019			    sizeof(void *))
1020				r2t = NULL;
1021			else
1022				r2t = tcp_task->r2t;
1023		}
1024		spin_unlock_bh(&tcp_task->queue2pool);
1025	}
1026
1027	return r2t;
1028}
1029
1030/**
1031 * iscsi_tcp_task_xmit - xmit normal PDU task
1032 * @task: iscsi command task
1033 *
1034 * We're expected to return 0 when everything was transmitted successfully,
1035 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1036 * of error.
1037 */
1038int iscsi_tcp_task_xmit(struct iscsi_task *task)
1039{
1040	struct iscsi_conn *conn = task->conn;
1041	struct iscsi_session *session = conn->session;
1042	struct iscsi_r2t_info *r2t;
1043	int rc = 0;
1044
1045flush:
1046	/* Flush any pending data first. */
1047	rc = session->tt->xmit_pdu(task);
1048	if (rc < 0)
1049		return rc;
1050
1051	/* mgmt command */
1052	if (!task->sc) {
1053		if (task->hdr->itt == RESERVED_ITT)
1054			iscsi_put_task(task);
1055		return 0;
1056	}
1057
1058	/* Are we done already? */
1059	if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1060		return 0;
1061
1062	r2t = iscsi_tcp_get_curr_r2t(task);
1063	if (r2t == NULL) {
1064		/* Waiting for more R2Ts to arrive. */
1065		ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
1066		return 0;
1067	}
1068
1069	rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1070	if (rc)
1071		return rc;
1072	iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1073
1074	ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1075		      r2t, r2t->datasn - 1, task->hdr->itt,
1076		      r2t->data_offset + r2t->sent, r2t->data_count);
1077
1078	rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1079					 r2t->data_count);
1080	if (rc) {
1081		iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
1082		return rc;
1083	}
1084
1085	r2t->sent += r2t->data_count;
1086	goto flush;
1087}
1088EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1089
1090struct iscsi_cls_conn *
1091iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1092		      uint32_t conn_idx)
1093
1094{
1095	struct iscsi_conn *conn;
1096	struct iscsi_cls_conn *cls_conn;
1097	struct iscsi_tcp_conn *tcp_conn;
1098
1099	cls_conn = iscsi_conn_setup(cls_session,
1100				    sizeof(*tcp_conn) + dd_data_size, conn_idx);
1101	if (!cls_conn)
1102		return NULL;
1103	conn = cls_conn->dd_data;
1104	/*
1105	 * due to strange issues with iser these are not set
1106	 * in iscsi_conn_setup
1107	 */
1108	conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1109
1110	tcp_conn = conn->dd_data;
1111	tcp_conn->iscsi_conn = conn;
1112	tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
1113	return cls_conn;
1114}
1115EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1116
1117void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1118{
1119	iscsi_conn_teardown(cls_conn);
1120}
1121EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1122
1123int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1124{
1125	int i;
1126	int cmd_i;
1127
1128	/*
1129	 * initialize per-task: R2T pool and xmit queue
1130	 */
1131	for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1132	        struct iscsi_task *task = session->cmds[cmd_i];
1133		struct iscsi_tcp_task *tcp_task = task->dd_data;
1134
1135		/*
1136		 * pre-allocated x2 as much r2ts to handle race when
1137		 * target acks DataOut faster than we data_xmit() queues
1138		 * could replenish r2tqueue.
1139		 */
1140
1141		/* R2T pool */
1142		if (iscsi_pool_init(&tcp_task->r2tpool,
1143				    session->max_r2t * 2, NULL,
1144				    sizeof(struct iscsi_r2t_info))) {
1145			goto r2t_alloc_fail;
1146		}
1147
1148		/* R2T xmit queue */
1149		if (kfifo_alloc(&tcp_task->r2tqueue,
1150		      session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
1151			iscsi_pool_free(&tcp_task->r2tpool);
1152			goto r2t_alloc_fail;
1153		}
1154		spin_lock_init(&tcp_task->pool2queue);
1155		spin_lock_init(&tcp_task->queue2pool);
1156	}
1157
1158	return 0;
1159
1160r2t_alloc_fail:
1161	for (i = 0; i < cmd_i; i++) {
1162		struct iscsi_task *task = session->cmds[i];
1163		struct iscsi_tcp_task *tcp_task = task->dd_data;
1164
1165		kfifo_free(&tcp_task->r2tqueue);
1166		iscsi_pool_free(&tcp_task->r2tpool);
1167	}
1168	return -ENOMEM;
1169}
1170EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1171
1172void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1173{
1174	int i;
1175
1176	for (i = 0; i < session->cmds_max; i++) {
1177		struct iscsi_task *task = session->cmds[i];
1178		struct iscsi_tcp_task *tcp_task = task->dd_data;
1179
1180		kfifo_free(&tcp_task->r2tqueue);
1181		iscsi_pool_free(&tcp_task->r2tpool);
1182	}
1183}
1184EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1185
1186int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1187{
1188	struct iscsi_session *session = conn->session;
1189	unsigned short r2ts = 0;
1190
1191	sscanf(buf, "%hu", &r2ts);
1192	if (session->max_r2t == r2ts)
1193		return 0;
1194
1195	if (!r2ts || !is_power_of_2(r2ts))
1196		return -EINVAL;
1197
1198	session->max_r2t = r2ts;
1199	iscsi_tcp_r2tpool_free(session);
1200	return iscsi_tcp_r2tpool_alloc(session);
1201}
1202EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1203
1204void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1205			      struct iscsi_stats *stats)
1206{
1207	struct iscsi_conn *conn = cls_conn->dd_data;
1208
1209	stats->txdata_octets = conn->txdata_octets;
1210	stats->rxdata_octets = conn->rxdata_octets;
1211	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1212	stats->dataout_pdus = conn->dataout_pdus_cnt;
1213	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1214	stats->datain_pdus = conn->datain_pdus_cnt;
1215	stats->r2t_pdus = conn->r2t_pdus_cnt;
1216	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1217	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1218}
1219EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);