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