xref: /netbsd-src/external/bsd/iscsi/dist/src/lib/initiator.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*
2  * IMPORTANT:  READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
3  * By downloading, copying, installing or using the software you agree
4  * to this license.  If you do not agree to this license, do not
5  * download, install, copy or use the software.
6  *
7  * Intel License Agreement
8  *
9  * Copyright (c) 2000, Intel Corporation
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * -Redistributions of source code must retain the above copyright
17  *  notice, this list of conditions and the following disclaimer.
18  *
19  * -Redistributions in binary form must reproduce the above copyright
20  *  notice, this list of conditions and the following disclaimer in the
21  *  documentation and/or other materials provided with the
22  *  distribution.
23  *
24  * -The name of Intel Corporation may not be used to endorse or
25  *  promote products derived from this software without specific prior
26  *  written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL INTEL
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 #include "config.h"
42 
43 #include <sys/types.h>
44 
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48 
49 #ifdef HAVE_SYS_SOCKET_H
50 #include <sys/socket.h>
51 #endif
52 
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
56 
57 #ifdef HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
59 #endif
60 
61 #ifdef HAVE_INTTYPES_H
62 #include <inttypes.h>
63 #endif
64 
65 #ifdef HAVE_SIGNAL_H
66 #include <signal.h>
67 #endif
68 
69 #include <stdio.h>
70 #include <stdlib.h>
71 
72 #ifdef HAVE_STRING_H
73 #include <string.h>
74 #endif
75 
76 #include <unistd.h>
77 
78 #include "iscsiprotocol.h"
79 #include "initiator.h"
80 
81 #include "iscsi.h"
82 
83 static initiator_target_t	g_target[CONFIG_INITIATOR_NUM_TARGETS];
84 
85 /*
86  * Globals
87  */
88 static uint32_t g_tag;
89 static iscsi_spin_t g_tag_spin;
90 static hash_t   g_tag_hash;
91 static iscsi_worker_t g_enqueue_worker;
92 static iscsi_queue_t g_enqueue_q;
93 static iscsi_queue_t g_session_q;
94 static int      g_initiator_state;
95 static char           *gfilename;
96 
97 /* Testing of initiator_abort */
98 
99 static initiator_cmd_t *g_cmd = NULL;
100 
101 /*
102  * Enqueue worker functions. The enqueue worker is responsible for enqueing
103  * all iSCSI commands to one of the Tx workers.  It is also the thread
104  * responsible for initializing sessions, discovering targets and getting
105  * each session into full feature phase.
106  */
107 
108 static int      enqueue_worker_proc(void *);
109 static int      login_phase_i(initiator_session_t *, char *, int);
110 static int      logout_phase_i(initiator_session_t *);
111 
112 /*
113  * Tx functions.  initiator_cmd_t pointers are enqueued to the Tx worker
114  * for a given session by the enqueue worker.  The Tx worker will send out these
115  * commands and wait for the Rx worker to process the response.  The pointer is
116  * inserted into the hashtable g_tag_hash, keyed by the initiator tag of the iSCSI
117  * commands.
118  */
119 
120 static int      tx_worker_proc_i(void *);
121 static int      text_command_i(initiator_cmd_t *);
122 static int      login_command_i(initiator_cmd_t *);
123 static int      logout_command_i(initiator_cmd_t *);
124 static int      scsi_command_i(initiator_cmd_t *);
125 static int      nop_out_i(initiator_cmd_t *);
126 
127 
128 /*
129  * Rx functions. Upon receipt of an incoming PDU, the Rx worker will first
130  * extract the tag (if it exists for the PDU) and then the associated
131  * initiator_cmd_t pointer stored in the hash table.  One of Rx functions
132  * will be called to processs the PDU. The Rx worker will invoke the callback
133  * function associated with the command once the command has been retired.
134  */
135 
136 static int      rx_worker_proc_i(void *);
137 static int      login_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
138 static int      text_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
139 static int      logout_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
140 static int      scsi_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
141 static int      scsi_read_data_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
142 static int      scsi_r2t_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
143 static int      nop_in_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
144 static int      reject_i(initiator_session_t *, uint8_t *);
145 static int      async_msg_i(initiator_session_t *, uint8_t *);
146 
147 
148 /*
149  * Misc. Prototypes
150  */
151 
152 
153 static int      session_init_i(initiator_session_t **, uint64_t );
154 static int      session_destroy_i(initiator_session_t *);
155 static int      wait_callback_i(void *);
156 static int      discovery_phase(int, strv_t *);
157 
158 /*
159  * Private Functions
160  */
161 
162 #if 0
163 static void
164 dump_session(initiator_session_t * sess)
165 {
166         iscsi_parameter_value_t *vp;
167         iscsi_parameter_t       *ip;
168 
169                 for (ip = sess->params ; ip ; ip = ip->next) {
170                         printf("Key: %s Type: %d\n",ip->key,ip->type);
171                         for (vp = ip->value_l ; vp ; vp = vp->next) {
172                                 printf("Value: %s\n",vp->value);
173                         }
174         }
175 }
176 #endif
177 
178 /* This function reads the target IP and target name information */
179 /* from the input configuration file, and populates the */
180 /* g_target data structure  fields. */
181 static int
182 get_target_config(const char *hostname, int port)
183 {
184 	int i;
185 
186 	for (i = 0 ; i < CONFIG_INITIATOR_NUM_TARGETS ; i++) {
187 		(void) strlcpy(g_target[i].name, hostname,
188 			sizeof(g_target[i].name));
189 		g_target[i].port = port;
190 	}
191 	return 0;
192 }
193 
194 static int
195 session_init_i(initiator_session_t ** sess, uint64_t isid)
196 {
197 	initiator_session_t	*s;
198 	iscsi_parameter_t	**l;
199         char			*user;
200         int			 auth_type;
201         int			 mutual_auth;
202 	int			 one = 1;
203 
204 	iscsi_trace(TRACE_ISCSI_DEBUG, "initializing session %llu\n", isid);
205 
206 	/* Get free session */
207 	if ((*sess = iscsi_queue_remove(&g_session_q)) == NULL) {
208 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_remove() failed\n");
209 		return -1;
210 	}
211 	s = *sess;
212 	user = NULL;
213         auth_type = s->sess_params.auth_type;
214         if (s->sess_params.cred.user && auth_type != AuthNone) {
215                 user = s->sess_params.cred.user;
216         }
217         mutual_auth = s->sess_params.mutual_auth;
218 	(void) memset(s, 0x0, sizeof(*s));
219 	s->state = INITIATOR_SESSION_STATE_INITIALIZING;
220 	s->isid = s->tx_worker.id = s->rx_worker.id = (int)isid;
221 	s->cmds = NULL;
222         s->sess_params.cred.user = user;
223         s->sess_params.auth_type = auth_type;
224         s->sess_params.mutual_auth = mutual_auth;
225 
226 	iscsi_spin_init(&s->cmds_spin);
227 	g_target[(int)isid].has_session = 1;
228 
229 	/* Create socket */
230 	if (!iscsi_sock_create(&s->sock)) {
231 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_create() failed\n");
232 		return -1;
233 	}
234 	if (!iscsi_sock_setsockopt(&s->sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one))) {
235 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_setsockopt() failed\n");
236 		return -1;
237 	}
238 
239 	/* Initialize wait queues */
240 
241 	ISCSI_MUTEX_INIT(&s->tx_worker.work_mutex, return -1);
242 	ISCSI_COND_INIT(&s->tx_worker.work_cond, return -1);
243 	ISCSI_MUTEX_INIT(&s->tx_worker.exit_mutex, return -1);
244 	ISCSI_COND_INIT(&s->tx_worker.exit_cond, return -1);
245 	ISCSI_MUTEX_INIT(&s->rx_worker.work_mutex, return -1);
246 	ISCSI_COND_INIT(&s->rx_worker.work_cond, return -1);
247 	ISCSI_MUTEX_INIT(&s->rx_worker.exit_mutex, return -1);
248 	ISCSI_COND_INIT(&s->rx_worker.exit_cond, return -1);
249 
250 	/* Build parameter list */
251 
252 	/*
253          * ISCSI_PARAM_TYPE_LIST format:        <type> <key> <dflt> <valid list values>
254          * ISCSI_PARAM_TYPE_BINARY format:      <type> <key> <dflt> <valid binary values>
255          * ISCSI_PARAM_TYPE_NUMERICAL format:   <type> <key> <dflt> <max>
256          * ISCSI_PARAM_TYPE_DECLARATIVE format: <type> <key> <dflt> ""
257          */
258 
259 	s->params = NULL;
260 	l = &(s->params);
261 	/* CHAP Support Parameters */
262 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "AuthMethod", "None", "CHAP,None", return -1);
263 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "CHAP_A", "None", "5", return -1);
264 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_N", "", "", return -1);
265 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_R", "", "", return -1);
266 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_I", "", "", return -1);
267 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_C", "", "", return -1);
268 	/* CHAP Support Parameters */
269 
270 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "HeaderDigest", "None", "None", return -1);
271 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "DataDigest", "None", "None", return -1);
272 
273 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "MaxConnections", "1", "1", return -1);
274 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "SendTargets", "", "", return -1);
275 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARE_MULTI, "TargetName", "", "", return -1);
276 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "InitiatorName", "iqn.1994-04.org.NetBSD:iscsi-initiator", "", return -1);
277 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "TargetAlias", "", "", return -1);
278 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "InitiatorAlias", "", "", return -1);
279 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARE_MULTI, "TargetAddress", "", "", return -1);
280 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "InitialR2T", "Yes", "Yes,No", return -1);
281 
282 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_AND, "ImmediateData", "Yes", "Yes,No", return -1);
283 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "MaxRecvDataSegmentLength", "8192", "16777215", return -1);
284 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "MaxBurstLength", "262144", "16777215", return -1);
285 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "FirstBurstLength", "65536", "16777215", return -1);
286 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "TargetPortalGroupTag", "1", "65535", return -1);
287 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "DefaultTime2Wait", "2", "2", return -1);
288 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "DefaultTime2Retain", "20", "20", return -1);
289 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "MaxOutstandingR2T", "1", "1", return -1);
290 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "DataPDUInOrder", "Yes", "Yes,No", return -1);
291 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "DataSequenceInOrder", "Yes", "Yes,No", return -1);
292 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "ErrorRecoveryLevel", "0", "0", return -1);
293 	PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "SessionType", "Normal", "Normal,Discovery", return -1);
294 
295 	/* Start Tx worker */
296 
297 	iscsi_trace(TRACE_ISCSI_DEBUG, "starting Tx worker %llu\n", isid);
298 	if (iscsi_queue_init(&s->tx_queue, CONFIG_INITIATOR_QUEUE_DEPTH) == -1) {
299 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
300 		return -1;
301 	}
302 	ISCSI_LOCK(&s->tx_worker.exit_mutex, return -1);
303 	if (iscsi_thread_create(&s->tx_worker.thread,
304 			(void *) tx_worker_proc_i, &s->tx_worker) != 0) {
305 		iscsi_err(__FILE__, __LINE__,
306 			"iscsi_threads_create() failed\n");
307 		return -1;
308 	}
309 	ISCSI_WAIT(&s->tx_worker.exit_cond, &s->tx_worker.exit_mutex,
310 			return -1);
311 	ISCSI_UNLOCK(&s->tx_worker.exit_mutex, return -1);
312 	if (s->state == INITIATOR_SESSION_STATE_DESTROYING) {
313 		iscsi_trace(TRACE_ISCSI_DEBUG,
314 			"session %llu is being destroyed, exiting\n", isid);
315 		return -1;
316 	}
317 	if (s->tx_worker.state & ISCSI_WORKER_STATE_ERROR) {
318 		iscsi_err(__FILE__, __LINE__,
319 			"Tx worker %llu started with an error\n", isid);
320 		return -1;
321 	}
322 	iscsi_trace(TRACE_ISCSI_DEBUG, "got signal from Tx worker\n");
323 	s->state = INITIATOR_SESSION_STATE_INITIALIZED;
324 
325 	return 0;
326 }
327 
328 static int
329 session_destroy_i(initiator_session_t * sess)
330 {
331 	initiator_cmd_t	*ptr;
332 	uint64_t	 isid = sess->isid;
333 
334 	if (sess == NULL) {
335 		iscsi_err(__FILE__, __LINE__, "session pointer is NULL\n");
336 		return -1;
337 	}
338 	if (g_target[(int)sess->isid].has_session == 0) {
339 		iscsi_err(__FILE__, __LINE__,
340 			"g_target[%llu].has_session==0??\n", sess->isid);
341 		return -1;
342 	}
343 	sess->state = INITIATOR_SESSION_STATE_DESTROYING;
344 
345 	/* Abort all outstanding commands */
346 
347 	for (ptr = sess->cmds; ptr != NULL; ptr = ptr->next) {
348 		if (initiator_abort(ptr) != 0) {
349 			iscsi_err(__FILE__, __LINE__, "initiator_abort() failed\n");
350 			return -1;
351 		}
352 	}
353 
354 	if (sess->tx_worker.state & ISCSI_WORKER_STATE_STARTED) {
355 		if (sess->tx_worker.state & ISCSI_WORKER_STATE_EXITING) {
356 			iscsi_trace(TRACE_ISCSI_DEBUG,
357 				"Tx worker %llu already signalled for exit\n",
358 				sess->isid);
359 		} else {
360 			iscsi_trace(TRACE_ISCSI_DEBUG,
361 				"signaling Tx worker %llu into exiting state\n",
362 				sess->isid);
363 			ISCSI_LOCK(&sess->tx_worker.work_mutex, return -1);
364 			iscsi_trace(TRACE_ISCSI_DEBUG,
365 				"signaling socket shutdown to Tx worker %llu\n",				sess->isid);
366 			if (iscsi_sock_shutdown(sess->sock, 1) != 0) {
367 				iscsi_err(__FILE__, __LINE__,
368 					"iscsi_sock_shutdown() failed\n");
369 			}
370 			ISCSI_SIGNAL(&sess->tx_worker.work_cond, return -1);
371 			ISCSI_UNLOCK(&sess->tx_worker.work_mutex, return -1);
372 		}
373 		iscsi_trace(TRACE_ISCSI_DEBUG,
374 			"Checking exit condition of Tx worker\n");
375 		while ((sess->tx_worker.state & ISCSI_WORKER_STATE_EXITING) !=
376 				ISCSI_WORKER_STATE_EXITING) {
377 			ISCSI_SPIN;
378 		}
379 		iscsi_trace(TRACE_ISCSI_DEBUG, "Tx worker %llu has exited\n",
380 			sess->isid);
381 	} else {
382 		iscsi_trace(TRACE_ISCSI_DEBUG,
383 			"Tx worker was not started. Nothing to signal\n");
384 	}
385 
386 	/* Destroy Tx state */
387 	while ((ptr = iscsi_queue_remove(&sess->tx_queue)) != NULL) {
388 		ptr->status = -1;
389 		if (ptr->callback && ((*ptr->callback)(ptr) != 0)) {
390 			iscsi_err(__FILE__, __LINE__, "callback() failed\n");
391 		}
392 	}
393 	iscsi_queue_destroy(&sess->tx_queue);
394 
395 	if (sess->rx_worker.state & ISCSI_WORKER_STATE_STARTED) {
396 		if (sess->rx_worker.state & ISCSI_WORKER_STATE_EXITING) {
397 			iscsi_trace(TRACE_ISCSI_DEBUG,
398 				"Rx worker %llu already signalled for exit\n",
399 				sess->isid);
400 		} else {
401 			iscsi_trace(TRACE_ISCSI_DEBUG,
402 				"signaling Rx worker %llu into exiting state\n",				sess->isid);
403 			if (iscsi_sock_shutdown(sess->sock, 0) != 0) {
404 				iscsi_err(__FILE__, __LINE__,
405 					"iscsi_sock_shutdown() failed\n");
406 			}
407 		}
408 		iscsi_trace(TRACE_ISCSI_DEBUG,
409 			"Checking exit condition of Rx worker\n");
410 		while ((sess->rx_worker.state & ISCSI_WORKER_STATE_EXITING) !=
411 				ISCSI_WORKER_STATE_EXITING) {
412 			ISCSI_SPIN;
413 		}
414 		iscsi_trace(TRACE_ISCSI_DEBUG, "Rx worker %llu has exited\n",
415 				sess->isid);
416 	} else {
417 		iscsi_trace(TRACE_ISCSI_DEBUG,
418 			"Rx worker was not started. Nothing to signal\n");
419 	}
420 
421 	/* Close socket */
422 
423 	if (iscsi_sock_close(sess->sock) != 0) {
424 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_close() failed\n");
425 		return -1;
426 	}
427 	/* Destroy wait queues */
428 
429 	ISCSI_MUTEX_DESTROY(&sess->tx_worker.work_mutex, return -1);
430 	ISCSI_COND_DESTROY(&sess->tx_worker.work_cond, return -1);
431 	ISCSI_MUTEX_DESTROY(&sess->tx_worker.exit_mutex, return -1);
432 	ISCSI_COND_DESTROY(&sess->tx_worker.exit_cond, return -1);
433 	ISCSI_MUTEX_DESTROY(&sess->rx_worker.work_mutex, return -1);
434 	ISCSI_COND_DESTROY(&sess->rx_worker.work_cond, return -1);
435 	ISCSI_MUTEX_DESTROY(&sess->rx_worker.exit_mutex, return -1);
436 	ISCSI_COND_DESTROY(&sess->rx_worker.exit_cond, return -1);
437 
438 	/* Destroy param list */
439 
440 	PARAM_LIST_DESTROY(sess->params, return -1);
441 
442 	/* Enqueue session to free list */
443 	if (iscsi_queue_insert(&g_session_q, sess) == -1) {
444 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
445 		return -1;
446 	}
447 	iscsi_trace(TRACE_ISCSI_DEBUG, "session %p destroyed and requeued\n",
448 			sess);
449 
450 	g_target[(int)isid].has_session = 0;
451 
452 	return 0;
453 }
454 
455 #define IS_DISCOVERY	1
456 #define IS_SECURITY	1
457 
458 enum {
459 	SESS_TYPE_DISCOVERY = 1,
460 	SESS_TYPE_NORMAL = 2,
461 	SESS_TYPE_NONE = 3
462 };
463 
464 static int
465 params_out(initiator_session_t * sess, char *text, int *len, int textsize, int sess_type, int security)
466 {
467 	if (security == IS_SECURITY) {
468 		PARAM_TEXT_ADD(sess->params, "InitiatorName", "iqn.1994-04.org.NetBSD.iscsi-initiator:agc", text, len, textsize, 1, return -1);
469 		PARAM_TEXT_ADD(sess->params, "InitiatorAlias", "NetBSD", text, len, textsize, 1, return -1);
470 		if (sess->sess_params.auth_type != AuthNone) {
471 			PARAM_TEXT_ADD(sess->params, "AuthMethod", "CHAP,None", text, len, textsize, 1, return -1);
472 		} else {
473 			PARAM_TEXT_ADD(sess->params, "AuthMethod", "None", text, len, textsize, 1, return -1);
474 		}
475 	} else {
476 		PARAM_TEXT_ADD(sess->params, "HeaderDigest", "None", text, len, textsize, 1, return -1);
477 		PARAM_TEXT_ADD(sess->params, "DataDigest", "None", text, len, textsize, 1, return -1);
478 		PARAM_TEXT_ADD(sess->params, "MaxConnections", "1", text, len, textsize, 1, return -1);
479 		PARAM_TEXT_ADD(sess->params, "InitialR2T", "Yes", text, len, textsize, 1, return -1);
480 		PARAM_TEXT_ADD(sess->params, "ImmediateData", "Yes", text, len, textsize, 1, return -1);
481 		PARAM_TEXT_ADD(sess->params, "MaxRecvDataSegmentLength", "8192", text, len, textsize, 1, return -1);
482 		PARAM_TEXT_ADD(sess->params, "FirstBurstLength", "65536", text, len, textsize, 1, return -1);
483 		PARAM_TEXT_ADD(sess->params, "MaxBurstLength", "262144", text, len, textsize, 1, return -1);
484 		PARAM_TEXT_ADD(sess->params, "DefaultTime2Wait", "2", text, len, textsize, 1, return -1);
485 		PARAM_TEXT_ADD(sess->params, "DefaultTime2Retain", "20", text, len, textsize, 1, return -1);
486 		PARAM_TEXT_ADD(sess->params, "MaxOutstandingR2T", "1", text, len, textsize, 1, return -1);
487 		PARAM_TEXT_ADD(sess->params, "DataPDUInOrder", "No", text, len, textsize, 1, return -1);
488 		PARAM_TEXT_ADD(sess->params, "DataSequenceInOrder", "No", text, len, textsize, 1, return -1);
489 		PARAM_TEXT_ADD(sess->params, "ErrorRecoveryLevel", "0", text, len, textsize, 1, return -1);
490 	}
491 	switch (sess_type) {
492 	case SESS_TYPE_DISCOVERY:
493 		PARAM_TEXT_ADD(sess->params, "SessionType", "Discovery", text, len, textsize, 1, return -1);
494 		break;
495 	case SESS_TYPE_NORMAL:
496 		PARAM_TEXT_ADD(sess->params, "SessionType", "Normal", text, len, textsize, 1, return -1);
497 		PARAM_TEXT_ADD(sess->params, "TargetName", g_target[(int)sess->isid].TargetName, text, len, textsize, 1, return -1);
498 		break;
499 	default:
500 		break;
501 	}
502 	PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text, *len, NULL, NULL, 0, 1, return -1);
503 	return 0;
504 }
505 
506 static int
507 full_feature_negotiation_phase_i(initiator_session_t * sess, char *text,
508 				int text_len)
509 {
510 	initiator_cmd_t *cmd = NULL;
511 	iscsi_text_cmd_args_t *text_cmd = NULL;
512 	initiator_wait_t iwait;
513 
514 	/* Allocate command pointers */
515 
516 	if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
517 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
518 		return -1;
519 	}
520 	(void) memset(cmd, 0x0, sizeof(*cmd));
521 	text_cmd = iscsi_malloc_atomic(sizeof(iscsi_text_cmd_args_t));
522 	if (text_cmd == NULL) {
523 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
524 		if (cmd != NULL)
525 			iscsi_free_atomic(cmd);	/* initiator command */
526 		return -1;
527 	}
528 #define FFN_ERROR {if (cmd != NULL) iscsi_free_atomic(cmd); if (text_cmd != NULL) iscsi_free_atomic(text_cmd); return -1;}
529 	(void) memset(text_cmd, 0x0, sizeof(*text_cmd));
530 
531 	/*
532          * Note that <final>, <length> and <text> are updated
533          * by text_response_i when we receive offers from
534          * the target.
535          */
536 	text_cmd->text = text;
537 	text_cmd->length = text_len;
538 
539 	do {
540 
541 		/* Build text command */
542 
543 		text_cmd->final = 1;
544 		text_cmd->cont = 0;
545 		ISCSI_SET_TAG(&text_cmd->tag);
546 		text_cmd->transfer_tag = 0xffffffff;
547 
548 		/* Build wait for callback */
549 
550 		ISCSI_MUTEX_INIT(&iwait.mutex, FFN_ERROR);
551 		ISCSI_COND_INIT(&iwait.cond, FFN_ERROR);
552 
553 		/* Build initiator command */
554 
555 		cmd->type = ISCSI_TEXT_CMD;
556 		cmd->ptr = text_cmd;
557 		cmd->callback = wait_callback_i;
558 		cmd->callback_arg = &iwait;
559 		cmd->isid = sess->isid;
560 
561 		/* Enqueue initiator command to Tx worker */
562 
563 		iscsi_trace(TRACE_ISCSI_DEBUG,
564 			"enqueing text command to tx worker %llu\n",
565 			sess->isid);
566 		ISCSI_LOCK(&iwait.mutex, FFN_ERROR);
567 		ISCSI_LOCK(&sess->tx_worker.work_mutex, FFN_ERROR);
568 		if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
569 			ISCSI_UNLOCK(&iwait.mutex, );
570 			iscsi_err(__FILE__, __LINE__,
571 				"iscsi_queue_insert() failed\n");
572 			FFN_ERROR;
573 		}
574 		ISCSI_SIGNAL(&sess->tx_worker.work_cond, FFN_ERROR);
575 		ISCSI_UNLOCK(&sess->tx_worker.work_mutex, FFN_ERROR);
576 		iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued text command ok\n");
577 
578 		/* Wait for callback  */
579 
580 		iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on text callback\n");
581 		ISCSI_WAIT(&iwait.cond, &iwait.mutex, FFN_ERROR);
582 		ISCSI_UNLOCK(&iwait.mutex, FFN_ERROR);
583 		ISCSI_COND_DESTROY(&iwait.cond, FFN_ERROR);
584 		ISCSI_MUTEX_DESTROY(&iwait.mutex, FFN_ERROR);
585 		iscsi_trace(TRACE_ISCSI_DEBUG, "received text callback ok\n");
586 
587 		/*
588 		 * See if we're done.  text_response_i() overwrites
589 		 * text_cmd->final
590 		 */
591 		/* with the final bit in the text response from the target. */
592 
593 		if (!text_cmd->final) {
594 			iscsi_trace(TRACE_ISCSI_PARAM,
595 				"more negotiation needed (sending %d bytes "
596 				"response parameters)\n",
597 				text_cmd->length);
598 		}
599 	} while (!text_cmd->final);
600 
601 	/* Free command pointers */
602 
603 	iscsi_free_atomic(cmd->ptr);	/* text command */
604 	iscsi_free_atomic(cmd);	/* initiator command */
605 
606 	return 0;
607 }
608 
609 #define DISCOVERY_PHASE_TEXT_LEN	1024
610 #define DP_CLEANUP {if (text != NULL) iscsi_free_atomic(text);}
611 #define DP_ERROR {DP_CLEANUP; return -1;}
612 
613 int
614 initiator_set_target_name(int target, char *target_name)
615 {
616 	(void) strlcpy(g_target[target].iqnwanted, target_name,
617 			sizeof(g_target[target].iqnwanted));
618 	(void) strlcpy(g_target[target].TargetName, target_name,
619 			sizeof(g_target[target].TargetName));
620 	return 0;
621 }
622 
623 
624 int
625 iscsi_initiator_get_max_targets(void)
626 {
627 	return CONFIG_INITIATOR_NUM_TARGETS;
628 }
629 
630 #if 0
631 int
632 iscsi_initiator_get_targets(int target, strv_t *svp)
633 {
634         initiator_session_t	*sess = g_target[target].sess;
635         iscsi_parameter_value_t	*vp;
636         iscsi_parameter_t	*ip;
637         char			*text = NULL;
638         int			 text_len = 0;
639         int			 pos = 0;
640 
641         if ((text = iscsi_malloc_atomic(DISCOVERY_PHASE_TEXT_LEN)) == NULL) {
642                 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
643                 return -1;
644         }
645 
646         text_len = 0;
647         text[0] = 0x0;
648 
649         PARAM_TEXT_ADD(sess->params, "SendTargets", "all", text, &text_len,
650 			DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
651         PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text,
652 			text_len, NULL, NULL, DISCOVERY_PHASE_TEXT_LEN, 1,
653 			DP_ERROR);
654         if (full_feature_negotiation_phase_i(sess, text, text_len) != 0) {
655                 iscsi_err(__FILE__, __LINE__,
656 			"full_feature_negotiation_phase_i() failed\n");
657                 DP_ERROR;
658         }
659         for (ip = sess->params ; ip ; ip = ip->next) {
660                 if (strcmp(ip->key, "TargetName") == 0) {
661                 	pos = 0;
662                         for (vp = ip->value_l ; vp ; vp = vp->next, pos++) {
663                         	/*
664                         	 * Skip items which have no name,
665                         	 * these have been blocked by the target
666                         	 */
667                         	if (!strlen(vp->value))
668                         		continue;
669 
670                                 ALLOC(char *, svp->v, svp->size, svp->c, 10,
671 						10, "igt", return -1);
672                                 svp->v[svp->c++] = strdup(vp->value);
673                                 ALLOC(char *, svp->v, svp->size, svp->c, 10,
674 						10, "igt2", return -1);
675                                 svp->v[svp->c++] =
676                                      strdup(param_val_which(sess->params,
677                                      "TargetAddress", pos));
678                         }
679                 }
680         }
681 
682 	return 1;
683 }
684 #else
685 /* SendTargets=All must be sent in discovery session. */
686 int
687 iscsi_initiator_get_targets(int target, strv_t *svp)
688 {
689 	initiator_session_t	*sess = g_target[target].sess;
690 	strv_t *tp = &g_target[target].all_targets;
691 	uint32_t i;
692 
693 	if (sess == NULL)
694 		return -1;
695 
696 	for (i = 0; i < tp->c; i++) {
697 		ALLOC(char *, svp->v, svp->size, svp->c, 10,
698 			10, "igt", return -1);
699 		svp->v[svp->c++] = strdup(tp->v[i]);
700 	}
701 
702 	return 1;
703 }
704 #endif
705 
706 static int
707 discovery_phase(int target, strv_t *svp)
708 {
709 	initiator_session_t	*sess;
710 	iscsi_parameter_value_t	*vp;
711 	iscsi_parameter_t	*ip;
712 	unsigned		 i;
713 	char           		*ptr;
714 	char           		*colon_ptr;
715 	char           		*comma_ptr;
716 	char            	 port[64];
717 	char           		*text = NULL;
718 	int             	 text_len = 0;
719 
720 	if (target >= CONFIG_INITIATOR_NUM_TARGETS) {
721 		iscsi_err(__FILE__, __LINE__,
722 			"target (%d) out of range [0..%d]\n", target,
723 			CONFIG_INITIATOR_NUM_TARGETS);
724 		return -1;
725 	}
726 	sess = g_target[target].sess;
727 	if ((text = iscsi_malloc_atomic(DISCOVERY_PHASE_TEXT_LEN)) == NULL) {
728 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
729 		return -1;
730 	}
731 	/* Login to target */
732 
733 	iscsi_trace(TRACE_ISCSI_DEBUG,
734 		"entering Discovery login phase with target %d (sock %#x)\n",
735 		target, (int) sess->sock);
736 	text[0] = 0x0;
737 	if (params_out(sess, text, &text_len, DISCOVERY_PHASE_TEXT_LEN,
738 			SESS_TYPE_DISCOVERY, IS_SECURITY) != 0) {
739 		iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
740 		DP_ERROR;
741 	}
742 	if (login_phase_i(sess, text, text_len) != 0) {
743 		iscsi_err(__FILE__, __LINE__, "login_phase_i() failed\n");
744 		DP_ERROR;
745 	}
746 	iscsi_trace(TRACE_ISCSI_DEBUG,
747 		"now full feature for Discovery with target %d\n", target);
748 
749 	/* Full Feature Phase Negotiation (for SendTargets) */
750 	text_len = 0;
751 	text[0] = 0x0;
752 	PARAM_TEXT_ADD(sess->params, "SendTargets", "all", text, &text_len,
753 		DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
754 	PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text,
755 		text_len, NULL, NULL, DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
756 	if (full_feature_negotiation_phase_i(sess, text, text_len) != 0) {
757 		iscsi_err(__FILE__, __LINE__,
758 			"full_feature_negotiation_phase_i() failed\n");
759 		DP_ERROR;
760 	}
761 
762 	/* fill in information on the targets from the TargetName values */
763 	(void) memset(svp, 0x0, sizeof(*svp));
764 	for (ip = sess->params ; ip ; ip = ip->next) {
765 		if (strcmp(ip->key, "TargetName") == 0) {
766 			for (vp = ip->value_l ; vp ; vp = vp->next) {
767 				ALLOC(char *, svp->v, svp->size, svp->c, 10,
768 					10, "discovery_phase", return -1);
769 				svp->v[svp->c++] = strdup(vp->value);
770 				ALLOC(char *, svp->v, svp->size, svp->c, 10,
771 					10, "discovery_phase2", return -1);
772 				svp->v[svp->c++] = strdup(param_val(
773 						sess->params, "TargetAddress"));
774 			}
775 		}
776 	}
777 
778 	if (g_target[target].iqnwanted[0] == 0x0) {
779 		/*
780 		 * Use the first TargetName and TargetAddress sent to
781 		 * us (all others are currently ignored)
782 		 */
783 		if (param_val(sess->params, "TargetName") != NULL) {
784 			strlcpy(g_target[target].TargetName,
785 				param_val(sess->params, "TargetName"),
786 				sizeof(g_target[target].TargetName));
787 		} else {
788 			iscsi_err(__FILE__, __LINE__, "SendTargets failed\n");
789 			DP_ERROR;
790 		}
791 		if ((ptr = param_val(sess->params, "TargetAddress")) == NULL) {
792 			iscsi_err(__FILE__, __LINE__, "SendTargets failed\n");
793 			DP_ERROR;
794 		}
795 	} else {
796 		/* the user has asked for a specific target - find it */
797 		for (i = 0 ; i < svp->c ; i += 2) {
798 			if (strcmp(g_target[target].iqnwanted,
799 					svp->v[i]) == 0) {
800 				strlcpy(g_target[target].TargetName, svp->v[i],
801 					sizeof(g_target[target].TargetName));
802 				ptr = svp->v[i + 1];
803 				break;
804 			}
805 		}
806 		if (i >= svp->c) {
807 			iscsi_err(__FILE__, __LINE__,
808 				"SendTargets failed - target `%s' not found\n",
809 				g_target[target].iqnwanted);
810 			DP_ERROR;
811 		}
812 	}
813 
814 	if (*ptr == 0x0) {
815 		iscsi_err(__FILE__, __LINE__,
816 			"Target is not allowing access\n");
817 		DP_ERROR;
818 	}
819 	colon_ptr = strchr(ptr, ':');
820 	if ((comma_ptr = strchr(ptr, ',')) == NULL) {
821 		iscsi_err(__FILE__, __LINE__,
822 			"portal group tag is missing in \"%s\"\n",
823 			param_val(sess->params, "TargetAddress"));
824 		DP_ERROR;
825 	}
826 	if (colon_ptr) {
827 		strncpy(g_target[target].ip, ptr, (size_t)(colon_ptr - ptr));
828 		strncpy(port, colon_ptr + 1,
829 			(size_t)(comma_ptr - colon_ptr - 1));
830 		port[comma_ptr - colon_ptr - 1] = 0x0;
831 		g_target[target].port = iscsi_atoi(port);
832 	} else {
833 		strncpy(g_target[target].ip, ptr, (size_t)(comma_ptr - ptr));
834 		g_target[target].port = ISCSI_PORT;
835 	}
836 
837 	iscsi_trace(TRACE_ISCSI_DEBUG, "Discovered \"%s\" at \"%s:%u\"\n",
838 		g_target[target].TargetName, g_target[target].name,
839 		g_target[target].port);
840 
841 	/* Logout from target */
842 
843 	iscsi_trace(TRACE_ISCSI_DEBUG,
844 		"entering logout phase with target %d\n", target);
845 	if (logout_phase_i(sess) != 0) {
846 		iscsi_err(__FILE__, __LINE__, "logout_phase_i() failed\n");
847 		DP_ERROR;
848 	}
849 	iscsi_trace(TRACE_ISCSI_DEBUG, "target %d logout phase complete\n",
850 		target);
851 	DP_CLEANUP;
852 	return 0;
853 }
854 
855 #define FULL_FEATURE_PHASE_TEXT_LEN	1024
856 
857 static int
858 full_feature_phase(initiator_session_t * sess)
859 {
860 	char           *text;
861 	int             text_len;
862 
863 	if ((text = iscsi_malloc_atomic(FULL_FEATURE_PHASE_TEXT_LEN)) == NULL) {
864 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
865 		return -1;
866 	}
867 #define FFP_CLEANUP {if (text != NULL) iscsi_free_atomic(text);}
868 #define FFP_ERROR {FFP_CLEANUP; return -1;}
869 	/* Set text parameters */
870 
871 	text[0] = 0x0;
872 	text_len = 0;
873 	if (params_out(sess, text, &text_len, FULL_FEATURE_PHASE_TEXT_LEN,
874 			SESS_TYPE_NORMAL, IS_SECURITY) != 0) {
875 		iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
876 		FFP_ERROR;
877 	}
878 	/* Send login command */
879 
880 	iscsi_trace(TRACE_ISCSI_DEBUG, "entering login phase\n");
881 	if (login_phase_i(sess, text, text_len) != 0) {
882 		iscsi_err(__FILE__, __LINE__, "login_phase_i() failed\n");
883 		FFP_ERROR;
884 	}
885 	iscsi_trace(TRACE_ISCSI_DEBUG, "login phase successful\n");
886 
887 	FFP_CLEANUP;
888 	return 0;
889 }
890 
891 int
892 iscsi_initiator_start(iscsi_initiator_t *ini)
893 {
894 	initiator_session_t	*sess = NULL;
895 	char			*dbg;
896 	char			*cp;
897 	int			 port;
898 	int			 i;
899 
900 #define INIT_CLEANUP {if (sess != NULL) iscsi_free_atomic(sess);}
901 #define INIT_ERROR {INIT_CLEANUP; return -1;}
902 
903 	if ((dbg = iscsi_initiator_getvar(ini, "debug")) != NULL) {
904 		set_debug(dbg);
905 	}
906 	iscsi_trace(TRACE_ISCSI_DEBUG, "initializing initiator\n");
907 	iscsi_trace(TRACE_ISCSI_DEBUG,
908 		"target config filename to read from:%s\n", gfilename);
909 	port = atoi(iscsi_initiator_getvar(ini, "target port"));
910 	if (get_target_config(iscsi_initiator_getvar(ini,
911 				"target hostname"), port) != 0) {
912 		iscsi_err(__FILE__, __LINE__,
913 			"Error getting target configuration in config file\n");
914 		return -1;
915 	}
916 	g_initiator_state = 0;
917 	if (iscsi_queue_init(&g_session_q,
918 			CONFIG_INITIATOR_MAX_SESSIONS) != 0) {
919 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
920 		return -1;
921 	}
922 	for (i = 0; i < CONFIG_INITIATOR_MAX_SESSIONS; i++) {
923 		sess = iscsi_malloc_atomic(sizeof(initiator_session_t));
924 		if (sess == NULL) {
925 			iscsi_err(__FILE__, __LINE__,
926 				"iscsi_malloc_atomic() failed\n");
927 			return -1;
928 		}
929 		if (iscsi_queue_insert(&g_session_q, sess) != 0) {
930 			iscsi_err(__FILE__, __LINE__,
931 				"iscsi_queue_init() failed\n");
932 			INIT_CLEANUP;
933 			return -1;
934 		}
935 		cp = iscsi_initiator_getvar(ini, "auth type");
936 		if (strcmp(cp, "none") == 0) {
937 			sess->sess_params.auth_type = AuthNone;
938 			sess->sess_params.cred.user = NULL;
939 		} else {
940 			sess->sess_params.cred.user =
941 				strdup(iscsi_initiator_getvar(ini, "user"));
942 		}
943 		cp = iscsi_initiator_getvar(ini, "mutual auth");
944 		if (strcmp(cp, "none") == 0) {
945 			sess->sess_params.mutual_auth = 0;
946 		}
947 		cp = iscsi_initiator_getvar(ini, "digest type");
948 		if (strcmp(cp, "none") == 0) {
949 			sess->sess_params.digest_wanted = DigestNone;
950 		}
951 	}
952 	iscsi_trace(TRACE_ISCSI_DEBUG, "%d free sessions available\n",
953 			CONFIG_INITIATOR_MAX_SESSIONS);
954 
955 	g_tag = 0xabc123;
956 	if (hash_init(&g_tag_hash, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
957 		iscsi_err(__FILE__, __LINE__, "hash_init() failed\n");
958 		INIT_CLEANUP;
959 		return -1;
960 	}
961 	iscsi_spin_init(&g_tag_spin);
962 	iscsi_trace(TRACE_ISCSI_DEBUG,
963 		"tag hash table initialized with queue depth %d\n",
964 		CONFIG_INITIATOR_QUEUE_DEPTH);
965 
966 	/*
967 	 * Start enqueue worker.  This thread accepts scsi commands from
968 	 * initiator_enqueue()
969 	 */
970 	/* and queues them onto one of the tx worker queues. */
971 
972 	iscsi_trace(TRACE_ISCSI_DEBUG, "starting enqueue worker\n");
973 	if (iscsi_queue_init(&g_enqueue_q, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
974 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
975 		INIT_CLEANUP;
976 		return -1;
977 	}
978 	iscsi_trace(TRACE_ISCSI_DEBUG, "about to initialize mutex\n");
979 	ISCSI_MUTEX_INIT(&g_enqueue_worker.work_mutex, INIT_ERROR);
980 	ISCSI_COND_INIT(&g_enqueue_worker.work_cond, INIT_ERROR);
981 	ISCSI_MUTEX_INIT(&g_enqueue_worker.exit_mutex, INIT_ERROR);
982 	ISCSI_COND_INIT(&g_enqueue_worker.exit_cond, INIT_ERROR);
983 	ISCSI_LOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
984 
985 	iscsi_trace(TRACE_ISCSI_DEBUG, "spawning thread for enqueue worker\n");
986 	if (iscsi_thread_create(&g_enqueue_worker.thread,
987 		(void *) &enqueue_worker_proc, &g_enqueue_worker) != 0) {
988 		iscsi_err(__FILE__, __LINE__,
989 			"iscsi_threads_create() failed\n");
990 		INIT_CLEANUP;
991 		return -1;
992 	}
993 	iscsi_trace(TRACE_ISCSI_DEBUG, "thread spawned, waiting for signal\n");
994 	ISCSI_WAIT(&g_enqueue_worker.exit_cond, &g_enqueue_worker.exit_mutex,
995 			INIT_ERROR);
996 	ISCSI_UNLOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
997 	iscsi_trace(TRACE_ISCSI_DEBUG, "successfully started enqueue worker\n");
998 
999 	iscsi_trace(TRACE_ISCSI_DEBUG, "initiator initialization complete\n");
1000 	return 0;
1001 }
1002 
1003 int
1004 iscsi_initiator_shutdown(void)
1005 {
1006 	initiator_session_t	*sess;
1007 	int			 i;
1008 
1009 	iscsi_trace(TRACE_ISCSI_DEBUG, "shutting down initiator\n");
1010 	for (i = 0; i < CONFIG_INITIATOR_NUM_TARGETS; i++) {
1011 		if (g_target[i].has_session) {
1012 			iscsi_trace(TRACE_ISCSI_DEBUG,
1013 				"entering logout phase for target %d\n", i);
1014 			if (g_target[i].sess->rx_worker.state &
1015 					ISCSI_WORKER_STATE_ERROR) {
1016 				iscsi_warn(__FILE__, __LINE__,
1017 					"rx worker exited abnormal, "
1018 					"skipping logout phase\n");
1019 			} else {
1020 				if (logout_phase_i(g_target[i].sess) != 0) {
1021 					iscsi_err(__FILE__, __LINE__,
1022 					"logout_phase_i() failed "
1023 					"for target %d\n", i);
1024 				}
1025 				iscsi_trace(TRACE_ISCSI_DEBUG,
1026 					"logout phase complete for target "
1027 					"%d (state %#x)\n",
1028 					i, g_target[i].sess->state);
1029 			}
1030 			iscsi_trace(TRACE_ISCSI_DEBUG,
1031 				"destroying session for target %d\n", i);
1032 			if (session_destroy_i(g_target[i].sess) != 0) {
1033 				iscsi_err(__FILE__, __LINE__,
1034 					"session_destroy_i() failed for "
1035 					"target %d\n", i);
1036 			}
1037 			iscsi_trace(TRACE_ISCSI_DEBUG,
1038 				"session destroyed for target %d\n", i);
1039 		}
1040 	}
1041 
1042 	g_initiator_state = INITIATOR_STATE_SHUTDOWN;
1043 	if (g_enqueue_worker.state & ISCSI_WORKER_STATE_EXITING) {
1044 		iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue already exiting\n");
1045 	} else {
1046 		iscsi_trace(TRACE_ISCSI_DEBUG,
1047 			"signaling enqueue worker into exiting state\n");
1048 		ISCSI_LOCK(&g_enqueue_worker.work_mutex, return -1);
1049 		ISCSI_SIGNAL(&g_enqueue_worker.work_cond, return -1);
1050 		ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, return -1);
1051 	}
1052 	iscsi_trace(TRACE_ISCSI_DEBUG,
1053 		"Checking exit condition of enqueue worker\n");
1054 	while ((g_enqueue_worker.state & ISCSI_WORKER_STATE_EXITING) !=
1055 			ISCSI_WORKER_STATE_EXITING) {
1056 		ISCSI_SPIN;
1057 	}
1058 	iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue worker has exited\n");
1059 
1060 	iscsi_queue_destroy(&g_enqueue_q);
1061 	ISCSI_MUTEX_DESTROY(&g_enqueue_worker.work_mutex, return -1);
1062 	ISCSI_COND_DESTROY(&g_enqueue_worker.work_cond, return -1);
1063 	ISCSI_MUTEX_DESTROY(&g_enqueue_worker.exit_mutex, return -1);
1064 	ISCSI_COND_DESTROY(&g_enqueue_worker.exit_cond, return -1);
1065 
1066 	while ((sess = iscsi_queue_remove(&g_session_q)) != NULL) {
1067 		iscsi_free_atomic(sess);
1068 	}
1069 	iscsi_queue_destroy(&g_session_q);
1070 	iscsi_spin_destroy(&g_tag_spin);
1071 	hash_destroy(&g_tag_hash);
1072 	iscsi_trace(TRACE_ISCSI_DEBUG, "initiator shutdown complete\n");
1073 	return 0;
1074 }
1075 
1076 static int
1077 wait_callback_i(void *ptr)
1078 {
1079 	initiator_wait_t *iwait = (initiator_wait_t *) (((initiator_cmd_t *) ptr)->callback_arg);
1080 
1081 	iwait = (initiator_wait_t *) (((initiator_cmd_t *) ptr)->callback_arg);
1082 	ISCSI_LOCK(&iwait->mutex, return -1);
1083 	ISCSI_SIGNAL(&iwait->cond, return -1);
1084 	ISCSI_UNLOCK(&iwait->mutex, return -1);
1085 	return 0;
1086 }
1087 
1088 int
1089 initiator_abort(initiator_cmd_t * cmd)
1090 {
1091 	initiator_cmd_t *ptr, *prev;
1092 	initiator_session_t *sess;
1093 
1094 	iscsi_err(__FILE__, __LINE__, "aborting iSCSI cmd 0x%p (type %d, isid %llu)\n",
1095 		    cmd, cmd->type, cmd->isid);
1096 
1097 	hash_remove(&g_tag_hash, cmd->key);
1098 	if (g_target[(int)cmd->isid].has_session) {
1099 		sess = g_target[(int)cmd->isid].sess;
1100 		iscsi_spin_lock(&sess->cmds_spin);
1101 		prev = ptr = sess->cmds;
1102 		while (ptr != NULL) {
1103 			prev = ptr;
1104 			if (ptr == cmd)
1105 				break;
1106 			ptr = ptr->next;
1107 		}
1108 		if (ptr != NULL) {
1109 			if (prev == sess->cmds) {
1110 				sess->cmds = cmd->next;
1111 			} else {
1112 				prev->next = cmd->next;
1113 			}
1114 		}
1115 		iscsi_spin_unlock(&sess->cmds_spin);
1116 	} else {
1117 		iscsi_err(__FILE__, __LINE__, "cmd 0x%p has no session\n", cmd);
1118 	}
1119 	cmd->status = -1;
1120 	if (cmd->callback) {
1121 		if ((*cmd->callback)(cmd) != 0) {
1122 			iscsi_err(__FILE__, __LINE__, "cmd->callback() failed\n");
1123 			return -1;
1124 		}
1125 	}
1126 	iscsi_err(__FILE__, __LINE__, "successfully aborted iSCSI cmd 0x%p (type %d, isid %llu)\n",
1127 		    cmd, cmd->type, cmd->isid);
1128 	return 0;
1129 }
1130 
1131 int
1132 initiator_command(initiator_cmd_t * cmd)
1133 {
1134 	initiator_wait_t iwait;
1135 
1136 	ISCSI_MUTEX_INIT(&iwait.mutex, return -1);
1137 	ISCSI_COND_INIT(&iwait.cond, return -1);
1138 	ISCSI_LOCK(&iwait.mutex, return -1);
1139 	cmd->callback = wait_callback_i;
1140 	cmd->callback_arg = &iwait;
1141 	cmd->status = -1;
1142 	if (initiator_enqueue(cmd) != 0) {
1143 		iscsi_err(__FILE__, __LINE__, "initiator_enqueue() failed\n");
1144 		return -1;
1145 	} else {
1146 		iscsi_trace(TRACE_ISCSI_DEBUG, "command (type %d) enqueued, waiting on condition\n", cmd->type);
1147 		ISCSI_WAIT(&iwait.cond, &iwait.mutex, return -1);
1148 		iscsi_trace(TRACE_ISCSI_DEBUG, "condition signaled\n");
1149 	}
1150 	ISCSI_UNLOCK(&iwait.mutex, return -1);
1151 	ISCSI_COND_DESTROY(&iwait.cond, return -1);
1152 	ISCSI_MUTEX_DESTROY(&iwait.mutex, return -1);
1153 
1154 	return cmd->status;
1155 }
1156 
1157 /*
1158  * initiator_enqueue() may be called from within interrupt context within
1159  * the midlayer.  This function cannot block or be scheduled within.
1160  * All we do is enqueue the args ptr to g_enqueue_q.  The thread in
1161  * enqueue_worker_proc will enqueue the ptr onto one of the tx queues.
1162  */
1163 
1164 int
1165 initiator_enqueue(initiator_cmd_t * cmd)
1166 {
1167 	initiator_session_t *sess;
1168 	iscsi_scsi_cmd_args_t *scsi_cmd;
1169 	iscsi_nop_out_args_t *nop_out;
1170 	uint64_t target;
1171 	uint32_t        tag;
1172 
1173 	if ((target = cmd->isid) >= CONFIG_INITIATOR_NUM_TARGETS) {
1174 		iscsi_err(__FILE__, __LINE__, "target (%d) out of range [0..%d]\n", target, CONFIG_INITIATOR_NUM_TARGETS);
1175 		return -1;
1176 	}
1177 	sess = g_target[(int)target].sess;
1178 	if (g_target[(int)target].has_session &&
1179 	    sess->state == INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL) {
1180 
1181 		/* Give command directly to tx worker */
1182 
1183 		ISCSI_SET_TAG_IN_INTR(&tag);
1184 		target = cmd->isid;
1185 
1186 		switch (cmd->type) {
1187 		case ISCSI_SCSI_CMD:
1188 			scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
1189 			scsi_cmd->tag = tag;
1190 			break;
1191 		case ISCSI_NOP_OUT:
1192 			nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
1193 			if (nop_out->tag != 0xffffffff) {
1194 				nop_out->tag = tag;
1195 			}
1196 			break;
1197 		default:
1198 			iscsi_err(__FILE__, __LINE__, "enqueue_worker: unknown command type %d\n", cmd->type);
1199 			return -1;
1200 		}
1201 		if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1202 			iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1203 			return -1;
1204 		}
1205 		ISCSI_LOCK(&sess->tx_worker.work_mutex, return -1);
1206 		ISCSI_SIGNAL(&sess->tx_worker.work_cond, return -1);
1207 		ISCSI_UNLOCK(&sess->tx_worker.work_mutex, return -1);
1208 		iscsi_trace(TRACE_ISCSI_DEBUG, "initiator_cmd_t 0x%p given to tx_worker[%llu]\n", cmd, cmd->isid);
1209 	} else {
1210 
1211 		/*
1212 		 * Give command to enqueue worker to get us into full feature
1213 		 * and then issue the command
1214 		 */
1215 		/* to one of the tx workers. */
1216 
1217 		if (iscsi_queue_insert(&g_enqueue_q, cmd) == -1) {
1218 			iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1219 			return -1;
1220 		}
1221 		ISCSI_LOCK(&g_enqueue_worker.work_mutex, return -1);
1222 		ISCSI_SIGNAL(&g_enqueue_worker.work_cond, return -1);
1223 		ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, return -1);
1224 		iscsi_trace(TRACE_ISCSI_DEBUG, "initiator_cmd_t 0x%p given to enqueue worker\n", cmd);
1225 	}
1226 	return 0;
1227 }
1228 
1229 static int
1230 enqueue_worker_proc(void *arg)
1231 {
1232 	initiator_session_t *sess;
1233 	initiator_cmd_t *cmd;
1234 	iscsi_scsi_cmd_args_t *scsi_cmd;
1235 	iscsi_nop_out_args_t *nop_out;
1236 	iscsi_worker_t *me = (iscsi_worker_t *) arg;
1237 	uint64_t	target;
1238 	uint32_t        tag;
1239 	int             rc;
1240 
1241 
1242 	ISCSI_THREAD_START("enqueue_worker");
1243 	ISCSI_SET_THREAD(me)
1244 		ISCSI_LOCK(&me->exit_mutex, goto done);
1245 
1246 	me->pid = getpid();
1247 	me->state = ISCSI_WORKER_STATE_STARTED;
1248 	ISCSI_SIGNAL(&me->exit_cond, goto done);
1249 	ISCSI_UNLOCK(&me->exit_mutex, goto done);
1250 	iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: started\n");
1251 	ISCSI_LOCK(&g_enqueue_worker.work_mutex, goto done);
1252 	for (;;) {
1253 		if (iscsi_queue_depth(&g_enqueue_q) || (g_initiator_state == INITIATOR_STATE_SHUTDOWN)) {
1254 			iscsi_trace(TRACE_ISCSI_DEBUG, "enqueu, start to work\n");
1255 			ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, goto done);
1256 			if (g_initiator_state == INITIATOR_STATE_SHUTDOWN) {
1257 				iscsi_trace(TRACE_ISCSI_DEBUG, "got shutdown signal\n");
1258 				goto done;
1259 			}
1260 			if ((cmd = iscsi_queue_remove(&g_enqueue_q)) == NULL) {
1261 				iscsi_err(__FILE__, __LINE__, "enqueue_worker: iscsi_queue_remove() failed\n");
1262 				goto done;
1263 			}
1264 			ISCSI_SET_TAG(&tag);
1265 			target = cmd->isid;
1266 			iscsi_trace(TRACE_ISCSI_CMD, "enqueue_worker: dequeued initiator_cmd_t 0x%p (type %d, target %llu)\n", cmd, cmd->type, target);
1267 			switch (cmd->type) {
1268 			case ISCSI_SCSI_CMD:
1269 				scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
1270 				scsi_cmd->tag = tag;
1271 				break;
1272 			case ISCSI_NOP_OUT:
1273 				nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
1274 				if (nop_out->tag != 0xffffffff) {
1275 					nop_out->tag = tag;
1276 				}
1277 				break;
1278 			default:
1279 				iscsi_err(__FILE__, __LINE__, "enqueue_worker: unknown command type %d\n", cmd->type);
1280 				goto done;
1281 			}
1282 
1283 			/* Initialize session (if not already) */
1284 initialize:
1285 			if (!g_target[(int)target].has_session) {
1286 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: initializing target %llu session\n", target);
1287 				if (session_init_i(&g_target[(int)target].sess, target) != 0) {
1288 					iscsi_err(__FILE__, __LINE__, "session_init_i() failed (ignoring command)\n");
1289 					goto next;
1290 				}
1291 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: target %llu session initialized\n", target);
1292 			} else {
1293 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: target %llu session already initialized\n", target);
1294 			}
1295 			sess = g_target[(int)target].sess;
1296 			iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: session 0x%p\n", sess);
1297 
1298 			/* Discovery login if TargetName is zero length */
1299 
1300 			if (strlen(g_target[(int)target].TargetName) == 0) {
1301 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: entering Discovery phase with target %llu\n", target);
1302 				rc = discovery_phase((int)target, &g_target[(int)target].all_targets);
1303 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: Discovery phase complete\n");
1304 
1305 				/* Destroy session */
1306 
1307 				if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1308 					if (g_target[(int)target].has_session) {
1309 						if (session_destroy_i(g_target[(int)target].sess) != 0) {
1310 							iscsi_err(__FILE__, __LINE__, "enqueue_worker: session_destroy_i() failed\n");
1311 							goto done;
1312 						}
1313 					}
1314 				}
1315 
1316 				/*
1317 				 * If the Discovery phase was
1318 				 * successful, we re-initialize the
1319 				 * session, enter full feature phase
1320 				 * and then execute the command.
1321 				 */
1322 
1323 				if (rc == 0) {
1324 					iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: discovery_phase() succeeded, entering full feature\n");
1325 					goto initialize;
1326 				} else {
1327 					iscsi_err(__FILE__, __LINE__, "enqueue_worker: discovery_phase() failed (ignoring command)\n");
1328 					goto next;
1329 				}
1330 			}
1331 			/* Get into full feature if we're not already */
1332 
1333 			if (sess->state != INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL) {
1334 				iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: entering full feature with target %llu (sock %#x)\n", target, (int) sess->sock);
1335 				if (full_feature_phase(sess) != 0) {
1336 					iscsi_err(__FILE__, __LINE__, "enqueue_worker: full_feature_phase() failed (ignoring command)\n");
1337 					goto next;
1338 				} else {
1339 					iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: now full feature with target %llu\n", target);
1340 				}
1341 			}
1342 			/*
1343 			 * Now we are in FPP, so set the mostly
1344 			 * accessed parameters for easy retrieval
1345 			 * during data transfer
1346 			 */
1347 			set_session_parameters(sess->params, &sess->sess_params);
1348 
1349 			/* Add command to tx work queue and signal worker */
1350 
1351 			ISCSI_LOCK(&sess->tx_worker.work_mutex, goto done);
1352 			if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1353 				ISCSI_UNLOCK(&sess->tx_worker.work_mutex, goto done);
1354 				iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1355 				goto done;
1356 			}
1357 			ISCSI_SIGNAL(&sess->tx_worker.work_cond, goto done);
1358 			ISCSI_UNLOCK(&sess->tx_worker.work_mutex, goto done);
1359 			iscsi_trace(TRACE_ISCSI_CMD, "enqueue_worker: gave initiator_cmd_t 0x%p to tx_worker[%llu]\n", cmd, cmd->isid);
1360 next:
1361 			ISCSI_LOCK(&g_enqueue_worker.work_mutex, goto done);
1362 		} else {
1363 			iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: queue empty, awaiting condition\n");
1364 			ISCSI_WAIT(&g_enqueue_worker.work_cond, &g_enqueue_worker.work_mutex, goto done);
1365 			iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: condition signaled\n");
1366 		}
1367 	}
1368 done:
1369 	ISCSI_WORKER_EXIT(me);
1370 	return 0;
1371 }
1372 
1373 
1374 /***********
1375  * Private *
1376  ***********/
1377 
1378 
1379 /*
1380  * Tx Worker (one per connection)
1381  */
1382 
1383 static int
1384 tx_worker_proc_i(void *arg)
1385 {
1386 	iscsi_worker_t *me = (iscsi_worker_t *) arg;
1387 	initiator_cmd_t *cmd, *ptr;
1388 	initiator_session_t *sess = g_target[me->id].sess;
1389 
1390 	ISCSI_THREAD_START("tx_worker");
1391 
1392 	ISCSI_SET_THREAD(me)
1393 		me->pid = getpid();
1394 	me->state = ISCSI_WORKER_STATE_STARTED;
1395 
1396 	/* Connect to target */
1397 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: connecting to %s:%d\n",
1398 	      me->id, g_target[me->id].name, g_target[me->id].port);
1399 	sess->state = INITIATOR_SESSION_STATE_CONNECTING;
1400 	if (iscsi_sock_connect(sess->sock, g_target[me->id].name,
1401 			g_target[me->id].port) != 0) {
1402 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_connect() failed\n");
1403 
1404 		ISCSI_LOCK(&me->exit_mutex, return -1);
1405 		me->state |= ISCSI_WORKER_STATE_ERROR;
1406 		ISCSI_SIGNAL(&me->exit_cond, return -1);
1407 		ISCSI_UNLOCK(&me->exit_mutex, return -1);
1408 		goto done;
1409 
1410 	}
1411 	sess->state = INITIATOR_SESSION_STATE_CONNECTED;
1412 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: connected to %s:%d\n",
1413 	      me->id, g_target[me->id].name, g_target[me->id].port);
1414 
1415 	/* Start Rx worker */
1416 
1417 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: starting Rx worker\n",
1418 		me->id);
1419 	ISCSI_LOCK(&sess->rx_worker.exit_mutex, return -1);
1420 	if (iscsi_thread_create(&sess->rx_worker.thread,
1421 		(void *) rx_worker_proc_i, sess) != 0) {
1422 		iscsi_err(__FILE__, __LINE__, "iscsi_thread_create() failed\n");
1423 		goto done;
1424 	}
1425 	ISCSI_WAIT(&sess->rx_worker.exit_cond, &sess->rx_worker.exit_mutex,
1426 		return -1);
1427 	ISCSI_UNLOCK(&sess->rx_worker.exit_mutex, return -1);
1428 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: Rx worker started\n",
1429 		me->id);
1430 
1431 	/* Signal that we've started */
1432 	ISCSI_LOCK(&me->exit_mutex, return -1);
1433 	ISCSI_SIGNAL(&me->exit_cond, return -1);
1434 	ISCSI_UNLOCK(&me->exit_mutex, return -1);
1435 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: successfully started\n",
1436 		me->id);
1437 
1438 	/* This Tx loop will exit when both the g_tx_queue is empty and  */
1439 	/* sess->state != INITIATOR_SESSION_STATE_DESTROYING */
1440 
1441 	ISCSI_LOCK(&me->work_mutex, return -1);
1442 	for (;;) {
1443 
1444 		if (iscsi_queue_depth(&g_target[me->id].sess->tx_queue) ||
1445 		    sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1446 
1447 			if (sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1448 				iscsi_trace(TRACE_ISCSI_DEBUG,
1449 					"tx_worker[%d]: session is being "
1450 					"destroyed, exiting\n", me->id);
1451 				ISCSI_UNLOCK(&me->work_mutex, return -1);
1452 				goto done;
1453 			}
1454 			/* Get initiator command */
1455 
1456 			cmd = iscsi_queue_remove(
1457 					&g_target[me->id].sess->tx_queue);
1458 			if (cmd == NULL) {
1459 				iscsi_err(__FILE__, __LINE__,
1460 					"tx_worker[%d]: iscsi_queue_remove "
1461 					"failed\n", me->id);
1462 				ISCSI_UNLOCK(&me->work_mutex, return -1);
1463 				goto done;
1464 			}
1465 			ISCSI_UNLOCK(&me->work_mutex, return -1);
1466 			iscsi_trace(TRACE_ISCSI_CMD,
1467 				"tx_worker[%d]: dequeued initiator_cmd_t 0x%p "
1468 				"(type %d, target %llu)\n",
1469 				me->id, cmd, cmd->type, cmd->isid);
1470 
1471 			/* Make sure we've got the right command */
1472 			if (cmd->isid != (unsigned)me->id) {
1473 				iscsi_err(__FILE__, __LINE__,
1474 					"got command %#x for target %llu, "
1475 					"expected %d\n", cmd->type,
1476 					cmd->isid, me->id);
1477 				goto done;
1478 			}
1479 			/*
1480 			 * Add to list of oustanding commands in session
1481 			 * (unless NOP_OUT without ping)
1482 			 */
1483 			if (!((cmd->type == ISCSI_NOP_OUT) &&
1484 			    (((iscsi_nop_out_args_t *)(cmd->ptr))->tag ==
1485 			    				0xffffffff))) {
1486 				cmd->next = NULL;
1487 				iscsi_spin_lock(&sess->cmds_spin);
1488 				for (ptr = sess->cmds;
1489 				     ptr && ptr->next != NULL;
1490 				     ptr = ptr->next) {
1491 				}
1492 				if (ptr) {
1493 					ptr->next = cmd;
1494 				} else {
1495 					sess->cmds = cmd;
1496 				}
1497 				iscsi_spin_unlock(&sess->cmds_spin);
1498 			}
1499 			cmd->tx_done = 0;
1500 			switch (cmd->type) {
1501 			case ISCSI_LOGIN_CMD:
1502 				iscsi_trace(TRACE_ISCSI_CMD,
1503 					"tx_worker[%d]: ISCSI_LOGIN_CMD\n",
1504 					me->id);
1505 				if (login_command_i(cmd) != 0) {
1506 					iscsi_err(__FILE__, __LINE__,
1507 						"tx_worker[%d]: "
1508 						"login_command_i() failed\n",
1509 						me->id);
1510 					goto done;
1511 				}
1512 				break;
1513 			case ISCSI_TEXT_CMD:
1514 				iscsi_trace(TRACE_ISCSI_CMD,
1515 					"tx_worker[%d]: ISCSI_TEXT_CMD\n",
1516 					me->id);
1517 				if (text_command_i(cmd) != 0) {
1518 					iscsi_err(__FILE__, __LINE__,
1519 						"tx_worker[%d]: text_command_i "
1520 						"failed\n", me->id);
1521 					goto done;
1522 				}
1523 				break;
1524 			case ISCSI_SCSI_CMD:
1525 				iscsi_trace(TRACE_ISCSI_CMD,
1526 					"tx_worker[%d]: ISCSI_SCSI_CMD\n",
1527 					me->id);
1528 				if (scsi_command_i(cmd) != 0) {
1529 					iscsi_err(__FILE__, __LINE__,
1530 						"tx_worker[%d]: scsi_command_i"
1531 						" failed\n", me->id);
1532 					goto done;
1533 				}
1534 				break;
1535 			case ISCSI_NOP_OUT:
1536 				iscsi_trace(TRACE_ISCSI_CMD,
1537 					"tx_worker[%d]: ISCSI_NOP_OUT\n",
1538 					me->id);
1539 				if (nop_out_i(cmd) != 0) {
1540 					iscsi_err(__FILE__, __LINE__,
1541 						"tx_worker[%d]: nop_out_i "
1542 						"failed\n", me->id);
1543 					goto done;
1544 				}
1545 				break;
1546 			case ISCSI_LOGOUT_CMD:
1547 				iscsi_trace(TRACE_ISCSI_CMD,
1548 					"tx_worker[%d]: ISCSI_LOGOUT_CMD\n",
1549 					me->id);
1550 				if (logout_command_i(cmd) != 0) {
1551 					iscsi_err(__FILE__, __LINE__,
1552 						"tx_worker[%d]: "
1553 						"logout_command_i() failed\n",
1554 						me->id);
1555 					goto done;
1556 				}
1557 				break;
1558 			default:
1559 				iscsi_err(__FILE__, __LINE__,
1560 					"tx_worker[%d]: unknown iSCSI command"
1561 					" %#x\n", me->id, cmd->type);
1562 				cmd->status = -1;
1563 				break;
1564 			}
1565 
1566 			/* Get lock for next iteration */
1567 
1568 			ISCSI_LOCK(&me->work_mutex, return -1);
1569 
1570 			/*
1571 			 * The Rx thread will receive a response for
1572 			 * the command and execute the callback.  We
1573 			 * need to make sure the callback function is
1574 			 * not executed before the Tx thread has
1575 			 * completed sending the command.  This is
1576 			 * what tx_done is used for.  The last step is
1577 			 * to set tx_done and signal the Rx thread,
1578 			 * which may be block on the condition.
1579 			 * NOP_OUT (without ping) will have no
1580 			 * response for the Rx thread to process - so
1581 			 * we execute the callback directly.  */
1582 
1583 			if ((cmd->type == ISCSI_NOP_OUT) &&
1584 			    (((iscsi_nop_out_args_t *)(cmd->ptr))->tag ==
1585 			    				0xffffffff)) {
1586 				iscsi_trace(TRACE_ISCSI_DEBUG,
1587 					"executing callback() function "
1588 					"directly for NOP_OUT (no NOP_IN)\n");
1589 				if (cmd->callback(cmd) != 0) {
1590 					iscsi_err(__FILE__, __LINE__,
1591 						"cmd->callback() failed\n");
1592 					return -1;
1593 				}
1594 			} else {
1595 				ISCSI_LOCK(&sess->rx_worker.work_mutex,
1596 					return -1);
1597 				cmd->tx_done = 1;
1598 				ISCSI_SIGNAL(&sess->rx_worker.work_cond,
1599 					return -1);
1600 				ISCSI_UNLOCK(&sess->rx_worker.work_mutex,
1601 					return -1);
1602 			}
1603 		} else {
1604 			iscsi_trace(TRACE_ISCSI_DEBUG,
1605 				"tx_worker[%d]: awaiting condition\n", me->id);
1606 			ISCSI_WAIT(&me->work_cond, &me->work_mutex, return -1);
1607 			iscsi_trace(TRACE_ISCSI_DEBUG,
1608 				"tx_worker[%d]: condition signaled\n", me->id);
1609 		}
1610 	}
1611 done:
1612 	if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1613 		iscsi_err(__FILE__, __LINE__,
1614 			"tx_worker[%d]: session exited prematurely "
1615 			"(state %#x)\n", me->id, sess->state);
1616 		me->state |= ISCSI_WORKER_STATE_ERROR;
1617 	}
1618 	ISCSI_WORKER_EXIT(me);
1619 	return 0;
1620 }
1621 
1622 /*
1623  * There is one Rx worker per connection.
1624  */
1625 
1626 static int
1627 rx_worker_proc_i(void *arg)
1628 {
1629 	uint8_t   header[ISCSI_HEADER_LEN];
1630 	initiator_session_t *sess = (initiator_session_t *) arg;
1631 	iscsi_worker_t *me = &sess->rx_worker;
1632 	initiator_cmd_t *cmd = NULL;
1633 	initiator_cmd_t *prev, *ptr;
1634 	uint32_t        tag;
1635 
1636 	ISCSI_THREAD_START("rx_worker");
1637 	ISCSI_SET_THREAD(me)
1638 		me->state = ISCSI_WORKER_STATE_STARTED;
1639 	me->pid = getpid();
1640 	ISCSI_LOCK(&me->exit_mutex, return -1);
1641 	ISCSI_SIGNAL(&me->exit_cond, return -1);
1642 	ISCSI_UNLOCK(&me->exit_mutex, return -1);
1643 
1644 	iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: started (sess %p)\n", me->id, sess);
1645 
1646 	for (;;) {
1647 		iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: reading iscsi header (sock %#x) \n",
1648 		      me->id, (int) sess->sock);
1649 		if (iscsi_sock_msg(sess->sock, 0, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
1650 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: iscsi_sock_msg() failed\n", me->id);
1651 			goto done;
1652 		}
1653 		if (sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1654 			iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: session is being destroyed\n", me->id);
1655 			goto done;
1656 		}
1657 		/* Get cmd ptr from hash table */
1658 
1659 		if ((ISCSI_OPCODE(header) != ISCSI_REJECT) && (ISCSI_OPCODE(header) != ISCSI_ASYNC)) {
1660 			(void) memcpy(&tag, header + 16, sizeof(tag));
1661 			tag = ISCSI_NTOHL(tag);
1662 			if (tag != 0xffffffff) {
1663 
1664 				/*
1665 				 * remove command from g_tag_hash, cmd is
1666 				 * local so we only need to lock the queue
1667 				 * remove operation
1668 				 */
1669 
1670 				if ((cmd = hash_remove(&g_tag_hash, tag)) == NULL) {
1671 					iscsi_err(__FILE__, __LINE__, "hash_remove() failed\n");
1672 					iscsi_trace(TRACE_ISCSI_DEBUG, "no cmd ptr associated with tag %#x\n", tag);
1673 				} else {
1674 					iscsi_trace(TRACE_ISCSI_DEBUG, "cmd ptr %p associated with tag %#x\n", cmd, tag);
1675 
1676 					ISCSI_LOCK(&sess->rx_worker.work_mutex, return -1);
1677 					if (!cmd->tx_done) {
1678 						ISCSI_WAIT(&sess->rx_worker.work_cond, &sess->rx_worker.work_mutex, return -1);
1679 					}
1680 					ISCSI_UNLOCK(&sess->rx_worker.work_mutex, return -1);
1681 				}
1682 			} else {
1683 				iscsi_trace(TRACE_ISCSI_DEBUG, "no command associated with tag %#x\n", tag);
1684 			}
1685 		}
1686 		/* Remove cmd ptr from outstanding list */
1687 		iscsi_spin_lock(&sess->cmds_spin);
1688 		prev = ptr = sess->cmds;
1689 		while (ptr != NULL) {
1690 			prev = ptr;
1691 			if (ptr == cmd)
1692 				break;
1693 			ptr = ptr->next;
1694 		}
1695 		if (ptr != NULL) {
1696 			if (prev == sess->cmds) {
1697 				sess->cmds = cmd->next;
1698 			} else {
1699 				prev->next = cmd->next;
1700 			}
1701 		}
1702 		iscsi_spin_unlock(&sess->cmds_spin);
1703 		switch (ISCSI_OPCODE(header)) {
1704 		case ISCSI_SCSI_RSP:
1705 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_SCSI_RSP\n", me->id);
1706 			if (scsi_response_i(sess, cmd, header) != 0) {
1707 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_response_i() failed\n", me->id);
1708 				goto done;
1709 			}
1710 			break;
1711 		case ISCSI_READ_DATA:
1712 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_READ_DATA\n", me->id);
1713 			if (scsi_read_data_i(sess, cmd, header) != 0) {
1714 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_read_data_i() failed\n", me->id);
1715 				goto done;
1716 			}
1717 			break;
1718 		case ISCSI_R2T:
1719 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_R2T\n", me->id);
1720 			if (scsi_r2t_i(sess, cmd, header) != 0) {
1721 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_r2t_i() failed\n", me->id);
1722 				goto done;
1723 			}
1724 			break;
1725 		case ISCSI_NOP_IN:
1726 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_NOP_IN\n", me->id);
1727 			if (nop_in_i(sess, cmd, header) != 0) {
1728 				iscsi_err(__FILE__, __LINE__, "nop_in_i() failed\n");
1729 				return -1;
1730 			}
1731 			break;
1732 		case ISCSI_LOGIN_RSP:
1733 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_LOGIN_RSP\n", me->id);
1734 			if (login_response_i(sess, cmd, header) != 0) {
1735 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: login_response_i() failed\n", me->id);
1736 				goto done;
1737 			}
1738 			break;
1739 		case ISCSI_TEXT_RSP:
1740 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_TEXT_RSP\n", me->id);
1741 			if (text_response_i(sess, cmd, header) != 0) {
1742 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: text_response_i() failed\n", me->id);
1743 				goto done;
1744 			}
1745 			break;
1746 		case ISCSI_LOGOUT_RSP:
1747 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_LOGOUT_RSP\n", me->id);
1748 			if (logout_response_i(sess, cmd, header) != 0) {
1749 				iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: logout_response_i() failed\n", me->id);
1750 				goto done;
1751 			}
1752 			break;
1753 		case ISCSI_REJECT:
1754 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_REJECT\n", me->id);
1755 			if (reject_i(sess, header) != 0) {
1756 				iscsi_err(__FILE__, __LINE__, "reject_i() failed\n");
1757 				return -1;
1758 			}
1759 			break;
1760 		case ISCSI_ASYNC:
1761 			iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_ASYNC\n", me->id);
1762 			if (async_msg_i(sess, header) != 0) {
1763 				iscsi_err(__FILE__, __LINE__, "async_msg_i() failed\n");
1764 				goto done;
1765 			}
1766 			break;
1767 		default:
1768 			iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: unexpected iSCSI op %#x\n", me->id, ISCSI_OPCODE(header));
1769 			goto done;
1770 		}
1771 	}
1772 done:
1773 	if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1774 
1775 		iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: session exited prematurely (state %#x)\n", me->id, sess->state);
1776 		me->state |= ISCSI_WORKER_STATE_ERROR;
1777 	}
1778 	ISCSI_WORKER_EXIT(me);
1779 	return 0;
1780 }
1781 
1782 static int
1783 text_command_i(initiator_cmd_t * cmd)
1784 {
1785 	iscsi_text_cmd_args_t	*text_cmd;
1786 	initiator_session_t	*sess;
1787 	uint8_t			 header[ISCSI_HEADER_LEN];
1788 
1789 	text_cmd = (iscsi_text_cmd_args_t *) cmd->ptr;
1790 	sess = g_target[(int)cmd->isid].sess;
1791 	/*
1792 	 * Insert cmd into the hash table, keyed by the tag. The Rx thread
1793 	 * will
1794 	 */
1795 	/* retreive the cmd ptr using the tag from the response PDU. */
1796 
1797 	if (hash_insert(&g_tag_hash, cmd, text_cmd->tag) != 0) {
1798 		iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
1799 		return -1;
1800 	}
1801 	/* Send text command PDU */
1802 
1803 	text_cmd->ExpStatSN = sess->ExpStatSN;
1804 	text_cmd->CmdSN = sess->CmdSN++;
1805 	iscsi_trace(TRACE_ISCSI_DEBUG, "sending text command\n");
1806 	if (iscsi_text_cmd_encap(header, text_cmd) != 0) {
1807 		iscsi_err(__FILE__, __LINE__, "(iscsi_text_cmd_encap() failed\n");
1808 		return -1;
1809 	}
1810 	if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, text_cmd->text, text_cmd->length, 0)
1811 	    != ISCSI_HEADER_LEN + text_cmd->length) {
1812 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed.\n");
1813 		return -1;
1814 	}
1815 	iscsi_trace(TRACE_ISCSI_DEBUG, "text command sent ok\n");
1816 
1817 	return 0;
1818 }
1819 
1820 static int
1821 login_command_i(initiator_cmd_t * cmd)
1822 {
1823 	iscsi_login_cmd_args_t	*login_cmd;
1824 	initiator_session_t	*sess;
1825 	uint8_t			 header[ISCSI_HEADER_LEN];
1826 
1827 	login_cmd = (iscsi_login_cmd_args_t *) cmd->ptr;
1828 	sess = g_target[(int)cmd->isid].sess;
1829 	/*
1830 	 * Insert cmd into the hash table, keyed by the tag. The Rx thread
1831 	 * will
1832 	 */
1833 	/* retreive the cmd ptr using the tag from the response PDU. */
1834 
1835 	if (hash_insert(&g_tag_hash, cmd, login_cmd->tag) != 0) {
1836 		iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
1837 		return -1;
1838 	}
1839 	/* Send login command PDU */
1840 	login_cmd->ExpStatSN = sess->ExpStatSN;
1841 	iscsi_trace(TRACE_ISCSI_DEBUG, "sending login command\n");
1842 	if (iscsi_login_cmd_encap(header, login_cmd) != 0) {
1843 		iscsi_err(__FILE__, __LINE__, "(iscsi_login_cmd_encap() failed\n");
1844 		return -1;
1845 	}
1846 	if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, login_cmd->text, login_cmd->length, 0)
1847 	    != ISCSI_HEADER_LEN + login_cmd->length) {
1848 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed.\n");
1849 		return -1;
1850 	}
1851 	iscsi_trace(TRACE_ISCSI_DEBUG, "login command sent ok\n");
1852 
1853 	return 0;
1854 }
1855 
1856 static int
1857 logout_phase_i(initiator_session_t * sess)
1858 {
1859 	initiator_cmd_t *cmd = NULL;
1860 	iscsi_logout_cmd_args_t *logout_cmd = NULL;
1861 	initiator_wait_t iwait;
1862 
1863 	sess->state = INITIATOR_SESSION_STATE_LOGGING_OUT;
1864 
1865 	/* Allocate command pointers */
1866 
1867 	if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
1868 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1869 		return -1;
1870 	}
1871 	(void) memset(cmd, 0x0, sizeof(*cmd));
1872 	if ((logout_cmd = iscsi_malloc_atomic(sizeof(iscsi_logout_cmd_args_t))) == NULL) {
1873 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1874 		if (cmd != NULL)
1875 			iscsi_free_atomic(cmd);
1876 		return -1;
1877 	}
1878 #define LO_CLEANUP {if (cmd != NULL) iscsi_free_atomic(cmd); if (logout_cmd != NULL) iscsi_free_atomic(logout_cmd); }
1879 #define LO_ERROR {LO_CLEANUP; return -1;}
1880 	(void) memset(logout_cmd, 0x0, sizeof(*logout_cmd));
1881 
1882 	/* Build logout command */
1883 
1884 	logout_cmd->cid = sess->cid;
1885 	logout_cmd->reason = ISCSI_LOGOUT_CLOSE_SESSION;
1886 	ISCSI_SET_TAG(&logout_cmd->tag);
1887 	logout_cmd->ExpStatSN = sess->ExpStatSN;
1888 	logout_cmd->CmdSN = sess->CmdSN++;
1889 
1890 	/* Build wait for callback */
1891 
1892 	ISCSI_MUTEX_INIT(&iwait.mutex, LO_ERROR);
1893 	ISCSI_COND_INIT(&iwait.cond, LO_ERROR);
1894 
1895 	/* Build initiator command */
1896 
1897 	cmd->type = ISCSI_LOGOUT_CMD;
1898 	cmd->ptr = logout_cmd;
1899 	cmd->callback = wait_callback_i;
1900 	cmd->callback_arg = &iwait;
1901 	cmd->isid = sess->isid;
1902 
1903 	/* Enqueue to Tx worker */
1904 
1905 	iscsi_trace(TRACE_ISCSI_DEBUG, "enqueing logout command to tx worker %llu\n", sess->isid);
1906 	ISCSI_LOCK(&iwait.mutex, LO_ERROR);
1907 	ISCSI_LOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1908 	if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1909 		ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1910 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1911 		LO_ERROR;
1912 	}
1913 	ISCSI_SIGNAL(&sess->tx_worker.work_cond, LO_ERROR);
1914 	ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1915 	iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued logout command ok\n");
1916 
1917 	/* Wait for callback */
1918 
1919 	iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on logout callback\n");
1920 	ISCSI_WAIT(&iwait.cond, &iwait.mutex, LO_ERROR);
1921 	ISCSI_UNLOCK(&iwait.mutex, LO_ERROR);
1922 	ISCSI_COND_DESTROY(&iwait.cond, LO_ERROR);
1923 	ISCSI_MUTEX_DESTROY(&iwait.mutex, LO_ERROR);
1924 	iscsi_trace(TRACE_ISCSI_DEBUG, "received logout callback ok\n");
1925 
1926 	sess->state = INITIATOR_SESSION_STATE_LOGGED_OUT;
1927 
1928 	LO_CLEANUP;
1929 	return 0;
1930 }
1931 
1932 static void
1933 alarm_handler(int arg)
1934 {
1935 	USE_ARG(arg);
1936 	iscsi_err(__FILE__, __LINE__, "***aborting cmd 0x%p***\n", g_cmd);
1937 	if (initiator_abort(g_cmd) != 0) {
1938 		iscsi_err(__FILE__, __LINE__, "initiator_abort() failed\n");
1939 	}
1940 }
1941 
1942 static int
1943 login_phase_i(initiator_session_t * sess, char *text, int text_len)
1944 {
1945 	initiator_cmd_t *cmd = NULL;
1946 	initiator_wait_t iwait;
1947 	iscsi_login_cmd_args_t *login_cmd = NULL;
1948 	struct sigaction act;
1949 
1950 	sess->state = INITIATOR_SESSION_STATE_LOGGING_IN;
1951 
1952 	/* Allocate command pointers */
1953 
1954 	if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
1955 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1956 		return -1;
1957 	}
1958 	(void) memset(cmd, 0x0, sizeof(*cmd));
1959 	if ((login_cmd = iscsi_malloc_atomic(sizeof(iscsi_login_cmd_args_t))) == NULL) {
1960 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1961 		if (cmd != NULL)
1962 			iscsi_free_atomic(cmd);
1963 		return -1;
1964 	}
1965 #define LI_CLEANUP {if (cmd != NULL) iscsi_free_atomic(cmd); if (login_cmd != NULL) iscsi_free_atomic(login_cmd); }
1966 #define LI_ERROR {LI_CLEANUP; return -1;}
1967 	(void) memset(login_cmd, 0x0, sizeof(*login_cmd));
1968 
1969 	/* This is the length of our original offer. */
1970 
1971 	login_cmd->text = text;
1972 	login_cmd->length = text_len;
1973 	login_cmd->transit = 1;
1974 	login_cmd->csg = ISCSI_LOGIN_STAGE_SECURITY;
1975 	login_cmd->nsg = ISCSI_LOGIN_STAGE_NEGOTIATE;
1976 	ISCSI_SET_TAG(&login_cmd->tag);
1977 	login_cmd->CmdSN = sess->CmdSN = 0;
1978 
1979 	do {
1980 
1981 		/*
1982 		 * Build login command.  Note that the <length> and
1983 		 * <text> fields may get updated by login_response_i.
1984 		 * Such is the case when we receive offers from the
1985 		 * target.  The new <length> and <text> fields will
1986 		 * represent the response that we need to send to the
1987 		 * target on the next login.
1988 		 */
1989 
1990 		login_cmd->cont = 0;
1991 		login_cmd->version_min = ISCSI_VERSION;
1992 		login_cmd->version_max = ISCSI_VERSION;
1993 		login_cmd->cid = sess->cid = (int)sess->isid;
1994 		login_cmd->isid = sess->isid = sess->isid;
1995 		login_cmd->tsih = 0;
1996 
1997 		/* Build wait for callback */
1998 
1999 		ISCSI_MUTEX_INIT(&iwait.mutex, LI_ERROR);
2000 		ISCSI_COND_INIT(&iwait.cond, LI_ERROR);
2001 
2002 		/* Build initiator command */
2003 
2004 		cmd->type = ISCSI_LOGIN_CMD;
2005 		cmd->ptr = login_cmd;
2006 		cmd->callback = wait_callback_i;
2007 		cmd->callback_arg = &iwait;
2008 		cmd->isid = sess->isid;
2009 
2010 		/* Set Alarm */
2011 
2012 		g_cmd = cmd;
2013 		act.sa_handler = alarm_handler;
2014 		sigaction(SIGALRM, &act, NULL);
2015 		alarm(5);
2016 
2017 		/* Enqueue initiator command to Tx worker */
2018 
2019 		iscsi_trace(TRACE_ISCSI_DEBUG, "enqueing login command to tx worker %llu\n", sess->isid);
2020 		ISCSI_LOCK(&iwait.mutex, LI_ERROR);
2021 		ISCSI_LOCK(&sess->tx_worker.work_mutex, LI_ERROR);
2022 		if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
2023 			ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LI_ERROR);
2024 			iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
2025 			LI_ERROR;
2026 
2027 		}
2028 		ISCSI_SIGNAL(&sess->tx_worker.work_cond, LI_ERROR);
2029 		ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LI_ERROR);
2030 		iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued login command ok\n");
2031 
2032 		/* Wait for callback  */
2033 
2034 		iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on login callback\n");
2035 		ISCSI_WAIT(&iwait.cond, &iwait.mutex, LI_ERROR);
2036 		ISCSI_UNLOCK(&iwait.mutex, LI_ERROR);
2037 		ISCSI_COND_DESTROY(&iwait.cond, LI_ERROR);
2038 		ISCSI_MUTEX_DESTROY(&iwait.mutex, LI_ERROR);
2039 		iscsi_trace(TRACE_ISCSI_DEBUG, "received login callback ok\n");
2040 
2041 		alarm(0);
2042 
2043 		if (cmd->status != 0) {
2044 			iscsi_err(__FILE__, __LINE__, "initiator_cmd_t failed\n");
2045 			LI_ERROR;
2046 		}
2047 		if (sess->state == INITIATOR_SESSION_STATE_LOGGING_IN) {
2048 			iscsi_trace(TRACE_ISCSI_PARAM, "more negotiation needed (sending %d bytes response parameters)\n",
2049 			      login_cmd->length);
2050 		}
2051 	} while (sess->state == INITIATOR_SESSION_STATE_LOGGING_IN);
2052 	iscsi_trace(TRACE_ISCSI_DEBUG, "login phase completed successfully\n");
2053 
2054 	LI_CLEANUP;
2055 	return 0;
2056 }
2057 
2058 
2059 #define TEXT_RESPONSE_TEXT_LEN	2048
2060 
2061 static int
2062 text_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2063 {
2064 	iscsi_text_cmd_args_t *text_cmd;
2065 	iscsi_text_rsp_args_t text_rsp;
2066 	iscsi_parameter_t *l = sess->params;
2067 	char           *text_in = NULL;
2068 	char           *text_out = NULL;
2069 	int             len_in = 0;
2070 	int             len_out = 0;
2071 	int             ret = 0;
2072 
2073 #define TI_CLEANUP {if (text_in != NULL) iscsi_free_atomic(text_in); if (text_out != NULL) iscsi_free_atomic(text_out);}
2074 #define TI_ERROR {cmd->status=-1; goto callback;}
2075 	if (cmd) {
2076 		text_cmd = (iscsi_text_cmd_args_t *) cmd->ptr;
2077 	} else {
2078 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_text_cmd_args_t??\n");
2079 		return -1;
2080 	}
2081 
2082 	/* Check arguments & update numbering */
2083 
2084 	if (iscsi_text_rsp_decap(header, &text_rsp) != 0) {
2085 		iscsi_err(__FILE__, __LINE__, "text_response_decap() failed\n");
2086 		TI_ERROR;
2087 	}
2088 	if (text_rsp.tag != text_cmd->tag) {
2089 		iscsi_err(__FILE__, __LINE__,
2090 				"Bad \"Tag\": %u != %u.\n",
2091 				text_rsp.tag, text_cmd->tag);
2092 		TI_ERROR;
2093 	}
2094 	if (text_rsp.transfer_tag != 0xffffffff) {
2095 		iscsi_err(__FILE__, __LINE__,
2096 				"Bad \"Transfer Tag\": %u != %u.\n",
2097 				text_rsp.transfer_tag, 0xffffffff);
2098 		TI_ERROR;
2099 	}
2100 	if (text_rsp.StatSN != sess->ExpStatSN) {
2101 		iscsi_err(__FILE__, __LINE__,
2102 				"Bad \"StatSN\": %u != %u.\n",
2103 				text_rsp.StatSN, sess->ExpStatSN);
2104 		TI_ERROR;
2105 	}
2106 	if (text_rsp.ExpCmdSN != sess->CmdSN) {
2107 		iscsi_err(__FILE__, __LINE__,
2108 				"Bad \"ExpCmdSN\": %u != %u.\n",
2109 				text_rsp.ExpCmdSN, sess->CmdSN);
2110 		TI_ERROR;
2111 	}
2112 	sess->ExpStatSN = text_rsp.StatSN + 1;
2113 
2114 	/* Parse input text parameters and generate any response */
2115 
2116 	if ((len_in = text_rsp.length) != 0) {
2117 		iscsi_trace(TRACE_ISCSI_PARAM, "allocating %d bytes input parameters\n", len_in);
2118 		if ((text_in = iscsi_malloc_atomic((unsigned)len_in + 1)) == NULL) {
2119 			iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2120 			TI_ERROR;
2121 		}
2122 		if ((text_out = iscsi_malloc_atomic(TEXT_RESPONSE_TEXT_LEN)) == NULL) {
2123 			iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2124 			if (text_in != NULL)
2125 				iscsi_free_atomic(text_in);
2126 			TI_ERROR;
2127 		}
2128 		if (iscsi_sock_msg(sess->sock, 0, (unsigned)len_in, text_in, 0) != len_in) {
2129 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2130 			TI_ERROR;
2131 		}
2132 		text_in[len_in] = 0x0;
2133 		iscsi_trace(TRACE_ISCSI_PARAM, "read %d bytes input parameters ok\n", len_in);
2134 
2135 		/* Reset the value lists for TargetName and TargetAddress */
2136 
2137 		if (param_val_reset(sess->params, "TargetName") != 0) {
2138 			iscsi_err(__FILE__, __LINE__, "parm_val_reset() failed\n");
2139 			TI_ERROR;
2140 		}
2141 		if (param_val_reset(sess->params, "TargetAddress") != 0) {
2142 			iscsi_err(__FILE__, __LINE__, "parm_val_reset() failed\n");
2143 			TI_ERROR;
2144 		}
2145 		/* Parse the incoming answer */
2146 
2147 		PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_in, len_in, text_out, &len_out, TEXT_RESPONSE_TEXT_LEN, 0, TI_ERROR);
2148 
2149 		if (len_out) {
2150 			if (text_rsp.final != 0) {
2151 				iscsi_err(__FILE__, __LINE__,
2152 					"Bad \"text_rsp.final\": %u != 0.\n",
2153 					text_rsp.final);
2154 				TI_ERROR;
2155 			}
2156 			/*
2157 			 * Copy response text into text_cmd->text and
2158 			 * update the length text_cmd->length.  This
2159 			 * will be sent out on the next text command.
2160 			 * */
2161 
2162 			PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_out, len_out, NULL, NULL, TEXT_RESPONSE_TEXT_LEN, 1, TI_ERROR);
2163 
2164 			iscsi_trace(TRACE_ISCSI_PARAM, "need to send %d bytes response back to target\n", len_out);
2165 			text_cmd->length = len_out;
2166 			memcpy(text_cmd->text, text_out, (size_t)len_out);
2167 		} else {
2168 			text_cmd->length = 0;
2169 		}
2170 	}
2171 	text_cmd->final = text_rsp.final;
2172 
2173 	/* Issue callback */
2174 
2175 	iscsi_trace(TRACE_ISCSI_DEBUG, "iscsi_text_cmd_args_t done\n");
2176 callback:
2177 	if (cmd->status == -1)
2178 		ret = -1;
2179 	if (cmd->callback(cmd) != 0) {
2180 		ret = -1;
2181 		iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2182 	}
2183 	TI_CLEANUP;
2184 	return ret;
2185 }
2186 
2187 #define LOGIN_RESPONSE_TEXT_LEN	2048
2188 
2189 static int
2190 login_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2191 {
2192 	iscsi_login_cmd_args_t *login_cmd;
2193 	iscsi_login_rsp_args_t login_rsp;
2194 	iscsi_parameter_t *l = sess->params;
2195 	char           *text_in = NULL;
2196 	char           *text_out = NULL;
2197 	int             len_in = 0;
2198 	int             len_out = 0;
2199 
2200 	if ((text_out = iscsi_malloc_atomic(LOGIN_RESPONSE_TEXT_LEN)) == NULL) {
2201 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2202 		cmd->status = -1;
2203 		goto callback;
2204 	}
2205 #define LIR_CLEANUP {if (text_in != NULL) iscsi_free_atomic(text_in); if (text_out != NULL) iscsi_free_atomic(text_out);}
2206 #define LIR_ERROR {cmd->status=-1; goto callback;}
2207 	if (cmd) {
2208 		login_cmd = (iscsi_login_cmd_args_t *) cmd->ptr;
2209 	} else {
2210 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_login_cmd_args_t??\n");
2211 		LIR_ERROR;
2212 	}
2213 
2214 	/* Read login response */
2215 
2216 	if (iscsi_login_rsp_decap(header, &login_rsp) != 0) {
2217 		iscsi_err(__FILE__, __LINE__, "login_response_decap() failed\n");
2218 		LIR_ERROR;
2219 	}
2220 	if (login_rsp.length > 8192) {
2221 		iscsi_err(__FILE__, __LINE__, "login_rsp.length %u\n",
2222 			login_rsp.length);
2223 		LIR_CLEANUP;
2224 		return -1;
2225 	}
2226 
2227 	/* Read & parse text response */
2228 	if ((len_in = login_rsp.length) != 0) {
2229 		if ((text_in = iscsi_malloc_atomic((unsigned)len_in + 1)) == NULL) {
2230 			iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2231 			LIR_ERROR;
2232 		}
2233 		if (iscsi_sock_msg(sess->sock, 0, (unsigned)len_in, text_in, 0) != len_in) {
2234 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2235 			LIR_ERROR;
2236 		}
2237 		text_in[len_in] = 0x0;
2238 		PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_in, len_in, text_out, &len_out, LOGIN_RESPONSE_TEXT_LEN, 0, LIR_ERROR);
2239 		if (login_rsp.transit && len_out != 0) {
2240 			iscsi_warn(__FILE__, __LINE__,
2241 				  "Bad \"len_out\": Got %u expected %u.\n",
2242 				  len_out, 0);
2243 		}
2244 	}
2245 
2246 	/* Check args */
2247 	if (login_rsp.status_class != 0) {
2248 		iscsi_err(__FILE__, __LINE__, "Bad Status-Class: got %d, expected %d\n", login_rsp.status_class, 0);
2249 		LIR_ERROR;
2250 	}
2251 	if (login_rsp.tag != login_cmd->tag) {
2252 		iscsi_err(__FILE__, __LINE__, "Bad Tag: got %x, expected %x\n", login_rsp.tag, login_cmd->tag);
2253 		LIR_ERROR;
2254 	}
2255 	sess->ExpStatSN = login_rsp.StatSN + 1;
2256 
2257 
2258 	if (login_rsp.transit) {
2259 
2260 		if (login_cmd->transit != 1)
2261 			iscsi_warn(__FILE__, __LINE__, "incoming packet transit bit not set, csg = %d, nsg = %d\n",
2262 				      login_cmd->csg, login_cmd->nsg);
2263 
2264 		switch (login_rsp.nsg) {
2265 		case ISCSI_LOGIN_STAGE_NEGOTIATE:
2266 			login_cmd->csg = login_cmd->nsg;
2267 			login_cmd->nsg = ISCSI_LOGIN_STAGE_FULL_FEATURE;
2268 			if (params_out(sess, text_out, &len_out, LOGIN_RESPONSE_TEXT_LEN, SESS_TYPE_NONE, /*LINTED*/!IS_SECURITY) != 0) {
2269 				iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
2270 				LIR_ERROR;
2271 			}
2272 			login_cmd->length = len_out;
2273 			(void) memcpy(login_cmd->text, text_out,
2274 						(size_t)len_out);
2275 			break;
2276 
2277 		case ISCSI_LOGIN_STAGE_FULL_FEATURE:
2278 			/* Check post conditions */
2279 
2280 			if (login_rsp.tsih == 0) {
2281 				iscsi_err(__FILE__, __LINE__,
2282 				"Bad \"TSIH\": %u == 0.\n", login_rsp.tsih);
2283 				LIR_ERROR;
2284 			}
2285 			if (login_rsp.isid != login_cmd->isid) {
2286 				iscsi_err(__FILE__, __LINE__,
2287 					"Bad \"ISID\": %" PRIu64 "u != %"
2288 							PRIu64 "u.\n",
2289 					(unsigned)login_rsp.isid,
2290 					(unsigned)login_cmd->isid);
2291 				LIR_ERROR;
2292 			}
2293 			if (login_rsp.ExpCmdSN != login_cmd->CmdSN) {
2294 				iscsi_err(__FILE__, __LINE__,
2295 					"Bad \"ExpCmdSN\": %u != %u.\n",
2296 					(unsigned)login_rsp.ExpCmdSN,
2297 					(unsigned)login_cmd->CmdSN);
2298 				LIR_ERROR;
2299 			}
2300 			if (login_rsp.ExpCmdSN > login_rsp.MaxCmdSN) {
2301 				iscsi_err(__FILE__, __LINE__,
2302 					"Bad \"MaxCmdSN\": %u > %u.\n",
2303 					(unsigned)login_rsp.ExpCmdSN,
2304 					(unsigned)login_rsp.MaxCmdSN);
2305 				LIR_ERROR;
2306 			}
2307 
2308 			/* Set remaining session parameters */
2309 
2310 			sess->CmdSN = login_rsp.ExpCmdSN;
2311 			sess->MaxCmdSN = login_rsp.MaxCmdSN;
2312 			sess->tsih = login_rsp.tsih;
2313 			sess->isid = login_rsp.isid;
2314 
2315 			if (param_equiv(sess->params, "SessionType", "Normal")) {
2316 				sess->state = INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL;
2317 			} else if (param_equiv(sess->params, "SessionType", "Discovery")) {
2318 				sess->state = INITIATOR_SESSION_STATE_LOGGED_IN_DISCOVERY;
2319 			} else {
2320 				iscsi_err(__FILE__, __LINE__, "Unknown SessionType \"%s\"\n", param_val(sess->params, "SessionType"));
2321 				LIR_ERROR;
2322 			}
2323 
2324 			iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2325 			iscsi_trace(TRACE_ISCSI_DEBUG, "*              LOGIN SUCCESSFUL             *\n");
2326 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CID", sess->cid);
2327 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20llu *\n", "ISID", sess->isid);
2328 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "TSIH", sess->tsih);
2329 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CmdSN", sess->CmdSN);
2330 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "MaxCmdSN", sess->MaxCmdSN);
2331 			iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "ExpStatSN", sess->ExpStatSN);
2332 			iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2333 			break;
2334 		default:
2335 			LIR_ERROR;
2336 		}
2337 	} else {
2338 		iscsi_trace(TRACE_ISCSI_DEBUG, "received partial login response\n");
2339 
2340 		/* Copy response text into login_cmd->text and update the */
2341 		/* length login_cmd->length.  This will be sent out on the */
2342 		/* next login command. */
2343 
2344 		if (len_out) {
2345 			PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_out, len_out, NULL, NULL, 0, 1, LIR_ERROR);
2346 			iscsi_trace(TRACE_ISCSI_PARAM, "need to send %d bytes response back to target\n", len_out);
2347 
2348 			login_cmd->length = len_out;
2349 			memcpy(login_cmd->text, text_out, (size_t)len_out);
2350 			if (strncmp(text_out, "CHAP_N=", strlen("CHAP_N=")) == 0) {
2351 				login_cmd->nsg = ISCSI_LOGIN_STAGE_NEGOTIATE;
2352 				login_cmd->transit = 1;
2353 			}
2354 		} else {
2355 			login_cmd->length = 0;
2356 		}
2357 	}
2358 
2359 	/* Callback */
2360 
2361 callback:
2362 	iscsi_trace(TRACE_ISCSI_DEBUG, "iscsi_login_cmd_args_t done (cmd status %d, iscsi status %d)\n",
2363 	      cmd->status, login_rsp.status_class);
2364 	if ((*cmd->callback)(cmd) != 0) {
2365 		iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2366 		LIR_CLEANUP;
2367 		return -1;
2368 	}
2369 	LIR_CLEANUP;
2370 	return 0;
2371 }
2372 
2373 static int
2374 logout_command_i(initiator_cmd_t * cmd)
2375 {
2376 	iscsi_logout_cmd_args_t	*logout_cmd;
2377 	initiator_session_t	*sess;
2378 	uint8_t			 header[ISCSI_HEADER_LEN];
2379 
2380 	logout_cmd = (iscsi_logout_cmd_args_t *) cmd->ptr;
2381 	sess = g_target[(int)cmd->isid].sess;
2382 	/*
2383 	 * Insert cmd into the hash table, keyed by the tag. The Rx thread
2384 	 * will
2385 	 */
2386 	/* retreive the cmd ptr using the tag from the response PDU. */
2387 
2388 	if (hash_insert(&g_tag_hash, cmd, logout_cmd->tag) != 0) {
2389 		iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2390 		return -1;
2391 	}
2392 	/* Send logout command PDU */
2393 
2394 	iscsi_trace(TRACE_ISCSI_DEBUG, "sending logout command\n");
2395 	if (iscsi_logout_cmd_encap(header, logout_cmd) != 0) {
2396 		iscsi_err(__FILE__, __LINE__, "iscsi_logout_cmd_encap() failed\n");
2397 		return -1;
2398 	}
2399 	if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2400 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed.\n");
2401 		return -1;
2402 	}
2403 	iscsi_trace(TRACE_ISCSI_DEBUG, "logout command sent ok\n");
2404 
2405 	return 0;
2406 }
2407 
2408 
2409 static int
2410 logout_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2411 {
2412 	iscsi_logout_cmd_args_t *logout_cmd;
2413 	iscsi_logout_rsp_args_t logout_rsp;
2414 
2415 #define LOR_ERROR {cmd->status=-1; goto callback;}
2416 	if (cmd) {
2417 		if (cmd->ptr) {
2418 			logout_cmd = (iscsi_logout_cmd_args_t *) cmd->ptr;
2419 		} else {
2420 			iscsi_err(__FILE__, __LINE__, "no iscsi_logout_cmd_args_t specified for initiator_cmd_t??\n");
2421 			LOR_ERROR;
2422 		}
2423 	} else {
2424 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_logout_cmd_args_t??\n");
2425 		return -1;
2426 	}
2427 	if (iscsi_logout_rsp_decap(header, &logout_rsp) != 0) {
2428 		iscsi_err(__FILE__, __LINE__, "iscsi_logout_rsp_decap() failed\n");
2429 		LOR_ERROR;
2430 	}
2431 	if (logout_rsp.response != ISCSI_LOGOUT_STATUS_SUCCESS) {
2432 		iscsi_err(__FILE__, __LINE__, "Bad \"Response\": Got %u\n",
2433 				logout_rsp.response);
2434 		LOR_ERROR;
2435 	}
2436 	if (logout_rsp.tag != logout_cmd->tag) {
2437 		iscsi_err(__FILE__, __LINE__, "Bad \"Tag\": Got %u\n",
2438 				logout_rsp.tag);
2439 		LOR_ERROR;
2440 	}
2441 
2442 	/* Check and update numbering */
2443 	if (logout_rsp.StatSN != sess->ExpStatSN) {
2444 		iscsi_err(__FILE__, __LINE__,
2445 			"Bad \"StatSN\": Got %u, needed %u\n",
2446 			logout_rsp.StatSN, sess->ExpStatSN);
2447 		LOR_ERROR;
2448 	}
2449 	sess->ExpStatSN += 1;
2450 	if (logout_rsp.ExpCmdSN != sess->CmdSN) {
2451 		iscsi_err(__FILE__, __LINE__,
2452 			"Bad \"ExpCmdSN\": Got %u, needed %u\n",
2453 			logout_rsp.ExpCmdSN, sess->CmdSN);
2454 		LOR_ERROR;
2455 	}
2456 	sess->MaxCmdSN = logout_rsp.MaxCmdSN;
2457 
2458 	/* Callback */
2459 	cmd->status = 0;
2460 	iscsi_trace(TRACE_ISCSI_DEBUG,
2461 		"LOGOUT_CMD_T done (cmd status %d, iscsi status %d)\n",
2462 		cmd->status, logout_rsp.response);
2463 callback:
2464 	if ((*cmd->callback)(cmd) != 0) {
2465 		iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2466 		return -1;
2467 	}
2468 
2469 	iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2470 	iscsi_trace(TRACE_ISCSI_DEBUG, "*             LOGOUT SUCCESSFUL             *\n");
2471 	iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CID", sess->cid);
2472 	iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20llu *\n", "ISID", sess->isid);
2473 	iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "TSIH", sess->tsih);
2474 	iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2475 
2476 	return 0;
2477 }
2478 
2479 static int
2480 nop_out_i(initiator_cmd_t * cmd)
2481 {
2482 	uint8_t   header[ISCSI_HEADER_LEN];
2483 	iscsi_nop_out_args_t *nop_out;
2484 	initiator_session_t *sess;
2485 	int             rc, length;
2486 
2487 	nop_out = cmd->ptr;
2488 	sess = g_target[(int)cmd->isid].sess;
2489 	length = nop_out->length;
2490 	if (nop_out->tag != 0xffffffff) {
2491 
2492 		/*
2493 		 * Insert cmd into the hash table, keyed by
2494 		 * nop_out->tag.  Upon receipt of the NOP_IN_T, the Rx
2495 		 * thread will retreive the cmd ptr using the tag from
2496 		 * the NOP_IN_T PDU.  */
2497 
2498 		if (hash_insert(&g_tag_hash, cmd, nop_out->tag) != 0) {
2499 			iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2500 			return -1;
2501 		}
2502 	}
2503 	/* Encapsulate and send NOP */
2504 
2505 	nop_out->ExpStatSN = sess->ExpStatSN;
2506 	nop_out->immediate = 1;
2507 	nop_out->CmdSN = sess->CmdSN;
2508 	nop_out->transfer_tag = 0xffffffff;
2509 	if (iscsi_nop_out_encap(header, nop_out) != 0) {
2510 		iscsi_err(__FILE__, __LINE__, "iscsi_nop_out_encap() failed\n");
2511 		return -1;
2512 	}
2513 	/*
2514 	 * We need to make a copy of nop_out->length and save in the
2515 	 * variable length.  Otherwise, we may get a seg fault - as if
2516 	 * this is a NOP_OUT without ping, the Tx thread will issue
2517 	 * the callback function immediately after we return - thereby
2518 	 * de-allocating the NOP_OUT and initiator command structures.
2519 	 * */
2520 
2521 	if ((rc = iscsi_sock_send_header_and_data(sess->sock, header,
2522 			ISCSI_HEADER_LEN, nop_out->data, (unsigned)length,
2523 			0)) != ISCSI_HEADER_LEN + length) {
2524 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed: got %d expected %d\n", rc, ISCSI_HEADER_LEN + length);
2525 		return -1;
2526 	}
2527 	cmd->status = 0;
2528 	return 0;
2529 }
2530 
2531 static int
2532 scsi_command_i(initiator_cmd_t * cmd)
2533 {
2534 	iscsi_scsi_cmd_args_t *scsi_cmd;
2535 	uint8_t   header[ISCSI_HEADER_LEN];
2536 	uint64_t target;
2537 	initiator_session_t *sess;
2538 	iscsi_write_data_t data;
2539 	struct iovec    sg_singleton;
2540 	struct iovec   *sg, *sg_copy, *sg_copy_orig, *sg_which;
2541 	int             sg_len, sg_len_copy, sg_len_which;
2542 	int             fragment_flag;
2543 
2544 	scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
2545 	target = cmd->isid;
2546 	sess = g_target[(int)target].sess;
2547 	fragment_flag = 0;
2548 	sg = sg_copy = sg_copy_orig = sg_which = NULL;
2549 	sg_len = sg_len_copy = sg_len_which = 0;
2550 	scsi_cmd->status = 0;
2551 
2552 	iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%llu]: scsi op %#x lun %llu trans_len %d length %d send_sg_len %d recv_sg_len %d\n", target, scsi_cmd->cdb[0], scsi_cmd->lun, scsi_cmd->trans_len, scsi_cmd->length, scsi_cmd->send_sg_len, scsi_cmd->recv_sg_len);
2553 
2554 	if ((uint32_t)target > CONFIG_INITIATOR_NUM_TARGETS) {
2555 		iscsi_err(__FILE__, __LINE__, "target %u\n",
2556 				(uint32_t)target);
2557 		NO_CLEANUP;
2558 		return -1;
2559 	}
2560 
2561 	/* Set and check scsi_cmd */
2562 	if (scsi_cmd->trans_len > sess->sess_params.max_burst_length) {
2563 		iscsi_err(__FILE__, __LINE__, "scsi_cmd->trans_len (%u) > MaxBurstLength (%u)\n",
2564 		   scsi_cmd->trans_len, sess->sess_params.max_burst_length);
2565 		return -1;
2566 	}
2567 	if (scsi_cmd->length > scsi_cmd->trans_len) {
2568 		iscsi_err(__FILE__, __LINE__, "scsi_cmd->length (%u) > scsi_cmd->trans_len (%u)\n",
2569 			    scsi_cmd->length, scsi_cmd->trans_len);
2570 		return -1;
2571 	}
2572 	scsi_cmd->ExpStatSN = sess->ExpStatSN;
2573 	scsi_cmd->CmdSN = sess->CmdSN;
2574 	scsi_cmd->bytes_sent = scsi_cmd->bytes_recv = 0;
2575 
2576 	/* Always use iovec for data */
2577 
2578 	if (scsi_cmd->output) {
2579 		if (scsi_cmd->send_sg_len) {	/* Data already an iovec */
2580 			sg = (struct iovec *)(void *)scsi_cmd->send_data;
2581 			sg_len = scsi_cmd->send_sg_len;
2582 		} else {	/* Make iovec for data */
2583 			sg_singleton.iov_base = scsi_cmd->send_data;
2584 			sg_singleton.iov_len = scsi_cmd->trans_len;
2585 			sg = &sg_singleton;
2586 			sg_len = 1;
2587 		}
2588 	}
2589 	/*
2590 	 * Insert cmd into the hash table, keyed by scsi_cmd->tag.  The Rx
2591 	 * thread will
2592 	 */
2593 	/* retreive the cmd ptr using the tag from the response PDU. */
2594 
2595 	if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
2596 		iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2597 		goto error;
2598 	}
2599 	/* Send command PDU */
2600 
2601 	if (scsi_cmd->output && sess->sess_params.immediate_data) {
2602 		if (sess->sess_params.max_dataseg_len) {
2603 			scsi_cmd->length = MIN(sess->sess_params.max_dataseg_len,
2604 					       scsi_cmd->trans_len);
2605 		} else {
2606 			scsi_cmd->length = scsi_cmd->trans_len;
2607 		}
2608 		if (scsi_cmd->length == scsi_cmd->trans_len)
2609 			scsi_cmd->final = 1;
2610 	} else {
2611 		scsi_cmd->length = 0;
2612 		scsi_cmd->final = 1;
2613 	}
2614 	if (iscsi_scsi_cmd_encap(header, scsi_cmd) != 0) {
2615 		iscsi_err(__FILE__, __LINE__, "iscsi_scsi_cmd_encap() failed\n");
2616 		goto error;
2617 	}
2618 	/*
2619 	 * If we're sending any immediate data, we need to make a new
2620 	 * iovec that contains only the immediata data (a subset of
2621 	 * the original iovec).  */
2622 	iscsi_trace(TRACE_ISCSI_DEBUG, "sending command PDU with %u bytes immediate data\n", scsi_cmd->length);
2623 	if (scsi_cmd->length && sess->sess_params.immediate_data) {
2624 		if ((sg_copy = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
2625 			iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2626 			goto error;
2627 		}
2628 		fragment_flag++;
2629 		sg_copy_orig = sg_copy;
2630 		memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
2631 		sg_len_copy = sg_len;
2632 		if (modify_iov(&sg_copy, &sg_len_copy, 0, scsi_cmd->length) != 0) {
2633 			iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
2634 			goto error;
2635 		}
2636 		if (scsi_cmd->ahs) {
2637 			if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2638 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2639 				goto error;
2640 			}
2641 			if (iscsi_sock_msg(sess->sock, 1, (unsigned)scsi_cmd->ahs_len, scsi_cmd->ahs, 0) != scsi_cmd->ahs_len) {
2642 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2643 				goto error;
2644 			}
2645 			if ((unsigned)iscsi_sock_msg(sess->sock, 1, scsi_cmd->length, sg_copy, sg_len_copy) != scsi_cmd->length) {
2646 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2647 				goto error;
2648 			}
2649 		} else {
2650 			if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_copy, scsi_cmd->length, sg_len_copy)
2651 			    != ISCSI_HEADER_LEN + scsi_cmd->length) {
2652 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
2653 				goto error;
2654 			}
2655 		}
2656 		scsi_cmd->bytes_sent += scsi_cmd->length;
2657 	} else {
2658 		if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2659 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2660 			goto error;
2661 		}
2662 		if (scsi_cmd->ahs_len) {
2663 			if (iscsi_sock_msg(sess->sock, 1, (unsigned)scsi_cmd->ahs_len, scsi_cmd->ahs, 0) != scsi_cmd->ahs_len) {
2664 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2665 				goto error;
2666 			}
2667 		}
2668 	}
2669 	iscsi_trace(TRACE_ISCSI_DEBUG, "command PDU sent with %u bytes immediate data (%u bytes AHS)\n", scsi_cmd->length, scsi_cmd->ahs_len);
2670 
2671 	/*
2672 	 * Send data PDUS if 1) we're not in R2T mode and 2) we
2673 	 * haven't sent everything as immediate data and 3) we have
2674 	 * not reached the first burst when sending immediate data
2675 	 */
2676 	if (scsi_cmd->output
2677 	    && (!sess->sess_params.initial_r2t)
2678 	    && (scsi_cmd->bytes_sent != scsi_cmd->trans_len)
2679 	    && ((!sess->sess_params.first_burst_length)
2680 	|| (scsi_cmd->bytes_sent < sess->sess_params.first_burst_length))) {
2681 
2682 		uint32_t        DataSN = 0;
2683 
2684 		iscsi_trace(TRACE_ISCSI_DEBUG, "preparing to send %d bytes write data\n", scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2685 
2686 		do {
2687 			(void) memset(&data, 0x0, sizeof(data));
2688 
2689 			/*
2690 			 * Take into account that MaxRecvPDULength and
2691 			 * FirstBurstLength could both be "0" (no limit)
2692 			 */
2693 			if (sess->sess_params.max_dataseg_len) {
2694 				if (sess->sess_params.first_burst_length) {
2695 					data.length = MIN_3(
2696 							    sess->sess_params.first_burst_length - scsi_cmd->bytes_sent,
2697 							    sess->sess_params.max_dataseg_len,
2698 							    scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2699 				} else {
2700 					data.length = MIN(
2701 							  sess->sess_params.max_dataseg_len,
2702 							  scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2703 				}
2704 			} else {
2705 				if (sess->sess_params.first_burst_length) {
2706 					data.length = MIN(
2707 							  sess->sess_params.first_burst_length - scsi_cmd->bytes_sent,
2708 							  scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2709 				} else {
2710 					data.length = scsi_cmd->trans_len - scsi_cmd->bytes_sent;
2711 				}
2712 			}
2713 #define FRAG_CLEANUP {if (fragment_flag) iscsi_free_atomic(sg_copy);}
2714 
2715 			if (data.length == 0) {
2716 				 iscsi_err(__FILE__, __LINE__,
2717 				 		"Zero data.length\n");
2718 				 FRAG_CLEANUP;
2719 				 return -1;
2720 			}
2721 
2722 			if (scsi_cmd->bytes_sent + data.length ==
2723 						scsi_cmd->trans_len) {
2724 				data.final = 1;
2725 			}
2726 			data.tag = scsi_cmd->tag;
2727 			data.transfer_tag = 0xffffffff;
2728 			data.ExpStatSN = sess->ExpStatSN;
2729 			data.DataSN = DataSN++;
2730 			data.offset = scsi_cmd->bytes_sent;
2731 
2732 			if (iscsi_write_data_encap(header, &data) != 0) {
2733 				iscsi_err(__FILE__, __LINE__, "iscsi_write_data_encap() failed\n");
2734 				goto error;
2735 			}
2736 			if (data.length != scsi_cmd->trans_len) {
2737 
2738 				/*
2739 				 * Make copy of iovec and modify with offset
2740 				 * and length
2741 				 */
2742 
2743 				if (!fragment_flag) {
2744 					if ((sg_copy = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
2745 						iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2746 						goto error;
2747 					}
2748 					sg_copy_orig = sg_copy;
2749 					fragment_flag++;
2750 				}
2751 				sg_copy = sg_copy_orig;
2752 				memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
2753 				sg_len_copy = sg_len;
2754 				if (modify_iov(&sg_copy, &sg_len_copy, scsi_cmd->bytes_sent, data.length) != 0) {
2755 					iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
2756 					goto error;
2757 				}
2758 				sg_which = sg_copy;
2759 				sg_len_which = sg_len_copy;
2760 
2761 			} else {
2762 
2763 				/*
2764 				 * Data was not fragmented; use the original
2765 				 * iovec.
2766 				 */
2767 
2768 				sg_which = sg;
2769 				sg_len_which = sg_len;
2770 			}
2771 
2772 			iscsi_trace(TRACE_ISCSI_DEBUG, "sending write data PDU (offset %u, len %u, sg_len %u)\n",
2773 			      data.offset, data.length, sg_len_which);
2774 
2775 			if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_which, data.length, sg_len_which)
2776 			    != ISCSI_HEADER_LEN + data.length) {
2777 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
2778 				goto error;
2779 			}
2780 			iscsi_trace(TRACE_ISCSI_DEBUG, "sent write data PDU (offset %u, len %u)\n", data.offset, data.length);
2781 			scsi_cmd->bytes_sent += data.length;
2782 		} while ((scsi_cmd->bytes_sent < scsi_cmd->trans_len)
2783 			 && ((scsi_cmd->bytes_sent < sess->sess_params.first_burst_length)
2784 			     || (!sess->sess_params.first_burst_length)));
2785 		if (scsi_cmd->trans_len - scsi_cmd->bytes_sent) {
2786 			iscsi_trace(TRACE_ISCSI_DEBUG, "REACHED FIRST BURST\n");
2787 		}
2788 		iscsi_trace(TRACE_ISCSI_DEBUG, "successfully sent %u of %u bytes write data\n", scsi_cmd->bytes_sent, scsi_cmd->trans_len);
2789 	}
2790 	if (scsi_cmd->output && (scsi_cmd->trans_len - scsi_cmd->bytes_sent)) {
2791 		iscsi_trace(TRACE_ISCSI_DEBUG, "expecting R2T for remaining %u bytes write data\n", scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2792 	}
2793 	if (fragment_flag)
2794 		iscsi_free_atomic(sg_copy_orig);
2795 	sess->CmdSN++;
2796 
2797 	return 0;
2798 
2799 error:
2800 	if (fragment_flag)
2801 		iscsi_free_atomic(sg_copy);
2802 	return -1;
2803 }
2804 
2805 static int
2806 reject_i(initiator_session_t * sess, uint8_t *header)
2807 {
2808 	initiator_cmd_t *cmd = NULL;
2809 	iscsi_reject_t  reject;
2810 	uint8_t   bad_header[ISCSI_HEADER_LEN];
2811 	uint32_t        tag;
2812 
2813 	/* Get & check args */
2814 
2815 	if (iscsi_reject_decap(header, &reject) != 0) {
2816 		iscsi_err(__FILE__, __LINE__, "iscsi_reject_decap() failed\n");
2817 		return -1;
2818 	}
2819 	if (reject.length != ISCSI_HEADER_LEN) {
2820 		iscsi_err(__FILE__, __LINE__, "reject.length %u\n",
2821 				reject.length);
2822 		NO_CLEANUP;
2823 		return -1;
2824 	}
2825 
2826 	/* Read bad header, extract tag, and get cmd from hash table */
2827 
2828 	if (iscsi_sock_msg(sess->sock, 0, ISCSI_HEADER_LEN, bad_header, 0) != ISCSI_HEADER_LEN) {
2829 		iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2830 		return -1;
2831 	}
2832 	(void) memcpy(&tag, bad_header + 16, sizeof(tag));
2833 	tag = ISCSI_NTOHL(tag);
2834 	iscsi_err(__FILE__, __LINE__, "REJECT PDU: tag %#x (reason %#x)\n", tag, reject.reason);
2835 	if (tag != 0xffffffff) {
2836 		if ((cmd = hash_remove(&g_tag_hash, tag)) == NULL) {
2837 			iscsi_trace(TRACE_ISCSI_DEBUG, "no cmd ptr associated with tag %#x\n", tag);
2838 		} else {
2839 			iscsi_trace(TRACE_ISCSI_DEBUG, "cmd %p associated with tag %#x\n", cmd, tag);
2840 			ISCSI_LOCK(&sess->rx_worker.work_mutex, return -1);
2841 			if (!cmd->tx_done)
2842 				ISCSI_WAIT(&sess->rx_worker.work_cond, &sess->rx_worker.work_mutex, return -1);
2843 			ISCSI_UNLOCK(&sess->rx_worker.work_mutex, return -1);
2844 		}
2845 	} else {
2846 		iscsi_err(__FILE__, __LINE__, "no command associated with tag %#x\n", tag);
2847 	}
2848 
2849 	/* Execute callback to complete initiator_cmd_t  */
2850 
2851 	if (cmd) {
2852 		cmd->status = -1;
2853 		if (cmd->callback) {
2854 			iscsi_trace(TRACE_ISCSI_DEBUG, "issuing callback for cmd associated with tag %#x\n", tag);
2855 			if ((*cmd->callback)(cmd) != 0) {
2856 				iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2857 				return -1;
2858 			}
2859 		} else {
2860 			iscsi_err(__FILE__, __LINE__, "no callback associated with tag %#x\n", tag);
2861 		}
2862 	}
2863 	return 0;
2864 }
2865 
2866 static int
2867 async_msg_i(initiator_session_t * sess, uint8_t *header)
2868 {
2869 	iscsi_async_msg_t    msg;
2870 
2871 	/* Get & check args */
2872 	if (iscsi_amsg_decap(header, &msg) != 0) {
2873 		iscsi_err(__FILE__, __LINE__, "iscsi_amsg_decap() failed\n");
2874 		return -1;
2875 	}
2876 	sess->CmdSN = msg.ExpCmdSN;
2877 	sess->MaxCmdSN = msg.MaxCmdSN;
2878 	sess->ExpStatSN = msg.StatSN + 1;
2879 
2880 	/* Read Sense Data */
2881 	if (msg.length) {
2882 		uint8_t  *sense_data = NULL;
2883 		if ((sense_data = iscsi_malloc(msg.length)) == NULL) {
2884 			iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
2885 			return -1;
2886 		}
2887 		iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes sense data \n", msg.length);
2888 		if ((unsigned)iscsi_sock_msg(sess->sock, 0, msg.length, sense_data, 0) != msg.length) {
2889 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2890 			if (sense_data != NULL)
2891 				iscsi_free(sense_data);
2892 			return -1;
2893 		}
2894 		iscsi_trace(TRACE_ISCSI_DEBUG, "read %d bytes sense data ok (currently discarding)\n", msg.length);
2895 		if (sense_data != NULL)
2896 			iscsi_free(sense_data);
2897 	} else {
2898 		iscsi_trace(TRACE_ISCSI_DEBUG, "no sense data available\n");
2899 	}
2900 
2901 	switch (msg.AsyncEvent) {
2902 	case 0:
2903 		/* Ignore SCSI asyn messages for now */
2904 		break;
2905 	case 1:
2906 	case 4:
2907 		/* Ignore Parameter Negotiation. Send Logout */
2908 		logout_phase_i(sess);
2909 		/* FALLTHROUGH */
2910 	case 2:
2911 	case 3:
2912 		if (iscsi_sock_shutdown(sess->sock, 1) != 0) {
2913 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_shutdown() failed\n");
2914 		}
2915 		return -1;
2916 	case 255:
2917 		break;
2918 	default:
2919 		break;
2920 	}
2921 
2922 	return 0;
2923 }
2924 
2925 static int
2926 nop_in_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2927 {
2928 	iscsi_nop_out_args_t *nop_out = NULL;
2929 	iscsi_nop_in_args_t  nop_in;
2930 	uint8_t  *ping_data = NULL;
2931 	unsigned             i;
2932 
2933 	if (cmd) {
2934 		nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
2935 	} else {
2936 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this NOP_IN\n");
2937 	}
2938 	if (iscsi_nop_in_decap(header, &nop_in) != 0) {
2939 		iscsi_err(__FILE__, __LINE__, "iscsi_nop_in() failed\n");
2940 		return -1;
2941 	}
2942 	if (nop_in.transfer_tag == 0xffffffff) {
2943 		if (nop_in.length != 0) {
2944 			iscsi_err(__FILE__, __LINE__,
2945 				"nop_in.length %u not 0\n",
2946 				nop_in.length);
2947 			NO_CLEANUP;
2948 			return -1;
2949 		}
2950 		return 0;
2951 	}
2952 	if (cmd) {
2953 #if 0
2954 		RETURN_NOT_EQUAL("nop_in.length", nop_in.length, nop_out->length, NO_CLEANUP, -1);
2955 #else
2956 		if (nop_in.length != nop_out->length) {
2957 			iscsi_err(__FILE__, __LINE__,
2958 				"nop_in.length %u, nopout->length %u\n",
2959 				nop_in.length, nop_out->length);
2960 			NO_CLEANUP;
2961 			return -1;
2962 		}
2963 #endif
2964 	}
2965 	if (nop_in.length) {
2966 		iscsi_trace(TRACE_ISCSI_DEBUG,
2967 				"reading %d bytes ping data\n", nop_in.length);
2968 		if ((ping_data = iscsi_malloc_atomic(nop_in.length)) == NULL) {
2969 			iscsi_err(__FILE__, __LINE__,
2970 				"iscsi_malloc_atomic() failed\n");
2971 			return -1;
2972 		}
2973 #define NOI_CLEANUP {if (ping_data) iscsi_free_atomic(ping_data);}
2974 #define NOI_ERROR {NOI_CLEANUP; return -1;}
2975 		if ((unsigned)iscsi_sock_msg(sess->sock, 0, nop_in.length,
2976 				ping_data, 0) != nop_in.length) {
2977 			iscsi_err(__FILE__, __LINE__,
2978 					"iscsi_sock_msg() failed\n");
2979 			NOI_ERROR;
2980 		}
2981 		iscsi_trace(TRACE_ISCSI_DEBUG,
2982 			"successfully read %d bytes ping data\n",
2983 			nop_in.length);
2984 		if (cmd) {
2985 			for (i = 0; i < nop_in.length; i++) {
2986 				if (nop_out->data[i] != ping_data[i]) {
2987 					iscsi_err(__FILE__, __LINE__,
2988 						"Bad ping data[%d]. "
2989 						"Got %#x, expected %#x\n",
2990 						i, ping_data[i],
2991 						nop_out->data[i]);
2992 					NOI_ERROR;
2993 				}
2994 			}
2995 		}
2996 	}
2997 
2998 	/* Send ping response (if initiated by target) */
2999 	if (nop_in.transfer_tag != 0xffffffff) {
3000 		uint8_t   nop_header[ISCSI_HEADER_LEN];
3001 		iscsi_nop_out_args_t nop_out_args;
3002 
3003 		iscsi_trace(TRACE_ISCSI_DEBUG,
3004 			"sending %d byte ping response\n", nop_in.length);
3005 		(void) memset(&nop_out_args, 0x0, sizeof(nop_out_args));
3006 		nop_out_args.tag = 0xffffffff;
3007 		nop_out_args.immediate = 0x40;
3008 		nop_out_args.transfer_tag = nop_in.transfer_tag;
3009 		nop_out_args.length = nop_in.length;
3010 		nop_out_args.lun = nop_in.lun;
3011 		nop_out_args.ExpStatSN = sess->ExpStatSN;
3012 		nop_out_args.CmdSN = sess->CmdSN;
3013 		if (iscsi_nop_out_encap(nop_header, &nop_out_args) != 0) {
3014 			iscsi_err(__FILE__, __LINE__,
3015 				"iscsi_nop_out_encap() failed\n");
3016 			NOI_ERROR;
3017 		}
3018 		if ((unsigned)iscsi_sock_send_header_and_data(sess->sock,
3019 				nop_header, nop_out_args.length, ping_data,
3020 				nop_in.length, 0) != nop_in.length) {
3021 			iscsi_err(__FILE__, __LINE__,
3022 					"iscsi_sock_msg() failed\n");
3023 			NOI_ERROR;
3024 		}
3025 		iscsi_trace(TRACE_ISCSI_DEBUG,
3026 			"successfully sent %d byte ping response\n",
3027 			nop_in.length);
3028 	}
3029 	NOI_CLEANUP;
3030 	/* Check and update numbering  */
3031 	sess->ExpStatSN = nop_in.StatSN + 1;
3032 	/*
3033 	 * RETURN_NOT_EQUAL("StatSN", nop_in.StatSN, sess->ExpStatSN++,
3034 	 * NO_CLEANUP, -1);
3035 	 */
3036 	sess->CmdSN = nop_in.ExpCmdSN;
3037 	/*
3038 	 * RETURN_NOT_EQUAL("ExpCmdSN", nop_in.ExpCmdSN, sess->CmdSN,
3039 	 * NO_CLEANUP, -1);
3040 	 */
3041 	sess->MaxCmdSN = nop_in.MaxCmdSN;
3042 
3043 	/* Callback */
3044 
3045 	if (cmd) {
3046 		cmd->status = 0;
3047 		if (cmd->callback) {
3048 			iscsi_trace(TRACE_ISCSI_DEBUG, "NOP_OUT_T done (cmd status %d)\n", cmd->status);
3049 			if ((*cmd->callback)(cmd) != 0) {
3050 				iscsi_err(__FILE__, __LINE__, "callback() failed\n");
3051 				return -1;
3052 			}
3053 		} else {
3054 			iscsi_trace(TRACE_ISCSI_DEBUG, "no callback associated with NOP_IN_T??\n");
3055 			return -1;
3056 		}
3057 	}
3058 	return 0;
3059 }
3060 
3061 static int
3062 scsi_r2t_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3063 {
3064 	iscsi_r2t_t     r2t;
3065 	iscsi_scsi_cmd_args_t *scsi_cmd;
3066 	iscsi_write_data_t data;
3067 	uint32_t        bytes_sent;
3068 	uint32_t        DataSN;
3069 	struct iovec    sg_singleton;
3070 	struct iovec   *sg, *sg_copy, *sg_copy_orig, *sg_which;
3071 	int             sg_len, sg_len_copy, sg_len_which;
3072 	int             fragment_flag;
3073 
3074 	/* Make sure an initiator_cmd_t was specified, that it has a
3075 	 * callback function specified and that it also has a
3076 	 * iscsi_scsi_cmd_args_t associated with it.  */
3077 
3078 	if (cmd) {
3079 		if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3080 			iscsi_err(__FILE__, __LINE__, "no iscsi_scsi_cmd_args_t associated with this initiator_cmd_t??\n");
3081 			return -1;
3082 		} else if (cmd->callback == NULL) {
3083 			iscsi_err(__FILE__, __LINE__, "no callback associated with this initiator_cmd_t??\n");
3084 			return -1;
3085 		}
3086 	} else {
3087 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this iscsi_r2t_t??\n");
3088 		return -1;
3089 	}
3090 
3091 	sg = sg_copy = sg_copy_orig = sg_which = NULL;
3092 	sg_len = sg_len_copy = sg_len_which = 0;
3093 	if (iscsi_r2t_decap(header, &r2t) != 0) {
3094 		iscsi_err(__FILE__, __LINE__, "iscsi_r2t_decap() failed\n");
3095 		return -1;
3096 	}
3097 
3098 	/* Check args */
3099 	if (r2t.length == 0) {
3100 		iscsi_err(__FILE__, __LINE__, "Zero r2t.length\n");
3101 		NO_CLEANUP;
3102 		return -1;
3103 	}
3104 
3105 	/* Check and update numbering */
3106 #if 0
3107 	RETURN_NOT_EQUAL("StatSN", r2t.StatSN, sess->ExpStatSN, NO_CLEANUP, -1);
3108 	RETURN_NOT_EQUAL("ExpCmdSN", r2t.ExpCmdSN, sess->CmdSN, NO_CLEANUP, -1);
3109 #else
3110 	if (r2t.StatSN != sess->ExpStatSN) {
3111 		iscsi_err(__FILE__, __LINE__,
3112 				"r2t.StatSN %u, sess->ExpStatSN %u\n",
3113 				r2t.StatSN, sess->ExpStatSN);
3114 		NO_CLEANUP;
3115 		return -1;
3116 	}
3117 	if (r2t.ExpCmdSN != sess->CmdSN) {
3118 		iscsi_err(__FILE__, __LINE__,
3119 				"r2t.ExpCmdSN %u, sess->CmdSN %u\n",
3120 				r2t.ExpCmdSN, sess->CmdSN);
3121 		NO_CLEANUP;
3122 		return -1;
3123 	}
3124 #endif
3125 	sess->MaxCmdSN = r2t.MaxCmdSN;
3126 
3127 	/* Send back requested data */
3128 	iscsi_trace(TRACE_ISCSI_DEBUG,
3129 		"sending %d bytes R2T write data (offset %u)\n",
3130 		r2t.length, r2t.offset);
3131 	if (scsi_cmd->send_sg_len) {
3132 		sg = (struct iovec *)(void *)scsi_cmd->send_data;
3133 		sg_len = scsi_cmd->send_sg_len;
3134 	} else {
3135 		sg_singleton.iov_base = scsi_cmd->send_data;
3136 		sg_singleton.iov_len = scsi_cmd->trans_len;
3137 		sg = &sg_singleton;
3138 		sg_len = 1;
3139 	}
3140 	fragment_flag = 0;
3141 	bytes_sent = 0;
3142 	DataSN = 0;
3143 #define FF_CLEANUP {if (fragment_flag) iscsi_free_atomic(sg_copy_orig);}
3144 	do {
3145 		(void) memset(&data, 0x0, sizeof(data));
3146 		if (sess->sess_params.max_dataseg_len) {
3147 			data.length = MIN(sess->sess_params.max_dataseg_len,
3148 					  r2t.length - bytes_sent);
3149 		} else {
3150 			data.length = r2t.length - bytes_sent;
3151 		}
3152 		if (bytes_sent + data.length == r2t.length) {
3153 			data.final = 1;
3154 		}
3155 		data.tag = r2t.tag;
3156 		data.transfer_tag = r2t.transfer_tag;
3157 		data.ExpStatSN = sess->ExpStatSN;
3158 		data.DataSN = DataSN++;
3159 		data.offset = r2t.offset + bytes_sent;
3160 		data.lun = scsi_cmd->lun;
3161 		if (iscsi_write_data_encap(header, &data) != 0) {
3162 			iscsi_err(__FILE__, __LINE__, "iscsi_write_data_encap() failed\n");
3163 			FF_CLEANUP;
3164 			return -1;
3165 		}
3166 		if ((data.length < r2t.length) || (r2t.offset)) {
3167 			if (data.length < r2t.length) {
3168 				iscsi_trace(TRACE_ISCSI_DEBUG, "R2T data is being fragmented: sending %u bytes of %u requested\n",
3169 				      data.length, r2t.length);
3170 			} else {
3171 				iscsi_trace(TRACE_ISCSI_DEBUG, "R2T data starts at offset %u, desired length %u\n",
3172 				      r2t.offset, r2t.length);
3173 			}
3174 
3175 			/* Allocate space for a copy of the original iovec */
3176 
3177 			if (!fragment_flag) {
3178 				if ((sg_copy_orig = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
3179 					iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
3180 					return -1;
3181 				}
3182 				fragment_flag++;
3183 			}
3184 			/*
3185 			 * Copy and modify original iovec with new offset and
3186 			 * length
3187 			 */
3188 
3189 			iscsi_trace(TRACE_ISCSI_DEBUG, "modifying original iovec with offset %u length %u\n",
3190 			      r2t.offset + bytes_sent, data.length);
3191 			sg_copy = sg_copy_orig;
3192 			sg_len_copy = sg_len;
3193 			memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
3194 			if (modify_iov(&sg_copy, &sg_len_copy, r2t.offset + bytes_sent, data.length) != 0) {
3195 				iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
3196 				FF_CLEANUP;
3197 				return -1;
3198 			}
3199 			sg_which = sg_copy;
3200 			sg_len_which = sg_len_copy;
3201 		} else {
3202 			iscsi_trace(TRACE_ISCSI_DEBUG, "using original iovec for R2T transfer (offset %u, length %u)\n",
3203 			      r2t.offset, r2t.length);
3204 			sg_which = sg;
3205 			sg_len_which = sg_len;
3206 		}
3207 		iscsi_trace(TRACE_ISCSI_DEBUG, "sending R2T write data PDU (offset %u, len %u, sg_len %u)\n",
3208 		      data.offset, data.length, sg_len_which);
3209 		if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_which, data.length, sg_len_which)
3210 		    != ISCSI_HEADER_LEN + data.length) {
3211 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
3212 			FF_CLEANUP;
3213 			return -1;
3214 		}
3215 		iscsi_trace(TRACE_ISCSI_DEBUG, "sent write data PDU OK (offset %u, len %u)\n", data.offset, data.length);
3216 		bytes_sent += data.length;
3217 		scsi_cmd->bytes_sent += data.length;
3218 	} while (bytes_sent < r2t.length);
3219 	FF_CLEANUP;
3220 	if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
3221 		iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
3222 		return -1;
3223 	}
3224 	return 0;
3225 }
3226 
3227 static int
3228 scsi_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3229 {
3230 	iscsi_scsi_cmd_args_t	*scsi_cmd;
3231 	iscsi_scsi_rsp_t	 scsi_rsp;
3232 	const char		*errmsg;
3233 
3234 	/* Make sure an initiator_cmd_t was specified, that it has a
3235 	 * callback function specified and that it also has a
3236 	 * iscsi_scsi_cmd_args_t associated with it.  */
3237 
3238 	if (cmd) {
3239 		if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3240 			iscsi_err(__FILE__, __LINE__, "no iscsi_scsi_cmd_args_t associated with this initiator_cmd_t??\n");
3241 			return -1;
3242 		} else if (cmd->callback == NULL) {
3243 			iscsi_err(__FILE__, __LINE__, "no callback associated with this initiator_cmd_t??\n");
3244 			return -1;
3245 		}
3246 	} else {
3247 		iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this iscsi_scsi_rsp_t??\n");
3248 		return -1;
3249 	}
3250 
3251 	/*
3252 	 * Read SCSI response and check return args.  Those marked
3253 	 * "FIX ME" are not yet implemented.  */
3254 
3255 	if (iscsi_scsi_rsp_decap(header, &scsi_rsp) != 0) {
3256 		iscsi_err(__FILE__, __LINE__, "iscsi_scsi_rsp_decap() failed\n");
3257 		return -1;
3258 	}
3259 #if 0
3260 	RETURN_NOT_EQUAL("o bit (FIX ME)", scsi_rsp.bidi_overflow, 0, NO_CLEANUP, -1);
3261 	RETURN_NOT_EQUAL("u bit (FIX ME)", scsi_rsp.bidi_underflow, 0, NO_CLEANUP, -1);
3262 	RETURN_NOT_EQUAL("O bit (FIX ME)", scsi_rsp.overflow, 0, NO_CLEANUP, -1);
3263 	RETURN_NOT_EQUAL("iSCSI Response (FIX ME)", scsi_rsp.response, 0, NO_CLEANUP, -1);
3264 	RETURN_NOT_EQUAL("Tag", scsi_rsp.tag, scsi_cmd->tag, NO_CLEANUP, -1);
3265 	RETURN_NOT_EQUAL("Bidi Residual Count", scsi_rsp.bidi_res_cnt, 0, NO_CLEANUP, -1);
3266 	RETURN_NOT_EQUAL("StatSN", scsi_rsp.StatSN, sess->ExpStatSN, NO_CLEANUP, -1);
3267 #else
3268 	errmsg = NULL;
3269 	if (scsi_rsp.bidi_overflow != 0) {
3270 		errmsg = "o bit (FIX ME)\n";
3271 	} else if (scsi_rsp.bidi_underflow != 0) {
3272 		errmsg = "u bit (FIX ME)\n";
3273 	} else if (scsi_rsp.overflow != 0) {
3274 		errmsg = "O bit (FIX ME)\n";
3275 	} else if (scsi_rsp.response != 0) {
3276 		errmsg = "Response (FIX ME)\n";
3277 	} else if (scsi_rsp.tag != scsi_cmd->tag) {
3278 		errmsg = "Tags don't match\n";
3279 	} else if (scsi_rsp.bidi_res_cnt != 0) {
3280 		errmsg = "Bidi Residual Count";
3281 	} else if (scsi_rsp.StatSN != sess->ExpStatSN) {
3282 		errmsg = "StatSN";
3283 	}
3284 	if (errmsg) {
3285 		iscsi_err(__FILE__, __LINE__, errmsg);
3286 		NO_CLEANUP;
3287 		return -1;
3288 	}
3289 #endif
3290 	sess->ExpStatSN = scsi_rsp.StatSN + 1;
3291 
3292 	if (sess->sess_params.max_dataseg_len &&
3293 	    scsi_rsp.length > sess->sess_params.max_dataseg_len) {
3294 		iscsi_err(__FILE__, __LINE__,
3295 			"scsi_rsp.length %u\n", scsi_rsp.length);
3296 		NO_CLEANUP;
3297 		return -1;
3298 	}
3299 	if ((scsi_rsp.status == 0) && (scsi_rsp.length != 0)) {
3300 		iscsi_err(__FILE__, __LINE__,
3301 			"Unexpected DataSegmentLength %u "
3302 			"with GOOD SCSI status\n", scsi_rsp.length);
3303 		return -1;
3304 	}
3305 	/*
3306 	 * Make sure all data was successfully transferred if command
3307 	 * completed successfully, otherwise read sense data.  */
3308 
3309 	if (scsi_rsp.status == 0) {
3310 		if (scsi_cmd->output) {
3311 #if 0
3312 			RETURN_NOT_EQUAL("scsi_cmd->bytes_sent", scsi_cmd->bytes_sent, scsi_cmd->trans_len, NO_CLEANUP, -1);
3313 #else
3314 			if (scsi_cmd->bytes_sent != scsi_cmd->trans_len) {
3315 				iscsi_err(__FILE__, __LINE__,
3316 					"scsi_cmd->bytes_sent\n");
3317 				NO_CLEANUP;
3318 				return -1;
3319 			}
3320 #endif
3321 			if (scsi_cmd->input) {
3322 
3323 #if 0
3324 				RETURN_NOT_EQUAL("scsi_cmd->bytes_recv", scsi_cmd->bytes_recv, scsi_cmd->bidi_trans_len, NO_CLEANUP, -1);
3325 #else
3326 				if (scsi_cmd->bytes_recv != scsi_cmd->bidi_trans_len) {
3327 					iscsi_err(__FILE__, __LINE__,
3328 						"scsi_cmd->bytes_recv\n");
3329 					NO_CLEANUP;
3330 					return -1;
3331 				}
3332 #endif
3333 			}
3334 		} else if (scsi_cmd->input) {
3335 
3336 
3337 		}
3338 	} else if (scsi_rsp.length) {
3339 		uint8_t  *sense_data = NULL;
3340 
3341 		if ((sense_data = iscsi_malloc(scsi_rsp.length)) == NULL) {
3342 			iscsi_err(__FILE__, __LINE__,
3343 				"iscsi_malloc() failed\n");
3344 			return -1;
3345 		}
3346 		iscsi_err(__FILE__, __LINE__,
3347 			"reading %d bytes sense data (recv_sg_len %u)\n",
3348 			scsi_rsp.length, scsi_cmd->recv_sg_len);
3349 		if ((unsigned)iscsi_sock_msg(sess->sock, 0, scsi_rsp.length,
3350 				sense_data, 0) != scsi_rsp.length) {
3351 			iscsi_err(__FILE__, __LINE__,
3352 				"iscsi_sock_msg() failed\n");
3353 			if (sense_data != NULL) {
3354 				iscsi_free(sense_data);
3355 			}
3356 			return -1;
3357 		}
3358 		iscsi_err(__FILE__, __LINE__,
3359 			"read %d bytes sense data ok (currently discarding)\n",
3360 			scsi_rsp.length);
3361 		if (sense_data != NULL) {
3362 			iscsi_free(sense_data);
3363 		}
3364 	} else {
3365 		iscsi_trace(TRACE_ISCSI_DEBUG, "no sense data available\n");
3366 	}
3367 
3368 	/* Check and update numbering  */
3369 
3370 	/*
3371 	 * RETURN_NOT_EQUAL("ExpCmdSN", scsi_rsp.ExpCmdSN, sess->CmdSN,
3372 	 * NO_CLEANUP, -1);
3373 	 */
3374 	sess->MaxCmdSN = scsi_rsp.MaxCmdSN;
3375 
3376 	/* Set initiator_cmd_t status, iscsi_scsi_cmd_args_t status  */
3377 	/* and execute callback function */
3378 
3379 	cmd->status = 0;
3380 	scsi_cmd->status = scsi_rsp.status;
3381 	iscsi_trace(TRACE_ISCSI_DEBUG,
3382 		"iscsi_scsi_cmd_args_t done (cmd status %d, iscsi status %d, "
3383 		"scsi status %d)\n",
3384 		cmd->status, scsi_rsp.response, scsi_rsp.status);
3385 	if ((*cmd->callback)(cmd) != 0) {
3386 		iscsi_err(__FILE__, __LINE__, "callback() failed\n");
3387 		return -1;
3388 
3389 	}
3390 	return 0;
3391 }
3392 
3393 static int
3394 scsi_read_data_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3395 {
3396 	iscsi_scsi_cmd_args_t	*scsi_cmd;
3397 	iscsi_read_data_t	 data;
3398 	const char 		*errmsg;
3399 	int			 rc;
3400 
3401 	iscsi_trace(TRACE_ISCSI_DEBUG, "processing read data\n");
3402 
3403 	/* Make sure an initiator_cmd_t was specified, that it has a
3404 	 * callback function specified and that it also has a
3405 	 * iscsi_scsi_cmd_args_t associated with it.  */
3406 
3407 	if (cmd) {
3408 		if (cmd->type != ISCSI_SCSI_CMD) {
3409 			iscsi_err(__FILE__, __LINE__,
3410 				"Invalid response from target for cmd "
3411 				"type (%#x)\n", cmd->type);
3412 			cmd->status = -1;
3413 			if (cmd->callback) {
3414 				(*cmd->callback)(cmd);
3415 			}
3416 			return -1;
3417 		}
3418 		if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3419 			iscsi_err(__FILE__, __LINE__,
3420 				"no iscsi_scsi_cmd_args_t associated with "
3421 				"this initiator_cmd_t??\n");
3422 			return -1;
3423 		} else if (cmd->callback == NULL) {
3424 			iscsi_err(__FILE__, __LINE__,
3425 				"no callback associated with this "
3426 				"initiator_cmd_t??\n");
3427 			return -1;
3428 		}
3429 	} else {
3430 		iscsi_err(__FILE__, __LINE__,
3431 			"no initiator_cmd_t associated with this "
3432 			"iscsi_read_data_t??\n");
3433 		return -1;
3434 	}
3435 	if (iscsi_read_data_decap(header, &data) != 0) {
3436 		iscsi_err(__FILE__, __LINE__,
3437 			"iscsi_scsi_rsp_decap() failed\n");
3438 		return -1;
3439 	}
3440 
3441 	/* Check args */
3442 #if 0
3443 	RETURN_NOT_EQUAL("Overflow bit", data.overflow, 0, NO_CLEANUP, -1);
3444 	RETURN_NOT_EQUAL("Underflow bit", data.underflow, 0, NO_CLEANUP, -1);
3445 	RETURN_NOT_EQUAL("Tag", data.task_tag, scsi_cmd->tag, NO_CLEANUP, -1);
3446 	RETURN_NOT_EQUAL("Residual Count", data.res_count, 0, NO_CLEANUP, -1);
3447 #else
3448 	errmsg = NULL;
3449 	if (data.overflow != 0) {
3450 		errmsg = "Overflow bit";
3451 	} else if (data.task_tag != scsi_cmd->tag) {
3452 		errmsg = "Tag";
3453 	} else if (!data.underflow) {
3454 		if (data.res_count != 0) {
3455 			errmsg = "Residual Count";
3456 		}
3457 	} else {
3458 		iscsi_warn(__FILE__, __LINE__, "Underflow %s\n", data.res_count);
3459 	}
3460 	if (errmsg) {
3461 		iscsi_err(__FILE__, __LINE__, errmsg);
3462 		NO_CLEANUP;
3463 		return -1;
3464 	}
3465 #endif
3466 
3467 	if (sess->sess_params.max_dataseg_len) {
3468 		if (data.length > sess->sess_params.max_dataseg_len) {
3469 			iscsi_err(__FILE__, __LINE__,
3470 				"data.length %u\n", data.length);
3471 			NO_CLEANUP;
3472 			return -1;
3473 		}
3474 	}
3475 
3476 	/* Check and update numbering  */
3477 	if (data.ExpCmdSN != sess->CmdSN) {
3478 		iscsi_warn(__FILE__, __LINE__,
3479 			"Bad \"ExpCmdSN\": Got %u expected %u.\n",
3480 			data.ExpCmdSN, sess->CmdSN);
3481 	}
3482 	sess->MaxCmdSN = data.MaxCmdSN;
3483 
3484 	/* Need to optimize this section */
3485 
3486 	if (scsi_cmd->recv_sg_len) {
3487 		int             sg_len = scsi_cmd->recv_sg_len;
3488 		struct iovec   *sg;
3489 		struct iovec   *sg_orig = NULL;
3490 		char		*sgp;
3491 		uint32_t        total_len, disp;
3492 		int             i;
3493 
3494 		if (data.length != scsi_cmd->trans_len) {
3495 
3496 			/* Make a copy of the iovec */
3497 
3498 			sg_orig = sg = iscsi_malloc_atomic(sizeof(struct iovec)
3499 					* sg_len);
3500 			if (sg_orig == NULL) {
3501 				iscsi_err(__FILE__, __LINE__,
3502 					"iscsi_malloc_atomic() failed\n");
3503 				return -1;
3504 
3505 			}
3506 			(void) memcpy(sg, scsi_cmd->recv_data,
3507 					sizeof(struct iovec) * sg_len);
3508 
3509 			/* Find offset in iovecs */
3510 			total_len = 0;
3511 			disp = data.offset;
3512 			for (i = 0; i < sg_len; i++) {
3513 				total_len += sg[i].iov_len;
3514 				if (total_len > data.offset) {
3515 					break;
3516 				}
3517 				disp -= sg[i].iov_len;
3518 			}
3519 			sg[i].iov_len -= disp;
3520 			sgp = sg[i].iov_base;
3521 			sgp += disp;
3522 			sg[i].iov_base = sgp;
3523 			sg_len -= i;
3524 			sg = &sg[i];
3525 
3526 			/* Find last iovec needed for read */
3527 
3528 			total_len = 0;
3529 			for (i = 0; i < sg_len; i++) {
3530 				total_len += sg[i].iov_len;
3531 				if (total_len >= data.length) {
3532 					break;
3533 				}
3534 			}
3535 			sg[i].iov_len -= (total_len - data.length);
3536 			sg_len = i + 1;
3537 		} else {
3538 			sg = (struct iovec *)(void *)scsi_cmd->recv_data;
3539 		}
3540 		iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes into sg buffer (total offset %u)\n", data.length, data.offset);
3541 		if ((rc = iscsi_sock_msg(sess->sock, 0, data.length, (uint8_t *)(void *) sg, sg_len)) != (int)data.length) {
3542 			iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed: got %u, expected %u\n", rc, data.length);
3543 			if (sg_orig)
3544 				iscsi_free_atomic(sg_orig);
3545 			return -1;
3546 		}
3547 		scsi_cmd->bytes_recv += data.length;
3548 		if (sg_orig)
3549 			iscsi_free_atomic(sg_orig);
3550 	} else {
3551 		if (data.length) {
3552 			iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes into dest buffer (offset %u)\n", data.length, data.offset);
3553 			if (iscsi_sock_msg(sess->sock, 0, data.length, scsi_cmd->recv_data + data.offset, 0) != (int)data.length) {
3554 				iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
3555 				return -1;
3556 			}
3557 			scsi_cmd->bytes_recv += data.length;
3558 		}
3559 	}
3560 
3561 
3562 	/* Check for status */
3563 
3564 	if (data.S_bit) {
3565 		iscsi_trace(TRACE_ISCSI_DEBUG,
3566 				"received status with final PDU\n");
3567 #if 0
3568 		RETURN_NOT_EQUAL("Final Bit", data.final, 1, NO_CLEANUP, -1);
3569 		RETURN_NOT_EQUAL("StatSN", data.StatSN, sess->ExpStatSN++, NO_CLEANUP, -1);
3570 		/* XXX - agc - increment in macro !!! */
3571 #else
3572 		if (data.final != 1) {
3573 			iscsi_err(__FILE__, __LINE__, "Final Bit");
3574 			NO_CLEANUP;
3575 			return -1;
3576 		}
3577 		if (data.StatSN != sess->ExpStatSN++) {
3578 			iscsi_err(__FILE__, __LINE__, "StatSN");
3579 			NO_CLEANUP;
3580 			return -1;
3581 		}
3582 #endif
3583 		scsi_cmd->status = data.status = 0;
3584 		cmd->status = 0;
3585 		iscsi_trace(TRACE_ISCSI_DEBUG,
3586 			"scsi op %#x done (tag %u, status %d)\n",
3587 			scsi_cmd->cdb[0], scsi_cmd->tag, scsi_cmd->status);
3588 		if ((*cmd->callback)(cmd) != 0) {
3589 			iscsi_err(__FILE__, __LINE__,
3590 				"callback() failed\n");
3591 			return -1;
3592 		}
3593 	} else {
3594 		if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
3595 			iscsi_err(__FILE__, __LINE__,
3596 				"hash_insert() failed\n");
3597 			return -1;
3598 		}
3599 	}
3600 	iscsi_trace(TRACE_ISCSI_DEBUG, "read data processed\n");
3601 	return 0;
3602 }
3603 
3604 int
3605 iscsi_initiator_info(char *ptr, int size, int len)
3606 {
3607 	initiator_session_t	*sess;
3608 	int			 i;
3609 
3610 	ptr[0] = 0x0;
3611 	len += snprintf(ptr, (size_t)(size - len),
3612 		"  %3s %30s %25s\n\n", "TID", "TargetName", "TargetAddress");
3613 	for (i = 0; i < CONFIG_INITIATOR_NUM_TARGETS; i++) {
3614 		len += snprintf(ptr + len, (size_t)(size - len),
3615 				"  %3i %30s %20s:%d (",
3616 				i, g_target[i].TargetName,
3617 				g_target[i].ip, g_target[i].port);
3618 		if (g_target[i].has_session) {
3619 			sess = g_target[i].sess;
3620 			if (sess->state & INITIATOR_SESSION_STATE_INITIALIZING)
3621 				len += snprintf(ptr + len,
3622 						(size_t)(size - len), "%s",
3623 						"initializing");
3624 			if (sess->state & INITIATOR_SESSION_STATE_INITIALIZED)
3625 				len += snprintf(ptr + len,
3626 						(size_t)(size - len), "%s",
3627 						"initialized");
3628 			if (sess->state & INITIATOR_SESSION_STATE_CONNECTING)
3629 				len += snprintf(ptr + len,
3630 						(size_t)(size - len),
3631 						"%s", "connecting");
3632 			if (sess->state & INITIATOR_SESSION_STATE_CONNECTED)
3633 				len += snprintf(ptr + len,
3634 						(size_t)(size - len), "%s",
3635 						"connected");
3636 			if (sess->state & INITIATOR_SESSION_STATE_LOGGING_IN)
3637 				len += snprintf(ptr + len,
3638 						(size_t)(size - len), "%s",
3639 						"logging in");
3640 			if (sess->state &
3641 				INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL)
3642 				len += snprintf(ptr + len,
3643 						(size_t)(size - len), "%s",
3644 						"Normal session");
3645 			if (sess->state &
3646 				INITIATOR_SESSION_STATE_LOGGED_IN_DISCOVERY)
3647 				len += snprintf(ptr + len,
3648 						(size_t)(size - len), "%s",
3649 						"Discovery session");
3650 			if (sess->state & INITIATOR_SESSION_STATE_LOGGING_OUT)
3651 				len += snprintf(ptr + len,
3652 						(size_t)(size - len), "%s",
3653 						"logging out");
3654 			if (sess->state & INITIATOR_SESSION_STATE_LOGGED_OUT)
3655 				len += snprintf(ptr + len,
3656 						(size_t)(size - len), "%s",
3657 						"logged out");
3658 			if (sess->state & INITIATOR_SESSION_STATE_DESTROYING)
3659 				len += snprintf(ptr + len,
3660 						(size_t)(size - len), "%s",
3661 						"destroying");
3662 			if (sess->tx_worker.state & ISCSI_WORKER_STATE_ERROR)
3663 				len += snprintf(ptr + len,
3664 						(size_t)(size - len), "%s",
3665 						" **Tx Error** ");
3666 			if (sess->rx_worker.state & ISCSI_WORKER_STATE_ERROR)
3667 				len += snprintf(ptr + len,
3668 						(size_t)(size - len), "%s",
3669 						" **Rx Error** ");
3670 		} else {
3671 			len += snprintf(ptr + len, (size_t)(size - len), "%s",
3672 					"No Session");
3673 		}
3674 		len += snprintf(ptr + len, (size_t)(size - len), ")\n");
3675 	}
3676 	return len;
3677 }
3678 
3679 int
3680 iscsi_initiator_discover(char *host, uint64_t target, int lun)
3681 {
3682 	iscsi_nop_out_args_t	discover_cmd;
3683 	initiator_cmd_t		cmd;
3684 
3685 	cmd.type = ISCSI_NOP_OUT;
3686 	cmd.ptr = &discover_cmd;
3687 	cmd.isid = target;
3688 	(void) strlcpy(cmd.targetname, host, sizeof(cmd.targetname));
3689 	(void) memset(&discover_cmd, 0x0, sizeof(iscsi_nop_out_args_t));
3690 	discover_cmd.length = 1;
3691 	discover_cmd.data = (const uint8_t *) "";
3692 	discover_cmd.lun = lun;
3693 	discover_cmd.tag = 0xffffffff;
3694 	if (initiator_command(&cmd) != 0) {
3695 		iscsi_err(__FILE__, __LINE__, "initiator_command() failed\n");
3696 		return -1;
3697 	}
3698 	return 0;
3699 }
3700 
3701 void
3702 get_target_info(uint64_t target, initiator_target_t *ip)
3703 {
3704 	(void) memcpy(ip, &g_target[(int)target], sizeof(*ip));
3705 }
3706 
3707 int
3708 ii_initiator_init(const char *hostname, int port, int address_family, const char *user, char *lun, int auth_type, int mutual_auth, int digest_type)
3709 {
3710 	initiator_session_t *sess = NULL;
3711 
3712 #define INIT_CLEANUP {if (sess != NULL) iscsi_free_atomic(sess);}
3713 #define INIT_ERROR {INIT_CLEANUP; return -1;}
3714 
3715 	USE_ARG(address_family);
3716 	iscsi_trace(TRACE_ISCSI_DEBUG, "initializing initiator\n");
3717 	iscsi_trace(TRACE_ISCSI_DEBUG, "target config filename to read from:%s\n", gfilename);
3718 	if (get_target_config(hostname, port) != 0) {
3719 		iscsi_err(__FILE__, __LINE__, "Error getting target configuration from config file\n");
3720 		return -1;
3721 	}
3722 	(void) strlcpy(g_target[0].iqnwanted, lun, sizeof(g_target[0].iqnwanted));
3723 	g_initiator_state = 0;
3724 	if (iscsi_queue_init(&g_session_q, CONFIG_INITIATOR_MAX_SESSIONS) != 0) {
3725 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3726 		return -1;
3727 	}
3728 	if ((sess = iscsi_malloc_atomic(sizeof(initiator_session_t))) == NULL) {
3729 		iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
3730 		return -1;
3731 	}
3732 	if (iscsi_queue_insert(&g_session_q, sess) != 0) {
3733 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3734 		INIT_CLEANUP;
3735 		return -1;
3736 	}
3737 	if (user)
3738 		sess->sess_params.cred.user = strdup(user);
3739 	else
3740 		sess->sess_params.cred.user = NULL;
3741 
3742 	sess->sess_params.auth_type = auth_type;
3743 	sess->sess_params.mutual_auth = mutual_auth;
3744 	sess->sess_params.digest_wanted = digest_type;
3745 	iscsi_trace(TRACE_ISCSI_DEBUG, "%d free sessions available\n",
3746 			CONFIG_INITIATOR_MAX_SESSIONS);
3747 
3748 	g_tag = 0xabc123;
3749 	if (hash_init(&g_tag_hash, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
3750 		iscsi_err(__FILE__, __LINE__, "hash_init() failed\n");
3751 		INIT_CLEANUP;
3752 		return -1;
3753 	}
3754 	iscsi_spin_init(&g_tag_spin);
3755 	iscsi_trace(TRACE_ISCSI_DEBUG,
3756 		"tag hash table initialized with queue depth %d\n",
3757 		CONFIG_INITIATOR_QUEUE_DEPTH);
3758 
3759 	/*
3760 	 * Start enqueue worker.  This thread accepts scsi commands
3761 	 * from initiator_enqueue() and queues them onto one of the tx
3762 	 * worker queues.
3763 	 */
3764 	iscsi_trace(TRACE_ISCSI_DEBUG, "starting enqueue worker\n");
3765 	if (iscsi_queue_init(&g_enqueue_q, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
3766 		iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3767 		INIT_CLEANUP;
3768 		return -1;
3769 	}
3770 	iscsi_trace(TRACE_ISCSI_DEBUG, "about to initialize mutex\n");
3771 	ISCSI_MUTEX_INIT(&g_enqueue_worker.work_mutex, INIT_ERROR);
3772 	ISCSI_COND_INIT(&g_enqueue_worker.work_cond, INIT_ERROR);
3773 	ISCSI_MUTEX_INIT(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3774 	ISCSI_COND_INIT(&g_enqueue_worker.exit_cond, INIT_ERROR);
3775 	ISCSI_LOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3776 
3777 	iscsi_trace(TRACE_ISCSI_DEBUG, "spawning thread for enqueue worker\n");
3778 	if (iscsi_thread_create(&g_enqueue_worker.thread,
3779 		(void *) &enqueue_worker_proc, &g_enqueue_worker) != 0) {
3780 		iscsi_err(__FILE__, __LINE__,
3781 				"iscsi_threads_create() failed\n");
3782 		INIT_CLEANUP;
3783 		return -1;
3784 	}
3785 	iscsi_trace(TRACE_ISCSI_DEBUG, "thread spawned, waiting for signal\n");
3786 	ISCSI_WAIT(&g_enqueue_worker.exit_cond, &g_enqueue_worker.exit_mutex,
3787 			INIT_ERROR);
3788 	ISCSI_UNLOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3789 	iscsi_trace(TRACE_ISCSI_DEBUG, "successfully started enqueue worker\n");
3790 
3791 	iscsi_trace(TRACE_ISCSI_DEBUG, "initiator initialization complete\n");
3792 	return 0;
3793 }
3794 
3795 int
3796 iscsi_initiator_set_defaults(iscsi_initiator_t *ini)
3797 {
3798 	char	buf[32];
3799 
3800 	/* set defaults */
3801 	(void) memset(ini, 0x0, sizeof(*ini));
3802 	iscsi_initiator_setvar(ini, "address family", "unspec");
3803 	iscsi_initiator_setvar(ini, "digest type", "none");
3804 	iscsi_initiator_setvar(ini, "auth type", "none");
3805 	iscsi_initiator_setvar(ini, "mutual auth", "none");
3806 	iscsi_initiator_setvar(ini, "target hostname", "localhost");
3807 	(void) snprintf(buf, sizeof(buf), "%d", ISCSI_PORT);
3808 	iscsi_initiator_setvar(ini, "target port", buf);
3809 	return 1;
3810 }
3811 
3812 /* check there's enough space in the arrays */
3813 static void
3814 size_arrays(iscsi_initiator_t *ini, unsigned needed)
3815 {
3816 	if (ini->size == 0) {
3817 		/* only get here first time around */
3818 		ini->size = needed;
3819 		ini->name = calloc(sizeof(char *), needed);
3820 		ini->value = calloc(sizeof(char *), needed);
3821 	} else if (ini->c == ini->size) {
3822 		/* only uses 'needed' when filled array */
3823 		ini->size += needed;
3824 		ini->name = realloc(ini->name, sizeof(char *) * needed);
3825 		ini->value = realloc(ini->value, sizeof(char *) * needed);
3826 	}
3827 }
3828 
3829 /* find the name in the array */
3830 static int
3831 findvar(iscsi_initiator_t *ini, const char *name)
3832 {
3833 	unsigned	i;
3834 
3835 	for (i = 0 ; i < ini->c && strcmp(ini->name[i], name) != 0; i++) {
3836 	}
3837 	return (i == ini->c) ? -1 : (int)i;
3838 }
3839 
3840 /* set a variable */
3841 int
3842 iscsi_initiator_setvar(iscsi_initiator_t *ini, const char *name,
3843 			const char *value)
3844 {
3845 	int	i;
3846 
3847 	if ((i = findvar(ini, name)) < 0) {
3848 		/* add the element to the array */
3849 		size_arrays(ini, ini->size + 15);
3850 		ini->name[i = ini->c++] = strdup(name);
3851 	} else {
3852 		/* replace the element in the array */
3853 		if (ini->value[i]) {
3854 			(void) free(ini->value[i]);
3855 			ini->value[i] = NULL;
3856 		}
3857 	}
3858 	/* sanity checks for range of values would go here */
3859 	ini->value[i] = strdup(value);
3860 	return 1;
3861 }
3862 
3863 /* get a variable's value (NULL if not set) */
3864 char *
3865 iscsi_initiator_getvar(iscsi_initiator_t *ini, const char *name)
3866 {
3867 	int	i;
3868 
3869 	return ((i = findvar(ini, name)) < 0) ? NULL : ini->value[i];
3870 }
3871