xref: /spdk/lib/iscsi/iscsi.c (revision ae7b5890ef728af40bd233a5011b924c482603bf)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/base64.h"
38 #include "spdk/crc32.h"
39 #include "spdk/endian.h"
40 #include "spdk/env.h"
41 #include "spdk/likely.h"
42 #include "spdk/trace.h"
43 #include "spdk/string.h"
44 #include "spdk/queue.h"
45 #include "spdk/net.h"
46 
47 #include "iscsi/md5.h"
48 #include "iscsi/iscsi.h"
49 #include "iscsi/param.h"
50 #include "iscsi/tgt_node.h"
51 #include "iscsi/task.h"
52 #include "iscsi/conn.h"
53 #include "spdk/scsi.h"
54 #include "spdk/bdev.h"
55 #include "iscsi/portal_grp.h"
56 
57 #include "spdk_internal/log.h"
58 
59 #define MAX_TMPBUF 1024
60 
61 #define SPDK_CRC32C_INITIAL    0xffffffffUL
62 #define SPDK_CRC32C_XOR        0xffffffffUL
63 
64 #ifdef __FreeBSD__
65 #define HAVE_SRANDOMDEV 1
66 #define HAVE_ARC4RANDOM 1
67 #endif
68 
69 struct spdk_iscsi_globals g_spdk_iscsi = {
70 	.mutex = PTHREAD_MUTEX_INITIALIZER,
71 	.portal_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.portal_head),
72 	.pg_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.pg_head),
73 	.ig_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.ig_head),
74 	.target_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.target_head),
75 	.auth_group_head = TAILQ_HEAD_INITIALIZER(g_spdk_iscsi.auth_group_head),
76 };
77 
78 /* random value generation */
79 static void gen_random(uint8_t *buf, size_t len);
80 #ifndef HAVE_SRANDOMDEV
81 static void srandomdev(void);
82 #endif /* HAVE_SRANDOMDEV */
83 #ifndef HAVE_ARC4RANDOM
84 /* static uint32_t arc4random(void); */
85 #endif /* HAVE_ARC4RANDOM */
86 
87 static int add_transfer_task(struct spdk_iscsi_conn *conn,
88 			     struct spdk_iscsi_task *task);
89 
90 static int iscsi_send_r2t(struct spdk_iscsi_conn *conn,
91 			  struct spdk_iscsi_task *task, int offset,
92 			  int len, uint32_t transfer_tag, uint32_t *R2TSN);
93 static int iscsi_send_r2t_recovery(struct spdk_iscsi_conn *conn,
94 				   struct spdk_iscsi_task *r2t_task, uint32_t r2t_sn,
95 				   bool send_new_r2tsn);
96 
97 static int create_iscsi_sess(struct spdk_iscsi_conn *conn,
98 			     struct spdk_iscsi_tgt_node *target, enum session_type session_type);
99 static uint8_t append_iscsi_sess(struct spdk_iscsi_conn *conn,
100 				 const char *initiator_port_name, uint16_t tsih, uint16_t cid);
101 
102 static void remove_acked_pdu(struct spdk_iscsi_conn *conn, uint32_t ExpStatSN);
103 
104 static int iscsi_reject(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
105 			int reason);
106 
107 #define DMIN32(A,B) ((uint32_t) ((uint32_t)(A) > (uint32_t)(B) ? (uint32_t)(B) : (uint32_t)(A)))
108 #define DMIN64(A,B) ((uint64_t) ((A) > (B) ? (B) : (A)))
109 
110 #define MATCH_DIGEST_WORD(BUF, CRC32C) \
111 	(    ((((uint32_t) *((uint8_t *)(BUF)+0)) << 0)		\
112 	    | (((uint32_t) *((uint8_t *)(BUF)+1)) << 8)		\
113 	    | (((uint32_t) *((uint8_t *)(BUF)+2)) << 16)	\
114 	    | (((uint32_t) *((uint8_t *)(BUF)+3)) << 24))	\
115 	    == (CRC32C))
116 
117 #if 0
118 static int
119 match_digest_word(const uint8_t *buf, uint32_t crc32c)
120 {
121 	uint32_t l;
122 
123 	l = (buf[0] & 0xffU) << 0;
124 	l |= (buf[1] & 0xffU) << 8;
125 	l |= (buf[2] & 0xffU) << 16;
126 	l |= (buf[3] & 0xffU) << 24;
127 	return (l == crc32c);
128 }
129 
130 static uint8_t *
131 make_digest_word(uint8_t *buf, size_t len, uint32_t crc32c)
132 {
133 	if (len < ISCSI_DIGEST_LEN) {
134 		return NULL;
135 	}
136 
137 	buf[0] = (crc32c >> 0) & 0xffU;
138 	buf[1] = (crc32c >> 8) & 0xffU;
139 	buf[2] = (crc32c >> 16) & 0xffU;
140 	buf[3] = (crc32c >> 24) & 0xffU;
141 	return buf;
142 }
143 #endif
144 
145 #ifndef HAVE_SRANDOMDEV
146 static void
147 srandomdev(void)
148 {
149 	unsigned long seed;
150 	time_t now;
151 	pid_t pid;
152 
153 	pid = getpid();
154 	now = time(NULL);
155 	seed = pid ^ now;
156 	srandom(seed);
157 }
158 #endif /* HAVE_SRANDOMDEV */
159 
160 #ifndef HAVE_ARC4RANDOM
161 static int g_arc4random_initialized = 0;
162 
163 static uint32_t
164 arc4random(void)
165 {
166 	uint32_t r;
167 	uint32_t r1, r2;
168 
169 	if (!g_arc4random_initialized) {
170 		srandomdev();
171 		g_arc4random_initialized = 1;
172 	}
173 	r1 = (uint32_t)(random() & 0xffff);
174 	r2 = (uint32_t)(random() & 0xffff);
175 	r = (r1 << 16) | r2;
176 	return r;
177 }
178 #endif /* HAVE_ARC4RANDOM */
179 
180 static void
181 gen_random(uint8_t *buf, size_t len)
182 {
183 #ifdef USE_RANDOM
184 	long l;
185 	size_t idx;
186 
187 	srandomdev();
188 	for (idx = 0; idx < len; idx++) {
189 		l = random();
190 		buf[idx] = (uint8_t) l;
191 	}
192 #else
193 	uint32_t r;
194 	size_t idx;
195 
196 	for (idx = 0; idx < len; idx++) {
197 		r = arc4random();
198 		buf[idx] = (uint8_t) r;
199 	}
200 #endif /* USE_RANDOM */
201 }
202 
203 static uint64_t
204 iscsi_get_isid(const uint8_t isid[6])
205 {
206 	return (uint64_t)isid[0] << 40 |
207 	       (uint64_t)isid[1] << 32 |
208 	       (uint64_t)isid[2] << 24 |
209 	       (uint64_t)isid[3] << 16 |
210 	       (uint64_t)isid[4] << 8 |
211 	       (uint64_t)isid[5];
212 }
213 
214 static int
215 bin2hex(char *buf, size_t len, const uint8_t *data, size_t data_len)
216 {
217 	const char *digits = "0123456789ABCDEF";
218 	size_t total = 0;
219 	size_t idx;
220 
221 	if (len < 3) {
222 		return -1;
223 	}
224 	buf[total] = '0';
225 	total++;
226 	buf[total] = 'x';
227 	total++;
228 	buf[total] = '\0';
229 
230 	for (idx = 0; idx < data_len; idx++) {
231 		if (total + 3 > len) {
232 			buf[total] = '\0';
233 			return - 1;
234 		}
235 		buf[total] = digits[(data[idx] >> 4) & 0x0fU];
236 		total++;
237 		buf[total] = digits[data[idx] & 0x0fU];
238 		total++;
239 	}
240 	buf[total] = '\0';
241 	return total;
242 }
243 
244 static int
245 hex2bin(uint8_t *data, size_t data_len, const char *str)
246 {
247 	const char *digits = "0123456789ABCDEF";
248 	const char *dp;
249 	const char *p;
250 	size_t total = 0;
251 	int n0, n1;
252 
253 	p = str;
254 	if (p[0] != '0' && (p[1] != 'x' && p[1] != 'X')) {
255 		return -1;
256 	}
257 	p += 2;
258 
259 	while (p[0] != '\0' && p[1] != '\0') {
260 		if (total >= data_len) {
261 			return -1;
262 		}
263 		dp = strchr(digits, toupper((int) p[0]));
264 		if (dp == NULL) {
265 			return -1;
266 		}
267 		n0 = (int)(dp - digits);
268 		dp = strchr(digits, toupper((int) p[1]));
269 		if (dp == NULL) {
270 			return -1;
271 		}
272 		n1 = (int)(dp - digits);
273 
274 		data[total] = (uint8_t)(((n0 & 0x0fU) << 4) | (n1 & 0x0fU));
275 		total++;
276 		p += 2;
277 	}
278 	return total;
279 }
280 
281 uint32_t
282 spdk_iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu)
283 {
284 	uint32_t crc32c;
285 	uint32_t ahs_len_bytes = pdu->bhs.total_ahs_len * 4;
286 
287 	crc32c = SPDK_CRC32C_INITIAL;
288 	crc32c = spdk_crc32c_update(&pdu->bhs, ISCSI_BHS_LEN, crc32c);
289 
290 	if (ahs_len_bytes) {
291 		crc32c = spdk_crc32c_update(pdu->ahs, ahs_len_bytes, crc32c);
292 	}
293 
294 	/* BHS and AHS are always 4-byte multiples in length, so no padding is necessary. */
295 	crc32c = crc32c ^ SPDK_CRC32C_XOR;
296 	return crc32c;
297 }
298 
299 uint32_t
300 spdk_iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu)
301 {
302 	uint32_t data_len = DGET24(pdu->bhs.data_segment_len);
303 	uint32_t crc32c;
304 	uint32_t mod;
305 	struct iovec iov;
306 	uint32_t num_blocks;
307 
308 	crc32c = SPDK_CRC32C_INITIAL;
309 	if (spdk_likely(!pdu->dif_insert_or_strip)) {
310 		crc32c = spdk_crc32c_update(pdu->data, data_len, crc32c);
311 	} else {
312 		iov.iov_base = pdu->data_buf;
313 		iov.iov_len = pdu->data_buf_len;
314 		num_blocks = pdu->data_buf_len / pdu->dif_ctx.block_size;
315 
316 		spdk_dif_update_crc32c(&iov, 1, num_blocks, &crc32c, &pdu->dif_ctx);
317 	}
318 
319 	mod = data_len % ISCSI_ALIGNMENT;
320 	if (mod != 0) {
321 		uint32_t pad_length = ISCSI_ALIGNMENT - mod;
322 		uint8_t pad[3] = {0, 0, 0};
323 
324 		assert(pad_length > 0);
325 		assert(pad_length <= sizeof(pad));
326 		crc32c = spdk_crc32c_update(pad, pad_length, crc32c);
327 	}
328 
329 	crc32c = crc32c ^ SPDK_CRC32C_XOR;
330 	return crc32c;
331 }
332 
333 static bool
334 iscsi_check_data_segment_length(struct spdk_iscsi_conn *conn,
335 				struct spdk_iscsi_pdu *pdu, int data_len)
336 {
337 	int max_segment_len;
338 
339 	/*
340 	 * Determine the maximum segment length expected for this PDU.
341 	 *  This will be used to make sure the initiator did not send
342 	 *  us too much immediate data.
343 	 *
344 	 * This value is specified separately by the initiator and target,
345 	 *  and not negotiated.  So we can use the #define safely here,
346 	 *  since the value is not dependent on the initiator's maximum
347 	 *  segment lengths (FirstBurstLength/MaxRecvDataSegmentLength),
348 	 *  and SPDK currently does not allow configuration of these values
349 	 *  at runtime.
350 	 */
351 	if (conn->sess == NULL) {
352 		/*
353 		 * If the connection does not yet have a session, then
354 		 *  login is not complete and we use the 8KB default
355 		 *  FirstBurstLength as our maximum data segment length
356 		 *  value.
357 		 */
358 		max_segment_len = SPDK_ISCSI_FIRST_BURST_LENGTH;
359 	} else if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAOUT ||
360 		   pdu->bhs.opcode == ISCSI_OP_NOPOUT) {
361 		max_segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
362 	} else {
363 		max_segment_len = spdk_get_max_immediate_data_size();
364 	}
365 	if (data_len <= max_segment_len) {
366 		return true;
367 	} else {
368 		SPDK_ERRLOG("Data(%d) > MaxSegment(%d)\n", data_len, max_segment_len);
369 		return false;
370 	}
371 }
372 
373 static int
374 iscsi_conn_read_data_segment(struct spdk_iscsi_conn *conn,
375 			     struct spdk_iscsi_pdu *pdu,
376 			     uint32_t segment_len)
377 {
378 	struct iovec buf_iov, iovs[32];
379 	int rc, _rc;
380 
381 	if (spdk_likely(!pdu->dif_insert_or_strip)) {
382 		return spdk_iscsi_conn_read_data(conn,
383 						 segment_len - pdu->data_valid_bytes,
384 						 pdu->data_buf + pdu->data_valid_bytes);
385 	} else {
386 		buf_iov.iov_base = pdu->data_buf;
387 		buf_iov.iov_len = pdu->data_buf_len;
388 		rc = spdk_dif_set_md_interleave_iovs(iovs, 32, &buf_iov, 1,
389 						     pdu->data_valid_bytes,
390 						     segment_len - pdu->data_valid_bytes, NULL,
391 						     &pdu->dif_ctx);
392 		if (rc > 0) {
393 			rc = spdk_iscsi_conn_readv_data(conn, iovs, rc);
394 			if (rc > 0) {
395 				_rc = spdk_dif_generate_stream(&buf_iov, 1,
396 							       pdu->data_valid_bytes, rc,
397 							       &pdu->dif_ctx);
398 				if (_rc != 0) {
399 					SPDK_ERRLOG("DIF generate failed\n");
400 					rc = _rc;
401 				}
402 			}
403 		} else {
404 			SPDK_ERRLOG("Setup iovs for interleaved metadata failed\n");
405 		}
406 		return rc;
407 	}
408 }
409 
410 int
411 spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu)
412 {
413 	struct spdk_iscsi_pdu *pdu;
414 	struct spdk_mempool *pool;
415 	uint32_t crc32c;
416 	int ahs_len;
417 	int data_len;
418 	int rc;
419 
420 	if (conn->pdu_in_progress == NULL) {
421 		conn->pdu_in_progress = spdk_get_pdu();
422 		if (conn->pdu_in_progress == NULL) {
423 			return SPDK_ISCSI_CONNECTION_FATAL;
424 		}
425 	}
426 
427 	pdu = conn->pdu_in_progress;
428 
429 	if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
430 		rc = spdk_iscsi_conn_read_data(conn,
431 					       ISCSI_BHS_LEN - pdu->bhs_valid_bytes,
432 					       (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes);
433 		if (rc < 0) {
434 			goto error;
435 		}
436 		pdu->bhs_valid_bytes += rc;
437 		if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
438 			return 0;
439 		}
440 	}
441 
442 	data_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len));
443 
444 	/* AHS */
445 	ahs_len = pdu->bhs.total_ahs_len * 4;
446 	assert(ahs_len <= ISCSI_AHS_LEN);
447 	if (pdu->ahs_valid_bytes < ahs_len) {
448 		rc = spdk_iscsi_conn_read_data(conn,
449 					       ahs_len - pdu->ahs_valid_bytes,
450 					       pdu->ahs + pdu->ahs_valid_bytes);
451 		if (rc < 0) {
452 			goto error;
453 		}
454 
455 		pdu->ahs_valid_bytes += rc;
456 		if (pdu->ahs_valid_bytes < ahs_len) {
457 			return 0;
458 		}
459 	}
460 
461 	/* Header Digest */
462 	if (conn->header_digest &&
463 	    pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) {
464 		rc = spdk_iscsi_conn_read_data(conn,
465 					       ISCSI_DIGEST_LEN - pdu->hdigest_valid_bytes,
466 					       pdu->header_digest + pdu->hdigest_valid_bytes);
467 		if (rc < 0) {
468 			goto error;
469 		}
470 
471 		pdu->hdigest_valid_bytes += rc;
472 		if (pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) {
473 			return 0;
474 		}
475 	}
476 
477 	/* copy the actual data into local buffer */
478 	if (pdu->data_valid_bytes < data_len) {
479 		if (pdu->data_buf == NULL) {
480 			if (data_len <= spdk_get_max_immediate_data_size()) {
481 				pool = g_spdk_iscsi.pdu_immediate_data_pool;
482 				pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(spdk_get_max_immediate_data_size());
483 			} else if (data_len <= SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) {
484 				pool = g_spdk_iscsi.pdu_data_out_pool;
485 				pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
486 			} else {
487 				SPDK_ERRLOG("Data(%d) > MaxSegment(%d)\n",
488 					    data_len, SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
489 				rc = SPDK_ISCSI_CONNECTION_FATAL;
490 				goto error;
491 			}
492 			pdu->mobj = spdk_mempool_get(pool);
493 			if (pdu->mobj == NULL) {
494 				return 0;
495 			}
496 			pdu->data_buf = pdu->mobj->buf;
497 
498 			if (spdk_unlikely(spdk_iscsi_get_dif_ctx(conn, pdu, &pdu->dif_ctx))) {
499 				pdu->dif_insert_or_strip = true;
500 			}
501 		}
502 
503 		rc = iscsi_conn_read_data_segment(conn, pdu, data_len);
504 		if (rc < 0) {
505 			goto error;
506 		}
507 
508 		pdu->data_valid_bytes += rc;
509 		if (pdu->data_valid_bytes < data_len) {
510 			return 0;
511 		}
512 	}
513 
514 	/* copy out the data digest */
515 	if (conn->data_digest && data_len != 0 &&
516 	    pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) {
517 		rc = spdk_iscsi_conn_read_data(conn,
518 					       ISCSI_DIGEST_LEN - pdu->ddigest_valid_bytes,
519 					       pdu->data_digest + pdu->ddigest_valid_bytes);
520 		if (rc < 0) {
521 			goto error;
522 		}
523 
524 		pdu->ddigest_valid_bytes += rc;
525 		if (pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) {
526 			return 0;
527 		}
528 	}
529 
530 	/* All data for this PDU has now been read from the socket. */
531 	conn->pdu_in_progress = NULL;
532 
533 	spdk_trace_record(TRACE_ISCSI_READ_PDU, conn->id, pdu->data_valid_bytes,
534 			  (uintptr_t)pdu, pdu->bhs.opcode);
535 
536 	/* Data Segment */
537 	if (data_len != 0) {
538 		if (!iscsi_check_data_segment_length(conn, pdu, data_len)) {
539 			rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
540 			/*
541 			 * If spdk_iscsi_reject() was not able to reject the PDU,
542 			 * treat it as a fatal connection error.  Otherwise,
543 			 * return SUCCESS here so that the caller will continue
544 			 * to attempt to read PDUs.
545 			 */
546 			if (rc == 0) {
547 				spdk_put_pdu(pdu);
548 				return 0;
549 			} else {
550 				goto error;
551 			}
552 		}
553 
554 		pdu->data = pdu->data_buf;
555 		pdu->data_from_mempool = true;
556 		pdu->data_segment_len = data_len;
557 	}
558 
559 	/* check digest */
560 	if (conn->header_digest) {
561 		crc32c = spdk_iscsi_pdu_calc_header_digest(pdu);
562 		rc = MATCH_DIGEST_WORD(pdu->header_digest, crc32c);
563 		if (rc == 0) {
564 			SPDK_ERRLOG("header digest error (%s)\n", conn->initiator_name);
565 			rc = SPDK_ISCSI_CONNECTION_FATAL;
566 			goto error;
567 		}
568 	}
569 	if (conn->data_digest && data_len != 0) {
570 		crc32c = spdk_iscsi_pdu_calc_data_digest(pdu);
571 		rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
572 		if (rc == 0) {
573 			rc = SPDK_ISCSI_CONNECTION_FATAL;
574 			SPDK_ERRLOG("data digest error (%s)\n", conn->initiator_name);
575 			goto error;
576 		}
577 	}
578 
579 	*_pdu = pdu;
580 	return 1;
581 
582 error:
583 	spdk_put_pdu(pdu);
584 	conn->pdu_in_progress = NULL;
585 	return rc;
586 }
587 
588 struct _iscsi_sgl {
589 	struct iovec	*iov;
590 	int		iovcnt;
591 	uint32_t	iov_offset;
592 	uint32_t	total_size;
593 };
594 
595 static inline void
596 _iscsi_sgl_init(struct _iscsi_sgl *s, struct iovec *iovs, int iovcnt,
597 		uint32_t iov_offset)
598 {
599 	s->iov = iovs;
600 	s->iovcnt = iovcnt;
601 	s->iov_offset = iov_offset;
602 	s->total_size = 0;
603 }
604 
605 static inline bool
606 _iscsi_sgl_append(struct _iscsi_sgl *s, uint8_t *data, uint32_t data_len)
607 {
608 	if (s->iov_offset >= data_len) {
609 		s->iov_offset -= data_len;
610 	} else {
611 		assert(s->iovcnt > 0);
612 		s->iov->iov_base = data + s->iov_offset;
613 		s->iov->iov_len = data_len - s->iov_offset;
614 		s->total_size += data_len - s->iov_offset;
615 		s->iov_offset = 0;
616 		s->iov++;
617 		s->iovcnt--;
618 		if (s->iovcnt == 0) {
619 			return false;
620 		}
621 	}
622 
623 	return true;
624 }
625 
626 /* Build iovec array to leave metadata space for every data block
627  * when reading data segment from socket.
628  */
629 static inline bool
630 _iscsi_sgl_append_with_md(struct _iscsi_sgl *s,
631 			  void *buf, uint32_t buf_len, uint32_t data_len,
632 			  struct spdk_dif_ctx *dif_ctx)
633 {
634 	int rc;
635 	uint32_t total_size = 0;
636 	struct iovec buf_iov;
637 
638 	if (s->iov_offset >= data_len) {
639 		s->iov_offset -= data_len;
640 	} else {
641 		buf_iov.iov_base = buf;
642 		buf_iov.iov_len = buf_len;
643 		rc = spdk_dif_set_md_interleave_iovs(s->iov, s->iovcnt, &buf_iov, 1,
644 						     s->iov_offset, data_len - s->iov_offset,
645 						     &total_size, dif_ctx);
646 		if (rc < 0) {
647 			SPDK_ERRLOG("Failed to setup iovs for DIF strip\n");
648 			return false;
649 		}
650 
651 		s->total_size += total_size;
652 		s->iov_offset = 0;
653 		assert(s->iovcnt >= rc);
654 		s->iovcnt -= rc;
655 		s->iov += rc;
656 
657 		if (s->iovcnt == 0) {
658 			return false;
659 		}
660 	}
661 
662 	return true;
663 }
664 
665 int
666 spdk_iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, int iovcnt,
667 		      struct spdk_iscsi_pdu *pdu, uint32_t *_mapped_length)
668 {
669 	struct _iscsi_sgl sgl;
670 	int enable_digest;
671 	uint32_t total_ahs_len;
672 	uint32_t data_len;
673 
674 	if (iovcnt == 0) {
675 		return 0;
676 	}
677 
678 	total_ahs_len = pdu->bhs.total_ahs_len;
679 	data_len = DGET24(pdu->bhs.data_segment_len);
680 	data_len = ISCSI_ALIGN(data_len);
681 
682 	enable_digest = 1;
683 	if (pdu->bhs.opcode == ISCSI_OP_LOGIN_RSP) {
684 		/* this PDU should be sent without digest */
685 		enable_digest = 0;
686 	}
687 
688 	_iscsi_sgl_init(&sgl, iovs, iovcnt, pdu->writev_offset);
689 
690 	/* BHS */
691 	if (!_iscsi_sgl_append(&sgl, (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN)) {
692 		goto end;
693 	}
694 	/* AHS */
695 	if (total_ahs_len > 0) {
696 		if (!_iscsi_sgl_append(&sgl, pdu->ahs, 4 * total_ahs_len)) {
697 			goto end;
698 		}
699 	}
700 
701 	/* Header Digest */
702 	if (enable_digest && conn->header_digest) {
703 		if (!_iscsi_sgl_append(&sgl, pdu->header_digest, ISCSI_DIGEST_LEN)) {
704 			goto end;
705 		}
706 	}
707 
708 	/* Data Segment */
709 	if (data_len > 0) {
710 		if (!pdu->dif_insert_or_strip) {
711 			if (!_iscsi_sgl_append(&sgl, pdu->data, data_len)) {
712 				goto end;
713 			}
714 		} else {
715 			if (!_iscsi_sgl_append_with_md(&sgl, pdu->data, pdu->data_buf_len,
716 						       data_len, &pdu->dif_ctx)) {
717 				goto end;
718 			}
719 		}
720 	}
721 
722 	/* Data Digest */
723 	if (enable_digest && conn->data_digest && data_len != 0) {
724 		_iscsi_sgl_append(&sgl, pdu->data_digest, ISCSI_DIGEST_LEN);
725 	}
726 
727 end:
728 	if (_mapped_length != NULL) {
729 		*_mapped_length = sgl.total_size;
730 	}
731 
732 	return iovcnt - sgl.iovcnt;
733 }
734 
735 static int
736 iscsi_append_text(struct spdk_iscsi_conn *conn __attribute__((__unused__)),
737 		  const char *key, const char *val, uint8_t *data,
738 		  int alloc_len, int data_len)
739 {
740 	int total;
741 	int len;
742 
743 	total = data_len;
744 	if (alloc_len < 1) {
745 		return 0;
746 	}
747 	if (total > alloc_len) {
748 		total = alloc_len;
749 		data[total - 1] = '\0';
750 		return total;
751 	}
752 
753 	if (alloc_len - total < 1) {
754 		SPDK_ERRLOG("data space small %d\n", alloc_len);
755 		return total;
756 	}
757 	len = snprintf((char *) data + total, alloc_len - total, "%s=%s", key, val);
758 	total += len + 1;
759 
760 	return total;
761 }
762 
763 static int
764 iscsi_append_param(struct spdk_iscsi_conn *conn, const char *key,
765 		   uint8_t *data, int alloc_len, int data_len)
766 {
767 	struct iscsi_param *param;
768 	int rc;
769 
770 	param = spdk_iscsi_param_find(conn->params, key);
771 	if (param == NULL) {
772 		param = spdk_iscsi_param_find(conn->sess->params, key);
773 		if (param == NULL) {
774 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "no key %.64s\n", key);
775 			return data_len;
776 		}
777 	}
778 	rc = iscsi_append_text(conn, param->key, param->val, data,
779 			       alloc_len, data_len);
780 	return rc;
781 }
782 
783 static int
784 iscsi_get_authinfo(struct spdk_iscsi_conn *conn, const char *authuser)
785 {
786 	int ag_tag;
787 	int rc;
788 
789 	if (conn->sess->target != NULL) {
790 		ag_tag = conn->sess->target->chap_group;
791 	} else {
792 		ag_tag = -1;
793 	}
794 	if (ag_tag < 0) {
795 		ag_tag = g_spdk_iscsi.chap_group;
796 	}
797 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "ag_tag=%d\n", ag_tag);
798 
799 	rc = spdk_iscsi_chap_get_authinfo(&conn->auth, authuser, ag_tag);
800 	if (rc < 0) {
801 		SPDK_ERRLOG("chap_get_authinfo() failed\n");
802 		return -1;
803 	}
804 	return 0;
805 }
806 
807 static int
808 iscsi_auth_params(struct spdk_iscsi_conn *conn,
809 		  struct iscsi_param *params, const char *method, uint8_t *data,
810 		  int alloc_len, int data_len)
811 {
812 	char *in_val;
813 	char *in_next;
814 	char *new_val;
815 	const char *algorithm;
816 	const char *name;
817 	const char *response;
818 	const char *identifier;
819 	const char *challenge;
820 	int total;
821 	int rc;
822 
823 	if (conn == NULL || params == NULL || method == NULL) {
824 		return -1;
825 	}
826 	if (strcasecmp(method, "CHAP") == 0) {
827 		/* method OK */
828 	} else {
829 		SPDK_ERRLOG("unsupported AuthMethod %.64s\n", method);
830 		return -1;
831 	}
832 
833 	total = data_len;
834 	if (alloc_len < 1) {
835 		return 0;
836 	}
837 	if (total > alloc_len) {
838 		total = alloc_len;
839 		data[total - 1] = '\0';
840 		return total;
841 	}
842 
843 	/* for temporary store */
844 	in_val = malloc(ISCSI_TEXT_MAX_VAL_LEN + 1);
845 	if (!in_val) {
846 		SPDK_ERRLOG("malloc() failed for temporary store\n");
847 		return -ENOMEM;
848 	}
849 
850 	/* CHAP method (RFC1994) */
851 	if ((algorithm = spdk_iscsi_param_get_val(params, "CHAP_A")) != NULL) {
852 		if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_A) {
853 			SPDK_ERRLOG("CHAP sequence error\n");
854 			goto error_return;
855 		}
856 
857 		/* CHAP_A is LIST type */
858 		snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", algorithm);
859 		in_next = in_val;
860 		while ((new_val = spdk_strsepq(&in_next, ",")) != NULL) {
861 			if (strcasecmp(new_val, "5") == 0) {
862 				/* CHAP with MD5 */
863 				break;
864 			}
865 		}
866 		if (new_val == NULL) {
867 			snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", "Reject");
868 			new_val = in_val;
869 			iscsi_append_text(conn, "CHAP_A", new_val,
870 					  data, alloc_len, total);
871 			goto error_return;
872 		}
873 		/* selected algorithm is 5 (MD5) */
874 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_A=%s\n", new_val);
875 		total = iscsi_append_text(conn, "CHAP_A", new_val,
876 					  data, alloc_len, total);
877 
878 		/* Identifier is one octet */
879 		gen_random(conn->auth.chap_id, 1);
880 		snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN, "%d",
881 			 (int) conn->auth.chap_id[0]);
882 		total = iscsi_append_text(conn, "CHAP_I", in_val,
883 					  data, alloc_len, total);
884 
885 		/* Challenge Value is a variable stream of octets */
886 		/* (binary length MUST not exceed 1024 bytes) */
887 		conn->auth.chap_challenge_len = ISCSI_CHAP_CHALLENGE_LEN;
888 		gen_random(conn->auth.chap_challenge, conn->auth.chap_challenge_len);
889 		bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN,
890 			conn->auth.chap_challenge, conn->auth.chap_challenge_len);
891 		total = iscsi_append_text(conn, "CHAP_C", in_val,
892 					  data, alloc_len, total);
893 
894 		conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_NR;
895 	} else if ((name = spdk_iscsi_param_get_val(params, "CHAP_N")) != NULL) {
896 		uint8_t resmd5[SPDK_MD5DIGEST_LEN];
897 		uint8_t tgtmd5[SPDK_MD5DIGEST_LEN];
898 		struct spdk_md5ctx md5ctx;
899 		size_t decoded_len = 0;
900 
901 		if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_NR) {
902 			SPDK_ERRLOG("CHAP sequence error\n");
903 			goto error_return;
904 		}
905 
906 		response = spdk_iscsi_param_get_val(params, "CHAP_R");
907 		if (response == NULL) {
908 			SPDK_ERRLOG("no response\n");
909 			goto error_return;
910 		}
911 		if (response[0] == '0' &&
912 		    (response[1] == 'x' || response[1] == 'X')) {
913 			rc = hex2bin(resmd5, SPDK_MD5DIGEST_LEN, response);
914 			if (rc < 0 || rc != SPDK_MD5DIGEST_LEN) {
915 				SPDK_ERRLOG("response format error\n");
916 				goto error_return;
917 			}
918 		} else if (response[0] == '0' &&
919 			   (response[1] == 'b' || response[1] == 'B')) {
920 			response += 2;
921 			rc = spdk_base64_decode(resmd5, &decoded_len, response);
922 			if (rc < 0 || decoded_len != SPDK_MD5DIGEST_LEN) {
923 				SPDK_ERRLOG("response format error\n");
924 				goto error_return;
925 			}
926 		} else {
927 			SPDK_ERRLOG("response format error\n");
928 			goto error_return;
929 		}
930 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_N/CHAP_R\n");
931 
932 		rc = iscsi_get_authinfo(conn, name);
933 		if (rc < 0) {
934 			/* SPDK_ERRLOG("auth user or secret is missing\n"); */
935 			SPDK_ERRLOG("iscsi_get_authinfo() failed\n");
936 			goto error_return;
937 		}
938 		if (conn->auth.user[0] == '\0' || conn->auth.secret[0] == '\0') {
939 			/* SPDK_ERRLOG("auth user or secret is missing\n"); */
940 			SPDK_ERRLOG("auth failed (name %.64s)\n", name);
941 			goto error_return;
942 		}
943 
944 		spdk_md5init(&md5ctx);
945 		/* Identifier */
946 		spdk_md5update(&md5ctx, conn->auth.chap_id, 1);
947 		/* followed by secret */
948 		spdk_md5update(&md5ctx, conn->auth.secret,
949 			       strlen(conn->auth.secret));
950 		/* followed by Challenge Value */
951 		spdk_md5update(&md5ctx, conn->auth.chap_challenge,
952 			       conn->auth.chap_challenge_len);
953 		/* tgtmd5 is expecting Response Value */
954 		spdk_md5final(tgtmd5, &md5ctx);
955 
956 		bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN);
957 
958 #if 0
959 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "tgtmd5=%s, resmd5=%s\n", in_val, response);
960 		spdk_dump("tgtmd5", tgtmd5, SPDK_MD5DIGEST_LEN);
961 		spdk_dump("resmd5", resmd5, SPDK_MD5DIGEST_LEN);
962 #endif
963 
964 		/* compare MD5 digest */
965 		if (memcmp(tgtmd5, resmd5, SPDK_MD5DIGEST_LEN) != 0) {
966 			/* not match */
967 			/* SPDK_ERRLOG("auth user or secret is missing\n"); */
968 			SPDK_ERRLOG("auth failed (name %.64s)\n", name);
969 			goto error_return;
970 		}
971 		/* OK initiator's secret */
972 		conn->authenticated = true;
973 
974 		/* mutual CHAP? */
975 		identifier = spdk_iscsi_param_get_val(params, "CHAP_I");
976 		if (identifier != NULL) {
977 			conn->auth.chap_mid[0] = (uint8_t) strtol(identifier, NULL, 10);
978 			challenge = spdk_iscsi_param_get_val(params, "CHAP_C");
979 			if (challenge == NULL) {
980 				SPDK_ERRLOG("CHAP sequence error\n");
981 				goto error_return;
982 			}
983 			if (challenge[0] == '0' &&
984 			    (challenge[1] == 'x' || challenge[1] == 'X')) {
985 				rc = hex2bin(conn->auth.chap_mchallenge,
986 					     ISCSI_CHAP_CHALLENGE_LEN, challenge);
987 				if (rc < 0) {
988 					SPDK_ERRLOG("challenge format error\n");
989 					goto error_return;
990 				}
991 				conn->auth.chap_mchallenge_len = rc;
992 			} else if (challenge[0] == '0' &&
993 				   (challenge[1] == 'b' || challenge[1] == 'B')) {
994 				challenge += 2;
995 				rc = spdk_base64_decode(conn->auth.chap_mchallenge,
996 							&decoded_len, challenge);
997 				if (rc < 0) {
998 					SPDK_ERRLOG("challenge format error\n");
999 					goto error_return;
1000 				}
1001 				conn->auth.chap_mchallenge_len = decoded_len;
1002 			} else {
1003 				SPDK_ERRLOG("challenge format error\n");
1004 				goto error_return;
1005 			}
1006 #if 0
1007 			spdk_dump("MChallenge", conn->auth.chap_mchallenge,
1008 				  conn->auth.chap_mchallenge_len);
1009 #endif
1010 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got CHAP_I/CHAP_C\n");
1011 
1012 			if (conn->auth.muser[0] == '\0' || conn->auth.msecret[0] == '\0') {
1013 				/* SPDK_ERRLOG("mutual auth user or secret is missing\n"); */
1014 				SPDK_ERRLOG("auth failed (name %.64s)\n", name);
1015 				goto error_return;
1016 			}
1017 
1018 			spdk_md5init(&md5ctx);
1019 			/* Identifier */
1020 			spdk_md5update(&md5ctx, conn->auth.chap_mid, 1);
1021 			/* followed by secret */
1022 			spdk_md5update(&md5ctx, conn->auth.msecret,
1023 				       strlen(conn->auth.msecret));
1024 			/* followed by Challenge Value */
1025 			spdk_md5update(&md5ctx, conn->auth.chap_mchallenge,
1026 				       conn->auth.chap_mchallenge_len);
1027 			/* tgtmd5 is Response Value */
1028 			spdk_md5final(tgtmd5, &md5ctx);
1029 
1030 			bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN);
1031 
1032 			total = iscsi_append_text(conn, "CHAP_N",
1033 						  conn->auth.muser, data, alloc_len, total);
1034 			total = iscsi_append_text(conn, "CHAP_R",
1035 						  in_val, data, alloc_len, total);
1036 		} else {
1037 			/* not mutual */
1038 			if (conn->mutual_chap) {
1039 				SPDK_ERRLOG("required mutual CHAP\n");
1040 				goto error_return;
1041 			}
1042 		}
1043 
1044 		conn->auth.chap_phase = ISCSI_CHAP_PHASE_END;
1045 	} else {
1046 		/* not found CHAP keys */
1047 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "start CHAP\n");
1048 		conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
1049 	}
1050 
1051 	free(in_val);
1052 	return total;
1053 
1054 error_return:
1055 	conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
1056 	free(in_val);
1057 	return -1;
1058 }
1059 
1060 static int
1061 iscsi_reject(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
1062 	     int reason)
1063 {
1064 	struct spdk_iscsi_pdu *rsp_pdu;
1065 	struct iscsi_bhs_reject *rsph;
1066 	uint8_t *data;
1067 	int total_ahs_len;
1068 	int data_len;
1069 	int alloc_len;
1070 
1071 	total_ahs_len = pdu->bhs.total_ahs_len;
1072 	data_len = 0;
1073 	alloc_len = ISCSI_BHS_LEN + (4 * total_ahs_len);
1074 
1075 	if (conn->header_digest) {
1076 		alloc_len += ISCSI_DIGEST_LEN;
1077 	}
1078 
1079 	data = calloc(1, alloc_len);
1080 	if (!data) {
1081 		SPDK_ERRLOG("calloc() failed for data segment\n");
1082 		return -ENOMEM;
1083 	}
1084 
1085 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Reject PDU reason=%d\n", reason);
1086 
1087 	if (conn->sess != NULL) {
1088 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
1089 			      "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
1090 			      conn->StatSN, conn->sess->ExpCmdSN,
1091 			      conn->sess->MaxCmdSN);
1092 	} else {
1093 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u\n", conn->StatSN);
1094 	}
1095 
1096 	memcpy(data, &pdu->bhs, ISCSI_BHS_LEN);
1097 	data_len += ISCSI_BHS_LEN;
1098 
1099 	if (total_ahs_len != 0) {
1100 		memcpy(data + data_len, pdu->ahs, (4 * total_ahs_len));
1101 		data_len += (4 * total_ahs_len);
1102 	}
1103 
1104 	if (conn->header_digest) {
1105 		memcpy(data + data_len, pdu->header_digest, ISCSI_DIGEST_LEN);
1106 		data_len += ISCSI_DIGEST_LEN;
1107 	}
1108 
1109 	rsp_pdu = spdk_get_pdu();
1110 	if (rsp_pdu == NULL) {
1111 		free(data);
1112 		return -ENOMEM;
1113 	}
1114 
1115 	rsph = (struct iscsi_bhs_reject *)&rsp_pdu->bhs;
1116 	rsp_pdu->data = data;
1117 	rsph->opcode = ISCSI_OP_REJECT;
1118 	rsph->flags |= 0x80;	/* bit 0 is default to 1 */
1119 	rsph->reason = reason;
1120 	DSET24(rsph->data_segment_len, data_len);
1121 
1122 	rsph->ffffffff = 0xffffffffU;
1123 	to_be32(&rsph->stat_sn, conn->StatSN);
1124 	conn->StatSN++;
1125 
1126 	if (conn->sess != NULL) {
1127 		to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
1128 		to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
1129 	} else {
1130 		to_be32(&rsph->exp_cmd_sn, 1);
1131 		to_be32(&rsph->max_cmd_sn, 1);
1132 	}
1133 
1134 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (void *)&rsp_pdu->bhs, ISCSI_BHS_LEN);
1135 
1136 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
1137 
1138 	return 0;
1139 }
1140 
1141 static int
1142 iscsi_check_values(struct spdk_iscsi_conn *conn)
1143 {
1144 	if (conn->sess->FirstBurstLength > conn->sess->MaxBurstLength) {
1145 		SPDK_ERRLOG("FirstBurstLength(%d) > MaxBurstLength(%d)\n",
1146 			    conn->sess->FirstBurstLength,
1147 			    conn->sess->MaxBurstLength);
1148 		return -1;
1149 	}
1150 	if (conn->sess->FirstBurstLength > g_spdk_iscsi.FirstBurstLength) {
1151 		SPDK_ERRLOG("FirstBurstLength(%d) > iSCSI target restriction(%d)\n",
1152 			    conn->sess->FirstBurstLength, g_spdk_iscsi.FirstBurstLength);
1153 		return -1;
1154 	}
1155 	if (conn->sess->MaxBurstLength > 0x00ffffff) {
1156 		SPDK_ERRLOG("MaxBurstLength(%d) > 0x00ffffff\n",
1157 			    conn->sess->MaxBurstLength);
1158 		return -1;
1159 	}
1160 
1161 	if (conn->MaxRecvDataSegmentLength < 512) {
1162 		SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) < 512\n",
1163 			    conn->MaxRecvDataSegmentLength);
1164 		return -1;
1165 	}
1166 	if (conn->MaxRecvDataSegmentLength > 0x00ffffff) {
1167 		SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) > 0x00ffffff\n",
1168 			    conn->MaxRecvDataSegmentLength);
1169 		return -1;
1170 	}
1171 	return 0;
1172 }
1173 
1174 /*
1175  * The response function of spdk_iscsi_op_login
1176  * return:
1177  * 0:success;
1178  * -1:error;
1179  */
1180 static int
1181 iscsi_op_login_response(struct spdk_iscsi_conn *conn,
1182 			struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param *params)
1183 {
1184 	struct iscsi_bhs_login_rsp *rsph;
1185 	int rc;
1186 
1187 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1188 	rsph->version_max = ISCSI_VERSION;
1189 	rsph->version_act = ISCSI_VERSION;
1190 	DSET24(rsph->data_segment_len, rsp_pdu->data_segment_len);
1191 
1192 	to_be32(&rsph->stat_sn, conn->StatSN);
1193 	conn->StatSN++;
1194 
1195 	if (conn->sess != NULL) {
1196 		to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
1197 		to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
1198 	} else {
1199 		to_be32(&rsph->exp_cmd_sn, rsp_pdu->cmd_sn);
1200 		to_be32(&rsph->max_cmd_sn, rsp_pdu->cmd_sn);
1201 	}
1202 
1203 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (uint8_t *)rsph, ISCSI_BHS_LEN);
1204 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "DATA", rsp_pdu->data, rsp_pdu->data_segment_len);
1205 
1206 	/* Set T/CSG/NSG to reserved if login error. */
1207 	if (rsph->status_class != 0) {
1208 		rsph->flags &= ~ISCSI_LOGIN_TRANSIT;
1209 		rsph->flags &= ~ISCSI_LOGIN_CURRENT_STAGE_MASK;
1210 		rsph->flags &= ~ISCSI_LOGIN_NEXT_STAGE_MASK;
1211 	}
1212 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
1213 
1214 	/* after send PDU digest on/off */
1215 	if (conn->full_feature) {
1216 		/* update internal variables */
1217 		rc = spdk_iscsi_copy_param2var(conn);
1218 		if (rc < 0) {
1219 			SPDK_ERRLOG("spdk_iscsi_copy_param2var() failed\n");
1220 			spdk_iscsi_param_free(params);
1221 			return -1;
1222 		}
1223 		/* check value */
1224 		rc = iscsi_check_values(conn);
1225 		if (rc < 0) {
1226 			SPDK_ERRLOG("iscsi_check_values() failed\n");
1227 			spdk_iscsi_param_free(params);
1228 			return -1;
1229 		}
1230 	}
1231 
1232 	spdk_iscsi_param_free(params);
1233 	return 0;
1234 }
1235 
1236 /*
1237  * This function is used to del the original param and update it with new
1238  * value
1239  * return:
1240  * 0: success
1241  * otherwise: error
1242  */
1243 static int
1244 iscsi_op_login_update_param(struct spdk_iscsi_conn *conn,
1245 			    const char *key, const char *value,
1246 			    const char *list)
1247 {
1248 	int rc = 0;
1249 	struct iscsi_param *new_param, *orig_param;
1250 	int index;
1251 
1252 	orig_param = spdk_iscsi_param_find(conn->params, key);
1253 	if (orig_param == NULL) {
1254 		SPDK_ERRLOG("orig_param %s not found\n", key);
1255 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1256 	}
1257 
1258 	index = orig_param->state_index;
1259 	rc = spdk_iscsi_param_del(&conn->params, key);
1260 	if (rc < 0) {
1261 		SPDK_ERRLOG("iscsi_param_del(%s) failed\n", key);
1262 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1263 	}
1264 	rc = spdk_iscsi_param_add(&conn->params, key, value, list, ISPT_LIST);
1265 	if (rc < 0) {
1266 		SPDK_ERRLOG("iscsi_param_add() failed\n");
1267 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1268 	}
1269 	new_param = spdk_iscsi_param_find(conn->params, key);
1270 	if (new_param == NULL) {
1271 		SPDK_ERRLOG("spdk_iscsi_param_find() failed\n");
1272 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1273 	}
1274 	new_param->state_index = index;
1275 	return rc;
1276 }
1277 
1278 static int
1279 iscsi_negotiate_chap_param(struct spdk_iscsi_conn *conn, bool disable_chap,
1280 			   bool require_chap, bool mutual_chap)
1281 {
1282 	int rc = 0;
1283 
1284 	if (disable_chap) {
1285 		conn->require_chap = false;
1286 		rc = iscsi_op_login_update_param(conn, "AuthMethod", "None", "None");
1287 		if (rc < 0) {
1288 			return rc;
1289 		}
1290 	} else if (require_chap) {
1291 		conn->require_chap = true;
1292 		rc = iscsi_op_login_update_param(conn, "AuthMethod", "CHAP", "CHAP");
1293 		if (rc < 0) {
1294 			return rc;
1295 		}
1296 	}
1297 	if (mutual_chap) {
1298 		conn->mutual_chap = true;
1299 	}
1300 
1301 	return rc;
1302 }
1303 
1304 /*
1305  * The function which is used to handle the part of session discovery
1306  * return:
1307  * 0, success;
1308  * otherwise: error;
1309  */
1310 static int
1311 iscsi_op_login_session_discovery_chap(struct spdk_iscsi_conn *conn)
1312 {
1313 	return iscsi_negotiate_chap_param(conn, g_spdk_iscsi.disable_chap,
1314 					  g_spdk_iscsi.require_chap,
1315 					  g_spdk_iscsi.mutual_chap);
1316 }
1317 
1318 /*
1319  * This function is used to update the param related with chap
1320  * return:
1321  * 0: success
1322  * otherwise: error
1323  */
1324 static int
1325 iscsi_op_login_negotiate_chap_param(struct spdk_iscsi_conn *conn,
1326 				    struct spdk_iscsi_tgt_node *target)
1327 {
1328 	return iscsi_negotiate_chap_param(conn, target->disable_chap,
1329 					  target->require_chap,
1330 					  target->mutual_chap);
1331 }
1332 
1333 static int
1334 iscsi_op_login_negotiate_digest_param(struct spdk_iscsi_conn *conn,
1335 				      struct spdk_iscsi_tgt_node *target)
1336 {
1337 	int rc;
1338 
1339 	if (target->header_digest) {
1340 		/*
1341 		 * User specified header digests, so update the list of
1342 		 *  HeaderDigest values to remove "None" so that only
1343 		 *  initiators who support CRC32C can connect.
1344 		 */
1345 		rc = iscsi_op_login_update_param(conn, "HeaderDigest", "CRC32C", "CRC32C");
1346 		if (rc < 0) {
1347 			return rc;
1348 		}
1349 	}
1350 
1351 	if (target->data_digest) {
1352 		/*
1353 		 * User specified data digests, so update the list of
1354 		 *  DataDigest values to remove "None" so that only
1355 		 *  initiators who support CRC32C can connect.
1356 		 */
1357 		rc = iscsi_op_login_update_param(conn, "DataDigest", "CRC32C", "CRC32C");
1358 		if (rc < 0) {
1359 			return rc;
1360 		}
1361 	}
1362 
1363 	return 0;
1364 }
1365 
1366 /*
1367  * This function use to check the session
1368  * return:
1369  * 0, success
1370  * otherwise: error
1371  */
1372 static int
1373 iscsi_op_login_check_session(struct spdk_iscsi_conn *conn,
1374 			     struct spdk_iscsi_pdu *rsp_pdu,
1375 			     char *initiator_port_name, int cid)
1376 
1377 {
1378 	int rc = 0;
1379 	struct iscsi_bhs_login_rsp *rsph;
1380 
1381 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1382 	/* check existing session */
1383 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "isid=%"PRIx64", tsih=%u, cid=%u\n",
1384 		      iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih), cid);
1385 	if (rsph->tsih != 0) {
1386 		/* multiple connections */
1387 		rc = append_iscsi_sess(conn, initiator_port_name,
1388 				       from_be16(&rsph->tsih), cid);
1389 		if (rc != 0) {
1390 			SPDK_ERRLOG("isid=%"PRIx64", tsih=%u, cid=%u:"
1391 				    "spdk_append_iscsi_sess() failed\n",
1392 				    iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih),
1393 				    cid);
1394 			/* Can't include in session */
1395 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1396 			rsph->status_detail = rc;
1397 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1398 		}
1399 	} else if (!g_spdk_iscsi.AllowDuplicateIsid) {
1400 		/* new session, drop old sess by the initiator */
1401 		spdk_iscsi_drop_conns(conn, initiator_port_name, 0 /* drop old */);
1402 	}
1403 
1404 	return rc;
1405 }
1406 
1407 /*
1408  * This function is used to check the target info
1409  * return:
1410  * 0: success
1411  * otherwise: error
1412  */
1413 static int
1414 iscsi_op_login_check_target(struct spdk_iscsi_conn *conn,
1415 			    struct spdk_iscsi_pdu *rsp_pdu,
1416 			    const char *target_name,
1417 			    struct spdk_iscsi_tgt_node **target)
1418 {
1419 	bool result;
1420 	struct iscsi_bhs_login_rsp *rsph;
1421 
1422 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1423 	*target = spdk_iscsi_find_tgt_node(target_name);
1424 	if (*target == NULL) {
1425 		SPDK_WARNLOG("target %s not found\n", target_name);
1426 		/* Not found */
1427 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1428 		rsph->status_detail = ISCSI_LOGIN_TARGET_NOT_FOUND;
1429 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1430 	}
1431 	if (spdk_iscsi_tgt_node_is_destructed(*target)) {
1432 		SPDK_ERRLOG("target %s is removed\n", target_name);
1433 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1434 		rsph->status_detail = ISCSI_LOGIN_TARGET_REMOVED;
1435 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1436 	}
1437 	result = spdk_iscsi_tgt_node_access(conn, *target,
1438 					    conn->initiator_name,
1439 					    conn->initiator_addr);
1440 	if (!result) {
1441 		SPDK_ERRLOG("access denied\n");
1442 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1443 		rsph->status_detail = ISCSI_LOGIN_AUTHORIZATION_FAIL;
1444 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1445 	}
1446 
1447 	return 0;
1448 }
1449 
1450 /*
1451  * The function which is used to handle the part of normal login session
1452  * return:
1453  * 0, success;
1454  * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error;
1455  */
1456 static int
1457 iscsi_op_login_session_normal(struct spdk_iscsi_conn *conn,
1458 			      struct spdk_iscsi_pdu *rsp_pdu,
1459 			      char *initiator_port_name,
1460 			      struct iscsi_param *params,
1461 			      struct spdk_iscsi_tgt_node **target,
1462 			      int cid)
1463 {
1464 	const char *target_name;
1465 	const char *target_short_name;
1466 	struct iscsi_bhs_login_rsp *rsph;
1467 	int rc = 0;
1468 
1469 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1470 	target_name = spdk_iscsi_param_get_val(params, "TargetName");
1471 
1472 	if (target_name == NULL) {
1473 		SPDK_ERRLOG("TargetName is empty\n");
1474 		/* Missing parameter */
1475 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1476 		rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1477 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1478 	}
1479 
1480 	memset(conn->target_short_name, 0, MAX_TARGET_NAME);
1481 	target_short_name = strstr(target_name, ":");
1482 	if (target_short_name != NULL) {
1483 		target_short_name++; /* Advance past the ':' */
1484 		if (strlen(target_short_name) >= MAX_TARGET_NAME) {
1485 			SPDK_ERRLOG("Target Short Name (%s) is more than %u characters\n",
1486 				    target_short_name, MAX_TARGET_NAME);
1487 			/* Invalid request */
1488 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1489 			rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
1490 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1491 		}
1492 		snprintf(conn->target_short_name, MAX_TARGET_NAME, "%s",
1493 			 target_short_name);
1494 	}
1495 
1496 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1497 	rc = iscsi_op_login_check_target(conn, rsp_pdu, target_name, target);
1498 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1499 
1500 	if (rc < 0) {
1501 		return rc;
1502 	}
1503 
1504 	conn->target = *target;
1505 	conn->dev = (*target)->dev;
1506 	conn->target_port = spdk_scsi_dev_find_port_by_id((*target)->dev,
1507 			    conn->portal->group->tag);
1508 
1509 	rc = iscsi_op_login_check_session(conn, rsp_pdu,
1510 					  initiator_port_name, cid);
1511 	if (rc < 0) {
1512 		return rc;
1513 	}
1514 
1515 	/* force target flags */
1516 	pthread_mutex_lock(&((*target)->mutex));
1517 	rc = iscsi_op_login_negotiate_chap_param(conn, *target);
1518 	pthread_mutex_unlock(&((*target)->mutex));
1519 
1520 	if (rc == 0) {
1521 		rc = iscsi_op_login_negotiate_digest_param(conn, *target);
1522 	}
1523 
1524 	if (rc != 0) {
1525 		/* Invalid request */
1526 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1527 		rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
1528 	}
1529 
1530 	return rc;
1531 }
1532 
1533 /*
1534  * This function is used to judge the session type
1535  * return
1536  * 0: success
1537  * otherwise, error
1538  */
1539 static int
1540 iscsi_op_login_session_type(struct spdk_iscsi_conn *conn,
1541 			    struct spdk_iscsi_pdu *rsp_pdu,
1542 			    enum session_type *session_type,
1543 			    struct iscsi_param *params)
1544 {
1545 	const char *session_type_str;
1546 	struct iscsi_bhs_login_rsp *rsph;
1547 
1548 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1549 	session_type_str = spdk_iscsi_param_get_val(params, "SessionType");
1550 	if (session_type_str == NULL) {
1551 		if (rsph->tsih != 0) {
1552 			*session_type = SESSION_TYPE_NORMAL;
1553 		} else {
1554 			SPDK_ERRLOG("SessionType is empty\n");
1555 			/* Missing parameter */
1556 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1557 			rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1558 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1559 		}
1560 	} else {
1561 		if (strcasecmp(session_type_str, "Discovery") == 0) {
1562 			*session_type = SESSION_TYPE_DISCOVERY;
1563 		} else if (strcasecmp(session_type_str, "Normal") == 0) {
1564 			*session_type = SESSION_TYPE_NORMAL;
1565 		} else {
1566 			*session_type = SESSION_TYPE_INVALID;
1567 			SPDK_ERRLOG("SessionType is invalid\n");
1568 			/* Missing parameter */
1569 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1570 			rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1571 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1572 		}
1573 	}
1574 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Session Type: %s\n", session_type_str);
1575 
1576 	return 0;
1577 }
1578 /*
1579  * This function is used to initialize the port info
1580  * return
1581  * 0: success
1582  * otherwise: error
1583  */
1584 static int
1585 iscsi_op_login_initialize_port(struct spdk_iscsi_conn *conn,
1586 			       struct spdk_iscsi_pdu *rsp_pdu,
1587 			       char *initiator_port_name,
1588 			       uint32_t name_length,
1589 			       struct iscsi_param *params)
1590 {
1591 	const char *val;
1592 	struct iscsi_bhs_login_rsp *rsph;
1593 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1594 
1595 	/* Initiator Name and Port */
1596 	val = spdk_iscsi_param_get_val(params, "InitiatorName");
1597 	if (val == NULL) {
1598 		SPDK_ERRLOG("InitiatorName is empty\n");
1599 		/* Missing parameter */
1600 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1601 		rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1602 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1603 	}
1604 	snprintf(conn->initiator_name, sizeof(conn->initiator_name), "%s", val);
1605 	snprintf(initiator_port_name, name_length,
1606 		 "%s,i,0x%12.12" PRIx64, val, iscsi_get_isid(rsph->isid));
1607 	spdk_strlwr(conn->initiator_name);
1608 	spdk_strlwr(initiator_port_name);
1609 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Initiator name: %s\n", conn->initiator_name);
1610 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Initiator port: %s\n", initiator_port_name);
1611 
1612 	return 0;
1613 }
1614 
1615 /*
1616  * This function is used to set the info in the connection data structure
1617  * return
1618  * 0: success
1619  * otherwise: error
1620  */
1621 static int
1622 iscsi_op_login_set_conn_info(struct spdk_iscsi_conn *conn,
1623 			     struct spdk_iscsi_pdu *rsp_pdu,
1624 			     char *initiator_port_name,
1625 			     enum session_type session_type,
1626 			     struct spdk_iscsi_tgt_node *target, int cid)
1627 {
1628 	int rc = 0;
1629 	struct iscsi_bhs_login_rsp *rsph;
1630 	struct spdk_scsi_port *initiator_port;
1631 
1632 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1633 	conn->authenticated = false;
1634 	conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
1635 	conn->cid = cid;
1636 
1637 	if (conn->sess == NULL) {
1638 		/* create initiator port */
1639 		initiator_port = spdk_scsi_port_create(iscsi_get_isid(rsph->isid), 0, initiator_port_name);
1640 		if (initiator_port == NULL) {
1641 			SPDK_ERRLOG("create_port() failed\n");
1642 			rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
1643 			rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
1644 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1645 		}
1646 
1647 		/* new session */
1648 		rc = create_iscsi_sess(conn, target, session_type);
1649 		if (rc < 0) {
1650 			spdk_scsi_port_free(&initiator_port);
1651 			SPDK_ERRLOG("create_sess() failed\n");
1652 			rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
1653 			rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
1654 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1655 		}
1656 		/* initialize parameters */
1657 		conn->sess->initiator_port = initiator_port;
1658 		conn->StatSN = from_be32(&rsph->stat_sn);
1659 		conn->sess->isid = iscsi_get_isid(rsph->isid);
1660 
1661 		/* Initiator port TransportID */
1662 		spdk_scsi_port_set_iscsi_transport_id(conn->sess->initiator_port,
1663 						      conn->initiator_name,
1664 						      conn->sess->isid);
1665 
1666 		/* Discovery sessions will not have a target. */
1667 		if (target != NULL) {
1668 			conn->sess->queue_depth = target->queue_depth;
1669 		} else {
1670 			/*
1671 			 * Assume discovery sessions have an effective command
1672 			 *  windows size of 1.
1673 			 */
1674 			conn->sess->queue_depth = 1;
1675 		}
1676 		conn->sess->ExpCmdSN = rsp_pdu->cmd_sn;
1677 		conn->sess->MaxCmdSN = rsp_pdu->cmd_sn + conn->sess->queue_depth - 1;
1678 	}
1679 
1680 	conn->initiator_port = conn->sess->initiator_port;
1681 
1682 	return 0;
1683 }
1684 
1685 /*
1686  * This function is used to set the target info
1687  * return
1688  * 0: success
1689  * otherwise: error
1690  */
1691 static int
1692 iscsi_op_login_set_target_info(struct spdk_iscsi_conn *conn,
1693 			       struct spdk_iscsi_pdu *rsp_pdu,
1694 			       enum session_type session_type,
1695 			       int alloc_len,
1696 			       struct spdk_iscsi_tgt_node *target)
1697 {
1698 	char buf[MAX_TMPBUF];
1699 	const char *val;
1700 	int rc = 0;
1701 	struct spdk_iscsi_portal *portal = conn->portal;
1702 
1703 	/* declarative parameters */
1704 	if (target != NULL) {
1705 		pthread_mutex_lock(&target->mutex);
1706 		if (target->alias[0] != '\0') {
1707 			snprintf(buf, sizeof buf, "%s", target->alias);
1708 		} else {
1709 			snprintf(buf, sizeof buf, "%s", "");
1710 		}
1711 		pthread_mutex_unlock(&target->mutex);
1712 		rc = spdk_iscsi_param_set(conn->sess->params, "TargetAlias", buf);
1713 		if (rc < 0) {
1714 			SPDK_ERRLOG("iscsi_param_set() failed\n");
1715 			return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1716 		}
1717 	}
1718 	snprintf(buf, sizeof buf, "%s:%s,%d", portal->host, portal->port,
1719 		 portal->group->tag);
1720 	rc = spdk_iscsi_param_set(conn->sess->params, "TargetAddress", buf);
1721 	if (rc < 0) {
1722 		SPDK_ERRLOG("iscsi_param_set() failed\n");
1723 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1724 	}
1725 	snprintf(buf, sizeof buf, "%d", portal->group->tag);
1726 	rc = spdk_iscsi_param_set(conn->sess->params, "TargetPortalGroupTag", buf);
1727 	if (rc < 0) {
1728 		SPDK_ERRLOG("iscsi_param_set() failed\n");
1729 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1730 	}
1731 
1732 	/* write in response */
1733 	if (target != NULL) {
1734 		val = spdk_iscsi_param_get_val(conn->sess->params, "TargetAlias");
1735 		if (val != NULL && strlen(val) != 0) {
1736 			rsp_pdu->data_segment_len = iscsi_append_param(conn,
1737 						    "TargetAlias",
1738 						    rsp_pdu->data,
1739 						    alloc_len,
1740 						    rsp_pdu->data_segment_len);
1741 		}
1742 		if (session_type == SESSION_TYPE_DISCOVERY) {
1743 			rsp_pdu->data_segment_len = iscsi_append_param(conn,
1744 						    "TargetAddress",
1745 						    rsp_pdu->data,
1746 						    alloc_len,
1747 						    rsp_pdu->data_segment_len);
1748 		}
1749 		rsp_pdu->data_segment_len = iscsi_append_param(conn,
1750 					    "TargetPortalGroupTag",
1751 					    rsp_pdu->data,
1752 					    alloc_len,
1753 					    rsp_pdu->data_segment_len);
1754 	}
1755 
1756 	return rc;
1757 }
1758 
1759 /*
1760  * This function is used to handle the login of iscsi initiator when there is
1761  * no session
1762  * return:
1763  * 0, success;
1764  * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error;
1765  * SPDK_ISCSI_LOGIN_ERROR_RESPONSE,  used to notify the login fail.
1766  */
1767 static int
1768 iscsi_op_login_phase_none(struct spdk_iscsi_conn *conn,
1769 			  struct spdk_iscsi_pdu *rsp_pdu,
1770 			  struct iscsi_param *params,
1771 			  int alloc_len, int cid)
1772 {
1773 	enum session_type session_type;
1774 	char initiator_port_name[MAX_INITIATOR_PORT_NAME];
1775 	struct iscsi_bhs_login_rsp *rsph;
1776 	struct spdk_iscsi_tgt_node *target = NULL;
1777 	int rc = 0;
1778 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1779 
1780 	conn->target = NULL;
1781 	conn->dev = NULL;
1782 
1783 	rc = iscsi_op_login_initialize_port(conn, rsp_pdu, initiator_port_name,
1784 					    MAX_INITIATOR_PORT_NAME, params);
1785 	if (rc < 0) {
1786 		return rc;
1787 	}
1788 
1789 	rc = iscsi_op_login_session_type(conn, rsp_pdu, &session_type, params);
1790 	if (rc < 0) {
1791 		return rc;
1792 	}
1793 
1794 	/* Target Name and Port */
1795 	if (session_type == SESSION_TYPE_NORMAL) {
1796 		rc = iscsi_op_login_session_normal(conn, rsp_pdu,
1797 						   initiator_port_name,
1798 						   params, &target, cid);
1799 		if (rc < 0) {
1800 			return rc;
1801 		}
1802 
1803 	} else if (session_type == SESSION_TYPE_DISCOVERY) {
1804 		target = NULL;
1805 		rsph->tsih = 0;
1806 
1807 		/* force target flags */
1808 		pthread_mutex_lock(&g_spdk_iscsi.mutex);
1809 		rc = iscsi_op_login_session_discovery_chap(conn);
1810 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1811 		if (rc < 0) {
1812 			return rc;
1813 		}
1814 	} else {
1815 		SPDK_ERRLOG("unknown session type\n");
1816 		/* Missing parameter */
1817 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1818 		rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1819 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1820 	}
1821 
1822 	rc = iscsi_op_login_set_conn_info(conn, rsp_pdu, initiator_port_name,
1823 					  session_type, target, cid);
1824 	if (rc < 0) {
1825 		return rc;
1826 	}
1827 
1828 	/* limit conns on discovery session */
1829 	if (session_type == SESSION_TYPE_DISCOVERY) {
1830 		conn->sess->MaxConnections = 1;
1831 		rc = spdk_iscsi_param_set_int(conn->sess->params,
1832 					      "MaxConnections",
1833 					      conn->sess->MaxConnections);
1834 		if (rc < 0) {
1835 			SPDK_ERRLOG("iscsi_param_set_int() failed\n");
1836 			return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1837 		}
1838 	}
1839 
1840 	return iscsi_op_login_set_target_info(conn, rsp_pdu, session_type,
1841 					      alloc_len, target);
1842 }
1843 
1844 /*
1845  * The function which is used to initialize the internal response data
1846  * structure of iscsi login function.
1847  * return:
1848  * 0, success;
1849  * otherwise, error;
1850  */
1851 static int
1852 iscsi_op_login_rsp_init(struct spdk_iscsi_conn *conn,
1853 			struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu,
1854 			struct iscsi_param **params, int *alloc_len, int *cid)
1855 {
1856 	struct iscsi_bhs_login_req *reqh;
1857 	struct iscsi_bhs_login_rsp *rsph;
1858 	int rc;
1859 
1860 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1861 	rsph->opcode = ISCSI_OP_LOGIN_RSP;
1862 	rsph->status_class = ISCSI_CLASS_SUCCESS;
1863 	rsph->status_detail = ISCSI_LOGIN_ACCEPT;
1864 	rsp_pdu->data_segment_len = 0;
1865 
1866 	/* Default MaxRecvDataSegmentLength - RFC3720(12.12) */
1867 	if (conn->MaxRecvDataSegmentLength < 8192) {
1868 		*alloc_len = 8192;
1869 	} else {
1870 		*alloc_len = conn->MaxRecvDataSegmentLength;
1871 	}
1872 
1873 	rsp_pdu->data = calloc(1, *alloc_len);
1874 	if (!rsp_pdu->data) {
1875 		SPDK_ERRLOG("calloc() failed for data segment\n");
1876 		rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
1877 		rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
1878 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1879 	}
1880 
1881 	reqh = (struct iscsi_bhs_login_req *)&pdu->bhs;
1882 	rsph->flags |= (reqh->flags & ISCSI_LOGIN_TRANSIT);
1883 	rsph->flags |= (reqh->flags & ISCSI_LOGIN_CONTINUE);
1884 	rsph->flags |= (reqh->flags & ISCSI_LOGIN_CURRENT_STAGE_MASK);
1885 	if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
1886 		rsph->flags |= (reqh->flags & ISCSI_LOGIN_NEXT_STAGE_MASK);
1887 	}
1888 
1889 	/* We don't need to convert from network byte order. Just store it */
1890 	memcpy(&rsph->isid, reqh->isid, 6);
1891 	rsph->tsih = reqh->tsih;
1892 	rsph->itt = reqh->itt;
1893 	rsp_pdu->cmd_sn = from_be32(&reqh->cmd_sn);
1894 	*cid = from_be16(&reqh->cid);
1895 
1896 	if (rsph->tsih) {
1897 		rsph->stat_sn = reqh->exp_stat_sn;
1898 	}
1899 
1900 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "PDU", (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN);
1901 
1902 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
1903 		      "T=%d, C=%d, CSG=%d, NSG=%d, Min=%d, Max=%d, ITT=%x\n",
1904 		      ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags),
1905 		      ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags),
1906 		      ISCSI_BHS_LOGIN_GET_CSG(rsph->flags),
1907 		      ISCSI_BHS_LOGIN_GET_NSG(rsph->flags),
1908 		      reqh->version_min, reqh->version_max, from_be32(&rsph->itt));
1909 
1910 	if (conn->sess != NULL) {
1911 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
1912 			      "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u,"
1913 			      "MaxCmdSN=%u\n", rsp_pdu->cmd_sn,
1914 			      from_be32(&rsph->stat_sn), conn->StatSN,
1915 			      conn->sess->ExpCmdSN,
1916 			      conn->sess->MaxCmdSN);
1917 	} else {
1918 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
1919 			      "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n",
1920 			      rsp_pdu->cmd_sn, from_be32(&rsph->stat_sn),
1921 			      conn->StatSN);
1922 	}
1923 
1924 	if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags) &&
1925 	    ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags)) {
1926 		SPDK_ERRLOG("transit error\n");
1927 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1928 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
1929 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1930 	}
1931 	/* make sure reqh->version_max < ISCSI_VERSION */
1932 	if (reqh->version_min > ISCSI_VERSION) {
1933 		SPDK_ERRLOG("unsupported version min %d/max %d, expecting %d\n", reqh->version_min,
1934 			    reqh->version_max, ISCSI_VERSION);
1935 		/* Unsupported version */
1936 		/* set all reserved flag to zero */
1937 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1938 		rsph->status_detail = ISCSI_LOGIN_UNSUPPORTED_VERSION;
1939 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1940 	}
1941 
1942 	if ((ISCSI_BHS_LOGIN_GET_NSG(rsph->flags) == ISCSI_NSG_RESERVED_CODE) &&
1943 	    ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
1944 		/* set NSG to zero */
1945 		rsph->flags &= ~ISCSI_LOGIN_NEXT_STAGE_MASK;
1946 		/* also set other bits to zero */
1947 		rsph->flags &= ~ISCSI_LOGIN_TRANSIT;
1948 		rsph->flags &= ~ISCSI_LOGIN_CURRENT_STAGE_MASK;
1949 		SPDK_ERRLOG("Received reserved NSG code: %d\n", ISCSI_NSG_RESERVED_CODE);
1950 		/* Initiator error */
1951 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1952 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
1953 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1954 	}
1955 
1956 	/* store incoming parameters */
1957 	rc = spdk_iscsi_parse_params(params, pdu->data,
1958 				     pdu->data_segment_len, ISCSI_BHS_LOGIN_GET_CBIT(reqh->flags),
1959 				     &conn->partial_text_parameter);
1960 	if (rc < 0) {
1961 		SPDK_ERRLOG("iscsi_parse_params() failed\n");
1962 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1963 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
1964 		return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
1965 	}
1966 	return 0;
1967 }
1968 
1969 /*
1970  * This function is used to set the csg bit case in rsp
1971  * return:
1972  * 0, success
1973  * otherwise: error
1974  */
1975 static int
1976 iscsi_op_login_rsp_handle_csg_bit(struct spdk_iscsi_conn *conn,
1977 				  struct spdk_iscsi_pdu *rsp_pdu,
1978 				  struct iscsi_param *params, int alloc_len)
1979 {
1980 	const char *auth_method;
1981 	int rc;
1982 	struct iscsi_bhs_login_rsp *rsph;
1983 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
1984 
1985 	switch (ISCSI_BHS_LOGIN_GET_CSG(rsph->flags)) {
1986 	case ISCSI_SECURITY_NEGOTIATION_PHASE:
1987 		/* SecurityNegotiation */
1988 		auth_method = spdk_iscsi_param_get_val(conn->params, "AuthMethod");
1989 		if (auth_method == NULL) {
1990 			SPDK_ERRLOG("AuthMethod is empty\n");
1991 			/* Missing parameter */
1992 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
1993 			rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
1994 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
1995 		}
1996 		if (strcasecmp(auth_method, "None") == 0) {
1997 			conn->authenticated = true;
1998 		} else {
1999 			rc = iscsi_auth_params(conn, params, auth_method,
2000 					       rsp_pdu->data, alloc_len,
2001 					       rsp_pdu->data_segment_len);
2002 			if (rc < 0) {
2003 				SPDK_ERRLOG("iscsi_auth_params() failed\n");
2004 				/* Authentication failure */
2005 				rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2006 				rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
2007 				return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2008 			}
2009 			rsp_pdu->data_segment_len = rc;
2010 			if (!conn->authenticated) {
2011 				/* not complete */
2012 				rsph->flags &= ~ISCSI_LOGIN_TRANSIT;
2013 			} else {
2014 				if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_END) {
2015 					SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CHAP phase not complete");
2016 				}
2017 			}
2018 
2019 			SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Auth Params",
2020 				     rsp_pdu->data, rsp_pdu->data_segment_len);
2021 		}
2022 		break;
2023 
2024 	case ISCSI_OPERATIONAL_NEGOTIATION_PHASE:
2025 		/* LoginOperationalNegotiation */
2026 		if (conn->state == ISCSI_CONN_STATE_INVALID) {
2027 			if (conn->require_chap) {
2028 				/* Authentication failure */
2029 				rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2030 				rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
2031 				return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2032 			} else {
2033 				/* AuthMethod=None */
2034 				conn->authenticated = true;
2035 			}
2036 		}
2037 		if (!conn->authenticated) {
2038 			SPDK_ERRLOG("authentication error\n");
2039 			/* Authentication failure */
2040 			rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2041 			rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
2042 			return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2043 		}
2044 		break;
2045 
2046 	case ISCSI_FULL_FEATURE_PHASE:
2047 		/* FullFeaturePhase */
2048 		SPDK_ERRLOG("XXX Login in FullFeaturePhase\n");
2049 		/* Initiator error */
2050 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2051 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
2052 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2053 
2054 	default:
2055 		SPDK_ERRLOG("unknown stage\n");
2056 		/* Initiator error */
2057 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2058 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
2059 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2060 	}
2061 
2062 	return 0;
2063 }
2064 
2065 /* This function is used to notify the session info
2066  * return
2067  * 0: success
2068  * otherwise: error
2069  */
2070 static int
2071 iscsi_op_login_notify_session_info(struct spdk_iscsi_conn *conn,
2072 				   struct spdk_iscsi_pdu *rsp_pdu)
2073 {
2074 	struct iscsi_bhs_login_rsp *rsph;
2075 
2076 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
2077 	if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
2078 		/* normal session */
2079 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Login from %s (%s) on %s tgt_node%d"
2080 			      " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
2081 			      " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
2082 			      conn->initiator_name, conn->initiator_addr,
2083 			      conn->target->name, conn->target->num,
2084 			      conn->portal->host, conn->portal->port, conn->portal->group->tag,
2085 			      conn->sess->isid, conn->sess->tsih, conn->cid,
2086 			      (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
2087 			       ? "on" : "off"),
2088 			      (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
2089 			       ? "on" : "off"));
2090 	} else if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
2091 		/* discovery session */
2092 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Login(discovery) from %s (%s) on"
2093 			      " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
2094 			      " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
2095 			      conn->initiator_name, conn->initiator_addr,
2096 			      conn->portal->host, conn->portal->port, conn->portal->group->tag,
2097 			      conn->sess->isid, conn->sess->tsih, conn->cid,
2098 			      (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
2099 			       ? "on" : "off"),
2100 			      (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
2101 			       ? "on" : "off"));
2102 	} else {
2103 		SPDK_ERRLOG("unknown session type\n");
2104 		/* Initiator error */
2105 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2106 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
2107 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2108 	}
2109 
2110 	return 0;
2111 }
2112 
2113 /*
2114  * This function is to handle the tbit cases
2115  * return
2116  * 0: success
2117  * otherwise error
2118  */
2119 static int
2120 iscsi_op_login_rsp_handle_t_bit(struct spdk_iscsi_conn *conn,
2121 				struct spdk_iscsi_pdu *rsp_pdu)
2122 {
2123 	int rc;
2124 	struct iscsi_bhs_login_rsp *rsph;
2125 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
2126 
2127 	switch (ISCSI_BHS_LOGIN_GET_NSG(rsph->flags)) {
2128 	case ISCSI_SECURITY_NEGOTIATION_PHASE:
2129 		/* SecurityNegotiation */
2130 		conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE;
2131 		break;
2132 
2133 	case ISCSI_OPERATIONAL_NEGOTIATION_PHASE:
2134 		/* LoginOperationalNegotiation */
2135 		conn->login_phase = ISCSI_OPERATIONAL_NEGOTIATION_PHASE;
2136 		break;
2137 
2138 	case ISCSI_FULL_FEATURE_PHASE:
2139 		/* FullFeaturePhase */
2140 		conn->login_phase = ISCSI_FULL_FEATURE_PHASE;
2141 		to_be16(&rsph->tsih, conn->sess->tsih);
2142 
2143 		rc = iscsi_op_login_notify_session_info(conn, rsp_pdu);
2144 		if (rc < 0) {
2145 			return rc;
2146 		}
2147 
2148 		conn->full_feature = 1;
2149 		break;
2150 
2151 	default:
2152 		SPDK_ERRLOG("unknown stage\n");
2153 		/* Initiator error */
2154 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2155 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
2156 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2157 	}
2158 
2159 	return 0;
2160 }
2161 
2162 /*
2163  * This function is used to set the values of the internal data structure used
2164  * by spdk_iscsi_op_login function
2165  * return:
2166  * 0, used to notify the a successful login
2167  * SPDK_ISCSI_LOGIN_ERROR_RESPONSE,  used to notify a failure login.
2168  */
2169 static int
2170 iscsi_op_login_rsp_handle(struct spdk_iscsi_conn *conn,
2171 			  struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param **params,
2172 			  int alloc_len)
2173 {
2174 	int rc;
2175 	struct iscsi_bhs_login_rsp *rsph;
2176 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
2177 
2178 	/* negotiate parameters */
2179 	rc = spdk_iscsi_negotiate_params(conn, params, rsp_pdu->data, alloc_len,
2180 					 rsp_pdu->data_segment_len);
2181 	if (rc < 0) {
2182 		/*
2183 		 * spdk_iscsi_negotiate_params just returns -1 on failure,
2184 		 *  so translate this into meaningful response codes and
2185 		 *  return values.
2186 		 */
2187 		rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
2188 		rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
2189 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
2190 	}
2191 
2192 	rsp_pdu->data_segment_len = rc;
2193 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Params", rsp_pdu->data, rc);
2194 
2195 	/* handle the CSG bit case */
2196 	rc = iscsi_op_login_rsp_handle_csg_bit(conn, rsp_pdu, *params,
2197 					       alloc_len);
2198 	if (rc < 0) {
2199 		return rc;
2200 	}
2201 
2202 	/* handle the T bit case */
2203 	if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
2204 		rc = iscsi_op_login_rsp_handle_t_bit(conn, rsp_pdu);
2205 	}
2206 
2207 	return rc;
2208 }
2209 
2210 static int
2211 iscsi_op_login(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
2212 {
2213 	int rc;
2214 	struct spdk_iscsi_pdu *rsp_pdu;
2215 	struct iscsi_param *params = NULL;
2216 	struct iscsi_param **params_p = &params;
2217 	int alloc_len;
2218 	int cid;
2219 
2220 	if (conn->full_feature && conn->sess != NULL &&
2221 	    conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
2222 		return SPDK_ISCSI_CONNECTION_FATAL;
2223 	}
2224 
2225 	rsp_pdu = spdk_get_pdu();
2226 	if (rsp_pdu == NULL) {
2227 		return SPDK_ISCSI_CONNECTION_FATAL;
2228 	}
2229 	rc = iscsi_op_login_rsp_init(conn, pdu, rsp_pdu, params_p,
2230 				     &alloc_len, &cid);
2231 	if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE || rc == SPDK_ISCSI_LOGIN_ERROR_PARAMETER) {
2232 		iscsi_op_login_response(conn, rsp_pdu, *params_p);
2233 		return rc;
2234 	}
2235 
2236 	/* For other values, we need to directly return */
2237 	if (rc < 0) {
2238 		spdk_put_pdu(rsp_pdu);
2239 		return rc;
2240 	}
2241 
2242 	if (conn->state == ISCSI_CONN_STATE_INVALID) {
2243 		rc = iscsi_op_login_phase_none(conn, rsp_pdu, *params_p,
2244 					       alloc_len, cid);
2245 		if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE || rc == SPDK_ISCSI_LOGIN_ERROR_PARAMETER) {
2246 			iscsi_op_login_response(conn, rsp_pdu, *params_p);
2247 			return rc;
2248 		}
2249 	}
2250 
2251 	rc = iscsi_op_login_rsp_handle(conn, rsp_pdu, params_p, alloc_len);
2252 	if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE) {
2253 		iscsi_op_login_response(conn, rsp_pdu, *params_p);
2254 		return rc;
2255 	}
2256 
2257 	rc = iscsi_op_login_response(conn, rsp_pdu, *params_p);
2258 	if (rc == 0) {
2259 		conn->state = ISCSI_CONN_STATE_RUNNING;
2260 		if (conn->full_feature != 0) {
2261 			spdk_iscsi_conn_schedule(conn);
2262 		}
2263 	} else {
2264 		SPDK_ERRLOG("login error - connection will be destroyed\n");
2265 	}
2266 
2267 	return rc;
2268 }
2269 
2270 static int
2271 iscsi_op_text(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
2272 {
2273 	struct iscsi_param *params = NULL;
2274 	struct iscsi_param **params_p = &params;
2275 	struct spdk_iscsi_pdu *rsp_pdu;
2276 	uint8_t *data;
2277 	uint64_t lun;
2278 	uint32_t task_tag;
2279 	uint32_t CmdSN;
2280 	uint32_t ExpStatSN;
2281 	const char *val;
2282 	int F_bit, C_bit;
2283 	int data_len;
2284 	int alloc_len;
2285 	int rc;
2286 	struct iscsi_bhs_text_req *reqh;
2287 	struct iscsi_bhs_text_resp *rsph;
2288 
2289 	data_len = 0;
2290 	alloc_len = conn->MaxRecvDataSegmentLength;
2291 
2292 	reqh = (struct iscsi_bhs_text_req *)&pdu->bhs;
2293 
2294 	F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
2295 	C_bit = !!(reqh->flags & ISCSI_TEXT_CONTINUE);
2296 	lun = from_be64(&reqh->lun);
2297 	task_tag = from_be32(&reqh->itt);
2298 	CmdSN = from_be32(&reqh->cmd_sn);
2299 	pdu->cmd_sn = CmdSN;
2300 	ExpStatSN = from_be32(&reqh->exp_stat_sn);
2301 
2302 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, F=%d, C=%d, ITT=%x, TTT=%x\n",
2303 		      reqh->immediate, F_bit, C_bit, task_tag, from_be32(&reqh->ttt));
2304 
2305 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
2306 		      "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
2307 		      CmdSN, ExpStatSN, conn->StatSN, conn->sess->ExpCmdSN,
2308 		      conn->sess->MaxCmdSN);
2309 
2310 	if (ExpStatSN != conn->StatSN) {
2311 #if 0
2312 		SPDK_ERRLOG("StatSN(%u) error\n", ExpStatSN);
2313 		return -1;
2314 #else
2315 		/* StarPort have a bug */
2316 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u) rewound\n", ExpStatSN);
2317 		conn->StatSN = ExpStatSN;
2318 #endif
2319 	}
2320 
2321 	if (F_bit && C_bit) {
2322 		SPDK_ERRLOG("final and continue\n");
2323 		return -1;
2324 	}
2325 
2326 	/*
2327 	 * If this is the first text op in a sequence, save the ITT so we can
2328 	 * compare it against the ITT for subsequent ops in the same sequence.
2329 	 * If a subsequent text op in same sequence has a different ITT, reject
2330 	 * that PDU.
2331 	 */
2332 	if (conn->sess->current_text_itt == 0xffffffffU) {
2333 		conn->sess->current_text_itt = task_tag;
2334 	} else if (conn->sess->current_text_itt != task_tag) {
2335 		SPDK_ERRLOG("The correct itt is %u, and the current itt is %u...\n",
2336 			    conn->sess->current_text_itt, task_tag);
2337 		return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
2338 	}
2339 
2340 	/* store incoming parameters */
2341 	rc = spdk_iscsi_parse_params(&params, pdu->data, pdu->data_segment_len,
2342 				     C_bit, &conn->partial_text_parameter);
2343 	if (rc < 0) {
2344 		SPDK_ERRLOG("iscsi_parse_params() failed\n");
2345 		spdk_iscsi_param_free(params);
2346 		return -1;
2347 	}
2348 
2349 	data = calloc(1, alloc_len);
2350 	if (!data) {
2351 		SPDK_ERRLOG("calloc() failed for data segment\n");
2352 		spdk_iscsi_param_free(params);
2353 		return -ENOMEM;
2354 	}
2355 
2356 	/* negotiate parameters */
2357 	data_len = spdk_iscsi_negotiate_params(conn, params_p,
2358 					       data, alloc_len, data_len);
2359 	if (data_len < 0) {
2360 		SPDK_ERRLOG("spdk_iscsi_negotiate_params() failed\n");
2361 		spdk_iscsi_param_free(*params_p);
2362 		free(data);
2363 		return -1;
2364 	}
2365 
2366 	/* sendtargets is special case */
2367 	val = spdk_iscsi_param_get_val(*params_p, "SendTargets");
2368 	if (val != NULL) {
2369 		if (spdk_iscsi_param_eq_val(conn->sess->params,
2370 					    "SessionType", "Discovery")) {
2371 			if (strcasecmp(val, "") == 0) {
2372 				val = "ALL";
2373 			}
2374 
2375 			data_len = spdk_iscsi_send_tgts(conn,
2376 							conn->initiator_name,
2377 							conn->initiator_addr,
2378 							val, data, alloc_len,
2379 							data_len);
2380 		} else {
2381 			if (strcasecmp(val, "") == 0) {
2382 				val = conn->target->name;
2383 			}
2384 
2385 			if (strcasecmp(val, "ALL") == 0) {
2386 				/* not in discovery session */
2387 				data_len = iscsi_append_text(conn,
2388 							     "SendTargets",
2389 							     "Reject", data,
2390 							     alloc_len, data_len);
2391 			} else {
2392 				data_len = spdk_iscsi_send_tgts(conn,
2393 								conn->initiator_name,
2394 								conn->initiator_addr,
2395 								val, data, alloc_len,
2396 								data_len);
2397 			}
2398 		}
2399 	} else {
2400 		if (spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Discovery")) {
2401 			spdk_iscsi_param_free(*params_p);
2402 			free(data);
2403 			return SPDK_ISCSI_CONNECTION_FATAL;
2404 		}
2405 	}
2406 
2407 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "Negotiated Params", data, data_len);
2408 
2409 	/* response PDU */
2410 	rsp_pdu = spdk_get_pdu();
2411 	if (rsp_pdu == NULL) {
2412 		spdk_iscsi_param_free(*params_p);
2413 		free(data);
2414 		return SPDK_ISCSI_CONNECTION_FATAL;
2415 	}
2416 	rsph = (struct iscsi_bhs_text_resp *)&rsp_pdu->bhs;
2417 
2418 	rsp_pdu->data = data;
2419 	rsph->opcode = ISCSI_OP_TEXT_RSP;
2420 
2421 	if (F_bit) {
2422 		rsph->flags |= ISCSI_FLAG_FINAL;
2423 	}
2424 
2425 	if (C_bit) {
2426 		rsph->flags |= ISCSI_TEXT_CONTINUE;
2427 	}
2428 
2429 	DSET24(rsph->data_segment_len, data_len);
2430 	to_be64(&rsph->lun, lun);
2431 	to_be32(&rsph->itt, task_tag);
2432 
2433 	if (F_bit) {
2434 		rsph->ttt = 0xffffffffU;
2435 		conn->sess->current_text_itt = 0xffffffffU;
2436 	} else {
2437 		to_be32(&rsph->ttt, 1 + conn->id);
2438 	}
2439 
2440 	to_be32(&rsph->stat_sn, conn->StatSN);
2441 	conn->StatSN++;
2442 
2443 	if (reqh->immediate == 0) {
2444 		conn->sess->MaxCmdSN++;
2445 	}
2446 
2447 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
2448 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
2449 
2450 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
2451 
2452 	/* update internal variables */
2453 	rc = spdk_iscsi_copy_param2var(conn);
2454 	if (rc < 0) {
2455 		SPDK_ERRLOG("spdk_iscsi_copy_param2var() failed\n");
2456 		spdk_iscsi_param_free(*params_p);
2457 		return -1;
2458 	}
2459 
2460 	/* check value */
2461 	rc = iscsi_check_values(conn);
2462 	if (rc < 0) {
2463 		SPDK_ERRLOG("iscsi_check_values() failed\n");
2464 		spdk_iscsi_param_free(*params_p);
2465 		return -1;
2466 	}
2467 
2468 	spdk_iscsi_param_free(*params_p);
2469 	return 0;
2470 }
2471 
2472 static int
2473 iscsi_op_logout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
2474 {
2475 	struct spdk_iscsi_pdu *rsp_pdu;
2476 	uint32_t task_tag;
2477 	uint32_t CmdSN;
2478 	uint32_t ExpStatSN;
2479 	int response;
2480 	struct iscsi_bhs_logout_req *reqh;
2481 	struct iscsi_bhs_logout_resp *rsph;
2482 	uint16_t cid;
2483 
2484 	reqh = (struct iscsi_bhs_logout_req *)&pdu->bhs;
2485 
2486 	cid = from_be16(&reqh->cid);
2487 	task_tag = from_be32(&reqh->itt);
2488 	CmdSN = from_be32(&reqh->cmd_sn);
2489 	pdu->cmd_sn = CmdSN;
2490 	ExpStatSN = from_be32(&reqh->exp_stat_sn);
2491 
2492 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "reason=%d, ITT=%x, cid=%d\n",
2493 		      reqh->reason, task_tag, cid);
2494 
2495 	if (reqh->reason != 0 && conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
2496 		SPDK_ERRLOG("only logout with close the session reason can be in discovery session");
2497 		return SPDK_ISCSI_CONNECTION_FATAL;
2498 	}
2499 
2500 	if (conn->sess != NULL) {
2501 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
2502 			      "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
2503 			      CmdSN, ExpStatSN, conn->StatSN,
2504 			      conn->sess->ExpCmdSN, conn->sess->MaxCmdSN);
2505 
2506 		if (CmdSN != conn->sess->ExpCmdSN) {
2507 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN(%u) might have dropped\n", CmdSN);
2508 			/* ignore error */
2509 		}
2510 	} else {
2511 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n",
2512 			      CmdSN, ExpStatSN, conn->StatSN);
2513 	}
2514 
2515 	if (ExpStatSN != conn->StatSN) {
2516 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u/%u) might have dropped\n",
2517 			      ExpStatSN, conn->StatSN);
2518 		/* ignore error */
2519 	}
2520 
2521 	if (conn->id == cid) {
2522 		/* connection or session closed successfully */
2523 		response = 0;
2524 		spdk_iscsi_conn_logout(conn);
2525 	} else {
2526 		response = 1;
2527 	}
2528 
2529 	/* response PDU */
2530 	rsp_pdu = spdk_get_pdu();
2531 	if (rsp_pdu == NULL) {
2532 		return SPDK_ISCSI_CONNECTION_FATAL;
2533 	}
2534 	rsph = (struct iscsi_bhs_logout_resp *)&rsp_pdu->bhs;
2535 	rsp_pdu->data = NULL;
2536 	rsph->opcode = ISCSI_OP_LOGOUT_RSP;
2537 	rsph->flags |= 0x80; /* bit 0 must be 1 */
2538 	rsph->response = response;
2539 	DSET24(rsph->data_segment_len, 0);
2540 	to_be32(&rsph->itt, task_tag);
2541 
2542 	if (conn->sess != NULL) {
2543 		to_be32(&rsph->stat_sn, conn->StatSN);
2544 		conn->StatSN++;
2545 
2546 		if (conn->sess->connections == 1) {
2547 			conn->sess->MaxCmdSN++;
2548 		}
2549 
2550 		to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
2551 		to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
2552 	} else {
2553 		to_be32(&rsph->stat_sn, conn->StatSN);
2554 		conn->StatSN++;
2555 		to_be32(&rsph->exp_cmd_sn, CmdSN);
2556 		to_be32(&rsph->max_cmd_sn, CmdSN);
2557 	}
2558 
2559 	rsph->time_2_wait = 0;
2560 	rsph->time_2_retain = 0;
2561 
2562 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
2563 
2564 	if (conn->sess == NULL) {
2565 		/*
2566 		 * login failed but initiator still sent a logout rather than
2567 		 *  just closing the TCP connection.
2568 		 */
2569 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout(login failed) from %s (%s) on"
2570 			      " (%s:%s,%d)\n",
2571 			      conn->initiator_name, conn->initiator_addr,
2572 			      conn->portal_host, conn->portal_port, conn->pg_tag);
2573 	} else if (spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) {
2574 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout from %s (%s) on %s tgt_node%d"
2575 			      " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
2576 			      " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
2577 			      conn->initiator_name, conn->initiator_addr,
2578 			      conn->target->name, conn->target->num,
2579 			      conn->portal_host, conn->portal_port, conn->pg_tag,
2580 			      conn->sess->isid, conn->sess->tsih, conn->cid,
2581 			      (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
2582 			       ? "on" : "off"),
2583 			      (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
2584 			       ? "on" : "off"));
2585 	} else {
2586 		/* discovery session */
2587 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Logout(discovery) from %s (%s) on"
2588 			      " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
2589 			      " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
2590 			      conn->initiator_name, conn->initiator_addr,
2591 			      conn->portal_host, conn->portal_port, conn->pg_tag,
2592 			      conn->sess->isid, conn->sess->tsih, conn->cid,
2593 			      (spdk_iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
2594 			       ? "on" : "off"),
2595 			      (spdk_iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
2596 			       ? "on" : "off"));
2597 	}
2598 
2599 	return 0;
2600 }
2601 
2602 /* This function returns the spdk_scsi_task by searching the snack list via
2603  * task transfertag and the pdu's opcode
2604  */
2605 static struct spdk_iscsi_task *
2606 get_scsi_task_from_ttt(struct spdk_iscsi_conn *conn, uint32_t transfer_tag)
2607 {
2608 	struct spdk_iscsi_pdu *pdu;
2609 	struct iscsi_bhs_data_in *datain_bhs;
2610 
2611 	TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
2612 		if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
2613 			datain_bhs = (struct iscsi_bhs_data_in *)&pdu->bhs;
2614 			if (from_be32(&datain_bhs->ttt) == transfer_tag) {
2615 				return pdu->task;
2616 			}
2617 		}
2618 	}
2619 
2620 	return NULL;
2621 }
2622 
2623 /* This function returns the spdk_scsi_task by searching the snack list via
2624  * initiator task tag and the pdu's opcode
2625  */
2626 static struct spdk_iscsi_task *
2627 get_scsi_task_from_itt(struct spdk_iscsi_conn *conn,
2628 		       uint32_t task_tag, enum iscsi_op opcode)
2629 {
2630 	struct spdk_iscsi_pdu *pdu;
2631 
2632 	TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
2633 		if (pdu->bhs.opcode == opcode &&
2634 		    pdu->task != NULL &&
2635 		    pdu->task->tag == task_tag) {
2636 			return pdu->task;
2637 		}
2638 	}
2639 
2640 	return NULL;
2641 }
2642 
2643 static int
2644 iscsi_send_datain(struct spdk_iscsi_conn *conn,
2645 		  struct spdk_iscsi_task *task, int datain_flag,
2646 		  int residual_len, int offset, int DataSN, int len)
2647 {
2648 	struct spdk_iscsi_pdu *rsp_pdu;
2649 	struct iscsi_bhs_data_in *rsph;
2650 	uint32_t task_tag;
2651 	uint32_t transfer_tag;
2652 	int F_bit, U_bit, O_bit, S_bit;
2653 	struct spdk_iscsi_task *primary;
2654 
2655 	primary = spdk_iscsi_task_get_primary(task);
2656 
2657 	/* DATA PDU */
2658 	rsp_pdu = spdk_get_pdu();
2659 	rsph = (struct iscsi_bhs_data_in *)&rsp_pdu->bhs;
2660 	rsp_pdu->data = task->scsi.iovs[0].iov_base + offset;
2661 	rsp_pdu->data_buf_len = task->scsi.iovs[0].iov_len - offset;
2662 	rsp_pdu->data_from_mempool = true;
2663 
2664 	task_tag = task->tag;
2665 	transfer_tag = 0xffffffffU;
2666 
2667 	F_bit = datain_flag & ISCSI_FLAG_FINAL;
2668 	O_bit = datain_flag & ISCSI_DATAIN_OVERFLOW;
2669 	U_bit = datain_flag & ISCSI_DATAIN_UNDERFLOW;
2670 	S_bit = datain_flag & ISCSI_DATAIN_STATUS;
2671 
2672 	/*
2673 	 * we need to hold onto this task/cmd because until the
2674 	 * PDU has been written out
2675 	 */
2676 	rsp_pdu->task = task;
2677 	task->scsi.ref++;
2678 
2679 	rsph->opcode = ISCSI_OP_SCSI_DATAIN;
2680 
2681 	if (F_bit) {
2682 		rsph->flags |= ISCSI_FLAG_FINAL;
2683 	}
2684 
2685 	/* we leave the A_bit clear */
2686 
2687 	if (F_bit && S_bit)  {
2688 		if (O_bit) {
2689 			rsph->flags |= ISCSI_DATAIN_OVERFLOW;
2690 		}
2691 
2692 		if (U_bit) {
2693 			rsph->flags |= ISCSI_DATAIN_UNDERFLOW;
2694 		}
2695 	}
2696 
2697 	if (S_bit) {
2698 		rsph->flags |= ISCSI_DATAIN_STATUS;
2699 		rsph->status = task->scsi.status;
2700 	}
2701 
2702 	DSET24(rsph->data_segment_len, len);
2703 
2704 	to_be32(&rsph->itt, task_tag);
2705 	to_be32(&rsph->ttt, transfer_tag);
2706 
2707 	if (S_bit) {
2708 		to_be32(&rsph->stat_sn, conn->StatSN);
2709 		conn->StatSN++;
2710 	}
2711 
2712 	if (F_bit && S_bit && !spdk_iscsi_task_is_immediate(primary)) {
2713 		conn->sess->MaxCmdSN++;
2714 	}
2715 
2716 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
2717 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
2718 
2719 	to_be32(&rsph->data_sn, DataSN);
2720 
2721 	if (conn->sess->ErrorRecoveryLevel >= 1) {
2722 		primary->datain_datasn = DataSN;
2723 	}
2724 	DataSN++;
2725 
2726 	if (task->parent) {
2727 		offset += primary->scsi.data_transferred;
2728 	}
2729 	to_be32(&rsph->buffer_offset, (uint32_t)offset);
2730 
2731 	if (F_bit && S_bit) {
2732 		to_be32(&rsph->res_cnt, residual_len);
2733 	}
2734 
2735 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
2736 
2737 	return DataSN;
2738 }
2739 
2740 static int
2741 iscsi_transfer_in(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
2742 {
2743 	uint32_t DataSN;
2744 	int transfer_len;
2745 	int data_len;
2746 	int segment_len;
2747 	int offset;
2748 	int residual_len = 0;
2749 	int sent_status;
2750 	int len;
2751 	int datain_flag = 0;
2752 	int datain_seq_cnt;
2753 	int i;
2754 	int sequence_end;
2755 	struct spdk_iscsi_task *primary;
2756 
2757 	primary = spdk_iscsi_task_get_primary(task);
2758 	segment_len = conn->MaxRecvDataSegmentLength;
2759 	data_len = task->scsi.data_transferred;
2760 	transfer_len = task->scsi.length;
2761 
2762 	if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
2763 		if (task != primary) {
2764 			conn->data_in_cnt--;
2765 			/* Handle the case when primary task return success but the subtask failed */
2766 			if (primary->bytes_completed == primary->scsi.transfer_len &&
2767 			    primary->scsi.status == SPDK_SCSI_STATUS_GOOD) {
2768 				conn->data_in_cnt--;
2769 			}
2770 		} else {
2771 			/* handle the case that it is a primary task which has subtasks */
2772 			if (primary->scsi.transfer_len != primary->scsi.length) {
2773 				conn->data_in_cnt--;
2774 			}
2775 		}
2776 
2777 		return 0;
2778 	}
2779 
2780 	if (data_len < transfer_len) {
2781 		/* underflow */
2782 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Underflow %u/%u\n", data_len, transfer_len);
2783 		residual_len = transfer_len - data_len;
2784 		transfer_len = data_len;
2785 		datain_flag |= ISCSI_DATAIN_UNDERFLOW;
2786 	} else if (data_len > transfer_len) {
2787 		/* overflow */
2788 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Overflow %u/%u\n", data_len, transfer_len);
2789 		residual_len = data_len - transfer_len;
2790 		datain_flag |= ISCSI_DATAIN_OVERFLOW;
2791 	} else {
2792 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer %u\n", transfer_len);
2793 		residual_len = 0;
2794 	}
2795 
2796 	DataSN = primary->datain_datasn;
2797 	sent_status = 0;
2798 
2799 	/* calculate the number of sequences for all data-in pdus */
2800 	datain_seq_cnt = 1 + ((transfer_len - 1) / (int)conn->sess->MaxBurstLength);
2801 	for (i = 0; i < datain_seq_cnt; i++) {
2802 		offset = i * conn->sess->MaxBurstLength;
2803 		sequence_end = DMIN32(((i + 1) * conn->sess->MaxBurstLength),
2804 				      transfer_len);
2805 
2806 		/* send data splitted by segment_len */
2807 		for (; offset < sequence_end; offset += segment_len) {
2808 			len = DMIN32(segment_len, (sequence_end - offset));
2809 
2810 			datain_flag &= ~ISCSI_FLAG_FINAL;
2811 			datain_flag &= ~ISCSI_DATAIN_STATUS;
2812 
2813 			if (offset + len == sequence_end) {
2814 				/* last PDU in a sequence */
2815 				datain_flag |= ISCSI_FLAG_FINAL;
2816 				if (task->scsi.sense_data_len == 0) {
2817 					/* The last pdu in all data-in pdus */
2818 					if ((offset + len) == transfer_len &&
2819 					    (primary->bytes_completed == primary->scsi.transfer_len)) {
2820 						datain_flag |= ISCSI_DATAIN_STATUS;
2821 						sent_status = 1;
2822 					}
2823 				}
2824 			}
2825 
2826 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer=%d, Offset=%d, Len=%d\n",
2827 				      sequence_end, offset, len);
2828 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, DataSN=%u, Offset=%u, Len=%d\n",
2829 				      conn->StatSN, DataSN, offset, len);
2830 
2831 			DataSN = iscsi_send_datain(conn, task, datain_flag, residual_len,
2832 						   offset, DataSN, len);
2833 		}
2834 	}
2835 
2836 	if (task != primary) {
2837 		primary->scsi.data_transferred += task->scsi.data_transferred;
2838 	}
2839 	primary->datain_datasn = DataSN;
2840 
2841 	return sent_status;
2842 }
2843 
2844 /*
2845  *  This function compare the input pdu's bhs with the pdu's bhs associated by
2846  *  active_r2t_tasks and queued_r2t_tasks in a connection
2847  */
2848 static bool
2849 iscsi_compare_pdu_bhs_within_existed_r2t_tasks(struct spdk_iscsi_conn *conn,
2850 		struct spdk_iscsi_pdu *pdu)
2851 {
2852 	struct spdk_iscsi_task	*task;
2853 
2854 	TAILQ_FOREACH(task, &conn->active_r2t_tasks, link) {
2855 		if (!memcmp(&pdu->bhs, spdk_iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) {
2856 			return true;
2857 		}
2858 	}
2859 
2860 	TAILQ_FOREACH(task, &conn->queued_r2t_tasks, link) {
2861 		if (!memcmp(&pdu->bhs, spdk_iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) {
2862 			return true;
2863 		}
2864 	}
2865 
2866 	return false;
2867 }
2868 
2869 static void
2870 iscsi_queue_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
2871 {
2872 	spdk_trace_record(TRACE_ISCSI_TASK_QUEUE, conn->id, task->scsi.length,
2873 			  (uintptr_t)task, (uintptr_t)task->pdu);
2874 	task->is_queued = true;
2875 	spdk_scsi_dev_queue_task(conn->dev, &task->scsi);
2876 }
2877 
2878 static void
2879 iscsi_queue_mgmt_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
2880 {
2881 	spdk_scsi_dev_queue_mgmt_task(conn->dev, &task->scsi);
2882 }
2883 
2884 int spdk_iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn)
2885 {
2886 	struct spdk_iscsi_task *task;
2887 
2888 	while (!TAILQ_EMPTY(&conn->queued_datain_tasks) &&
2889 	       conn->data_in_cnt < MAX_LARGE_DATAIN_PER_CONNECTION) {
2890 		task = TAILQ_FIRST(&conn->queued_datain_tasks);
2891 		assert(task->current_datain_offset <= task->scsi.transfer_len);
2892 
2893 		if (task->current_datain_offset == 0) {
2894 			task->scsi.lun = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
2895 			if (task->scsi.lun == NULL) {
2896 				TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
2897 				spdk_scsi_task_process_null_lun(&task->scsi);
2898 				spdk_iscsi_task_cpl(&task->scsi);
2899 				return 0;
2900 			}
2901 			task->current_datain_offset = task->scsi.length;
2902 			conn->data_in_cnt++;
2903 			iscsi_queue_task(conn, task);
2904 			continue;
2905 		}
2906 		if (task->current_datain_offset < task->scsi.transfer_len) {
2907 			struct spdk_iscsi_task *subtask;
2908 			uint32_t remaining_size = 0;
2909 
2910 			remaining_size = task->scsi.transfer_len - task->current_datain_offset;
2911 			subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl);
2912 			assert(subtask != NULL);
2913 			subtask->scsi.offset = task->current_datain_offset;
2914 			subtask->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size);
2915 			spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
2916 			task->current_datain_offset += subtask->scsi.length;
2917 			conn->data_in_cnt++;
2918 
2919 			task->scsi.lun = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
2920 			if (task->scsi.lun == NULL) {
2921 				/* Remove the primary task from the list if this is the last subtask */
2922 				if (task->current_datain_offset == task->scsi.transfer_len) {
2923 					TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
2924 				}
2925 				subtask->scsi.transfer_len = subtask->scsi.length;
2926 				spdk_scsi_task_process_null_lun(&subtask->scsi);
2927 				spdk_iscsi_task_cpl(&subtask->scsi);
2928 				return 0;
2929 			}
2930 
2931 			iscsi_queue_task(conn, subtask);
2932 		}
2933 		if (task->current_datain_offset == task->scsi.transfer_len) {
2934 			TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
2935 		}
2936 	}
2937 	return 0;
2938 }
2939 
2940 static int
2941 iscsi_op_scsi_read(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
2942 {
2943 	int32_t remaining_size;
2944 
2945 	TAILQ_INIT(&task->subtask_list);
2946 	task->scsi.dxfer_dir = SPDK_SCSI_DIR_FROM_DEV;
2947 	task->parent = NULL;
2948 	task->scsi.offset = 0;
2949 	task->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, task->scsi.transfer_len);
2950 	spdk_scsi_task_set_data(&task->scsi, NULL, 0);
2951 
2952 	remaining_size = task->scsi.transfer_len - task->scsi.length;
2953 	task->current_datain_offset = 0;
2954 
2955 	if (remaining_size == 0) {
2956 		iscsi_queue_task(conn, task);
2957 		return 0;
2958 	}
2959 
2960 	TAILQ_INSERT_TAIL(&conn->queued_datain_tasks, task, link);
2961 
2962 	return spdk_iscsi_conn_handle_queued_datain_tasks(conn);
2963 }
2964 
2965 static int
2966 iscsi_op_scsi(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
2967 {
2968 	struct spdk_iscsi_task	*task;
2969 	struct spdk_scsi_dev	*dev;
2970 	uint8_t *cdb;
2971 	uint64_t lun;
2972 	uint32_t task_tag;
2973 	uint32_t transfer_len;
2974 	uint32_t scsi_data_len;
2975 	int F_bit, R_bit, W_bit;
2976 	int lun_i, rc;
2977 	struct iscsi_bhs_scsi_req *reqh;
2978 
2979 	if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
2980 		SPDK_ERRLOG("ISCSI_OP_SCSI not allowed in discovery and invalid session\n");
2981 		return SPDK_ISCSI_CONNECTION_FATAL;
2982 	}
2983 
2984 	reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
2985 
2986 	F_bit = reqh->final_bit;
2987 	R_bit = reqh->read_bit;
2988 	W_bit = reqh->write_bit;
2989 	lun = from_be64(&reqh->lun);
2990 	task_tag = from_be32(&reqh->itt);
2991 	transfer_len = from_be32(&reqh->expected_data_xfer_len);
2992 	cdb = reqh->cdb;
2993 
2994 	SPDK_LOGDUMP(SPDK_LOG_ISCSI, "CDB", cdb, 16);
2995 
2996 	task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_cpl);
2997 	if (!task) {
2998 		SPDK_ERRLOG("Unable to acquire task\n");
2999 		return SPDK_ISCSI_CONNECTION_FATAL;
3000 	}
3001 
3002 	spdk_iscsi_task_associate_pdu(task, pdu);
3003 	lun_i = spdk_scsi_lun_id_fmt_to_int(lun);
3004 	task->lun_id = lun_i;
3005 	dev = conn->dev;
3006 	task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i);
3007 
3008 	if ((R_bit != 0) && (W_bit != 0)) {
3009 		SPDK_ERRLOG("Bidirectional CDB is not supported\n");
3010 		spdk_iscsi_task_put(task);
3011 		return SPDK_ISCSI_CONNECTION_FATAL;
3012 	}
3013 
3014 	task->scsi.cdb = cdb;
3015 	task->tag = task_tag;
3016 	task->scsi.transfer_len = transfer_len;
3017 	task->scsi.target_port = conn->target_port;
3018 	task->scsi.initiator_port = conn->initiator_port;
3019 	task->parent = NULL;
3020 	task->rsp_scsi_status = SPDK_SCSI_STATUS_GOOD;
3021 
3022 	if (task->scsi.lun == NULL) {
3023 		spdk_scsi_task_process_null_lun(&task->scsi);
3024 		spdk_iscsi_task_cpl(&task->scsi);
3025 		return 0;
3026 	}
3027 
3028 	/* no bi-directional support */
3029 	if (R_bit) {
3030 		return iscsi_op_scsi_read(conn, task);
3031 	} else if (W_bit) {
3032 		task->scsi.dxfer_dir = SPDK_SCSI_DIR_TO_DEV;
3033 
3034 		if ((conn->sess->ErrorRecoveryLevel >= 1) &&
3035 		    (iscsi_compare_pdu_bhs_within_existed_r2t_tasks(conn, pdu))) {
3036 			spdk_iscsi_task_response(conn, task);
3037 			spdk_iscsi_task_put(task);
3038 			return 0;
3039 		}
3040 
3041 		if (pdu->data_segment_len > transfer_len) {
3042 			SPDK_ERRLOG("data segment len(=%d) > task transfer len(=%d)\n",
3043 				    (int)pdu->data_segment_len, transfer_len);
3044 			spdk_iscsi_task_put(task);
3045 			return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
3046 		}
3047 
3048 		/* check the ImmediateData and also pdu->data_segment_len */
3049 		if ((!conn->sess->ImmediateData && (pdu->data_segment_len > 0)) ||
3050 		    (pdu->data_segment_len > conn->sess->FirstBurstLength)) {
3051 			spdk_iscsi_task_put(task);
3052 			return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
3053 		}
3054 
3055 		if (spdk_likely(!pdu->dif_insert_or_strip)) {
3056 			scsi_data_len = pdu->data_segment_len;
3057 		} else {
3058 			scsi_data_len = pdu->data_buf_len;
3059 		}
3060 
3061 		if (F_bit && pdu->data_segment_len < transfer_len) {
3062 			/* needs R2T */
3063 			rc = add_transfer_task(conn, task);
3064 			if (rc < 0) {
3065 				SPDK_ERRLOG("add_transfer_task() failed\n");
3066 				spdk_iscsi_task_put(task);
3067 				return SPDK_ISCSI_CONNECTION_FATAL;
3068 			}
3069 
3070 			/* Non-immediate writes */
3071 			if (pdu->data_segment_len == 0) {
3072 				return 0;
3073 			} else {
3074 				/* we are doing the first partial write task */
3075 				task->scsi.ref++;
3076 				spdk_scsi_task_set_data(&task->scsi, pdu->data, scsi_data_len);
3077 				task->scsi.length = pdu->data_segment_len;
3078 			}
3079 		}
3080 
3081 		if (pdu->data_segment_len == transfer_len) {
3082 			/* we are doing small writes with no R2T */
3083 			spdk_scsi_task_set_data(&task->scsi, pdu->data, scsi_data_len);
3084 			task->scsi.length = transfer_len;
3085 		}
3086 	} else {
3087 		/* neither R nor W bit set */
3088 		task->scsi.dxfer_dir = SPDK_SCSI_DIR_NONE;
3089 		if (transfer_len > 0) {
3090 			spdk_iscsi_task_put(task);
3091 			SPDK_ERRLOG("Reject scsi cmd with EDTL > 0 but (R | W) == 0\n");
3092 			return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
3093 		}
3094 	}
3095 
3096 	iscsi_queue_task(conn, task);
3097 	return 0;
3098 }
3099 
3100 static void
3101 abort_transfer_task_in_task_mgmt_resp(struct spdk_iscsi_conn *conn,
3102 				      struct spdk_iscsi_task *task)
3103 {
3104 	struct spdk_iscsi_pdu *pdu;
3105 
3106 	pdu = spdk_iscsi_task_get_pdu(task);
3107 
3108 	switch (task->scsi.function) {
3109 	/* abort task identified by Reference Task Tag field */
3110 	case ISCSI_TASK_FUNC_ABORT_TASK:
3111 		spdk_del_transfer_task(conn, task->scsi.abort_id);
3112 		break;
3113 
3114 	/* abort all tasks issued via this session on the LUN */
3115 	case ISCSI_TASK_FUNC_ABORT_TASK_SET:
3116 		spdk_clear_all_transfer_task(conn, task->scsi.lun, pdu);
3117 		break;
3118 
3119 	case ISCSI_TASK_FUNC_LOGICAL_UNIT_RESET:
3120 		spdk_clear_all_transfer_task(conn, task->scsi.lun, pdu);
3121 		break;
3122 	}
3123 }
3124 
3125 void
3126 spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
3127 			      struct spdk_iscsi_task *task)
3128 {
3129 	struct spdk_iscsi_pdu *rsp_pdu;
3130 	struct iscsi_bhs_task_req *reqh;
3131 	struct iscsi_bhs_task_resp *rsph;
3132 
3133 	if (task->pdu == NULL) {
3134 		/*
3135 		 * This was an internally generated task management command,
3136 		 *  usually from LUN cleanup when a connection closes.
3137 		 */
3138 		return;
3139 	}
3140 
3141 	reqh = (struct iscsi_bhs_task_req *)&task->pdu->bhs;
3142 	/* response PDU */
3143 	rsp_pdu = spdk_get_pdu();
3144 	rsph = (struct iscsi_bhs_task_resp *)&rsp_pdu->bhs;
3145 	rsph->opcode = ISCSI_OP_TASK_RSP;
3146 	rsph->flags |= 0x80; /* bit 0 default to 1 */
3147 	switch (task->scsi.response) {
3148 	case SPDK_SCSI_TASK_MGMT_RESP_COMPLETE:
3149 		abort_transfer_task_in_task_mgmt_resp(conn, task);
3150 		rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE;
3151 		break;
3152 	case SPDK_SCSI_TASK_MGMT_RESP_SUCCESS:
3153 		abort_transfer_task_in_task_mgmt_resp(conn, task);
3154 		rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE;
3155 		break;
3156 	case SPDK_SCSI_TASK_MGMT_RESP_REJECT:
3157 		rsph->response = ISCSI_TASK_FUNC_REJECTED;
3158 		break;
3159 	case SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN:
3160 		rsph->response = ISCSI_TASK_FUNC_RESP_LUN_NOT_EXIST;
3161 		break;
3162 	case SPDK_SCSI_TASK_MGMT_RESP_TARGET_FAILURE:
3163 		rsph->response = ISCSI_TASK_FUNC_REJECTED;
3164 		break;
3165 	case SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED:
3166 		rsph->response = ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED;
3167 		break;
3168 	}
3169 	rsph->itt = reqh->itt;
3170 
3171 	to_be32(&rsph->stat_sn, conn->StatSN);
3172 	conn->StatSN++;
3173 
3174 	if (reqh->immediate == 0) {
3175 		conn->sess->MaxCmdSN++;
3176 	}
3177 
3178 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
3179 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
3180 
3181 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
3182 }
3183 
3184 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn,
3185 			      struct spdk_iscsi_task *task)
3186 {
3187 	struct spdk_iscsi_pdu *rsp_pdu;
3188 	struct iscsi_bhs_scsi_resp *rsph;
3189 	uint32_t task_tag;
3190 	uint32_t transfer_len;
3191 	size_t residual_len;
3192 	size_t data_len;
3193 	int O_bit, U_bit;
3194 	int rc;
3195 	struct spdk_iscsi_task *primary;
3196 
3197 	primary = spdk_iscsi_task_get_primary(task);
3198 
3199 	transfer_len = primary->scsi.transfer_len;
3200 	task_tag = task->tag;
3201 
3202 	/* transfer data from logical unit */
3203 	/* (direction is view of initiator side) */
3204 	if (spdk_iscsi_task_is_read(primary)) {
3205 		rc = iscsi_transfer_in(conn, task);
3206 		if (rc > 0) {
3207 			/* sent status by last DATAIN PDU */
3208 			return;
3209 		}
3210 
3211 		if (primary->bytes_completed != primary->scsi.transfer_len) {
3212 			return;
3213 		}
3214 	}
3215 
3216 	O_bit = U_bit = 0;
3217 	residual_len = 0;
3218 	data_len = primary->scsi.data_transferred;
3219 
3220 	if ((transfer_len != 0) &&
3221 	    (task->scsi.status == SPDK_SCSI_STATUS_GOOD)) {
3222 		if (data_len < transfer_len) {
3223 			/* underflow */
3224 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Underflow %zu/%u\n", data_len, transfer_len);
3225 			residual_len = transfer_len - data_len;
3226 			U_bit = 1;
3227 		} else if (data_len > transfer_len) {
3228 			/* overflow */
3229 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Overflow %zu/%u\n", data_len, transfer_len);
3230 			residual_len = data_len - transfer_len;
3231 			O_bit = 1;
3232 		} else {
3233 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Transfer %u\n", transfer_len);
3234 		}
3235 	}
3236 
3237 	/* response PDU */
3238 	rsp_pdu = spdk_get_pdu();
3239 	assert(rsp_pdu != NULL);
3240 	rsph = (struct iscsi_bhs_scsi_resp *)&rsp_pdu->bhs;
3241 	assert(task->scsi.sense_data_len <= sizeof(rsp_pdu->sense.data));
3242 	memcpy(rsp_pdu->sense.data, task->scsi.sense_data, task->scsi.sense_data_len);
3243 	to_be16(&rsp_pdu->sense.length, task->scsi.sense_data_len);
3244 	rsp_pdu->data = (uint8_t *)&rsp_pdu->sense;
3245 	rsp_pdu->data_from_mempool = true;
3246 
3247 	/*
3248 	 * we need to hold onto this task/cmd because until the
3249 	 * PDU has been written out
3250 	 */
3251 	rsp_pdu->task = task;
3252 	task->scsi.ref++;
3253 
3254 	rsph->opcode = ISCSI_OP_SCSI_RSP;
3255 	rsph->flags |= 0x80; /* bit 0 is default to 1 */
3256 
3257 	if (O_bit) {
3258 		rsph->flags |= ISCSI_SCSI_OVERFLOW;
3259 	}
3260 
3261 	if (U_bit) {
3262 		rsph->flags |= ISCSI_SCSI_UNDERFLOW;
3263 	}
3264 
3265 	rsph->status = task->scsi.status;
3266 	if (task->scsi.sense_data_len) {
3267 		/* SenseLength (2 bytes) + SenseData  */
3268 		DSET24(rsph->data_segment_len, 2 + task->scsi.sense_data_len);
3269 	}
3270 	to_be32(&rsph->itt, task_tag);
3271 
3272 	to_be32(&rsph->stat_sn, conn->StatSN);
3273 	conn->StatSN++;
3274 
3275 	if (!spdk_iscsi_task_is_immediate(primary)) {
3276 		conn->sess->MaxCmdSN++;
3277 	}
3278 
3279 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
3280 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
3281 
3282 	to_be32(&rsph->bi_read_res_cnt, 0);
3283 	to_be32(&rsph->res_cnt, residual_len);
3284 
3285 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
3286 }
3287 
3288 static struct spdk_iscsi_task *
3289 get_transfer_task(struct spdk_iscsi_conn *conn, uint32_t transfer_tag)
3290 {
3291 	int i;
3292 
3293 	for (i = 0; i < conn->pending_r2t; i++) {
3294 		if (conn->outstanding_r2t_tasks[i]->ttt == transfer_tag) {
3295 			return (conn->outstanding_r2t_tasks[i]);
3296 		}
3297 	}
3298 
3299 	return NULL;
3300 }
3301 
3302 static int
3303 _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
3304 				     struct spdk_iscsi_task *task)
3305 {
3306 	struct spdk_iscsi_task *subtask;
3307 	uint32_t remaining_size;
3308 
3309 	while (conn->data_in_cnt < MAX_LARGE_DATAIN_PER_CONNECTION) {
3310 		assert(task->current_datain_offset <= task->scsi.transfer_len);
3311 
3312 		/* If no IO is submitted yet, just abort the primary task. */
3313 		if (task->current_datain_offset == 0) {
3314 			TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
3315 			spdk_scsi_task_process_abort(&task->scsi);
3316 			spdk_iscsi_task_cpl(&task->scsi);
3317 			return 0;
3318 		}
3319 
3320 		/* If any IO is submitted already, abort all subtasks by repetition. */
3321 		if (task->current_datain_offset < task->scsi.transfer_len) {
3322 			remaining_size = task->scsi.transfer_len - task->current_datain_offset;
3323 			subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl);
3324 			assert(subtask != NULL);
3325 			subtask->scsi.offset = task->current_datain_offset;
3326 			subtask->scsi.length = DMIN32(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size);
3327 			spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
3328 			task->current_datain_offset += subtask->scsi.length;
3329 			conn->data_in_cnt++;
3330 
3331 			subtask->scsi.transfer_len = subtask->scsi.length;
3332 			spdk_scsi_task_process_abort(&subtask->scsi);
3333 			spdk_iscsi_task_cpl(&subtask->scsi);
3334 		}
3335 
3336 		/* Remove the primary task from the list if this is the last subtask */
3337 		if (task->current_datain_offset == task->scsi.transfer_len) {
3338 			TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
3339 			return 0;
3340 		}
3341 	}
3342 
3343 	return -1;
3344 }
3345 
3346 static int
3347 iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
3348 				    uint32_t ref_task_tag)
3349 {
3350 	struct spdk_iscsi_task *task;
3351 
3352 	TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) {
3353 		if (task->tag == ref_task_tag) {
3354 			return _iscsi_conn_abort_queued_datain_task(conn, task);
3355 		}
3356 	}
3357 
3358 	return 0;
3359 }
3360 
3361 static int
3362 iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn,
3363 				     struct spdk_scsi_lun *lun,
3364 				     struct spdk_iscsi_pdu *pdu)
3365 {
3366 	struct spdk_iscsi_task *task, *task_tmp;
3367 	struct spdk_iscsi_pdu *pdu_tmp;
3368 	int rc;
3369 
3370 	TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) {
3371 		pdu_tmp = spdk_iscsi_task_get_pdu(task);
3372 		if ((lun == NULL || lun == task->scsi.lun) &&
3373 		    (pdu == NULL || (SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn)))) {
3374 			rc = _iscsi_conn_abort_queued_datain_task(conn, task);
3375 			if (rc != 0) {
3376 				return rc;
3377 			}
3378 		}
3379 	}
3380 
3381 	return 0;
3382 }
3383 
3384 static int
3385 _iscsi_op_abort_task(void *arg)
3386 {
3387 	struct spdk_iscsi_task *task = arg;
3388 	int rc;
3389 
3390 	rc = iscsi_conn_abort_queued_datain_task(task->conn, task->scsi.abort_id);
3391 	if (rc != 0) {
3392 		return 1;
3393 	}
3394 
3395 	spdk_poller_unregister(&task->mgmt_poller);
3396 	iscsi_queue_mgmt_task(task->conn, task);
3397 	return 1;
3398 }
3399 
3400 static void
3401 iscsi_op_abort_task(struct spdk_iscsi_task *task, uint32_t ref_task_tag)
3402 {
3403 	task->scsi.abort_id = ref_task_tag;
3404 	task->scsi.function = SPDK_SCSI_TASK_FUNC_ABORT_TASK;
3405 	task->mgmt_poller = spdk_poller_register(_iscsi_op_abort_task, task, 10);
3406 }
3407 
3408 static int
3409 _iscsi_op_abort_task_set(void *arg)
3410 {
3411 	struct spdk_iscsi_task *task = arg;
3412 	int rc;
3413 
3414 	rc = iscsi_conn_abort_queued_datain_tasks(task->conn, task->scsi.lun,
3415 			task->pdu);
3416 	if (rc != 0) {
3417 		return 1;
3418 	}
3419 
3420 	spdk_poller_unregister(&task->mgmt_poller);
3421 	iscsi_queue_mgmt_task(task->conn, task);
3422 	return 1;
3423 }
3424 
3425 void
3426 spdk_iscsi_op_abort_task_set(struct spdk_iscsi_task *task, uint8_t function)
3427 {
3428 	task->scsi.function = function;
3429 	task->mgmt_poller = spdk_poller_register(_iscsi_op_abort_task_set, task, 10);
3430 }
3431 
3432 static int
3433 iscsi_op_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
3434 {
3435 	struct iscsi_bhs_task_req *reqh;
3436 	uint64_t lun;
3437 	uint32_t task_tag;
3438 	uint32_t ref_task_tag;
3439 	uint8_t function;
3440 	int lun_i;
3441 	struct spdk_iscsi_task *task;
3442 	struct spdk_scsi_dev *dev;
3443 
3444 	if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
3445 		SPDK_ERRLOG("ISCSI_OP_TASK not allowed in discovery and invalid session\n");
3446 		return SPDK_ISCSI_CONNECTION_FATAL;
3447 	}
3448 
3449 	reqh = (struct iscsi_bhs_task_req *)&pdu->bhs;
3450 	function = reqh->flags & ISCSI_TASK_FUNCTION_MASK;
3451 	lun = from_be64(&reqh->lun);
3452 	task_tag = from_be32(&reqh->itt);
3453 	ref_task_tag = from_be32(&reqh->ref_task_tag);
3454 
3455 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, func=%d, ITT=%x, ref TT=%x, LUN=0x%16.16"PRIx64"\n",
3456 		      reqh->immediate, function, task_tag, ref_task_tag, lun);
3457 
3458 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
3459 		      conn->StatSN, conn->sess->ExpCmdSN, conn->sess->MaxCmdSN);
3460 
3461 	lun_i = spdk_scsi_lun_id_fmt_to_int(lun);
3462 	dev = conn->dev;
3463 
3464 	task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
3465 	if (!task) {
3466 		SPDK_ERRLOG("Unable to acquire task\n");
3467 		return SPDK_ISCSI_CONNECTION_FATAL;
3468 	}
3469 
3470 	spdk_iscsi_task_associate_pdu(task, pdu);
3471 	task->scsi.target_port = conn->target_port;
3472 	task->scsi.initiator_port = conn->initiator_port;
3473 	task->tag = task_tag;
3474 	task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i);
3475 
3476 	if (task->scsi.lun == NULL) {
3477 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN;
3478 		spdk_iscsi_task_mgmt_response(conn, task);
3479 		spdk_iscsi_task_put(task);
3480 		return 0;
3481 	}
3482 
3483 	switch (function) {
3484 	/* abort task identified by Referenced Task Tag field */
3485 	case ISCSI_TASK_FUNC_ABORT_TASK:
3486 		SPDK_NOTICELOG("ABORT_TASK\n");
3487 
3488 		iscsi_op_abort_task(task, ref_task_tag);
3489 		return 0;
3490 
3491 	/* abort all tasks issued via this session on the LUN */
3492 	case ISCSI_TASK_FUNC_ABORT_TASK_SET:
3493 		SPDK_NOTICELOG("ABORT_TASK_SET\n");
3494 
3495 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_ABORT_TASK_SET);
3496 		return 0;
3497 
3498 	case ISCSI_TASK_FUNC_CLEAR_TASK_SET:
3499 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
3500 		SPDK_NOTICELOG("CLEAR_TASK_SET (Unsupported)\n");
3501 		break;
3502 
3503 	case ISCSI_TASK_FUNC_CLEAR_ACA:
3504 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
3505 		SPDK_NOTICELOG("CLEAR_ACA (Unsupported)\n");
3506 		break;
3507 
3508 	case ISCSI_TASK_FUNC_LOGICAL_UNIT_RESET:
3509 		SPDK_NOTICELOG("LOGICAL_UNIT_RESET\n");
3510 
3511 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
3512 		return 0;
3513 
3514 	case ISCSI_TASK_FUNC_TARGET_WARM_RESET:
3515 		SPDK_NOTICELOG("TARGET_WARM_RESET (Unsupported)\n");
3516 
3517 #if 0
3518 		spdk_iscsi_drop_conns(conn, conn->initiator_name, 1 /* drop all */);
3519 		rc = spdk_iscsi_tgt_node_reset(conn->sess->target, lun);
3520 		if (rc < 0) {
3521 			SPDK_ERRLOG("tgt_node reset failed\n");
3522 		}
3523 #else
3524 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
3525 #endif
3526 		break;
3527 
3528 	case ISCSI_TASK_FUNC_TARGET_COLD_RESET:
3529 		SPDK_NOTICELOG("TARGET_COLD_RESET\n");
3530 
3531 #if 0
3532 		spdk_iscsi_drop_conns(conn, conn->initiator_name, 1 /* drop all */);
3533 
3534 		rc = spdk_iscsi_tgt_node_reset(conn->sess->target, lun);
3535 		if (rc < 0) {
3536 			SPDK_ERRLOG("tgt_node reset failed\n");
3537 		}
3538 
3539 		conn->state = ISCSI_CONN_STATE_EXITING;
3540 #else
3541 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
3542 #endif
3543 		break;
3544 
3545 	case ISCSI_TASK_FUNC_TASK_REASSIGN:
3546 		SPDK_NOTICELOG("TASK_REASSIGN (Unsupported)\n");
3547 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
3548 		break;
3549 
3550 	default:
3551 		SPDK_ERRLOG("unsupported function %d\n", function);
3552 		task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT;
3553 		break;
3554 	}
3555 
3556 	spdk_iscsi_task_mgmt_response(conn, task);
3557 	spdk_iscsi_task_put(task);
3558 	return 0;
3559 }
3560 
3561 static int
3562 iscsi_op_nopout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
3563 {
3564 	struct spdk_iscsi_pdu *rsp_pdu;
3565 	struct iscsi_bhs_nop_out *reqh;
3566 	struct iscsi_bhs_nop_in *rsph;
3567 	uint8_t *data;
3568 	uint64_t lun;
3569 	uint32_t task_tag;
3570 	uint32_t transfer_tag;
3571 	uint32_t CmdSN;
3572 	int I_bit;
3573 	int data_len;
3574 
3575 	if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
3576 		SPDK_ERRLOG("ISCSI_OP_NOPOUT not allowed in discovery session\n");
3577 		return SPDK_ISCSI_CONNECTION_FATAL;
3578 	}
3579 
3580 	reqh = (struct iscsi_bhs_nop_out *)&pdu->bhs;
3581 	I_bit = reqh->immediate;
3582 
3583 	data_len = DGET24(reqh->data_segment_len);
3584 	if (data_len > conn->MaxRecvDataSegmentLength) {
3585 		data_len = conn->MaxRecvDataSegmentLength;
3586 	}
3587 
3588 	lun = from_be64(&reqh->lun);
3589 	task_tag = from_be32(&reqh->itt);
3590 	transfer_tag = from_be32(&reqh->ttt);
3591 	CmdSN = from_be32(&reqh->cmd_sn);
3592 	pdu->cmd_sn = CmdSN;
3593 
3594 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "I=%d, ITT=%x, TTT=%x\n",
3595 		      I_bit, task_tag, transfer_tag);
3596 
3597 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CmdSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
3598 		      CmdSN, conn->StatSN, conn->sess->ExpCmdSN,
3599 		      conn->sess->MaxCmdSN);
3600 
3601 	if (transfer_tag != 0xFFFFFFFF && transfer_tag != (uint32_t)conn->id) {
3602 		SPDK_ERRLOG("invalid transfer tag 0x%x\n", transfer_tag);
3603 		/*
3604 		 * Technically we should probably fail the connection here, but for now
3605 		 *  just print the error message and continue.
3606 		 */
3607 	}
3608 
3609 	/*
3610 	 * We don't actually check to see if this is a response to the NOP-In
3611 	 * that we sent.  Our goal is to just verify that the initiator is
3612 	 * alive and responding to commands, not to verify that it tags
3613 	 * NOP-Outs correctly
3614 	 */
3615 	conn->nop_outstanding = false;
3616 
3617 	if (task_tag == 0xffffffffU) {
3618 		if (I_bit == 1) {
3619 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "got NOPOUT ITT=0xffffffff\n");
3620 			return 0;
3621 		} else {
3622 			SPDK_ERRLOG("got NOPOUT ITT=0xffffffff, I=0\n");
3623 			return SPDK_ISCSI_CONNECTION_FATAL;
3624 		}
3625 	}
3626 
3627 	data = calloc(1, data_len);
3628 	if (!data) {
3629 		SPDK_ERRLOG("calloc() failed for ping data\n");
3630 		return SPDK_ISCSI_CONNECTION_FATAL;
3631 	}
3632 
3633 	/* response of NOPOUT */
3634 	if (data_len > 0) {
3635 		/* copy ping data */
3636 		memcpy(data, pdu->data, data_len);
3637 	}
3638 
3639 	transfer_tag = 0xffffffffU;
3640 
3641 	/* response PDU */
3642 	rsp_pdu = spdk_get_pdu();
3643 	if (rsp_pdu == NULL) {
3644 		free(data);
3645 		return SPDK_ISCSI_CONNECTION_FATAL;
3646 	}
3647 	rsph = (struct iscsi_bhs_nop_in *)&rsp_pdu->bhs;
3648 	rsp_pdu->data = data;
3649 	rsph->opcode = ISCSI_OP_NOPIN;
3650 	rsph->flags |= 0x80; /* bit 0 default to 1 */
3651 	DSET24(rsph->data_segment_len, data_len);
3652 	to_be64(&rsph->lun, lun);
3653 	to_be32(&rsph->itt, task_tag);
3654 	to_be32(&rsph->ttt, transfer_tag);
3655 
3656 	to_be32(&rsph->stat_sn, conn->StatSN);
3657 	conn->StatSN++;
3658 
3659 	if (I_bit == 0) {
3660 		conn->sess->MaxCmdSN++;
3661 	}
3662 
3663 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
3664 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
3665 
3666 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
3667 	conn->last_nopin = spdk_get_ticks();
3668 
3669 	return 0;
3670 }
3671 
3672 static int
3673 add_transfer_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
3674 {
3675 	uint32_t transfer_len;
3676 	size_t max_burst_len;
3677 	size_t segment_len;
3678 	size_t data_len;
3679 	int len;
3680 	int idx;
3681 	int rc;
3682 	int data_out_req;
3683 
3684 	transfer_len = task->scsi.transfer_len;
3685 	data_len = spdk_iscsi_task_get_pdu(task)->data_segment_len;
3686 	max_burst_len = conn->sess->MaxBurstLength;
3687 	segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
3688 	data_out_req = 1 + (transfer_len - data_len - 1) / segment_len;
3689 	task->data_out_cnt = data_out_req;
3690 
3691 	/*
3692 	 * If we already have too many tasks using R2T, then queue this task
3693 	 *  and start sending R2T for it after some of the tasks using R2T/data
3694 	 *  out buffers complete.
3695 	 */
3696 	if (conn->pending_r2t >= DEFAULT_MAXR2T) {
3697 		TAILQ_INSERT_TAIL(&conn->queued_r2t_tasks, task, link);
3698 		return 0;
3699 	}
3700 
3701 	conn->data_out_cnt += data_out_req;
3702 	idx = conn->pending_r2t++;
3703 
3704 	conn->outstanding_r2t_tasks[idx] = task;
3705 	task->next_expected_r2t_offset = data_len;
3706 	task->current_r2t_length = 0;
3707 	task->R2TSN = 0;
3708 	/* According to RFC3720 10.8.5, 0xffffffff is
3709 	 * reserved for TTT in R2T.
3710 	 */
3711 	if (++conn->ttt == 0xffffffffu) {
3712 		conn->ttt = 0;
3713 	}
3714 	task->ttt = conn->ttt;
3715 
3716 	while (data_len != transfer_len) {
3717 		len = DMIN32(max_burst_len, (transfer_len - data_len));
3718 		rc = iscsi_send_r2t(conn, task, data_len, len,
3719 				    task->ttt, &task->R2TSN);
3720 		if (rc < 0) {
3721 			SPDK_ERRLOG("iscsi_send_r2t() failed\n");
3722 			return rc;
3723 		}
3724 		data_len += len;
3725 		task->next_r2t_offset = data_len;
3726 		task->outstanding_r2t++;
3727 		if (conn->sess->MaxOutstandingR2T == task->outstanding_r2t) {
3728 			break;
3729 		}
3730 	}
3731 
3732 	TAILQ_INSERT_TAIL(&conn->active_r2t_tasks, task, link);
3733 	return 0;
3734 }
3735 
3736 /* If there are additional large writes queued for R2Ts, start them now.
3737  *  This is called when a large write is just completed or when multiple LUNs
3738  *  are attached and large write tasks for the specific LUN are cleared.
3739  */
3740 static void
3741 start_queued_transfer_tasks(struct spdk_iscsi_conn *conn)
3742 {
3743 	struct spdk_iscsi_task *task, *tmp;
3744 
3745 	TAILQ_FOREACH_SAFE(task, &conn->queued_r2t_tasks, link, tmp) {
3746 		if (conn->pending_r2t < DEFAULT_MAXR2T) {
3747 			TAILQ_REMOVE(&conn->queued_r2t_tasks, task, link);
3748 			add_transfer_task(conn, task);
3749 		} else {
3750 			break;
3751 		}
3752 	}
3753 }
3754 
3755 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t task_tag)
3756 {
3757 	struct spdk_iscsi_task *task;
3758 	int i;
3759 
3760 	for (i = 0; i < conn->pending_r2t; i++) {
3761 		if (conn->outstanding_r2t_tasks[i]->tag == task_tag) {
3762 			task = conn->outstanding_r2t_tasks[i];
3763 			conn->data_out_cnt -= task->data_out_cnt;
3764 
3765 			conn->pending_r2t--;
3766 			for (; i < conn->pending_r2t; i++) {
3767 				conn->outstanding_r2t_tasks[i] = conn->outstanding_r2t_tasks[i + 1];
3768 			}
3769 			conn->outstanding_r2t_tasks[conn->pending_r2t] = NULL;
3770 			break;
3771 		}
3772 	}
3773 
3774 	start_queued_transfer_tasks(conn);
3775 }
3776 
3777 static void
3778 del_connection_queued_task(struct spdk_iscsi_conn *conn, void *tailq,
3779 			   struct spdk_scsi_lun *lun,
3780 			   struct spdk_iscsi_pdu *pdu)
3781 {
3782 	struct spdk_iscsi_task *task, *task_tmp;
3783 	struct spdk_iscsi_pdu *pdu_tmp;
3784 
3785 	/*
3786 	 * Temporary used to index spdk_scsi_task related
3787 	 *  queues of the connection.
3788 	 */
3789 	TAILQ_HEAD(queued_tasks, spdk_iscsi_task) *head;
3790 	head = (struct queued_tasks *)tailq;
3791 
3792 	TAILQ_FOREACH_SAFE(task, head, link, task_tmp) {
3793 		pdu_tmp = spdk_iscsi_task_get_pdu(task);
3794 		if ((lun == NULL || lun == task->scsi.lun) &&
3795 		    (pdu == NULL || SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn))) {
3796 			TAILQ_REMOVE(head, task, link);
3797 			if (lun != NULL && spdk_scsi_lun_is_removing(lun)) {
3798 				spdk_scsi_task_process_null_lun(&task->scsi);
3799 				spdk_iscsi_task_response(conn, task);
3800 			}
3801 			spdk_iscsi_task_put(task);
3802 		}
3803 	}
3804 }
3805 
3806 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
3807 				  struct spdk_scsi_lun *lun,
3808 				  struct spdk_iscsi_pdu *pdu)
3809 {
3810 	int i, j, pending_r2t;
3811 	struct spdk_iscsi_task *task;
3812 	struct spdk_iscsi_pdu *pdu_tmp;
3813 
3814 	pending_r2t = conn->pending_r2t;
3815 	for (i = 0; i < pending_r2t; i++) {
3816 		task = conn->outstanding_r2t_tasks[i];
3817 		pdu_tmp = spdk_iscsi_task_get_pdu(task);
3818 		if ((lun == NULL || lun == task->scsi.lun) &&
3819 		    (pdu == NULL || SN32_LT(pdu_tmp->cmd_sn, pdu->cmd_sn))) {
3820 			conn->outstanding_r2t_tasks[i] = NULL;
3821 			task->outstanding_r2t = 0;
3822 			task->next_r2t_offset = 0;
3823 			task->next_expected_r2t_offset = 0;
3824 			conn->data_out_cnt -= task->data_out_cnt;
3825 			conn->pending_r2t--;
3826 		}
3827 	}
3828 
3829 	for (i = 0; i < pending_r2t; i++) {
3830 		if (conn->outstanding_r2t_tasks[i] != NULL) {
3831 			continue;
3832 		}
3833 		for (j = i + 1; j < pending_r2t; j++) {
3834 			if (conn->outstanding_r2t_tasks[j] != NULL) {
3835 				conn->outstanding_r2t_tasks[i] = conn->outstanding_r2t_tasks[j];
3836 				conn->outstanding_r2t_tasks[j] = NULL;
3837 				break;
3838 			}
3839 		}
3840 	}
3841 
3842 	del_connection_queued_task(conn, &conn->active_r2t_tasks, lun, pdu);
3843 	del_connection_queued_task(conn, &conn->queued_r2t_tasks, lun, pdu);
3844 
3845 	start_queued_transfer_tasks(conn);
3846 }
3847 
3848 /* This function is used to handle the r2t snack */
3849 static int
3850 iscsi_handle_r2t_snack(struct spdk_iscsi_conn *conn,
3851 		       struct spdk_iscsi_task *task,
3852 		       struct spdk_iscsi_pdu *pdu, uint32_t beg_run,
3853 		       uint32_t run_length, int32_t task_tag)
3854 {
3855 	int32_t last_r2tsn;
3856 	int i;
3857 
3858 	if (beg_run < task->acked_r2tsn) {
3859 		SPDK_ERRLOG("ITT: 0x%08x, R2T SNACK requests retransmission of"
3860 			    "R2TSN: from 0x%08x to 0x%08x. But it has already"
3861 			    "ack to R2TSN:0x%08x, protocol error.\n",
3862 			    task_tag, beg_run, (beg_run + run_length),
3863 			    (task->acked_r2tsn - 1));
3864 		return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
3865 	}
3866 
3867 	if (run_length) {
3868 		if ((beg_run + run_length) > task->R2TSN) {
3869 			SPDK_ERRLOG("ITT: 0x%08x, received R2T SNACK with"
3870 				    "BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
3871 				    "current R2TSN: 0x%08x, protocol error.\n",
3872 				    task_tag, beg_run, run_length,
3873 				    task->R2TSN);
3874 
3875 			return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
3876 		}
3877 		last_r2tsn = (beg_run + run_length);
3878 	} else {
3879 		last_r2tsn = task->R2TSN;
3880 	}
3881 
3882 	for (i = beg_run; i < last_r2tsn; i++) {
3883 		if (iscsi_send_r2t_recovery(conn, task, i, false) < 0) {
3884 			SPDK_ERRLOG("The r2t_sn=%d of r2t_task=%p is not sent\n", i, task);
3885 		}
3886 	}
3887 	return 0;
3888 }
3889 
3890 /* This function is used to recover the data in packet */
3891 static int
3892 iscsi_handle_recovery_datain(struct spdk_iscsi_conn *conn,
3893 			     struct spdk_iscsi_task *task,
3894 			     struct spdk_iscsi_pdu *pdu, uint32_t beg_run,
3895 			     uint32_t run_length, uint32_t task_tag)
3896 {
3897 	struct spdk_iscsi_pdu *old_pdu, *pdu_temp;
3898 	uint32_t i;
3899 	struct iscsi_bhs_data_in *datain_header;
3900 	uint32_t last_statsn;
3901 
3902 	task = spdk_iscsi_task_get_primary(task);
3903 
3904 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "iscsi_handle_recovery_datain\n");
3905 
3906 	if (beg_run < task->acked_data_sn) {
3907 		SPDK_ERRLOG("ITT: 0x%08x, DATA IN SNACK requests retransmission of"
3908 			    "DATASN: from 0x%08x to 0x%08x but already acked to "
3909 			    "DATASN: 0x%08x protocol error\n",
3910 			    task_tag, beg_run,
3911 			    (beg_run + run_length), (task->acked_data_sn - 1));
3912 
3913 		return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
3914 	}
3915 
3916 	if (run_length == 0) {
3917 		/* as the DataSN begins at 0 */
3918 		run_length = task->datain_datasn + 1;
3919 	}
3920 
3921 	if ((beg_run + run_length - 1) > task->datain_datasn) {
3922 		SPDK_ERRLOG("Initiator requests BegRun: 0x%08x, RunLength:"
3923 			    "0x%08x greater than maximum DataSN: 0x%08x.\n",
3924 			    beg_run, run_length, task->datain_datasn);
3925 
3926 		return -1;
3927 	} else {
3928 		last_statsn = beg_run + run_length - 1;
3929 	}
3930 
3931 	for (i = beg_run; i <= last_statsn; i++) {
3932 		TAILQ_FOREACH_SAFE(old_pdu, &conn->snack_pdu_list, tailq, pdu_temp) {
3933 			if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
3934 				datain_header = (struct iscsi_bhs_data_in *)&old_pdu->bhs;
3935 				if (from_be32(&datain_header->itt) == task_tag &&
3936 				    from_be32(&datain_header->data_sn) == i) {
3937 					TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
3938 					spdk_iscsi_conn_write_pdu(conn, old_pdu);
3939 					break;
3940 				}
3941 			}
3942 		}
3943 	}
3944 	return 0;
3945 }
3946 
3947 /* This function is used to handle the status snack */
3948 static int
3949 iscsi_handle_status_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
3950 {
3951 	uint32_t beg_run;
3952 	uint32_t run_length;
3953 	struct iscsi_bhs_snack_req *reqh;
3954 	uint32_t i;
3955 	uint32_t last_statsn;
3956 	bool found_pdu;
3957 	struct spdk_iscsi_pdu *old_pdu;
3958 
3959 	reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
3960 	beg_run = from_be32(&reqh->beg_run);
3961 	run_length = from_be32(&reqh->run_len);
3962 
3963 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d, run_length=%d, conn->StatSN="
3964 		      "%d, conn->exp_statsn=%d\n", beg_run, run_length,
3965 		      conn->StatSN, conn->exp_statsn);
3966 
3967 	if (!beg_run) {
3968 		beg_run = conn->exp_statsn;
3969 	} else if (beg_run < conn->exp_statsn) {
3970 		SPDK_ERRLOG("Got Status SNACK Begrun: 0x%08x, RunLength: 0x%08x "
3971 			    "but already got ExpStatSN: 0x%08x on CID:%hu.\n",
3972 			    beg_run, run_length, conn->StatSN, conn->cid);
3973 
3974 		return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
3975 	}
3976 
3977 	last_statsn = (!run_length) ? conn->StatSN : (beg_run + run_length);
3978 
3979 	for (i = beg_run; i < last_statsn; i++) {
3980 		found_pdu = false;
3981 		TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) {
3982 			if (from_be32(&old_pdu->bhs.stat_sn) == i) {
3983 				found_pdu = true;
3984 				break;
3985 			}
3986 		}
3987 
3988 		if (!found_pdu) {
3989 			SPDK_ERRLOG("Unable to find StatSN: 0x%08x. For a Status"
3990 				    "SNACK, assuming this is a proactive SNACK "
3991 				    "for an untransmitted StatSN, ignoring.\n",
3992 				    beg_run);
3993 		} else {
3994 			TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
3995 			spdk_iscsi_conn_write_pdu(conn, old_pdu);
3996 		}
3997 	}
3998 
3999 	return 0;
4000 }
4001 
4002 /* This function is used to handle the data ack snack */
4003 static int
4004 iscsi_handle_data_ack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
4005 {
4006 	uint32_t transfer_tag;
4007 	uint32_t beg_run;
4008 	uint32_t run_length;
4009 	struct spdk_iscsi_pdu *old_pdu;
4010 	uint32_t old_datasn;
4011 	struct iscsi_bhs_snack_req *reqh;
4012 	struct spdk_iscsi_task *task;
4013 	struct iscsi_bhs_data_in *datain_header;
4014 	struct spdk_iscsi_task *primary;
4015 
4016 	reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
4017 	transfer_tag = from_be32(&reqh->ttt);
4018 	beg_run = from_be32(&reqh->beg_run);
4019 	run_length = from_be32(&reqh->run_len);
4020 	task = NULL;
4021 	datain_header = NULL;
4022 
4023 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d,transfer_tag=%d,run_len=%d\n",
4024 		      beg_run, transfer_tag, run_length);
4025 
4026 	task = get_scsi_task_from_ttt(conn, transfer_tag);
4027 	if (!task) {
4028 		SPDK_ERRLOG("Data ACK SNACK for TTT: 0x%08x is invalid.\n",
4029 			    transfer_tag);
4030 		goto reject_return;
4031 	}
4032 
4033 	primary = spdk_iscsi_task_get_primary(task);
4034 	if ((run_length != 0) || (beg_run < primary->acked_data_sn)) {
4035 		SPDK_ERRLOG("TTT: 0x%08x Data ACK SNACK BegRUN: %d is less than "
4036 			    "the next expected acked DataSN: %d\n",
4037 			    transfer_tag, beg_run, primary->acked_data_sn);
4038 		goto reject_return;
4039 	}
4040 
4041 	primary->acked_data_sn = beg_run;
4042 
4043 	/* To free the pdu */
4044 	TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) {
4045 		if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
4046 			datain_header = (struct iscsi_bhs_data_in *) &old_pdu->bhs;
4047 			old_datasn = from_be32(&datain_header->data_sn);
4048 			if ((from_be32(&datain_header->ttt) == transfer_tag) &&
4049 			    (old_datasn == beg_run - 1)) {
4050 				TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
4051 				if (old_pdu->task) {
4052 					spdk_iscsi_task_put(old_pdu->task);
4053 				}
4054 				spdk_put_pdu(old_pdu);
4055 				break;
4056 			}
4057 		}
4058 	}
4059 
4060 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Received Data ACK SNACK for TTT: 0x%08x,"
4061 		      " updated acked DataSN to 0x%08x.\n", transfer_tag,
4062 		      (task->acked_data_sn - 1));
4063 
4064 	return 0;
4065 
4066 reject_return:
4067 	return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_SNACK);
4068 }
4069 
4070 /* This function is used to remove the r2t pdu from snack_pdu_list by < task, r2t_sn> info */
4071 static struct spdk_iscsi_pdu *
4072 iscsi_remove_r2t_pdu_from_snack_list(struct spdk_iscsi_conn *conn,
4073 				     struct spdk_iscsi_task *task,
4074 				     uint32_t r2t_sn)
4075 {
4076 	struct spdk_iscsi_pdu *pdu;
4077 	struct iscsi_bhs_r2t *r2t_header;
4078 
4079 	TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
4080 		if (pdu->bhs.opcode == ISCSI_OP_R2T) {
4081 			r2t_header = (struct iscsi_bhs_r2t *)&pdu->bhs;
4082 			if (pdu->task == task &&
4083 			    from_be32(&r2t_header->r2t_sn) == r2t_sn) {
4084 				TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
4085 				return pdu;
4086 			}
4087 		}
4088 	}
4089 
4090 	return NULL;
4091 }
4092 
4093 /* This function is used re-send the r2t packet */
4094 static int
4095 iscsi_send_r2t_recovery(struct spdk_iscsi_conn *conn,
4096 			struct spdk_iscsi_task *task, uint32_t r2t_sn,
4097 			bool send_new_r2tsn)
4098 {
4099 	struct spdk_iscsi_pdu *pdu;
4100 	struct iscsi_bhs_r2t *rsph;
4101 	uint32_t transfer_len;
4102 	uint32_t len;
4103 	int rc;
4104 
4105 	/* remove the r2t pdu from the snack_list */
4106 	pdu = iscsi_remove_r2t_pdu_from_snack_list(conn, task, r2t_sn);
4107 	if (!pdu) {
4108 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "No pdu is found\n");
4109 		return -1;
4110 	}
4111 
4112 	/* flag
4113 	 * false: only need to re-send the old r2t with changing statsn
4114 	 * true: we send a r2t with new r2tsn
4115 	 */
4116 	if (!send_new_r2tsn) {
4117 		to_be32(&pdu->bhs.stat_sn, conn->StatSN);
4118 		spdk_iscsi_conn_write_pdu(conn, pdu);
4119 	} else {
4120 		rsph = (struct iscsi_bhs_r2t *)&pdu->bhs;
4121 		transfer_len = from_be32(&rsph->desired_xfer_len);
4122 
4123 		/* still need to increase the acked r2tsn */
4124 		task->acked_r2tsn++;
4125 		len = DMIN32(conn->sess->MaxBurstLength, (transfer_len -
4126 				task->next_expected_r2t_offset));
4127 
4128 		/* remove the old_r2t_pdu */
4129 		if (pdu->task) {
4130 			spdk_iscsi_task_put(pdu->task);
4131 		}
4132 		spdk_put_pdu(pdu);
4133 
4134 		/* re-send a new r2t pdu */
4135 		rc = iscsi_send_r2t(conn, task, task->next_expected_r2t_offset,
4136 				    len, task->ttt, &task->R2TSN);
4137 		if (rc < 0) {
4138 			return SPDK_ISCSI_CONNECTION_FATAL;
4139 		}
4140 	}
4141 
4142 	return 0;
4143 }
4144 
4145 /* This function is used to handle the snack request from the initiator */
4146 static int
4147 iscsi_op_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
4148 {
4149 	struct iscsi_bhs_snack_req *reqh;
4150 	struct spdk_iscsi_task *task;
4151 	int type;
4152 	uint32_t task_tag;
4153 	uint32_t beg_run;
4154 	uint32_t run_length;
4155 	int rc;
4156 
4157 	if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
4158 		SPDK_ERRLOG("ISCSI_OP_SNACK not allowed in  discovery session\n");
4159 		return SPDK_ISCSI_CONNECTION_FATAL;
4160 	}
4161 
4162 	reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
4163 	if (!conn->sess->ErrorRecoveryLevel) {
4164 		SPDK_ERRLOG("Got a SNACK request in ErrorRecoveryLevel=0\n");
4165 		return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
4166 	}
4167 
4168 	type = reqh->flags & ISCSI_FLAG_SNACK_TYPE_MASK;
4169 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "The value of type is %d\n", type);
4170 
4171 	switch (type) {
4172 	case 0:
4173 		reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
4174 		task_tag = from_be32(&reqh->itt);
4175 		beg_run = from_be32(&reqh->beg_run);
4176 		run_length = from_be32(&reqh->run_len);
4177 
4178 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "beg_run=%d, run_length=%d, "
4179 			      "task_tag=%x, transfer_tag=%u\n", beg_run,
4180 			      run_length, task_tag, from_be32(&reqh->ttt));
4181 
4182 		task = get_scsi_task_from_itt(conn, task_tag,
4183 					      ISCSI_OP_SCSI_DATAIN);
4184 		if (task) {
4185 			return iscsi_handle_recovery_datain(conn, task, pdu,
4186 							    beg_run, run_length, task_tag);
4187 		}
4188 		task = get_scsi_task_from_itt(conn, task_tag, ISCSI_OP_R2T);
4189 		if (task) {
4190 			return iscsi_handle_r2t_snack(conn, task, pdu, beg_run,
4191 						      run_length, task_tag);
4192 		}
4193 		SPDK_ERRLOG("It is Neither datain nor r2t recovery request\n");
4194 		rc = -1;
4195 		break;
4196 	case ISCSI_FLAG_SNACK_TYPE_STATUS:
4197 		rc = iscsi_handle_status_snack(conn, pdu);
4198 		break;
4199 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
4200 		rc = iscsi_handle_data_ack(conn, pdu);
4201 		break;
4202 	case ISCSI_FLAG_SNACK_TYPE_RDATA:
4203 		SPDK_ERRLOG("R-Data SNACK is Not Supported int spdk\n");
4204 		rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
4205 		break;
4206 	default:
4207 		SPDK_ERRLOG("Unknown SNACK type %d, protocol error\n", type);
4208 		rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
4209 		break;
4210 	}
4211 
4212 	return rc;
4213 }
4214 
4215 /* This function is used to refree the pdu when it is acknowledged */
4216 static void
4217 remove_acked_pdu(struct spdk_iscsi_conn *conn, uint32_t ExpStatSN)
4218 {
4219 	struct spdk_iscsi_pdu *pdu, *pdu_temp;
4220 	uint32_t stat_sn;
4221 
4222 	conn->exp_statsn = DMIN32(ExpStatSN, conn->StatSN);
4223 	TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, pdu_temp) {
4224 		stat_sn = from_be32(&pdu->bhs.stat_sn);
4225 		if (SN32_LT(stat_sn, conn->exp_statsn)) {
4226 			TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
4227 			spdk_iscsi_conn_free_pdu(conn, pdu);
4228 		}
4229 	}
4230 }
4231 
4232 static int
4233 iscsi_op_data(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
4234 {
4235 	struct spdk_iscsi_task	*task, *subtask;
4236 	struct iscsi_bhs_data_out *reqh;
4237 	struct spdk_scsi_lun	*lun_dev;
4238 	uint32_t transfer_tag;
4239 	uint32_t task_tag;
4240 	uint32_t transfer_len;
4241 	uint32_t DataSN;
4242 	uint32_t buffer_offset;
4243 	uint32_t len;
4244 	int F_bit;
4245 	int rc;
4246 	int reject_reason = ISCSI_REASON_INVALID_PDU_FIELD;
4247 
4248 	if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
4249 		SPDK_ERRLOG("ISCSI_OP_SCSI_DATAOUT not allowed in discovery session\n");
4250 		return SPDK_ISCSI_CONNECTION_FATAL;
4251 	}
4252 
4253 	reqh = (struct iscsi_bhs_data_out *)&pdu->bhs;
4254 	F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
4255 	transfer_tag = from_be32(&reqh->ttt);
4256 	task_tag = from_be32(&reqh->itt);
4257 	DataSN = from_be32(&reqh->data_sn);
4258 	buffer_offset = from_be32(&reqh->buffer_offset);
4259 
4260 	task = get_transfer_task(conn, transfer_tag);
4261 	if (task == NULL) {
4262 		SPDK_ERRLOG("Not found task for transfer_tag=%x\n", transfer_tag);
4263 		goto reject_return;
4264 	}
4265 
4266 	lun_dev = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
4267 
4268 	if (pdu->data_segment_len > task->desired_data_transfer_length) {
4269 		SPDK_ERRLOG("the dataout pdu data length is larger than the value sent by R2T PDU\n");
4270 		return SPDK_ISCSI_CONNECTION_FATAL;
4271 	}
4272 
4273 	if (task->tag != task_tag) {
4274 		SPDK_ERRLOG("The r2t task tag is %u, and the dataout task tag is %u\n",
4275 			    task->tag, task_tag);
4276 		goto reject_return;
4277 	}
4278 
4279 	if (DataSN != task->r2t_datasn) {
4280 		SPDK_ERRLOG("DataSN(%u) exp=%d error\n", DataSN, task->r2t_datasn);
4281 		if (conn->sess->ErrorRecoveryLevel >= 1) {
4282 			goto send_r2t_recovery_return;
4283 		} else {
4284 			reject_reason = ISCSI_REASON_PROTOCOL_ERROR;
4285 			goto reject_return;
4286 		}
4287 	}
4288 
4289 	if (buffer_offset != task->next_expected_r2t_offset) {
4290 		SPDK_ERRLOG("offset(%u) error\n", buffer_offset);
4291 		return SPDK_ISCSI_CONNECTION_FATAL;
4292 	}
4293 
4294 	transfer_len = task->scsi.transfer_len;
4295 	task->current_r2t_length += pdu->data_segment_len;
4296 	task->next_expected_r2t_offset += pdu->data_segment_len;
4297 	task->r2t_datasn++;
4298 
4299 	if (task->current_r2t_length > conn->sess->MaxBurstLength) {
4300 		SPDK_ERRLOG("R2T burst(%u) > MaxBurstLength(%u)\n",
4301 			    task->current_r2t_length,
4302 			    conn->sess->MaxBurstLength);
4303 		return SPDK_ISCSI_CONNECTION_FATAL;
4304 	}
4305 
4306 	if (F_bit) {
4307 		/*
4308 		 * This R2T burst is done.  Clear the length before we
4309 		 *  receive a PDU for the next R2T burst.
4310 		 */
4311 		task->current_r2t_length = 0;
4312 	}
4313 
4314 	subtask = spdk_iscsi_task_get(conn, task, spdk_iscsi_task_cpl);
4315 	if (subtask == NULL) {
4316 		SPDK_ERRLOG("Unable to acquire subtask\n");
4317 		return SPDK_ISCSI_CONNECTION_FATAL;
4318 	}
4319 	subtask->scsi.offset = buffer_offset;
4320 	subtask->scsi.length = pdu->data_segment_len;
4321 	if (spdk_likely(!pdu->dif_insert_or_strip)) {
4322 		spdk_scsi_task_set_data(&subtask->scsi, pdu->data, pdu->data_segment_len);
4323 	} else {
4324 		spdk_scsi_task_set_data(&subtask->scsi, pdu->data, pdu->data_buf_len);
4325 	}
4326 	spdk_iscsi_task_associate_pdu(subtask, pdu);
4327 
4328 	if (task->next_expected_r2t_offset == transfer_len) {
4329 		task->acked_r2tsn++;
4330 	} else if (F_bit && (task->next_r2t_offset < transfer_len)) {
4331 		task->acked_r2tsn++;
4332 		len = DMIN32(conn->sess->MaxBurstLength, (transfer_len -
4333 				task->next_r2t_offset));
4334 		rc = iscsi_send_r2t(conn, task, task->next_r2t_offset, len,
4335 				    task->ttt, &task->R2TSN);
4336 		if (rc < 0) {
4337 			SPDK_ERRLOG("iscsi_send_r2t() failed\n");
4338 		}
4339 		task->next_r2t_offset += len;
4340 	}
4341 
4342 	if (lun_dev == NULL) {
4343 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "LUN %d is removed, complete the task immediately\n",
4344 			      task->lun_id);
4345 		subtask->scsi.transfer_len = subtask->scsi.length;
4346 		spdk_scsi_task_process_null_lun(&subtask->scsi);
4347 		spdk_iscsi_task_cpl(&subtask->scsi);
4348 		return 0;
4349 	}
4350 
4351 	iscsi_queue_task(conn, subtask);
4352 	return 0;
4353 
4354 send_r2t_recovery_return:
4355 	rc = iscsi_send_r2t_recovery(conn, task, task->acked_r2tsn, true);
4356 	if (rc == 0) {
4357 		return 0;
4358 	}
4359 
4360 reject_return:
4361 	return iscsi_reject(conn, pdu, reject_reason);
4362 }
4363 
4364 static int
4365 iscsi_send_r2t(struct spdk_iscsi_conn *conn,
4366 	       struct spdk_iscsi_task *task, int offset,
4367 	       int len, uint32_t transfer_tag, uint32_t *R2TSN)
4368 {
4369 	struct spdk_iscsi_pdu *rsp_pdu;
4370 	struct iscsi_bhs_r2t *rsph;
4371 	uint64_t fmt_lun;
4372 
4373 	/* R2T PDU */
4374 	rsp_pdu = spdk_get_pdu();
4375 	if (rsp_pdu == NULL) {
4376 		return SPDK_ISCSI_CONNECTION_FATAL;
4377 	}
4378 	rsph = (struct iscsi_bhs_r2t *)&rsp_pdu->bhs;
4379 	rsp_pdu->data = NULL;
4380 	rsph->opcode = ISCSI_OP_R2T;
4381 	rsph->flags |= 0x80; /* bit 0 is default to 1 */
4382 	fmt_lun = spdk_scsi_lun_id_int_to_fmt(task->lun_id);
4383 	to_be64(&rsph->lun, fmt_lun);
4384 	to_be32(&rsph->itt, task->tag);
4385 	to_be32(&rsph->ttt, transfer_tag);
4386 
4387 	to_be32(&rsph->stat_sn, conn->StatSN);
4388 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
4389 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
4390 
4391 	to_be32(&rsph->r2t_sn, *R2TSN);
4392 	*R2TSN += 1;
4393 
4394 	task->r2t_datasn = 0; /* next expected datasn to ack */
4395 
4396 	to_be32(&rsph->buffer_offset, (uint32_t)offset);
4397 	to_be32(&rsph->desired_xfer_len, (uint32_t)len);
4398 	task->desired_data_transfer_length = (size_t)len;
4399 
4400 	/* we need to hold onto this task/cmd because until the PDU has been
4401 	 * written out */
4402 	rsp_pdu->task = task;
4403 	task->scsi.ref++;
4404 
4405 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
4406 
4407 	return 0;
4408 }
4409 
4410 void spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn)
4411 {
4412 	struct spdk_iscsi_pdu *rsp_pdu;
4413 	struct iscsi_bhs_nop_in	*rsp;
4414 
4415 	/* Only send nopin if we have logged in and are in a normal session. */
4416 	if (conn->sess == NULL ||
4417 	    !conn->full_feature ||
4418 	    !spdk_iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) {
4419 		return;
4420 	}
4421 
4422 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n",
4423 		      conn->sess->isid, conn->sess->tsih, conn->cid);
4424 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
4425 		      conn->StatSN, conn->sess->ExpCmdSN,
4426 		      conn->sess->MaxCmdSN);
4427 
4428 	rsp_pdu = spdk_get_pdu();
4429 	rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs;
4430 	rsp_pdu->data = NULL;
4431 
4432 	/*
4433 	 * spdk_get_pdu() memset's the PDU for us, so only fill out the needed
4434 	 *  fields.
4435 	 */
4436 	rsp->opcode = ISCSI_OP_NOPIN;
4437 	rsp->flags = 0x80;
4438 	/*
4439 	 * Technically the to_be32() is not needed here, since
4440 	 *  to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU.
4441 	 */
4442 	to_be32(&rsp->itt, 0xFFFFFFFFU);
4443 	to_be32(&rsp->ttt, conn->id);
4444 	to_be32(&rsp->stat_sn, conn->StatSN);
4445 	to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN);
4446 	to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN);
4447 
4448 	spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
4449 	conn->last_nopin = spdk_get_ticks();
4450 	conn->nop_outstanding = true;
4451 }
4452 
4453 static void
4454 init_login_reject_response(struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu)
4455 {
4456 	struct iscsi_bhs_login_rsp *rsph;
4457 
4458 	memset(rsp_pdu, 0, sizeof(struct spdk_iscsi_pdu));
4459 	rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
4460 	rsph->version_max = ISCSI_VERSION;
4461 	rsph->version_act = ISCSI_VERSION;
4462 	rsph->opcode = ISCSI_OP_LOGIN_RSP;
4463 	rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
4464 	rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
4465 	rsph->itt = pdu->bhs.itt;
4466 }
4467 
4468 static void
4469 iscsi_pdu_dump(struct spdk_iscsi_pdu *pdu)
4470 {
4471 	SPDK_ERRLOGDUMP("PDU", (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN);
4472 }
4473 
4474 int
4475 spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
4476 {
4477 	int opcode;
4478 	int rc;
4479 	struct spdk_iscsi_pdu *rsp_pdu = NULL;
4480 	uint32_t ExpStatSN;
4481 	int I_bit;
4482 	struct spdk_iscsi_sess *sess;
4483 	struct iscsi_bhs_scsi_req *reqh;
4484 
4485 	if (pdu == NULL) {
4486 		return -1;
4487 	}
4488 
4489 	opcode = pdu->bhs.opcode;
4490 	reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
4491 	pdu->cmd_sn = from_be32(&reqh->cmd_sn);
4492 
4493 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "opcode %x\n", opcode);
4494 
4495 	if (opcode == ISCSI_OP_LOGIN) {
4496 		rc = iscsi_op_login(conn, pdu);
4497 		if (rc < 0) {
4498 			SPDK_ERRLOG("iscsi_op_login() failed\n");
4499 		}
4500 		return rc;
4501 	}
4502 
4503 	/* connection in login phase but receive non-login opcode
4504 	 * return response code 0x020b to initiator.
4505 	 * */
4506 	if (!conn->full_feature && conn->state == ISCSI_CONN_STATE_RUNNING) {
4507 		rsp_pdu = spdk_get_pdu();
4508 		if (rsp_pdu == NULL) {
4509 			return SPDK_ISCSI_CONNECTION_FATAL;
4510 		}
4511 		init_login_reject_response(pdu, rsp_pdu);
4512 		spdk_iscsi_conn_write_pdu(conn, rsp_pdu);
4513 		SPDK_ERRLOG("Received opcode %d in login phase\n", opcode);
4514 		return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
4515 	} else if (conn->state == ISCSI_CONN_STATE_INVALID) {
4516 		SPDK_ERRLOG("before Full Feature\n");
4517 		iscsi_pdu_dump(pdu);
4518 		return SPDK_ISCSI_CONNECTION_FATAL;
4519 	}
4520 
4521 	sess = conn->sess;
4522 	if (!sess) {
4523 		SPDK_ERRLOG("Connection has no associated session!\n");
4524 		return SPDK_ISCSI_CONNECTION_FATAL;
4525 	}
4526 	I_bit = reqh->immediate;
4527 	if (I_bit == 0) {
4528 		if (SN32_LT(pdu->cmd_sn, sess->ExpCmdSN) ||
4529 		    SN32_GT(pdu->cmd_sn, sess->MaxCmdSN)) {
4530 			if (sess->session_type == SESSION_TYPE_NORMAL &&
4531 			    opcode != ISCSI_OP_SCSI_DATAOUT) {
4532 				SPDK_ERRLOG("CmdSN(%u) ignore (ExpCmdSN=%u, MaxCmdSN=%u)\n",
4533 					    pdu->cmd_sn, sess->ExpCmdSN, sess->MaxCmdSN);
4534 
4535 				if (sess->ErrorRecoveryLevel >= 1) {
4536 					SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Skip the error in ERL 1 and 2\n");
4537 				} else {
4538 					return SPDK_PDU_FATAL;
4539 				}
4540 			}
4541 		}
4542 	} else if (pdu->cmd_sn != sess->ExpCmdSN) {
4543 		SPDK_ERRLOG("CmdSN(%u) error ExpCmdSN=%u\n", pdu->cmd_sn, sess->ExpCmdSN);
4544 
4545 		if (sess->ErrorRecoveryLevel >= 1) {
4546 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Skip the error in ERL 1 and 2\n");
4547 		} else if (opcode != ISCSI_OP_NOPOUT) {
4548 			/*
4549 			 * The Linux initiator does not send valid CmdSNs for
4550 			 *  nopout under heavy load, so do not close the
4551 			 *  connection in that case.
4552 			 */
4553 			return SPDK_ISCSI_CONNECTION_FATAL;
4554 		}
4555 	}
4556 
4557 	ExpStatSN = from_be32(&reqh->exp_stat_sn);
4558 	if (SN32_GT(ExpStatSN, conn->StatSN)) {
4559 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN(%u) advanced\n", ExpStatSN);
4560 		ExpStatSN = conn->StatSN;
4561 	}
4562 
4563 	if (sess->ErrorRecoveryLevel >= 1) {
4564 		remove_acked_pdu(conn, ExpStatSN);
4565 	}
4566 
4567 	if (!I_bit && opcode != ISCSI_OP_SCSI_DATAOUT) {
4568 		sess->ExpCmdSN++;
4569 	}
4570 
4571 	switch (opcode) {
4572 	case ISCSI_OP_NOPOUT:
4573 		rc = iscsi_op_nopout(conn, pdu);
4574 		if (rc < 0) {
4575 			SPDK_ERRLOG("spdk_iscsi_op_nopout() failed\n");
4576 			return rc;
4577 		}
4578 		break;
4579 
4580 	case ISCSI_OP_SCSI:
4581 		rc = iscsi_op_scsi(conn, pdu);
4582 		if (rc < 0) {
4583 			SPDK_ERRLOG("spdk_iscsi_op_scsi() failed\n");
4584 			return rc;
4585 		}
4586 		break;
4587 	case ISCSI_OP_TASK:
4588 		rc = iscsi_op_task(conn, pdu);
4589 		if (rc < 0) {
4590 			SPDK_ERRLOG("spdk_iscsi_op_task() failed\n");
4591 			return rc;
4592 		}
4593 		break;
4594 
4595 	case ISCSI_OP_TEXT:
4596 		rc = iscsi_op_text(conn, pdu);
4597 		if (rc < 0) {
4598 			SPDK_ERRLOG("spdk_iscsi_op_text() failed\n");
4599 			return rc;
4600 		}
4601 		break;
4602 
4603 	case ISCSI_OP_LOGOUT:
4604 		rc = iscsi_op_logout(conn, pdu);
4605 		if (rc < 0) {
4606 			SPDK_ERRLOG("spdk_iscsi_op_logout() failed\n");
4607 			return rc;
4608 		}
4609 		break;
4610 
4611 	case ISCSI_OP_SCSI_DATAOUT:
4612 		rc = iscsi_op_data(conn, pdu);
4613 		if (rc < 0) {
4614 			SPDK_ERRLOG("spdk_iscsi_op_data() failed\n");
4615 			return rc;
4616 		}
4617 		break;
4618 
4619 	case ISCSI_OP_SNACK:
4620 		rc = iscsi_op_snack(conn, pdu);
4621 		if (rc < 0) {
4622 			SPDK_ERRLOG("spdk_iscsi_op_snack() failed\n");
4623 			return rc;
4624 		}
4625 		break;
4626 
4627 	default:
4628 		SPDK_ERRLOG("unsupported opcode %x\n", opcode);
4629 		return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
4630 	}
4631 
4632 	return 0;
4633 }
4634 
4635 bool
4636 spdk_iscsi_get_dif_ctx(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
4637 		       struct spdk_dif_ctx *dif_ctx)
4638 {
4639 	struct iscsi_bhs *bhs;
4640 	uint32_t data_offset = 0;
4641 	uint8_t *cdb = NULL;
4642 	uint64_t lun;
4643 	int lun_id = 0;
4644 	struct spdk_scsi_lun *lun_dev;
4645 
4646 	/* connection is not in full feature phase but non-login opcode
4647 	 * was received.
4648 	 */
4649 	if ((!conn->full_feature && conn->state == ISCSI_CONN_STATE_RUNNING) ||
4650 	    conn->state == ISCSI_CONN_STATE_INVALID) {
4651 		return false;
4652 	}
4653 
4654 	/* SCSI Command is allowed only in normal session */
4655 	if (conn->sess == NULL ||
4656 	    conn->sess->session_type != SESSION_TYPE_NORMAL) {
4657 		return false;
4658 	}
4659 
4660 	bhs = &pdu->bhs;
4661 
4662 	switch (bhs->opcode) {
4663 	case ISCSI_OP_SCSI: {
4664 		struct iscsi_bhs_scsi_req *sbhs;
4665 
4666 		sbhs = (struct iscsi_bhs_scsi_req *)bhs;
4667 		data_offset = 0;
4668 		cdb = sbhs->cdb;
4669 		lun = from_be64(&sbhs->lun);
4670 		lun_id = spdk_scsi_lun_id_fmt_to_int(lun);
4671 		break;
4672 	}
4673 	case ISCSI_OP_SCSI_DATAOUT: {
4674 		struct iscsi_bhs_data_out *dbhs;
4675 		struct spdk_iscsi_task *task;
4676 		int transfer_tag;
4677 
4678 		dbhs = (struct iscsi_bhs_data_out *)bhs;
4679 		data_offset = from_be32(&dbhs->buffer_offset);
4680 		transfer_tag = from_be32(&dbhs->ttt);
4681 		task = get_transfer_task(conn, transfer_tag);
4682 		if (task == NULL) {
4683 			return false;
4684 		}
4685 		cdb = task->scsi.cdb;
4686 		lun_id = task->lun_id;
4687 		break;
4688 	}
4689 	case ISCSI_OP_SCSI_DATAIN: {
4690 		struct iscsi_bhs_data_in *dbhs;
4691 		struct spdk_iscsi_task *task;
4692 
4693 		dbhs = (struct iscsi_bhs_data_in *)bhs;
4694 		data_offset = from_be32(&dbhs->buffer_offset);
4695 		task = pdu->task;
4696 		assert(task != NULL);
4697 		cdb = task->scsi.cdb;
4698 		lun_id = task->lun_id;
4699 		break;
4700 	}
4701 	default:
4702 		return false;
4703 	}
4704 
4705 	lun_dev = spdk_scsi_dev_get_lun(conn->dev, lun_id);
4706 	if (lun_dev == NULL) {
4707 		return false;
4708 	}
4709 
4710 	return spdk_scsi_lun_get_dif_ctx(lun_dev, cdb, data_offset, dif_ctx);
4711 }
4712 
4713 void spdk_free_sess(struct spdk_iscsi_sess *sess)
4714 {
4715 	if (sess == NULL) {
4716 		return;
4717 	}
4718 
4719 	sess->tag = 0;
4720 	sess->target = NULL;
4721 	sess->session_type = SESSION_TYPE_INVALID;
4722 	spdk_iscsi_param_free(sess->params);
4723 	free(sess->conns);
4724 	spdk_scsi_port_free(&sess->initiator_port);
4725 	spdk_mempool_put(g_spdk_iscsi.session_pool, (void *)sess);
4726 }
4727 
4728 static int
4729 create_iscsi_sess(struct spdk_iscsi_conn *conn,
4730 		  struct spdk_iscsi_tgt_node *target,
4731 		  enum session_type session_type)
4732 {
4733 	struct spdk_iscsi_sess *sess;
4734 	int rc;
4735 
4736 	sess = spdk_mempool_get(g_spdk_iscsi.session_pool);
4737 	if (!sess) {
4738 		SPDK_ERRLOG("Unable to get session object\n");
4739 		SPDK_ERRLOG("MaxSessions set to %d\n", g_spdk_iscsi.MaxSessions);
4740 		return -ENOMEM;
4741 	}
4742 
4743 	/* configuration values */
4744 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
4745 
4746 	sess->MaxConnections = g_spdk_iscsi.MaxConnectionsPerSession;
4747 	sess->MaxOutstandingR2T = DEFAULT_MAXOUTSTANDINGR2T;
4748 
4749 	sess->DefaultTime2Wait = g_spdk_iscsi.DefaultTime2Wait;
4750 	sess->DefaultTime2Retain = g_spdk_iscsi.DefaultTime2Retain;
4751 	sess->FirstBurstLength = g_spdk_iscsi.FirstBurstLength;
4752 	sess->MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
4753 	sess->InitialR2T = DEFAULT_INITIALR2T;
4754 	sess->ImmediateData = g_spdk_iscsi.ImmediateData;
4755 	sess->DataPDUInOrder = DEFAULT_DATAPDUINORDER;
4756 	sess->DataSequenceInOrder = DEFAULT_DATASEQUENCEINORDER;
4757 	sess->ErrorRecoveryLevel = g_spdk_iscsi.ErrorRecoveryLevel;
4758 
4759 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
4760 
4761 	sess->tag = conn->portal->group->tag;
4762 
4763 	sess->conns = calloc(sess->MaxConnections, sizeof(*sess->conns));
4764 	if (!sess->conns) {
4765 		SPDK_ERRLOG("calloc() failed for connection array\n");
4766 		return -ENOMEM;
4767 	}
4768 
4769 	sess->connections = 0;
4770 
4771 	sess->conns[sess->connections] = conn;
4772 	sess->connections++;
4773 
4774 	sess->params = NULL;
4775 	sess->target = target;
4776 	sess->isid = 0;
4777 	sess->session_type = session_type;
4778 	sess->current_text_itt = 0xffffffffU;
4779 
4780 	/* set default params */
4781 	rc = spdk_iscsi_sess_params_init(&sess->params);
4782 	if (rc < 0) {
4783 		SPDK_ERRLOG("iscsi_sess_params_init() failed\n");
4784 		goto error_return;
4785 	}
4786 	/* replace with config value */
4787 	rc = spdk_iscsi_param_set_int(sess->params, "MaxConnections",
4788 				      sess->MaxConnections);
4789 	if (rc < 0) {
4790 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4791 		goto error_return;
4792 	}
4793 
4794 	rc = spdk_iscsi_param_set_int(sess->params, "MaxOutstandingR2T",
4795 				      sess->MaxOutstandingR2T);
4796 	if (rc < 0) {
4797 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4798 		goto error_return;
4799 	}
4800 
4801 	rc = spdk_iscsi_param_set_int(sess->params, "DefaultTime2Wait",
4802 				      sess->DefaultTime2Wait);
4803 	if (rc < 0) {
4804 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4805 		goto error_return;
4806 	}
4807 
4808 	rc = spdk_iscsi_param_set_int(sess->params, "DefaultTime2Retain",
4809 				      sess->DefaultTime2Retain);
4810 	if (rc < 0) {
4811 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4812 		goto error_return;
4813 	}
4814 
4815 	rc = spdk_iscsi_param_set_int(sess->params, "FirstBurstLength",
4816 				      sess->FirstBurstLength);
4817 	if (rc < 0) {
4818 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4819 		goto error_return;
4820 	}
4821 
4822 	rc = spdk_iscsi_param_set_int(sess->params, "MaxBurstLength",
4823 				      sess->MaxBurstLength);
4824 	if (rc < 0) {
4825 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4826 		goto error_return;
4827 	}
4828 
4829 	rc = spdk_iscsi_param_set(sess->params, "InitialR2T",
4830 				  sess->InitialR2T ? "Yes" : "No");
4831 	if (rc < 0) {
4832 		SPDK_ERRLOG("iscsi_param_set() failed\n");
4833 		goto error_return;
4834 	}
4835 
4836 	rc = spdk_iscsi_param_set(sess->params, "ImmediateData",
4837 				  sess->ImmediateData ? "Yes" : "No");
4838 	if (rc < 0) {
4839 		SPDK_ERRLOG("iscsi_param_set() failed\n");
4840 		goto error_return;
4841 	}
4842 
4843 	rc = spdk_iscsi_param_set(sess->params, "DataPDUInOrder",
4844 				  sess->DataPDUInOrder ? "Yes" : "No");
4845 	if (rc < 0) {
4846 		SPDK_ERRLOG("iscsi_param_set() failed\n");
4847 		goto error_return;
4848 	}
4849 
4850 	rc = spdk_iscsi_param_set(sess->params, "DataSequenceInOrder",
4851 				  sess->DataSequenceInOrder ? "Yes" : "No");
4852 	if (rc < 0) {
4853 		SPDK_ERRLOG("iscsi_param_set() failed\n");
4854 		goto error_return;
4855 	}
4856 
4857 	rc = spdk_iscsi_param_set_int(sess->params, "ErrorRecoveryLevel",
4858 				      sess->ErrorRecoveryLevel);
4859 	if (rc < 0) {
4860 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4861 		goto error_return;
4862 	}
4863 
4864 	/* realloc buffer */
4865 	rc = spdk_iscsi_param_set_int(conn->params, "MaxRecvDataSegmentLength",
4866 				      conn->MaxRecvDataSegmentLength);
4867 	if (rc < 0) {
4868 		SPDK_ERRLOG("iscsi_param_set_int() failed\n");
4869 		goto error_return;
4870 	}
4871 
4872 	/* sess for first connection of session */
4873 	conn->sess = sess;
4874 	return 0;
4875 
4876 error_return:
4877 	spdk_free_sess(sess);
4878 	conn->sess = NULL;
4879 	return -1;
4880 }
4881 
4882 static struct spdk_iscsi_sess *
4883 get_iscsi_sess_by_tsih(uint16_t tsih)
4884 {
4885 	struct spdk_iscsi_sess *session;
4886 
4887 	if (tsih == 0 || tsih > g_spdk_iscsi.MaxSessions) {
4888 		return NULL;
4889 	}
4890 
4891 	session = g_spdk_iscsi.session[tsih - 1];
4892 	assert(tsih == session->tsih);
4893 
4894 	return session;
4895 }
4896 
4897 static uint8_t
4898 append_iscsi_sess(struct spdk_iscsi_conn *conn,
4899 		  const char *initiator_port_name, uint16_t tsih, uint16_t cid)
4900 {
4901 	struct spdk_iscsi_sess *sess;
4902 
4903 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "append session: init port name=%s, tsih=%u, cid=%u\n",
4904 		      initiator_port_name, tsih, cid);
4905 
4906 	sess = get_iscsi_sess_by_tsih(tsih);
4907 	if (sess == NULL) {
4908 		SPDK_ERRLOG("spdk_get_iscsi_sess_by_tsih failed\n");
4909 		return ISCSI_LOGIN_CONN_ADD_FAIL;
4910 	}
4911 	if ((conn->portal->group->tag != sess->tag) ||
4912 	    (strcasecmp(initiator_port_name, spdk_scsi_port_get_name(sess->initiator_port)) != 0) ||
4913 	    (conn->target != sess->target)) {
4914 		/* no match */
4915 		SPDK_ERRLOG("no MCS session for init port name=%s, tsih=%d, cid=%d\n",
4916 			    initiator_port_name, tsih, cid);
4917 		return ISCSI_LOGIN_CONN_ADD_FAIL;
4918 	}
4919 
4920 	if (sess->connections >= sess->MaxConnections) {
4921 		/* no slot for connection */
4922 		SPDK_ERRLOG("too many connections for init port name=%s, tsih=%d, cid=%d\n",
4923 			    initiator_port_name, tsih, cid);
4924 		return ISCSI_LOGIN_TOO_MANY_CONNECTIONS;
4925 	}
4926 
4927 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Connections (tsih %d): %d\n", sess->tsih, sess->connections);
4928 	conn->sess = sess;
4929 
4930 	/*
4931 	 * TODO: need a mutex or other sync mechanism to protect the session's
4932 	 *  connection list.
4933 	 */
4934 	sess->conns[sess->connections] = conn;
4935 	sess->connections++;
4936 
4937 	return 0;
4938 }
4939 
4940 bool spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu)
4941 {
4942 	if (pdu == NULL) {
4943 		return false;
4944 	}
4945 
4946 	if (pdu->bhs.opcode == ISCSI_OP_R2T ||
4947 	    pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
4948 		return true;
4949 	}
4950 
4951 	return false;
4952 }
4953