No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / iscsi / dist / src / lib / initiator.c
blob5dbd4f96dff4dbdef2c086fd83570d16b46d6ad0
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.
7 * Intel License Agreement
9 * Copyright (c) 2000, Intel Corporation
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * -Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
41 #include "config.h"
43 #include <sys/types.h>
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
49 #ifdef HAVE_SYS_SOCKET_H
50 #include <sys/socket.h>
51 #endif
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
57 #ifdef HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
59 #endif
61 #ifdef HAVE_SIGNAL_H
62 #include <signal.h>
63 #endif
65 #include <stdio.h>
66 #include <stdlib.h>
68 #ifdef HAVE_STRING_H
69 #include <string.h>
70 #endif
72 #include <unistd.h>
74 #include "iscsiprotocol.h"
75 #include "initiator.h"
77 #include "iscsi.h"
79 static initiator_target_t g_target[CONFIG_INITIATOR_NUM_TARGETS];
82 * Globals
84 static uint32_t g_tag;
85 static iscsi_spin_t g_tag_spin;
86 static hash_t g_tag_hash;
87 static iscsi_worker_t g_enqueue_worker;
88 static iscsi_queue_t g_enqueue_q;
89 static iscsi_queue_t g_session_q;
90 static int g_initiator_state;
91 static char *gfilename;
93 /* Testing of initiator_abort */
95 static initiator_cmd_t *g_cmd = NULL;
98 * Enqueue worker functions. The enqueue worker is responsible for enqueing
99 * all iSCSI commands to one of the Tx workers. It is also the thread
100 * responsible for initializing sessions, discovering targets and getting
101 * each session into full feature phase.
104 static int enqueue_worker_proc(void *);
105 static int login_phase_i(initiator_session_t *, char *, int);
106 static int logout_phase_i(initiator_session_t *);
109 * Tx functions. initiator_cmd_t pointers are enqueued to the Tx worker
110 * for a given session by the enqueue worker. The Tx worker will send out these
111 * commands and wait for the Rx worker to process the response. The pointer is
112 * inserted into the hashtable g_tag_hash, keyed by the initiator tag of the iSCSI
113 * commands.
116 static int tx_worker_proc_i(void *);
117 static int text_command_i(initiator_cmd_t *);
118 static int login_command_i(initiator_cmd_t *);
119 static int logout_command_i(initiator_cmd_t *);
120 static int scsi_command_i(initiator_cmd_t *);
121 static int nop_out_i(initiator_cmd_t *);
125 * Rx functions. Upon receipt of an incoming PDU, the Rx worker will first
126 * extract the tag (if it exists for the PDU) and then the associated
127 * initiator_cmd_t pointer stored in the hash table. One of Rx functions
128 * will be called to processs the PDU. The Rx worker will invoke the callback
129 * function associated with the command once the command has been retired.
132 static int rx_worker_proc_i(void *);
133 static int login_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
134 static int text_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
135 static int logout_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
136 static int scsi_response_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
137 static int scsi_read_data_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
138 static int scsi_r2t_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
139 static int nop_in_i(initiator_session_t *, initiator_cmd_t *, uint8_t *);
140 static int reject_i(initiator_session_t *, uint8_t *);
141 static int async_msg_i(initiator_session_t *, uint8_t *);
145 * Misc. Prototypes
149 static int session_init_i(initiator_session_t **, uint64_t );
150 static int session_destroy_i(initiator_session_t *);
151 static int wait_callback_i(void *);
152 static int discovery_phase(int, strv_t *);
156 * Private Functions
159 #if 0
160 static void
161 dump_session(initiator_session_t * sess)
163 iscsi_parameter_value_t *vp;
164 iscsi_parameter_t *ip;
166 for (ip = sess->params ; ip ; ip = ip->next) {
167 printf("Key: %s Type: %d\n",ip->key,ip->type);
168 for (vp = ip->value_l ; vp ; vp = vp->next) {
169 printf("Value: %s\n",vp->value);
173 #endif
175 /* This function reads the target IP and target name information */
176 /* from the input configuration file, and populates the */
177 /* g_target data structure fields. */
178 static int
179 get_target_config(const char *hostname, int port)
181 int i;
183 for (i = 0 ; i < CONFIG_INITIATOR_NUM_TARGETS ; i++) {
184 (void) strlcpy(g_target[i].name, hostname,
185 sizeof(g_target[i].name));
186 g_target[i].port = port;
188 return 0;
191 static int
192 session_init_i(initiator_session_t ** sess, uint64_t isid)
194 initiator_session_t *s;
195 iscsi_parameter_t **l;
196 char *user;
197 int auth_type;
198 int mutual_auth;
199 int one = 1;
201 iscsi_trace(TRACE_ISCSI_DEBUG, "initializing session %llu\n", isid);
203 /* Get free session */
204 if ((*sess = iscsi_queue_remove(&g_session_q)) == NULL) {
205 iscsi_err(__FILE__, __LINE__, "iscsi_queue_remove() failed\n");
206 return -1;
208 s = *sess;
209 user = NULL;
210 if (s->sess_params.cred.user) {
211 user = s->sess_params.cred.user;
213 auth_type = s->sess_params.auth_type;
214 mutual_auth = s->sess_params.mutual_auth;
215 (void) memset(s, 0x0, sizeof(*s));
216 s->state = INITIATOR_SESSION_STATE_INITIALIZING;
217 s->isid = s->tx_worker.id = s->rx_worker.id = (int)isid;
218 s->cmds = NULL;
219 s->sess_params.cred.user = user;
220 s->sess_params.auth_type = auth_type;
221 s->sess_params.mutual_auth = mutual_auth;
223 iscsi_spin_init(&s->cmds_spin);
224 g_target[(int)isid].has_session = 1;
226 /* Create socket */
227 if (!iscsi_sock_create(&s->sock)) {
228 iscsi_err(__FILE__, __LINE__, "iscsi_sock_create() failed\n");
229 return -1;
231 if (!iscsi_sock_setsockopt(&s->sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one))) {
232 iscsi_err(__FILE__, __LINE__, "iscsi_sock_setsockopt() failed\n");
233 return -1;
236 /* Initialize wait queues */
238 ISCSI_MUTEX_INIT(&s->tx_worker.work_mutex, return -1);
239 ISCSI_COND_INIT(&s->tx_worker.work_cond, return -1);
240 ISCSI_MUTEX_INIT(&s->tx_worker.exit_mutex, return -1);
241 ISCSI_COND_INIT(&s->tx_worker.exit_cond, return -1);
242 ISCSI_MUTEX_INIT(&s->rx_worker.work_mutex, return -1);
243 ISCSI_COND_INIT(&s->rx_worker.work_cond, return -1);
244 ISCSI_MUTEX_INIT(&s->rx_worker.exit_mutex, return -1);
245 ISCSI_COND_INIT(&s->rx_worker.exit_cond, return -1);
247 /* Build parameter list */
250 * ISCSI_PARAM_TYPE_LIST format: <type> <key> <dflt> <valid list values>
251 * ISCSI_PARAM_TYPE_BINARY format: <type> <key> <dflt> <valid binary values>
252 * ISCSI_PARAM_TYPE_NUMERICAL format: <type> <key> <dflt> <max>
253 * ISCSI_PARAM_TYPE_DECLARATIVE format: <type> <key> <dflt> ""
256 s->params = NULL;
257 l = &(s->params);
258 /* CHAP Support Parameters */
259 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "AuthMethod", "None", "CHAP,None", return -1);
260 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "CHAP_A", "None", "5", return -1);
261 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_N", "", "", return -1);
262 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_R", "", "", return -1);
263 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_I", "", "", return -1);
264 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "CHAP_C", "", "", return -1);
265 /* CHAP Support Parameters */
267 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "HeaderDigest", "None", "None", return -1);
268 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_LIST, "DataDigest", "None", "None", return -1);
270 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "MaxConnections", "1", "1", return -1);
271 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "SendTargets", "", "", return -1);
272 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARE_MULTI, "TargetName", "", "", return -1);
273 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "InitiatorName", "iqn.1994-04.org.NetBSD:iscsi-initiator", "", return -1);
274 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "TargetAlias", "", "", return -1);
275 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "InitiatorAlias", "", "", return -1);
276 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARE_MULTI, "TargetAddress", "", "", return -1);
277 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "InitialR2T", "Yes", "Yes,No", return -1);
279 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_AND, "ImmediateData", "Yes", "Yes,No", return -1);
280 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "MaxRecvDataSegmentLength", "8192", "16777215", return -1);
281 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "MaxBurstLength", "262144", "16777215", return -1);
282 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL_Z, "FirstBurstLength", "65536", "16777215", return -1);
283 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "TargetPortalGroupTag", "1", "65535", return -1);
284 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "DefaultTime2Wait", "2", "2", return -1);
285 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "DefaultTime2Retain", "20", "20", return -1);
286 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "MaxOutstandingR2T", "1", "1", return -1);
287 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "DataPDUInOrder", "Yes", "Yes,No", return -1);
288 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_BINARY_OR, "DataSequenceInOrder", "Yes", "Yes,No", return -1);
289 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_NUMERICAL, "ErrorRecoveryLevel", "0", "0", return -1);
290 PARAM_LIST_ADD(l, ISCSI_PARAM_TYPE_DECLARATIVE, "SessionType", "Normal", "Normal,Discovery", return -1);
292 /* Start Tx worker */
294 iscsi_trace(TRACE_ISCSI_DEBUG, "starting Tx worker %llu\n", isid);
295 if (iscsi_queue_init(&s->tx_queue, CONFIG_INITIATOR_QUEUE_DEPTH) == -1) {
296 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
297 return -1;
299 ISCSI_LOCK(&s->tx_worker.exit_mutex, return -1);
300 if (iscsi_thread_create(&s->tx_worker.thread,
301 (void *) tx_worker_proc_i, &s->tx_worker) != 0) {
302 iscsi_err(__FILE__, __LINE__,
303 "iscsi_threads_create() failed\n");
304 return -1;
306 ISCSI_WAIT(&s->tx_worker.exit_cond, &s->tx_worker.exit_mutex,
307 return -1);
308 ISCSI_UNLOCK(&s->tx_worker.exit_mutex, return -1);
309 if (s->state == INITIATOR_SESSION_STATE_DESTROYING) {
310 iscsi_trace(TRACE_ISCSI_DEBUG,
311 "session %llu is being destroyed, exiting\n", isid);
312 return -1;
314 if (s->tx_worker.state & ISCSI_WORKER_STATE_ERROR) {
315 iscsi_err(__FILE__, __LINE__,
316 "Tx worker %llu started with an error\n", isid);
317 return -1;
319 iscsi_trace(TRACE_ISCSI_DEBUG, "got signal from Tx worker\n");
320 s->state = INITIATOR_SESSION_STATE_INITIALIZED;
322 return 0;
325 static int
326 session_destroy_i(initiator_session_t * sess)
328 initiator_cmd_t *ptr;
329 uint64_t isid = sess->isid;
331 if (sess == NULL) {
332 iscsi_err(__FILE__, __LINE__, "session pointer is NULL\n");
333 return -1;
335 if (g_target[(int)sess->isid].has_session == 0) {
336 iscsi_err(__FILE__, __LINE__,
337 "g_target[%llu].has_session==0??\n", sess->isid);
338 return -1;
340 sess->state = INITIATOR_SESSION_STATE_DESTROYING;
342 /* Abort all outstanding commands */
344 for (ptr = sess->cmds; ptr != NULL; ptr = ptr->next) {
345 if (initiator_abort(ptr) != 0) {
346 iscsi_err(__FILE__, __LINE__, "initiator_abort() failed\n");
347 return -1;
351 if (sess->tx_worker.state & ISCSI_WORKER_STATE_STARTED) {
352 if (sess->tx_worker.state & ISCSI_WORKER_STATE_EXITING) {
353 iscsi_trace(TRACE_ISCSI_DEBUG,
354 "Tx worker %llu already signalled for exit\n",
355 sess->isid);
356 } else {
357 iscsi_trace(TRACE_ISCSI_DEBUG,
358 "signaling Tx worker %llu into exiting state\n",
359 sess->isid);
360 ISCSI_LOCK(&sess->tx_worker.work_mutex, return -1);
361 iscsi_trace(TRACE_ISCSI_DEBUG,
362 "signaling socket shutdown to Tx worker %llu\n", sess->isid);
363 if (iscsi_sock_shutdown(sess->sock, 1) != 0) {
364 iscsi_err(__FILE__, __LINE__,
365 "iscsi_sock_shutdown() failed\n");
367 ISCSI_SIGNAL(&sess->tx_worker.work_cond, return -1);
368 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, return -1);
370 iscsi_trace(TRACE_ISCSI_DEBUG,
371 "Checking exit condition of Tx worker\n");
372 while ((sess->tx_worker.state & ISCSI_WORKER_STATE_EXITING) !=
373 ISCSI_WORKER_STATE_EXITING) {
374 ISCSI_SPIN;
376 iscsi_trace(TRACE_ISCSI_DEBUG, "Tx worker %llu has exited\n",
377 sess->isid);
378 } else {
379 iscsi_trace(TRACE_ISCSI_DEBUG,
380 "Tx worker was not started. Nothing to signal\n");
383 /* Destroy Tx state */
384 while ((ptr = iscsi_queue_remove(&sess->tx_queue)) != NULL) {
385 ptr->status = -1;
386 if (ptr->callback && ((*ptr->callback)(ptr) != 0)) {
387 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
390 iscsi_queue_destroy(&sess->tx_queue);
392 if (sess->rx_worker.state & ISCSI_WORKER_STATE_STARTED) {
393 if (sess->rx_worker.state & ISCSI_WORKER_STATE_EXITING) {
394 iscsi_trace(TRACE_ISCSI_DEBUG,
395 "Rx worker %llu already signalled for exit\n",
396 sess->isid);
397 } else {
398 iscsi_trace(TRACE_ISCSI_DEBUG,
399 "signaling Rx worker %llu into exiting state\n", sess->isid);
400 if (iscsi_sock_shutdown(sess->sock, 0) != 0) {
401 iscsi_err(__FILE__, __LINE__,
402 "iscsi_sock_shutdown() failed\n");
405 iscsi_trace(TRACE_ISCSI_DEBUG,
406 "Checking exit condition of Rx worker\n");
407 while ((sess->rx_worker.state & ISCSI_WORKER_STATE_EXITING) !=
408 ISCSI_WORKER_STATE_EXITING) {
409 ISCSI_SPIN;
411 iscsi_trace(TRACE_ISCSI_DEBUG, "Rx worker %llu has exited\n",
412 sess->isid);
413 } else {
414 iscsi_trace(TRACE_ISCSI_DEBUG,
415 "Rx worker was not started. Nothing to signal\n");
418 /* Close socket */
420 if (iscsi_sock_close(sess->sock) != 0) {
421 iscsi_err(__FILE__, __LINE__, "iscsi_sock_close() failed\n");
422 return -1;
424 /* Destroy wait queues */
426 ISCSI_MUTEX_DESTROY(&sess->tx_worker.work_mutex, return -1);
427 ISCSI_COND_DESTROY(&sess->tx_worker.work_cond, return -1);
428 ISCSI_MUTEX_DESTROY(&sess->tx_worker.exit_mutex, return -1);
429 ISCSI_COND_DESTROY(&sess->tx_worker.exit_cond, return -1);
430 ISCSI_MUTEX_DESTROY(&sess->rx_worker.work_mutex, return -1);
431 ISCSI_COND_DESTROY(&sess->rx_worker.work_cond, return -1);
432 ISCSI_MUTEX_DESTROY(&sess->rx_worker.exit_mutex, return -1);
433 ISCSI_COND_DESTROY(&sess->rx_worker.exit_cond, return -1);
435 /* Destroy param list */
437 PARAM_LIST_DESTROY(sess->params, return -1);
439 /* Enqueue session to free list */
440 if (iscsi_queue_insert(&g_session_q, sess) == -1) {
441 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
442 return -1;
444 iscsi_trace(TRACE_ISCSI_DEBUG, "session %p destroyed and requeued\n",
445 sess);
447 g_target[(int)isid].has_session = 0;
449 return 0;
452 #define IS_DISCOVERY 1
453 #define IS_SECURITY 1
455 enum {
456 SESS_TYPE_DISCOVERY = 1,
457 SESS_TYPE_NORMAL = 2,
458 SESS_TYPE_NONE = 3
461 static int
462 params_out(initiator_session_t * sess, char *text, int *len, int textsize, int sess_type, int security)
464 if (security == IS_SECURITY) {
465 PARAM_TEXT_ADD(sess->params, "InitiatorName", "iqn.1994-04.org.NetBSD.iscsi-initiator:agc", text, len, textsize, 1, return -1);
466 PARAM_TEXT_ADD(sess->params, "InitiatorAlias", "NetBSD", text, len, textsize, 1, return -1);
467 PARAM_TEXT_ADD(sess->params, "AuthMethod", "CHAP,None", text, len, textsize, 1, return -1);
468 } else {
469 PARAM_TEXT_ADD(sess->params, "HeaderDigest", "None", text, len, textsize, 1, return -1);
470 PARAM_TEXT_ADD(sess->params, "DataDigest", "None", text, len, textsize, 1, return -1);
471 PARAM_TEXT_ADD(sess->params, "MaxConnections", "1", text, len, textsize, 1, return -1);
472 PARAM_TEXT_ADD(sess->params, "InitialR2T", "Yes", text, len, textsize, 1, return -1);
473 PARAM_TEXT_ADD(sess->params, "ImmediateData", "Yes", text, len, textsize, 1, return -1);
474 PARAM_TEXT_ADD(sess->params, "MaxRecvDataSegmentLength", "8192", text, len, textsize, 1, return -1);
475 PARAM_TEXT_ADD(sess->params, "FirstBurstLength", "65536", text, len, textsize, 1, return -1);
476 PARAM_TEXT_ADD(sess->params, "MaxBurstLength", "262144", text, len, textsize, 1, return -1);
477 PARAM_TEXT_ADD(sess->params, "DefaultTime2Wait", "2", text, len, textsize, 1, return -1);
478 PARAM_TEXT_ADD(sess->params, "DefaultTime2Retain", "20", text, len, textsize, 1, return -1);
479 PARAM_TEXT_ADD(sess->params, "MaxOutstandingR2T", "1", text, len, textsize, 1, return -1);
480 PARAM_TEXT_ADD(sess->params, "DataPDUInOrder", "No", text, len, textsize, 1, return -1);
481 PARAM_TEXT_ADD(sess->params, "DataSequenceInOrder", "No", text, len, textsize, 1, return -1);
482 PARAM_TEXT_ADD(sess->params, "ErrorRecoveryLevel", "0", text, len, textsize, 1, return -1);
484 switch (sess_type) {
485 case SESS_TYPE_DISCOVERY:
486 PARAM_TEXT_ADD(sess->params, "SessionType", "Discovery", text, len, textsize, 1, return -1);
487 break;
488 case SESS_TYPE_NORMAL:
489 PARAM_TEXT_ADD(sess->params, "SessionType", "Normal", text, len, textsize, 1, return -1);
490 PARAM_TEXT_ADD(sess->params, "TargetName", g_target[(int)sess->isid].TargetName, text, len, textsize, 1, return -1);
491 break;
492 default:
493 break;
495 PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text, *len, NULL, NULL, 0, 1, return -1);
496 return 0;
499 static int
500 full_feature_negotiation_phase_i(initiator_session_t * sess, char *text,
501 int text_len)
503 initiator_cmd_t *cmd = NULL;
504 iscsi_text_cmd_args_t *text_cmd = NULL;
505 initiator_wait_t iwait;
507 /* Allocate command pointers */
509 if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
510 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
511 return -1;
513 (void) memset(cmd, 0x0, sizeof(*cmd));
514 text_cmd = iscsi_malloc_atomic(sizeof(iscsi_text_cmd_args_t));
515 if (text_cmd == NULL) {
516 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
517 if (cmd != NULL)
518 iscsi_free_atomic(cmd); /* initiator command */
519 return -1;
521 #define FFN_ERROR {if (cmd != NULL) iscsi_free_atomic(cmd); if (text_cmd != NULL) iscsi_free_atomic(text_cmd); return -1;}
522 (void) memset(text_cmd, 0x0, sizeof(*text_cmd));
525 * Note that <final>, <length> and <text> are updated
526 * by text_response_i when we receive offers from
527 * the target.
529 text_cmd->text = text;
530 text_cmd->length = text_len;
532 do {
534 /* Build text command */
536 text_cmd->final = 1;
537 text_cmd->cont = 0;
538 ISCSI_SET_TAG(&text_cmd->tag);
539 text_cmd->transfer_tag = 0xffffffff;
541 /* Build wait for callback */
543 ISCSI_MUTEX_INIT(&iwait.mutex, FFN_ERROR);
544 ISCSI_COND_INIT(&iwait.cond, FFN_ERROR);
546 /* Build initiator command */
548 cmd->type = ISCSI_TEXT_CMD;
549 cmd->ptr = text_cmd;
550 cmd->callback = wait_callback_i;
551 cmd->callback_arg = &iwait;
552 cmd->isid = sess->isid;
554 /* Enqueue initiator command to Tx worker */
556 iscsi_trace(TRACE_ISCSI_DEBUG,
557 "enqueing text command to tx worker %llu\n",
558 sess->isid);
559 ISCSI_LOCK(&iwait.mutex, FFN_ERROR);
560 ISCSI_LOCK(&sess->tx_worker.work_mutex, FFN_ERROR);
561 if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
562 ISCSI_UNLOCK(&iwait.mutex, );
563 iscsi_err(__FILE__, __LINE__,
564 "iscsi_queue_insert() failed\n");
565 FFN_ERROR;
567 ISCSI_SIGNAL(&sess->tx_worker.work_cond, FFN_ERROR);
568 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, FFN_ERROR);
569 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued text command ok\n");
571 /* Wait for callback */
573 iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on text callback\n");
574 ISCSI_WAIT(&iwait.cond, &iwait.mutex, FFN_ERROR);
575 ISCSI_UNLOCK(&iwait.mutex, FFN_ERROR);
576 ISCSI_COND_DESTROY(&iwait.cond, FFN_ERROR);
577 ISCSI_MUTEX_DESTROY(&iwait.mutex, FFN_ERROR);
578 iscsi_trace(TRACE_ISCSI_DEBUG, "received text callback ok\n");
581 * See if we're done. text_response_i() overwrites
582 * text_cmd->final
584 /* with the final bit in the text response from the target. */
586 if (!text_cmd->final) {
587 iscsi_trace(TRACE_ISCSI_PARAM,
588 "more negotiation needed (sending %d bytes "
589 "response parameters)\n",
590 text_cmd->length);
592 } while (!text_cmd->final);
594 /* Free command pointers */
596 iscsi_free_atomic(cmd->ptr); /* text command */
597 iscsi_free_atomic(cmd); /* initiator command */
599 return 0;
602 #define DISCOVERY_PHASE_TEXT_LEN 1024
603 #define DP_CLEANUP {if (text != NULL) iscsi_free_atomic(text);}
604 #define DP_ERROR {DP_CLEANUP; return -1;}
607 initiator_set_target_name(int target, char *target_name)
609 (void) strlcpy(g_target[target].iqnwanted, target_name,
610 sizeof(g_target[target].iqnwanted));
611 (void) strlcpy(g_target[target].TargetName, target_name,
612 sizeof(g_target[target].TargetName));
613 return 0;
618 iscsi_initiator_get_targets(int target, strv_t *svp)
620 initiator_session_t *sess = g_target[target].sess;
621 iscsi_parameter_value_t *vp;
622 iscsi_parameter_t *ip;
623 char *text = NULL;
624 int text_len = 0;
626 if ((text = iscsi_malloc_atomic(DISCOVERY_PHASE_TEXT_LEN)) == NULL) {
627 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
628 return -1;
631 text_len = 0;
632 text[0] = 0x0;
634 PARAM_TEXT_ADD(sess->params, "SendTargets", "all", text, &text_len,
635 DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
636 PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text,
637 text_len, NULL, NULL, DISCOVERY_PHASE_TEXT_LEN, 1,
638 DP_ERROR);
639 if (full_feature_negotiation_phase_i(sess, text, text_len) != 0) {
640 iscsi_err(__FILE__, __LINE__,
641 "full_feature_negotiation_phase_i() failed\n");
642 DP_ERROR;
644 for (ip = sess->params ; ip ; ip = ip->next) {
645 if (strcmp(ip->key, "TargetName") == 0) {
646 for (vp = ip->value_l ; vp ; vp = vp->next) {
647 ALLOC(char *, svp->v, svp->size, svp->c, 10,
648 10, "igt", return -1);
649 svp->v[svp->c++] = strdup(vp->value);
650 ALLOC(char *, svp->v, svp->size, svp->c, 10,
651 10, "igt2", return -1);
652 svp->v[svp->c++] = strdup(param_val(sess->params, "TargetAddress"));
657 return 1;
660 static int
661 discovery_phase(int target, strv_t *svp)
663 initiator_session_t *sess;
664 iscsi_parameter_value_t *vp;
665 iscsi_parameter_t *ip;
666 unsigned i;
667 char *ptr;
668 char *colon_ptr;
669 char *comma_ptr;
670 char port[64];
671 char *text = NULL;
672 int text_len = 0;
674 if (target >= CONFIG_INITIATOR_NUM_TARGETS) {
675 iscsi_err(__FILE__, __LINE__,
676 "target (%d) out of range [0..%d]\n", target,
677 CONFIG_INITIATOR_NUM_TARGETS);
678 return -1;
680 sess = g_target[target].sess;
681 if ((text = iscsi_malloc_atomic(DISCOVERY_PHASE_TEXT_LEN)) == NULL) {
682 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
683 return -1;
685 /* Login to target */
687 iscsi_trace(TRACE_ISCSI_DEBUG,
688 "entering Discovery login phase with target %d (sock %#x)\n",
689 target, (int) sess->sock);
690 text[0] = 0x0;
691 if (params_out(sess, text, &text_len, DISCOVERY_PHASE_TEXT_LEN,
692 SESS_TYPE_DISCOVERY, IS_SECURITY) != 0) {
693 iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
694 DP_ERROR;
696 if (login_phase_i(sess, text, text_len) != 0) {
697 iscsi_err(__FILE__, __LINE__, "login_phase_i() failed\n");
698 DP_ERROR;
700 iscsi_trace(TRACE_ISCSI_DEBUG,
701 "now full feature for Discovery with target %d\n", target);
703 /* Full Feature Phase Negotiation (for SendTargets) */
704 text_len = 0;
705 text[0] = 0x0;
706 PARAM_TEXT_ADD(sess->params, "SendTargets", "all", text, &text_len,
707 DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
708 PARAM_TEXT_PARSE(sess->params, &sess->sess_params.cred, text,
709 text_len, NULL, NULL, DISCOVERY_PHASE_TEXT_LEN, 1, DP_ERROR);
710 if (full_feature_negotiation_phase_i(sess, text, text_len) != 0) {
711 iscsi_err(__FILE__, __LINE__,
712 "full_feature_negotiation_phase_i() failed\n");
713 DP_ERROR;
716 /* fill in information on the targets from the TargetName values */
717 (void) memset(svp, 0x0, sizeof(*svp));
718 for (ip = sess->params ; ip ; ip = ip->next) {
719 if (strcmp(ip->key, "TargetName") == 0) {
720 for (vp = ip->value_l ; vp ; vp = vp->next) {
721 ALLOC(char *, svp->v, svp->size, svp->c, 10,
722 10, "discovery_phase", return -1);
723 svp->v[svp->c++] = strdup(vp->value);
724 ALLOC(char *, svp->v, svp->size, svp->c, 10,
725 10, "discovery_phase2", return -1);
726 svp->v[svp->c++] = strdup(param_val(
727 sess->params, "TargetAddress"));
732 if (g_target[target].iqnwanted[0] == 0x0) {
734 * Use the first TargetName and TargetAddress sent to
735 * us (all others are currently ignored)
737 if (param_val(sess->params, "TargetName") != NULL) {
738 strlcpy(g_target[target].TargetName,
739 param_val(sess->params, "TargetName"),
740 sizeof(g_target[target].TargetName));
741 } else {
742 iscsi_err(__FILE__, __LINE__, "SendTargets failed\n");
743 DP_ERROR;
745 if ((ptr = param_val(sess->params, "TargetAddress")) == NULL) {
746 iscsi_err(__FILE__, __LINE__, "SendTargets failed\n");
747 DP_ERROR;
749 } else {
750 /* the user has asked for a specific target - find it */
751 for (i = 0 ; i < svp->c ; i += 2) {
752 if (strcmp(g_target[target].iqnwanted,
753 svp->v[i]) == 0) {
754 strlcpy(g_target[target].TargetName, svp->v[i],
755 sizeof(g_target[target].TargetName));
756 ptr = svp->v[i + 1];
757 break;
760 if (i >= svp->c) {
761 iscsi_err(__FILE__, __LINE__,
762 "SendTargets failed - target `%s' not found\n",
763 g_target[target].iqnwanted);
764 DP_ERROR;
768 if (*ptr == 0x0) {
769 iscsi_err(__FILE__, __LINE__,
770 "Target is not allowing access\n");
771 DP_ERROR;
773 colon_ptr = strchr(ptr, ':');
774 if ((comma_ptr = strchr(ptr, ',')) == NULL) {
775 iscsi_err(__FILE__, __LINE__,
776 "portal group tag is missing in \"%s\"\n",
777 param_val(sess->params, "TargetAddress"));
778 DP_ERROR;
780 if (colon_ptr) {
781 strncpy(g_target[target].ip, ptr, (size_t)(colon_ptr - ptr));
782 strncpy(port, colon_ptr + 1,
783 (size_t)(comma_ptr - colon_ptr - 1));
784 port[comma_ptr - colon_ptr - 1] = 0x0;
785 g_target[target].port = iscsi_atoi(port);
786 } else {
787 strncpy(g_target[target].ip, ptr, (size_t)(comma_ptr - ptr));
788 g_target[target].port = ISCSI_PORT;
791 iscsi_trace(TRACE_ISCSI_DEBUG, "Discovered \"%s\" at \"%s:%u\"\n",
792 g_target[target].TargetName, g_target[target].name,
793 g_target[target].port);
795 /* Logout from target */
797 iscsi_trace(TRACE_ISCSI_DEBUG,
798 "entering logout phase with target %d\n", target);
799 if (logout_phase_i(sess) != 0) {
800 iscsi_err(__FILE__, __LINE__, "logout_phase_i() failed\n");
801 DP_ERROR;
803 iscsi_trace(TRACE_ISCSI_DEBUG, "target %d logout phase complete\n",
804 target);
805 DP_CLEANUP;
806 return 0;
809 #define FULL_FEATURE_PHASE_TEXT_LEN 1024
811 static int
812 full_feature_phase(initiator_session_t * sess)
814 char *text;
815 int text_len;
817 if ((text = iscsi_malloc_atomic(FULL_FEATURE_PHASE_TEXT_LEN)) == NULL) {
818 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
819 return -1;
821 #define FFP_CLEANUP {if (text != NULL) iscsi_free_atomic(text);}
822 #define FFP_ERROR {FFP_CLEANUP; return -1;}
823 /* Set text parameters */
825 text[0] = 0x0;
826 text_len = 0;
827 if (params_out(sess, text, &text_len, FULL_FEATURE_PHASE_TEXT_LEN,
828 SESS_TYPE_NORMAL, IS_SECURITY) != 0) {
829 iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
830 FFP_ERROR;
832 /* Send login command */
834 iscsi_trace(TRACE_ISCSI_DEBUG, "entering login phase\n");
835 if (login_phase_i(sess, text, text_len) != 0) {
836 iscsi_err(__FILE__, __LINE__, "login_phase_i() failed\n");
837 FFP_ERROR;
839 iscsi_trace(TRACE_ISCSI_DEBUG, "login phase successful\n");
841 FFP_CLEANUP;
842 return 0;
845 int
846 iscsi_initiator_start(iscsi_initiator_t *ini)
848 initiator_session_t *sess = NULL;
849 char *dbg;
850 char *cp;
851 int port;
852 int i;
854 #define INIT_CLEANUP {if (sess != NULL) iscsi_free_atomic(sess);}
855 #define INIT_ERROR {INIT_CLEANUP; return -1;}
857 if ((dbg = iscsi_initiator_getvar(ini, "debug")) != NULL) {
858 set_debug(dbg);
860 iscsi_trace(TRACE_ISCSI_DEBUG, "initializing initiator\n");
861 iscsi_trace(TRACE_ISCSI_DEBUG,
862 "target config filename to read from:%s\n", gfilename);
863 port = atoi(iscsi_initiator_getvar(ini, "target port"));
864 if (get_target_config(iscsi_initiator_getvar(ini,
865 "target hostname"), port) != 0) {
866 iscsi_err(__FILE__, __LINE__,
867 "Error getting target configuration in config file\n");
868 return -1;
870 g_initiator_state = 0;
871 if (iscsi_queue_init(&g_session_q,
872 CONFIG_INITIATOR_MAX_SESSIONS) != 0) {
873 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
874 return -1;
876 for (i = 0; i < CONFIG_INITIATOR_MAX_SESSIONS; i++) {
877 sess = iscsi_malloc_atomic(sizeof(initiator_session_t));
878 if (sess == NULL) {
879 iscsi_err(__FILE__, __LINE__,
880 "iscsi_malloc_atomic() failed\n");
881 return -1;
883 if (iscsi_queue_insert(&g_session_q, sess) != 0) {
884 iscsi_err(__FILE__, __LINE__,
885 "iscsi_queue_init() failed\n");
886 INIT_CLEANUP;
887 return -1;
889 sess->sess_params.cred.user =
890 strdup(iscsi_initiator_getvar(ini, "user"));
891 cp = iscsi_initiator_getvar(ini, "auth type");
892 if (strcmp(cp, "none") == 0) {
893 sess->sess_params.auth_type = AuthNone;
895 cp = iscsi_initiator_getvar(ini, "mutual auth");
896 if (strcmp(cp, "none") == 0) {
897 sess->sess_params.mutual_auth = 0;
899 cp = iscsi_initiator_getvar(ini, "digest type");
900 if (strcmp(cp, "none") == 0) {
901 sess->sess_params.digest_wanted = DigestNone;
904 iscsi_trace(TRACE_ISCSI_DEBUG, "%d free sessions available\n",
905 CONFIG_INITIATOR_MAX_SESSIONS);
907 g_tag = 0xabc123;
908 if (hash_init(&g_tag_hash, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
909 iscsi_err(__FILE__, __LINE__, "hash_init() failed\n");
910 INIT_CLEANUP;
911 return -1;
913 iscsi_spin_init(&g_tag_spin);
914 iscsi_trace(TRACE_ISCSI_DEBUG,
915 "tag hash table initialized with queue depth %d\n",
916 CONFIG_INITIATOR_QUEUE_DEPTH);
919 * Start enqueue worker. This thread accepts scsi commands from
920 * initiator_enqueue()
922 /* and queues them onto one of the tx worker queues. */
924 iscsi_trace(TRACE_ISCSI_DEBUG, "starting enqueue worker\n");
925 if (iscsi_queue_init(&g_enqueue_q, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
926 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
927 INIT_CLEANUP;
928 return -1;
930 iscsi_trace(TRACE_ISCSI_DEBUG, "about to initialize mutex\n");
931 ISCSI_MUTEX_INIT(&g_enqueue_worker.work_mutex, INIT_ERROR);
932 ISCSI_COND_INIT(&g_enqueue_worker.work_cond, INIT_ERROR);
933 ISCSI_MUTEX_INIT(&g_enqueue_worker.exit_mutex, INIT_ERROR);
934 ISCSI_COND_INIT(&g_enqueue_worker.exit_cond, INIT_ERROR);
935 ISCSI_LOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
937 iscsi_trace(TRACE_ISCSI_DEBUG, "spawning thread for enqueue worker\n");
938 if (iscsi_thread_create(&g_enqueue_worker.thread,
939 (void *) &enqueue_worker_proc, &g_enqueue_worker) != 0) {
940 iscsi_err(__FILE__, __LINE__,
941 "iscsi_threads_create() failed\n");
942 INIT_CLEANUP;
943 return -1;
945 iscsi_trace(TRACE_ISCSI_DEBUG, "thread spawned, waiting for signal\n");
946 ISCSI_WAIT(&g_enqueue_worker.exit_cond, &g_enqueue_worker.exit_mutex,
947 INIT_ERROR);
948 ISCSI_UNLOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
949 iscsi_trace(TRACE_ISCSI_DEBUG, "successfully started enqueue worker\n");
951 iscsi_trace(TRACE_ISCSI_DEBUG, "initiator initialization complete\n");
952 return 0;
955 int
956 iscsi_initiator_shutdown(void)
958 initiator_session_t *sess;
959 int i;
961 iscsi_trace(TRACE_ISCSI_DEBUG, "shutting down initiator\n");
962 for (i = 0; i < CONFIG_INITIATOR_NUM_TARGETS; i++) {
963 if (g_target[i].has_session) {
964 iscsi_trace(TRACE_ISCSI_DEBUG,
965 "entering logout phase for target %d\n", i);
966 if (g_target[i].sess->rx_worker.state &
967 ISCSI_WORKER_STATE_ERROR) {
968 iscsi_warn(__FILE__, __LINE__,
969 "rx worker exited abnormal, "
970 "skipping logout phase\n");
971 } else {
972 if (logout_phase_i(g_target[i].sess) != 0) {
973 iscsi_err(__FILE__, __LINE__,
974 "logout_phase_i() failed "
975 "for target %d\n", i);
977 iscsi_trace(TRACE_ISCSI_DEBUG,
978 "logout phase complete for target "
979 "%d (state %#x)\n",
980 i, g_target[i].sess->state);
982 iscsi_trace(TRACE_ISCSI_DEBUG,
983 "destroying session for target %d\n", i);
984 if (session_destroy_i(g_target[i].sess) != 0) {
985 iscsi_err(__FILE__, __LINE__,
986 "session_destroy_i() failed for "
987 "target %d\n", i);
989 iscsi_trace(TRACE_ISCSI_DEBUG,
990 "session destroyed for target %d\n", i);
994 g_initiator_state = INITIATOR_STATE_SHUTDOWN;
995 if (g_enqueue_worker.state & ISCSI_WORKER_STATE_EXITING) {
996 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue already exiting\n");
997 } else {
998 iscsi_trace(TRACE_ISCSI_DEBUG,
999 "signaling enqueue worker into exiting state\n");
1000 ISCSI_LOCK(&g_enqueue_worker.work_mutex, return -1);
1001 ISCSI_SIGNAL(&g_enqueue_worker.work_cond, return -1);
1002 ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, return -1);
1004 iscsi_trace(TRACE_ISCSI_DEBUG,
1005 "Checking exit condition of enqueue worker\n");
1006 while ((g_enqueue_worker.state & ISCSI_WORKER_STATE_EXITING) !=
1007 ISCSI_WORKER_STATE_EXITING) {
1008 ISCSI_SPIN;
1010 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue worker has exited\n");
1012 iscsi_queue_destroy(&g_enqueue_q);
1013 ISCSI_MUTEX_DESTROY(&g_enqueue_worker.work_mutex, return -1);
1014 ISCSI_COND_DESTROY(&g_enqueue_worker.work_cond, return -1);
1015 ISCSI_MUTEX_DESTROY(&g_enqueue_worker.exit_mutex, return -1);
1016 ISCSI_COND_DESTROY(&g_enqueue_worker.exit_cond, return -1);
1018 while ((sess = iscsi_queue_remove(&g_session_q)) != NULL) {
1019 iscsi_free_atomic(sess);
1021 iscsi_queue_destroy(&g_session_q);
1022 iscsi_spin_destroy(&g_tag_spin);
1023 hash_destroy(&g_tag_hash);
1024 iscsi_trace(TRACE_ISCSI_DEBUG, "initiator shutdown complete\n");
1025 return 0;
1028 static int
1029 wait_callback_i(void *ptr)
1031 initiator_wait_t *iwait = (initiator_wait_t *) (((initiator_cmd_t *) ptr)->callback_arg);
1033 iwait = (initiator_wait_t *) (((initiator_cmd_t *) ptr)->callback_arg);
1034 ISCSI_LOCK(&iwait->mutex, return -1);
1035 ISCSI_SIGNAL(&iwait->cond, return -1);
1036 ISCSI_UNLOCK(&iwait->mutex, return -1);
1037 return 0;
1040 int
1041 initiator_abort(initiator_cmd_t * cmd)
1043 initiator_cmd_t *ptr, *prev;
1044 initiator_session_t *sess;
1046 iscsi_err(__FILE__, __LINE__, "aborting iSCSI cmd 0x%p (type %d, isid %llu)\n",
1047 cmd, cmd->type, cmd->isid);
1049 hash_remove(&g_tag_hash, cmd->key);
1050 if (g_target[(int)cmd->isid].has_session) {
1051 sess = g_target[(int)cmd->isid].sess;
1052 iscsi_spin_lock(&sess->cmds_spin);
1053 prev = ptr = sess->cmds;
1054 while (ptr != NULL) {
1055 prev = ptr;
1056 if (ptr == cmd)
1057 break;
1058 ptr = ptr->next;
1060 if (ptr != NULL) {
1061 if (prev == sess->cmds) {
1062 sess->cmds = cmd->next;
1063 } else {
1064 prev->next = cmd->next;
1067 iscsi_spin_unlock(&sess->cmds_spin);
1068 } else {
1069 iscsi_err(__FILE__, __LINE__, "cmd 0x%p has no session\n", cmd);
1071 cmd->status = -1;
1072 if (cmd->callback) {
1073 if ((*cmd->callback)(cmd) != 0) {
1074 iscsi_err(__FILE__, __LINE__, "cmd->callback() failed\n");
1075 return -1;
1078 iscsi_err(__FILE__, __LINE__, "successfully aborted iSCSI cmd 0x%p (type %d, isid %llu)\n",
1079 cmd, cmd->type, cmd->isid);
1080 return 0;
1083 int
1084 initiator_command(initiator_cmd_t * cmd)
1086 initiator_wait_t iwait;
1088 ISCSI_MUTEX_INIT(&iwait.mutex, return -1);
1089 ISCSI_COND_INIT(&iwait.cond, return -1);
1090 ISCSI_LOCK(&iwait.mutex, return -1);
1091 cmd->callback = wait_callback_i;
1092 cmd->callback_arg = &iwait;
1093 cmd->status = -1;
1094 if (initiator_enqueue(cmd) != 0) {
1095 iscsi_err(__FILE__, __LINE__, "initiator_enqueue() failed\n");
1096 return -1;
1097 } else {
1098 iscsi_trace(TRACE_ISCSI_DEBUG, "command (type %d) enqueued, waiting on condition\n", cmd->type);
1099 ISCSI_WAIT(&iwait.cond, &iwait.mutex, return -1);
1100 iscsi_trace(TRACE_ISCSI_DEBUG, "condition signaled\n");
1102 ISCSI_UNLOCK(&iwait.mutex, return -1);
1103 ISCSI_COND_DESTROY(&iwait.cond, return -1);
1104 ISCSI_MUTEX_DESTROY(&iwait.mutex, return -1);
1106 return cmd->status;
1110 * initiator_enqueue() may be called from within interrupt context within
1111 * the midlayer. This function cannot block or be scheduled within.
1112 * All we do is enqueue the args ptr to g_enqueue_q. The thread in
1113 * enqueue_worker_proc will enqueue the ptr onto one of the tx queues.
1116 int
1117 initiator_enqueue(initiator_cmd_t * cmd)
1119 initiator_session_t *sess;
1120 iscsi_scsi_cmd_args_t *scsi_cmd;
1121 iscsi_nop_out_args_t *nop_out;
1122 uint64_t target;
1123 uint32_t tag;
1125 if ((target = cmd->isid) >= CONFIG_INITIATOR_NUM_TARGETS) {
1126 iscsi_err(__FILE__, __LINE__, "target (%d) out of range [0..%d]\n", target, CONFIG_INITIATOR_NUM_TARGETS);
1127 return -1;
1129 sess = g_target[(int)target].sess;
1130 if (g_target[(int)target].has_session &&
1131 sess->state == INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL) {
1133 /* Give command directly to tx worker */
1135 ISCSI_SET_TAG_IN_INTR(&tag);
1136 target = cmd->isid;
1138 switch (cmd->type) {
1139 case ISCSI_SCSI_CMD:
1140 scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
1141 scsi_cmd->tag = tag;
1142 break;
1143 case ISCSI_NOP_OUT:
1144 nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
1145 if (nop_out->tag != 0xffffffff) {
1146 nop_out->tag = tag;
1148 break;
1149 default:
1150 iscsi_err(__FILE__, __LINE__, "enqueue_worker: unknown command type %d\n", cmd->type);
1151 return -1;
1153 if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1154 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1155 return -1;
1157 ISCSI_LOCK(&sess->tx_worker.work_mutex, return -1);
1158 ISCSI_SIGNAL(&sess->tx_worker.work_cond, return -1);
1159 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, return -1);
1160 iscsi_trace(TRACE_ISCSI_DEBUG, "initiator_cmd_t 0x%p given to tx_worker[%llu]\n", cmd, cmd->isid);
1161 } else {
1164 * Give command to enqueue worker to get us into full feature
1165 * and then issue the command
1167 /* to one of the tx workers. */
1169 if (iscsi_queue_insert(&g_enqueue_q, cmd) == -1) {
1170 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1171 return -1;
1173 ISCSI_LOCK(&g_enqueue_worker.work_mutex, return -1);
1174 ISCSI_SIGNAL(&g_enqueue_worker.work_cond, return -1);
1175 ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, return -1);
1176 iscsi_trace(TRACE_ISCSI_DEBUG, "initiator_cmd_t 0x%p given to enqueue worker\n", cmd);
1178 return 0;
1181 static int
1182 enqueue_worker_proc(void *arg)
1184 initiator_session_t *sess;
1185 initiator_cmd_t *cmd;
1186 iscsi_scsi_cmd_args_t *scsi_cmd;
1187 iscsi_nop_out_args_t *nop_out;
1188 iscsi_worker_t *me = (iscsi_worker_t *) arg;
1189 uint64_t target;
1190 uint32_t tag;
1191 strv_t sv;
1192 int rc;
1195 ISCSI_THREAD_START("enqueue_worker");
1196 ISCSI_SET_THREAD(me)
1197 ISCSI_LOCK(&me->exit_mutex, goto done);
1199 me->pid = getpid();
1200 me->state = ISCSI_WORKER_STATE_STARTED;
1201 ISCSI_SIGNAL(&me->exit_cond, goto done);
1202 ISCSI_UNLOCK(&me->exit_mutex, goto done);
1203 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: started\n");
1204 ISCSI_LOCK(&g_enqueue_worker.work_mutex, goto done);
1205 for (;;) {
1206 if (iscsi_queue_depth(&g_enqueue_q) || (g_initiator_state == INITIATOR_STATE_SHUTDOWN)) {
1207 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueu, start to work\n");
1208 ISCSI_UNLOCK(&g_enqueue_worker.work_mutex, goto done);
1209 if (g_initiator_state == INITIATOR_STATE_SHUTDOWN) {
1210 iscsi_trace(TRACE_ISCSI_DEBUG, "got shutdown signal\n");
1211 goto done;
1213 if ((cmd = iscsi_queue_remove(&g_enqueue_q)) == NULL) {
1214 iscsi_err(__FILE__, __LINE__, "enqueue_worker: iscsi_queue_remove() failed\n");
1215 goto done;
1217 ISCSI_SET_TAG(&tag);
1218 target = cmd->isid;
1219 iscsi_trace(TRACE_ISCSI_CMD, "enqueue_worker: dequeued initiator_cmd_t 0x%p (type %d, target %llu)\n", cmd, cmd->type, target);
1220 switch (cmd->type) {
1221 case ISCSI_SCSI_CMD:
1222 scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
1223 scsi_cmd->tag = tag;
1224 break;
1225 case ISCSI_NOP_OUT:
1226 nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
1227 if (nop_out->tag != 0xffffffff) {
1228 nop_out->tag = tag;
1230 break;
1231 default:
1232 iscsi_err(__FILE__, __LINE__, "enqueue_worker: unknown command type %d\n", cmd->type);
1233 goto done;
1236 /* Initialize session (if not already) */
1237 initialize:
1238 if (!g_target[(int)target].has_session) {
1239 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: initializing target %llu session\n", target);
1240 if (session_init_i(&g_target[(int)target].sess, target) != 0) {
1241 iscsi_err(__FILE__, __LINE__, "session_init_i() failed (ignoring command)\n");
1242 goto next;
1244 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: target %llu session initialized\n", target);
1245 } else {
1246 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: target %llu session already initialized\n", target);
1248 sess = g_target[(int)target].sess;
1249 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: session 0x%p\n", sess);
1251 /* Discovery login if TargetName is zero length */
1253 if (strlen(g_target[(int)target].TargetName) == 0) {
1254 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: entering Discovery phase with target %llu\n", target);
1255 rc = discovery_phase((int)target, &sv);
1256 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: Discovery phase complete\n");
1258 /* Destroy session */
1260 if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1261 if (g_target[(int)target].has_session) {
1262 if (session_destroy_i(g_target[(int)target].sess) != 0) {
1263 iscsi_err(__FILE__, __LINE__, "enqueue_worker: session_destroy_i() failed\n");
1264 goto done;
1270 * If the Discovery phase was
1271 * successful, we re-initialize the
1272 * session, enter full feature phase
1273 * and then execute the command.
1276 if (rc == 0) {
1277 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: discovery_phase() succeeded, entering full feature\n");
1278 goto initialize;
1279 } else {
1280 iscsi_err(__FILE__, __LINE__, "enqueue_worker: discovery_phase() failed (ignoring command)\n");
1281 goto next;
1284 /* Get into full feature if we're not already */
1286 if (sess->state != INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL) {
1287 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: entering full feature with target %llu (sock %#x)\n", target, (int) sess->sock);
1288 if (full_feature_phase(sess) != 0) {
1289 iscsi_err(__FILE__, __LINE__, "enqueue_worker: full_feature_phase() failed (ignoring command)\n");
1290 goto next;
1291 } else {
1292 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: now full feature with target %llu\n", target);
1296 * Now we are in FPP, so set the mostly
1297 * accessed parameters for easy retrieval
1298 * during data transfer
1300 set_session_parameters(sess->params, &sess->sess_params);
1302 /* Add command to tx work queue and signal worker */
1304 ISCSI_LOCK(&sess->tx_worker.work_mutex, goto done);
1305 if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1306 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, goto done);
1307 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1308 goto done;
1310 ISCSI_SIGNAL(&sess->tx_worker.work_cond, goto done);
1311 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, goto done);
1312 iscsi_trace(TRACE_ISCSI_CMD, "enqueue_worker: gave initiator_cmd_t 0x%p to tx_worker[%llu]\n", cmd, cmd->isid);
1313 next:
1314 ISCSI_LOCK(&g_enqueue_worker.work_mutex, goto done);
1315 } else {
1316 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: queue empty, awaiting condition\n");
1317 ISCSI_WAIT(&g_enqueue_worker.work_cond, &g_enqueue_worker.work_mutex, goto done);
1318 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueue_worker: condition signaled\n");
1321 done:
1322 ISCSI_WORKER_EXIT(me);
1323 return 0;
1327 /***********
1328 * Private *
1329 ***********/
1333 * Tx Worker (one per connection)
1336 static int
1337 tx_worker_proc_i(void *arg)
1339 iscsi_worker_t *me = (iscsi_worker_t *) arg;
1340 initiator_cmd_t *cmd, *ptr;
1341 initiator_session_t *sess = g_target[me->id].sess;
1343 ISCSI_THREAD_START("tx_worker");
1345 ISCSI_SET_THREAD(me)
1346 me->pid = getpid();
1347 me->state = ISCSI_WORKER_STATE_STARTED;
1349 /* Connect to target */
1350 iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: connecting to %s:%d\n",
1351 me->id, g_target[me->id].name, g_target[me->id].port);
1352 sess->state = INITIATOR_SESSION_STATE_CONNECTING;
1353 if (iscsi_sock_connect(sess->sock, g_target[me->id].name,
1354 g_target[me->id].port) != 0) {
1355 iscsi_err(__FILE__, __LINE__, "iscsi_sock_connect() failed\n");
1357 ISCSI_LOCK(&me->exit_mutex, return -1);
1358 me->state |= ISCSI_WORKER_STATE_ERROR;
1359 ISCSI_SIGNAL(&me->exit_cond, return -1);
1360 ISCSI_UNLOCK(&me->exit_mutex, return -1);
1361 goto done;
1364 sess->state = INITIATOR_SESSION_STATE_CONNECTED;
1365 iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: connected to %s:%d\n",
1366 me->id, g_target[me->id].name, g_target[me->id].port);
1368 /* Start Rx worker */
1370 iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: starting Rx worker\n",
1371 me->id);
1372 ISCSI_LOCK(&sess->rx_worker.exit_mutex, return -1);
1373 if (iscsi_thread_create(&sess->rx_worker.thread,
1374 (void *) rx_worker_proc_i, sess) != 0) {
1375 iscsi_err(__FILE__, __LINE__, "iscsi_thread_create() failed\n");
1376 goto done;
1378 ISCSI_WAIT(&sess->rx_worker.exit_cond, &sess->rx_worker.exit_mutex,
1379 return -1);
1380 ISCSI_UNLOCK(&sess->rx_worker.exit_mutex, return -1);
1381 iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: Rx worker started\n",
1382 me->id);
1384 /* Signal that we've started */
1385 ISCSI_LOCK(&me->exit_mutex, return -1);
1386 ISCSI_SIGNAL(&me->exit_cond, return -1);
1387 ISCSI_UNLOCK(&me->exit_mutex, return -1);
1388 iscsi_trace(TRACE_ISCSI_DEBUG, "tx_worker[%d]: successfully started\n",
1389 me->id);
1391 /* This Tx loop will exit when both the g_tx_queue is empty and */
1392 /* sess->state != INITIATOR_SESSION_STATE_DESTROYING */
1394 ISCSI_LOCK(&me->work_mutex, return -1);
1395 for (;;) {
1397 if (iscsi_queue_depth(&g_target[me->id].sess->tx_queue) ||
1398 sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1400 if (sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1401 iscsi_trace(TRACE_ISCSI_DEBUG,
1402 "tx_worker[%d]: session is being "
1403 "destroyed, exiting\n", me->id);
1404 ISCSI_UNLOCK(&me->work_mutex, return -1);
1405 goto done;
1407 /* Get initiator command */
1409 cmd = iscsi_queue_remove(
1410 &g_target[me->id].sess->tx_queue);
1411 if (cmd == NULL) {
1412 iscsi_err(__FILE__, __LINE__,
1413 "tx_worker[%d]: iscsi_queue_remove "
1414 "failed\n", me->id);
1415 ISCSI_UNLOCK(&me->work_mutex, return -1);
1416 goto done;
1418 ISCSI_UNLOCK(&me->work_mutex, return -1);
1419 iscsi_trace(TRACE_ISCSI_CMD,
1420 "tx_worker[%d]: dequeued initiator_cmd_t 0x%p "
1421 "(type %d, target %llu)\n",
1422 me->id, cmd, cmd->type, cmd->isid);
1424 /* Make sure we've got the right command */
1425 if (cmd->isid != (unsigned)me->id) {
1426 iscsi_err(__FILE__, __LINE__,
1427 "got command %#x for target %llu, "
1428 "expected %d\n", cmd->type,
1429 cmd->isid, me->id);
1430 goto done;
1433 * Add to list of oustanding commands in session
1434 * (unless NOP_OUT without ping)
1436 if (!((cmd->type == ISCSI_NOP_OUT) &&
1437 (((iscsi_nop_out_args_t *)(cmd->ptr))->tag ==
1438 0xffffffff))) {
1439 cmd->next = NULL;
1440 iscsi_spin_lock(&sess->cmds_spin);
1441 for (ptr = sess->cmds;
1442 ptr && ptr->next != NULL;
1443 ptr = ptr->next) {
1445 if (ptr) {
1446 ptr->next = cmd;
1447 } else {
1448 sess->cmds = cmd;
1450 iscsi_spin_unlock(&sess->cmds_spin);
1452 cmd->tx_done = 0;
1453 switch (cmd->type) {
1454 case ISCSI_LOGIN_CMD:
1455 iscsi_trace(TRACE_ISCSI_CMD,
1456 "tx_worker[%d]: ISCSI_LOGIN_CMD\n",
1457 me->id);
1458 if (login_command_i(cmd) != 0) {
1459 iscsi_err(__FILE__, __LINE__,
1460 "tx_worker[%d]: "
1461 "login_command_i() failed\n",
1462 me->id);
1463 goto done;
1465 break;
1466 case ISCSI_TEXT_CMD:
1467 iscsi_trace(TRACE_ISCSI_CMD,
1468 "tx_worker[%d]: ISCSI_TEXT_CMD\n",
1469 me->id);
1470 if (text_command_i(cmd) != 0) {
1471 iscsi_err(__FILE__, __LINE__,
1472 "tx_worker[%d]: text_command_i "
1473 "failed\n", me->id);
1474 goto done;
1476 break;
1477 case ISCSI_SCSI_CMD:
1478 iscsi_trace(TRACE_ISCSI_CMD,
1479 "tx_worker[%d]: ISCSI_SCSI_CMD\n",
1480 me->id);
1481 if (scsi_command_i(cmd) != 0) {
1482 iscsi_err(__FILE__, __LINE__,
1483 "tx_worker[%d]: scsi_command_i"
1484 " failed\n", me->id);
1485 goto done;
1487 break;
1488 case ISCSI_NOP_OUT:
1489 iscsi_trace(TRACE_ISCSI_CMD,
1490 "tx_worker[%d]: ISCSI_NOP_OUT\n",
1491 me->id);
1492 if (nop_out_i(cmd) != 0) {
1493 iscsi_err(__FILE__, __LINE__,
1494 "tx_worker[%d]: nop_out_i "
1495 "failed\n", me->id);
1496 goto done;
1498 break;
1499 case ISCSI_LOGOUT_CMD:
1500 iscsi_trace(TRACE_ISCSI_CMD,
1501 "tx_worker[%d]: ISCSI_LOGOUT_CMD\n",
1502 me->id);
1503 if (logout_command_i(cmd) != 0) {
1504 iscsi_err(__FILE__, __LINE__,
1505 "tx_worker[%d]: "
1506 "logout_command_i() failed\n",
1507 me->id);
1508 goto done;
1510 break;
1511 default:
1512 iscsi_err(__FILE__, __LINE__,
1513 "tx_worker[%d]: unknown iSCSI command"
1514 " %#x\n", me->id, cmd->type);
1515 cmd->status = -1;
1516 break;
1519 /* Get lock for next iteration */
1521 ISCSI_LOCK(&me->work_mutex, return -1);
1524 * The Rx thread will receive a response for
1525 * the command and execute the callback. We
1526 * need to make sure the callback function is
1527 * not executed before the Tx thread has
1528 * completed sending the command. This is
1529 * what tx_done is used for. The last step is
1530 * to set tx_done and signal the Rx thread,
1531 * which may be block on the condition.
1532 * NOP_OUT (without ping) will have no
1533 * response for the Rx thread to process - so
1534 * we execute the callback directly. */
1536 if ((cmd->type == ISCSI_NOP_OUT) &&
1537 (((iscsi_nop_out_args_t *)(cmd->ptr))->tag ==
1538 0xffffffff)) {
1539 iscsi_trace(TRACE_ISCSI_DEBUG,
1540 "executing callback() function "
1541 "directly for NOP_OUT (no NOP_IN)\n");
1542 if (cmd->callback(cmd) != 0) {
1543 iscsi_err(__FILE__, __LINE__,
1544 "cmd->callback() failed\n");
1545 return -1;
1547 } else {
1548 ISCSI_LOCK(&sess->rx_worker.work_mutex,
1549 return -1);
1550 cmd->tx_done = 1;
1551 ISCSI_SIGNAL(&sess->rx_worker.work_cond,
1552 return -1);
1553 ISCSI_UNLOCK(&sess->rx_worker.work_mutex,
1554 return -1);
1556 } else {
1557 iscsi_trace(TRACE_ISCSI_DEBUG,
1558 "tx_worker[%d]: awaiting condition\n", me->id);
1559 ISCSI_WAIT(&me->work_cond, &me->work_mutex, return -1);
1560 iscsi_trace(TRACE_ISCSI_DEBUG,
1561 "tx_worker[%d]: condition signaled\n", me->id);
1564 done:
1565 if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1566 iscsi_err(__FILE__, __LINE__,
1567 "tx_worker[%d]: session exited prematurely "
1568 "(state %#x)\n", me->id, sess->state);
1569 me->state |= ISCSI_WORKER_STATE_ERROR;
1571 ISCSI_WORKER_EXIT(me);
1572 return 0;
1576 * There is one Rx worker per connection.
1579 static int
1580 rx_worker_proc_i(void *arg)
1582 uint8_t header[ISCSI_HEADER_LEN];
1583 initiator_session_t *sess = (initiator_session_t *) arg;
1584 iscsi_worker_t *me = &sess->rx_worker;
1585 initiator_cmd_t *cmd = NULL;
1586 initiator_cmd_t *prev, *ptr;
1587 uint32_t tag;
1589 ISCSI_THREAD_START("rx_worker");
1590 ISCSI_SET_THREAD(me)
1591 me->state = ISCSI_WORKER_STATE_STARTED;
1592 me->pid = getpid();
1593 ISCSI_LOCK(&me->exit_mutex, return -1);
1594 ISCSI_SIGNAL(&me->exit_cond, return -1);
1595 ISCSI_UNLOCK(&me->exit_mutex, return -1);
1597 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: started (sess %p)\n", me->id, sess);
1599 for (;;) {
1600 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: reading iscsi header (sock %#x) \n",
1601 me->id, (int) sess->sock);
1602 if (iscsi_sock_msg(sess->sock, 0, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
1603 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: iscsi_sock_msg() failed\n", me->id);
1604 goto done;
1606 if (sess->state == INITIATOR_SESSION_STATE_DESTROYING) {
1607 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: session is being destroyed\n", me->id);
1608 goto done;
1610 /* Get cmd ptr from hash table */
1612 if ((ISCSI_OPCODE(header) != ISCSI_REJECT) && (ISCSI_OPCODE(header) != ISCSI_ASYNC)) {
1613 (void) memcpy(&tag, header + 16, sizeof(tag));
1614 tag = ISCSI_NTOHL(tag);
1615 if (tag != 0xffffffff) {
1618 * remove command from g_tag_hash, cmd is
1619 * local so we only need to lock the queue
1620 * remove operation
1623 if ((cmd = hash_remove(&g_tag_hash, tag)) == NULL) {
1624 iscsi_err(__FILE__, __LINE__, "hash_remove() failed\n");
1625 iscsi_trace(TRACE_ISCSI_DEBUG, "no cmd ptr associated with tag %#x\n", tag);
1626 } else {
1627 iscsi_trace(TRACE_ISCSI_DEBUG, "cmd ptr %p associated with tag %#x\n", cmd, tag);
1629 ISCSI_LOCK(&sess->rx_worker.work_mutex, return -1);
1630 if (!cmd->tx_done) {
1631 ISCSI_WAIT(&sess->rx_worker.work_cond, &sess->rx_worker.work_mutex, return -1);
1633 ISCSI_UNLOCK(&sess->rx_worker.work_mutex, return -1);
1635 } else {
1636 iscsi_trace(TRACE_ISCSI_DEBUG, "no command associated with tag %#x\n", tag);
1639 /* Remove cmd ptr from outstanding list */
1640 iscsi_spin_lock(&sess->cmds_spin);
1641 prev = ptr = sess->cmds;
1642 while (ptr != NULL) {
1643 prev = ptr;
1644 if (ptr == cmd)
1645 break;
1646 ptr = ptr->next;
1648 if (ptr != NULL) {
1649 if (prev == sess->cmds) {
1650 sess->cmds = cmd->next;
1651 } else {
1652 prev->next = cmd->next;
1655 iscsi_spin_unlock(&sess->cmds_spin);
1656 switch (ISCSI_OPCODE(header)) {
1657 case ISCSI_SCSI_RSP:
1658 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_SCSI_RSP\n", me->id);
1659 if (scsi_response_i(sess, cmd, header) != 0) {
1660 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_response_i() failed\n", me->id);
1661 goto done;
1663 break;
1664 case ISCSI_READ_DATA:
1665 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_READ_DATA\n", me->id);
1666 if (scsi_read_data_i(sess, cmd, header) != 0) {
1667 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_read_data_i() failed\n", me->id);
1668 goto done;
1670 break;
1671 case ISCSI_R2T:
1672 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_R2T\n", me->id);
1673 if (scsi_r2t_i(sess, cmd, header) != 0) {
1674 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: scsi_r2t_i() failed\n", me->id);
1675 goto done;
1677 break;
1678 case ISCSI_NOP_IN:
1679 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_NOP_IN\n", me->id);
1680 if (nop_in_i(sess, cmd, header) != 0) {
1681 iscsi_err(__FILE__, __LINE__, "nop_in_i() failed\n");
1682 return -1;
1684 break;
1685 case ISCSI_LOGIN_RSP:
1686 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_LOGIN_RSP\n", me->id);
1687 if (login_response_i(sess, cmd, header) != 0) {
1688 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: login_response_i() failed\n", me->id);
1689 goto done;
1691 break;
1692 case ISCSI_TEXT_RSP:
1693 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_TEXT_RSP\n", me->id);
1694 if (text_response_i(sess, cmd, header) != 0) {
1695 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: text_response_i() failed\n", me->id);
1696 goto done;
1698 break;
1699 case ISCSI_LOGOUT_RSP:
1700 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_LOGOUT_RSP\n", me->id);
1701 if (logout_response_i(sess, cmd, header) != 0) {
1702 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: logout_response_i() failed\n", me->id);
1703 goto done;
1705 break;
1706 case ISCSI_REJECT:
1707 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_REJECT\n", me->id);
1708 if (reject_i(sess, header) != 0) {
1709 iscsi_err(__FILE__, __LINE__, "reject_i() failed\n");
1710 return -1;
1712 break;
1713 case ISCSI_ASYNC:
1714 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: ISCSI_ASYNC\n", me->id);
1715 if (async_msg_i(sess, header) != 0) {
1716 iscsi_err(__FILE__, __LINE__, "async_msg_i() failed\n");
1717 goto done;
1719 break;
1720 default:
1721 iscsi_err(__FILE__, __LINE__, "rx_worker[%d]: unexpected iSCSI op %#x\n", me->id, ISCSI_OPCODE(header));
1722 goto done;
1725 done:
1726 if (sess->state != INITIATOR_SESSION_STATE_DESTROYING) {
1728 iscsi_trace(TRACE_ISCSI_DEBUG, "rx_worker[%d]: session exited prematurely (state %#x)\n", me->id, sess->state);
1729 me->state |= ISCSI_WORKER_STATE_ERROR;
1731 ISCSI_WORKER_EXIT(me);
1732 return 0;
1735 static int
1736 text_command_i(initiator_cmd_t * cmd)
1738 iscsi_text_cmd_args_t *text_cmd;
1739 initiator_session_t *sess;
1740 uint8_t header[ISCSI_HEADER_LEN];
1742 text_cmd = (iscsi_text_cmd_args_t *) cmd->ptr;
1743 sess = g_target[(int)cmd->isid].sess;
1745 * Insert cmd into the hash table, keyed by the tag. The Rx thread
1746 * will
1748 /* retreive the cmd ptr using the tag from the response PDU. */
1750 if (hash_insert(&g_tag_hash, cmd, text_cmd->tag) != 0) {
1751 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
1752 return -1;
1754 /* Send text command PDU */
1756 text_cmd->ExpStatSN = sess->ExpStatSN;
1757 text_cmd->CmdSN = sess->CmdSN++;
1758 iscsi_trace(TRACE_ISCSI_DEBUG, "sending text command\n");
1759 if (iscsi_text_cmd_encap(header, text_cmd) != 0) {
1760 iscsi_err(__FILE__, __LINE__, "(iscsi_text_cmd_encap() failed\n");
1761 return -1;
1763 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, text_cmd->text, text_cmd->length, 0)
1764 != ISCSI_HEADER_LEN + text_cmd->length) {
1765 iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed.\n");
1766 return -1;
1768 iscsi_trace(TRACE_ISCSI_DEBUG, "text command sent ok\n");
1770 return 0;
1773 static int
1774 login_command_i(initiator_cmd_t * cmd)
1776 iscsi_login_cmd_args_t *login_cmd;
1777 initiator_session_t *sess;
1778 uint8_t header[ISCSI_HEADER_LEN];
1780 login_cmd = (iscsi_login_cmd_args_t *) cmd->ptr;
1781 sess = g_target[(int)cmd->isid].sess;
1783 * Insert cmd into the hash table, keyed by the tag. The Rx thread
1784 * will
1786 /* retreive the cmd ptr using the tag from the response PDU. */
1788 if (hash_insert(&g_tag_hash, cmd, login_cmd->tag) != 0) {
1789 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
1790 return -1;
1792 /* Send login command PDU */
1793 login_cmd->ExpStatSN = sess->ExpStatSN;
1794 iscsi_trace(TRACE_ISCSI_DEBUG, "sending login command\n");
1795 if (iscsi_login_cmd_encap(header, login_cmd) != 0) {
1796 iscsi_err(__FILE__, __LINE__, "(iscsi_login_cmd_encap() failed\n");
1797 return -1;
1799 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, login_cmd->text, login_cmd->length, 0)
1800 != ISCSI_HEADER_LEN + login_cmd->length) {
1801 iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed.\n");
1802 return -1;
1804 iscsi_trace(TRACE_ISCSI_DEBUG, "login command sent ok\n");
1806 return 0;
1809 static int
1810 logout_phase_i(initiator_session_t * sess)
1812 initiator_cmd_t *cmd = NULL;
1813 iscsi_logout_cmd_args_t *logout_cmd = NULL;
1814 initiator_wait_t iwait;
1816 sess->state = INITIATOR_SESSION_STATE_LOGGING_OUT;
1818 /* Allocate command pointers */
1820 if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
1821 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1822 return -1;
1824 (void) memset(cmd, 0x0, sizeof(*cmd));
1825 if ((logout_cmd = iscsi_malloc_atomic(sizeof(iscsi_logout_cmd_args_t))) == NULL) {
1826 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1827 if (cmd != NULL)
1828 iscsi_free_atomic(cmd);
1829 return -1;
1831 #define LO_CLEANUP {if (cmd != NULL) iscsi_free_atomic(cmd); if (logout_cmd != NULL) iscsi_free_atomic(logout_cmd); }
1832 #define LO_ERROR {LO_CLEANUP; return -1;}
1833 (void) memset(logout_cmd, 0x0, sizeof(*logout_cmd));
1835 /* Build logout command */
1837 logout_cmd->cid = sess->cid;
1838 logout_cmd->reason = ISCSI_LOGOUT_CLOSE_SESSION;
1839 ISCSI_SET_TAG(&logout_cmd->tag);
1840 logout_cmd->ExpStatSN = sess->ExpStatSN;
1841 logout_cmd->CmdSN = sess->CmdSN++;
1843 /* Build wait for callback */
1845 ISCSI_MUTEX_INIT(&iwait.mutex, LO_ERROR);
1846 ISCSI_COND_INIT(&iwait.cond, LO_ERROR);
1848 /* Build initiator command */
1850 cmd->type = ISCSI_LOGOUT_CMD;
1851 cmd->ptr = logout_cmd;
1852 cmd->callback = wait_callback_i;
1853 cmd->callback_arg = &iwait;
1854 cmd->isid = sess->isid;
1856 /* Enqueue to Tx worker */
1858 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueing logout command to tx worker %llu\n", sess->isid);
1859 ISCSI_LOCK(&iwait.mutex, LO_ERROR);
1860 ISCSI_LOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1861 if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1862 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1863 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1864 LO_ERROR;
1866 ISCSI_SIGNAL(&sess->tx_worker.work_cond, LO_ERROR);
1867 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LO_ERROR);
1868 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued logout command ok\n");
1870 /* Wait for callback */
1872 iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on logout callback\n");
1873 ISCSI_WAIT(&iwait.cond, &iwait.mutex, LO_ERROR);
1874 ISCSI_UNLOCK(&iwait.mutex, LO_ERROR);
1875 ISCSI_COND_DESTROY(&iwait.cond, LO_ERROR);
1876 ISCSI_MUTEX_DESTROY(&iwait.mutex, LO_ERROR);
1877 iscsi_trace(TRACE_ISCSI_DEBUG, "received logout callback ok\n");
1879 sess->state = INITIATOR_SESSION_STATE_LOGGED_OUT;
1881 LO_CLEANUP;
1882 return 0;
1885 static void
1886 alarm_handler(int arg)
1888 USE_ARG(arg);
1889 iscsi_err(__FILE__, __LINE__, "***aborting cmd 0x%p***\n", g_cmd);
1890 if (initiator_abort(g_cmd) != 0) {
1891 iscsi_err(__FILE__, __LINE__, "initiator_abort() failed\n");
1895 static int
1896 login_phase_i(initiator_session_t * sess, char *text, int text_len)
1898 initiator_cmd_t *cmd = NULL;
1899 initiator_wait_t iwait;
1900 iscsi_login_cmd_args_t *login_cmd = NULL;
1901 struct sigaction act;
1903 sess->state = INITIATOR_SESSION_STATE_LOGGING_IN;
1905 /* Allocate command pointers */
1907 if ((cmd = iscsi_malloc_atomic(sizeof(initiator_cmd_t))) == NULL) {
1908 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1909 return -1;
1911 (void) memset(cmd, 0x0, sizeof(*cmd));
1912 if ((login_cmd = iscsi_malloc_atomic(sizeof(iscsi_login_cmd_args_t))) == NULL) {
1913 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
1914 if (cmd != NULL)
1915 iscsi_free_atomic(cmd);
1916 return -1;
1918 #define LI_CLEANUP {if (cmd != NULL) iscsi_free_atomic(cmd); if (login_cmd != NULL) iscsi_free_atomic(login_cmd); }
1919 #define LI_ERROR {LI_CLEANUP; return -1;}
1920 (void) memset(login_cmd, 0x0, sizeof(*login_cmd));
1922 /* This is the length of our original offer. */
1924 login_cmd->text = text;
1925 login_cmd->length = text_len;
1926 login_cmd->transit = 1;
1927 login_cmd->csg = ISCSI_LOGIN_STAGE_SECURITY;
1928 login_cmd->nsg = ISCSI_LOGIN_STAGE_NEGOTIATE;
1929 ISCSI_SET_TAG(&login_cmd->tag);
1930 login_cmd->CmdSN = sess->CmdSN = 0;
1932 do {
1935 * Build login command. Note that the <length> and
1936 * <text> fields may get updated by login_response_i.
1937 * Such is the case when we receive offers from the
1938 * target. The new <length> and <text> fields will
1939 * represent the response that we need to send to the
1940 * target on the next login.
1943 login_cmd->cont = 0;
1944 login_cmd->version_min = ISCSI_VERSION;
1945 login_cmd->version_max = ISCSI_VERSION;
1946 login_cmd->cid = sess->cid = (int)sess->isid;
1947 login_cmd->isid = sess->isid = sess->isid;
1948 login_cmd->tsih = 0;
1950 /* Build wait for callback */
1952 ISCSI_MUTEX_INIT(&iwait.mutex, LI_ERROR);
1953 ISCSI_COND_INIT(&iwait.cond, LI_ERROR);
1955 /* Build initiator command */
1957 cmd->type = ISCSI_LOGIN_CMD;
1958 cmd->ptr = login_cmd;
1959 cmd->callback = wait_callback_i;
1960 cmd->callback_arg = &iwait;
1961 cmd->isid = sess->isid;
1963 /* Set Alarm */
1965 g_cmd = cmd;
1966 act.sa_handler = alarm_handler;
1967 sigaction(SIGALRM, &act, NULL);
1968 alarm(5);
1970 /* Enqueue initiator command to Tx worker */
1972 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueing login command to tx worker %llu\n", sess->isid);
1973 ISCSI_LOCK(&iwait.mutex, LI_ERROR);
1974 ISCSI_LOCK(&sess->tx_worker.work_mutex, LI_ERROR);
1975 if (iscsi_queue_insert(&sess->tx_queue, cmd) == -1) {
1976 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LI_ERROR);
1977 iscsi_err(__FILE__, __LINE__, "iscsi_queue_insert() failed\n");
1978 LI_ERROR;
1981 ISCSI_SIGNAL(&sess->tx_worker.work_cond, LI_ERROR);
1982 ISCSI_UNLOCK(&sess->tx_worker.work_mutex, LI_ERROR);
1983 iscsi_trace(TRACE_ISCSI_DEBUG, "enqueued login command ok\n");
1985 /* Wait for callback */
1987 iscsi_trace(TRACE_ISCSI_DEBUG, "waiting on login callback\n");
1988 ISCSI_WAIT(&iwait.cond, &iwait.mutex, LI_ERROR);
1989 ISCSI_UNLOCK(&iwait.mutex, LI_ERROR);
1990 ISCSI_COND_DESTROY(&iwait.cond, LI_ERROR);
1991 ISCSI_MUTEX_DESTROY(&iwait.mutex, LI_ERROR);
1992 iscsi_trace(TRACE_ISCSI_DEBUG, "received login callback ok\n");
1994 alarm(0);
1996 if (cmd->status != 0) {
1997 iscsi_err(__FILE__, __LINE__, "initiator_cmd_t failed\n");
1998 LI_ERROR;
2000 if (sess->state == INITIATOR_SESSION_STATE_LOGGING_IN) {
2001 iscsi_trace(TRACE_ISCSI_PARAM, "more negotiation needed (sending %d bytes response parameters)\n",
2002 login_cmd->length);
2004 } while (sess->state == INITIATOR_SESSION_STATE_LOGGING_IN);
2005 iscsi_trace(TRACE_ISCSI_DEBUG, "login phase completed successfully\n");
2007 LI_CLEANUP;
2008 return 0;
2012 #define TEXT_RESPONSE_TEXT_LEN 2048
2014 static int
2015 text_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2017 iscsi_text_cmd_args_t *text_cmd;
2018 iscsi_text_rsp_args_t text_rsp;
2019 iscsi_parameter_t *l = sess->params;
2020 char *text_in = NULL;
2021 char *text_out = NULL;
2022 int len_in = 0;
2023 int len_out = 0;
2024 int ret = 0;
2026 #define TI_CLEANUP {if (text_in != NULL) iscsi_free_atomic(text_in); if (text_out != NULL) iscsi_free_atomic(text_out);}
2027 #define TI_ERROR {cmd->status=-1; goto callback;}
2028 if (cmd) {
2029 text_cmd = (iscsi_text_cmd_args_t *) cmd->ptr;
2030 } else {
2031 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_text_cmd_args_t??\n");
2032 return -1;
2035 /* Check arguments & update numbering */
2037 if (iscsi_text_rsp_decap(header, &text_rsp) != 0) {
2038 iscsi_err(__FILE__, __LINE__, "text_response_decap() failed\n");
2039 TI_ERROR;
2041 if (text_rsp.tag != text_cmd->tag) {
2042 iscsi_err(__FILE__, __LINE__,
2043 "Bad \"Tag\": %u != %u.\n",
2044 text_rsp.tag, text_cmd->tag);
2045 TI_ERROR;
2047 if (text_rsp.transfer_tag != 0xffffffff) {
2048 iscsi_err(__FILE__, __LINE__,
2049 "Bad \"Transfer Tag\": %u != %u.\n",
2050 text_rsp.transfer_tag, 0xffffffff);
2051 TI_ERROR;
2053 if (text_rsp.StatSN != sess->ExpStatSN) {
2054 iscsi_err(__FILE__, __LINE__,
2055 "Bad \"StatSN\": %u != %u.\n",
2056 text_rsp.StatSN, sess->ExpStatSN);
2057 TI_ERROR;
2059 if (text_rsp.ExpCmdSN != sess->CmdSN) {
2060 iscsi_err(__FILE__, __LINE__,
2061 "Bad \"ExpCmdSN\": %u != %u.\n",
2062 text_rsp.ExpCmdSN, sess->CmdSN);
2063 TI_ERROR;
2065 sess->ExpStatSN = text_rsp.StatSN + 1;
2067 /* Parse input text parameters and generate any response */
2069 if ((len_in = text_rsp.length) != 0) {
2070 iscsi_trace(TRACE_ISCSI_PARAM, "allocating %d bytes input parameters\n", len_in);
2071 if ((text_in = iscsi_malloc_atomic((unsigned)len_in + 1)) == NULL) {
2072 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2073 TI_ERROR;
2075 if ((text_out = iscsi_malloc_atomic(TEXT_RESPONSE_TEXT_LEN)) == NULL) {
2076 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2077 if (text_in != NULL)
2078 iscsi_free_atomic(text_in);
2079 TI_ERROR;
2081 if (iscsi_sock_msg(sess->sock, 0, (unsigned)len_in, text_in, 0) != len_in) {
2082 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2083 TI_ERROR;
2085 text_in[len_in] = 0x0;
2086 iscsi_trace(TRACE_ISCSI_PARAM, "read %d bytes input parameters ok\n", len_in);
2088 /* Reset the value lists for TargetName and TargetAddress */
2090 if (param_val_reset(sess->params, "TargetName") != 0) {
2091 iscsi_err(__FILE__, __LINE__, "parm_val_reset() failed\n");
2092 TI_ERROR;
2094 if (param_val_reset(sess->params, "TargetAddress") != 0) {
2095 iscsi_err(__FILE__, __LINE__, "parm_val_reset() failed\n");
2096 TI_ERROR;
2098 /* Parse the incoming answer */
2100 PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_in, len_in, text_out, &len_out, TEXT_RESPONSE_TEXT_LEN, 0, TI_ERROR);
2102 if (len_out) {
2103 if (text_rsp.final != 0) {
2104 iscsi_err(__FILE__, __LINE__,
2105 "Bad \"text_rsp.final\": %u != 0.\n",
2106 text_rsp.final);
2107 TI_ERROR;
2110 * Copy response text into text_cmd->text and
2111 * update the length text_cmd->length. This
2112 * will be sent out on the next text command.
2113 * */
2115 PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_out, len_out, NULL, NULL, TEXT_RESPONSE_TEXT_LEN, 1, TI_ERROR);
2117 iscsi_trace(TRACE_ISCSI_PARAM, "need to send %d bytes response back to target\n", len_out);
2118 text_cmd->length = len_out;
2119 memcpy(text_cmd->text, text_out, (size_t)len_out);
2120 } else {
2121 text_cmd->length = 0;
2124 text_cmd->final = text_rsp.final;
2126 /* Issue callback */
2128 iscsi_trace(TRACE_ISCSI_DEBUG, "iscsi_text_cmd_args_t done\n");
2129 callback:
2130 if (cmd->status == -1)
2131 ret = -1;
2132 if (cmd->callback(cmd) != 0) {
2133 ret = -1;
2134 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2136 TI_CLEANUP;
2137 return ret;
2140 #define LOGIN_RESPONSE_TEXT_LEN 2048
2142 static int
2143 login_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2145 iscsi_login_cmd_args_t *login_cmd;
2146 iscsi_login_rsp_args_t login_rsp;
2147 iscsi_parameter_t *l = sess->params;
2148 char *text_in = NULL;
2149 char *text_out = NULL;
2150 int len_in = 0;
2151 int len_out = 0;
2153 if ((text_out = iscsi_malloc_atomic(LOGIN_RESPONSE_TEXT_LEN)) == NULL) {
2154 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2155 cmd->status = -1;
2156 goto callback;
2158 #define LIR_CLEANUP {if (text_in != NULL) iscsi_free_atomic(text_in); if (text_out != NULL) iscsi_free_atomic(text_out);}
2159 #define LIR_ERROR {cmd->status=-1; goto callback;}
2160 if (cmd) {
2161 login_cmd = (iscsi_login_cmd_args_t *) cmd->ptr;
2162 } else {
2163 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_login_cmd_args_t??\n");
2164 LIR_ERROR;
2167 /* Read login response */
2169 if (iscsi_login_rsp_decap(header, &login_rsp) != 0) {
2170 iscsi_err(__FILE__, __LINE__, "login_response_decap() failed\n");
2171 LIR_ERROR;
2173 if (login_rsp.length > 8192) {
2174 iscsi_err(__FILE__, __LINE__, "login_rsp.length %u\n",
2175 login_rsp.length);
2176 LIR_CLEANUP;
2177 return -1;
2180 /* Read & parse text response */
2181 if ((len_in = login_rsp.length) != 0) {
2182 if ((text_in = iscsi_malloc_atomic((unsigned)len_in + 1)) == NULL) {
2183 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2184 LIR_ERROR;
2186 if (iscsi_sock_msg(sess->sock, 0, (unsigned)len_in, text_in, 0) != len_in) {
2187 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2188 LIR_ERROR;
2190 text_in[len_in] = 0x0;
2191 PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_in, len_in, text_out, &len_out, LOGIN_RESPONSE_TEXT_LEN, 0, LIR_ERROR);
2192 if (login_rsp.transit && len_out != 0) {
2193 iscsi_warn(__FILE__, __LINE__,
2194 "Bad \"len_out\": Got %u expected %u.\n",
2195 len_out, 0);
2199 /* Check args */
2200 if (login_rsp.status_class != 0) {
2201 iscsi_err(__FILE__, __LINE__, "Bad Status-Class: got %d, expected %d\n", login_rsp.status_class, 0);
2202 LIR_ERROR;
2204 if (login_rsp.tag != login_cmd->tag) {
2205 iscsi_err(__FILE__, __LINE__, "Bad Tag: got %x, expected %x\n", login_rsp.tag, login_cmd->tag);
2206 LIR_ERROR;
2208 sess->ExpStatSN = login_rsp.StatSN + 1;
2211 if (login_rsp.transit) {
2213 if (login_cmd->transit != 1)
2214 iscsi_warn(__FILE__, __LINE__, "incoming packet transit bit not set, csg = %d, nsg = %d\n",
2215 login_cmd->csg, login_cmd->nsg);
2217 switch (login_rsp.nsg) {
2218 case ISCSI_LOGIN_STAGE_NEGOTIATE:
2219 login_cmd->csg = login_cmd->nsg;
2220 login_cmd->nsg = ISCSI_LOGIN_STAGE_FULL_FEATURE;
2221 if (params_out(sess, text_out, &len_out, LOGIN_RESPONSE_TEXT_LEN, SESS_TYPE_NONE, /*LINTED*/!IS_SECURITY) != 0) {
2222 iscsi_err(__FILE__, __LINE__, "params_out() failed\n");
2223 LIR_ERROR;
2225 login_cmd->length = len_out;
2226 (void) memcpy(login_cmd->text, text_out,
2227 (size_t)len_out);
2228 break;
2230 case ISCSI_LOGIN_STAGE_FULL_FEATURE:
2231 /* Check post conditions */
2233 if (login_rsp.tsih == 0) {
2234 iscsi_err(__FILE__, __LINE__,
2235 "Bad \"TSIH\": %u == 0.\n", login_rsp.tsih);
2236 LIR_ERROR;
2238 if (login_rsp.isid != login_cmd->isid) {
2239 iscsi_err(__FILE__, __LINE__,
2240 "Bad \"ISID\": %" PRIu64 "u != %"
2241 PRIu64 "u.\n",
2242 (unsigned)login_rsp.isid,
2243 (unsigned)login_cmd->isid);
2244 LIR_ERROR;
2246 if (login_rsp.ExpCmdSN != login_cmd->CmdSN) {
2247 iscsi_err(__FILE__, __LINE__,
2248 "Bad \"ExpCmdSN\": %u != %u.\n",
2249 (unsigned)login_rsp.ExpCmdSN,
2250 (unsigned)login_cmd->CmdSN);
2251 LIR_ERROR;
2253 if (login_rsp.ExpCmdSN > login_rsp.MaxCmdSN) {
2254 iscsi_err(__FILE__, __LINE__,
2255 "Bad \"MaxCmdSN\": %u > %u.\n",
2256 (unsigned)login_rsp.ExpCmdSN,
2257 (unsigned)login_rsp.MaxCmdSN);
2258 LIR_ERROR;
2261 /* Set remaining session parameters */
2263 sess->CmdSN = login_rsp.ExpCmdSN;
2264 sess->MaxCmdSN = login_rsp.MaxCmdSN;
2265 sess->tsih = login_rsp.tsih;
2266 sess->isid = login_rsp.isid;
2268 if (param_equiv(sess->params, "SessionType", "Normal")) {
2269 sess->state = INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL;
2270 } else if (param_equiv(sess->params, "SessionType", "Discovery")) {
2271 sess->state = INITIATOR_SESSION_STATE_LOGGED_IN_DISCOVERY;
2272 } else {
2273 iscsi_err(__FILE__, __LINE__, "Unknown SessionType \"%s\"\n", param_val(sess->params, "SessionType"));
2274 LIR_ERROR;
2277 iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2278 iscsi_trace(TRACE_ISCSI_DEBUG, "* LOGIN SUCCESSFUL *\n");
2279 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CID", sess->cid);
2280 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20llu *\n", "ISID", sess->isid);
2281 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "TSIH", sess->tsih);
2282 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CmdSN", sess->CmdSN);
2283 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "MaxCmdSN", sess->MaxCmdSN);
2284 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "ExpStatSN", sess->ExpStatSN);
2285 iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2286 break;
2287 default:
2288 LIR_ERROR;
2290 } else {
2291 iscsi_trace(TRACE_ISCSI_DEBUG, "received partial login response\n");
2293 /* Copy response text into login_cmd->text and update the */
2294 /* length login_cmd->length. This will be sent out on the */
2295 /* next login command. */
2297 if (len_out) {
2298 PARAM_TEXT_PARSE(l, &sess->sess_params.cred, text_out, len_out, NULL, NULL, 0, 1, LIR_ERROR);
2299 iscsi_trace(TRACE_ISCSI_PARAM, "need to send %d bytes response back to target\n", len_out);
2301 login_cmd->length = len_out;
2302 memcpy(login_cmd->text, text_out, (size_t)len_out);
2303 if (strncmp(text_out, "CHAP_N=", strlen("CHAP_N=")) == 0) {
2304 login_cmd->nsg = ISCSI_LOGIN_STAGE_NEGOTIATE;
2305 login_cmd->transit = 1;
2307 } else {
2308 login_cmd->length = 0;
2312 /* Callback */
2314 callback:
2315 iscsi_trace(TRACE_ISCSI_DEBUG, "iscsi_login_cmd_args_t done (cmd status %d, iscsi status %d)\n",
2316 cmd->status, login_rsp.status_class);
2317 if ((*cmd->callback)(cmd) != 0) {
2318 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2319 LIR_CLEANUP;
2320 return -1;
2322 LIR_CLEANUP;
2323 return 0;
2326 static int
2327 logout_command_i(initiator_cmd_t * cmd)
2329 iscsi_logout_cmd_args_t *logout_cmd;
2330 initiator_session_t *sess;
2331 uint8_t header[ISCSI_HEADER_LEN];
2333 logout_cmd = (iscsi_logout_cmd_args_t *) cmd->ptr;
2334 sess = g_target[(int)cmd->isid].sess;
2336 * Insert cmd into the hash table, keyed by the tag. The Rx thread
2337 * will
2339 /* retreive the cmd ptr using the tag from the response PDU. */
2341 if (hash_insert(&g_tag_hash, cmd, logout_cmd->tag) != 0) {
2342 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2343 return -1;
2345 /* Send logout command PDU */
2347 iscsi_trace(TRACE_ISCSI_DEBUG, "sending logout command\n");
2348 if (iscsi_logout_cmd_encap(header, logout_cmd) != 0) {
2349 iscsi_err(__FILE__, __LINE__, "iscsi_logout_cmd_encap() failed\n");
2350 return -1;
2352 if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2353 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed.\n");
2354 return -1;
2356 iscsi_trace(TRACE_ISCSI_DEBUG, "logout command sent ok\n");
2358 return 0;
2362 static int
2363 logout_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2365 iscsi_logout_cmd_args_t *logout_cmd;
2366 iscsi_logout_rsp_args_t logout_rsp;
2368 #define LOR_ERROR {cmd->status=-1; goto callback;}
2369 if (cmd) {
2370 if (cmd->ptr) {
2371 logout_cmd = (iscsi_logout_cmd_args_t *) cmd->ptr;
2372 } else {
2373 iscsi_err(__FILE__, __LINE__, "no iscsi_logout_cmd_args_t specified for initiator_cmd_t??\n");
2374 LOR_ERROR;
2376 } else {
2377 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t specified for iscsi_logout_cmd_args_t??\n");
2378 return -1;
2380 if (iscsi_logout_rsp_decap(header, &logout_rsp) != 0) {
2381 iscsi_err(__FILE__, __LINE__, "iscsi_logout_rsp_decap() failed\n");
2382 LOR_ERROR;
2384 if (logout_rsp.response != ISCSI_LOGOUT_STATUS_SUCCESS) {
2385 iscsi_err(__FILE__, __LINE__, "Bad \"Response\": Got %u\n",
2386 logout_rsp.response);
2387 LOR_ERROR;
2389 if (logout_rsp.tag != logout_cmd->tag) {
2390 iscsi_err(__FILE__, __LINE__, "Bad \"Tag\": Got %u\n",
2391 logout_rsp.tag);
2392 LOR_ERROR;
2395 /* Check and update numbering */
2396 if (logout_rsp.StatSN != sess->ExpStatSN) {
2397 iscsi_err(__FILE__, __LINE__,
2398 "Bad \"StatSN\": Got %u, needed %u\n",
2399 logout_rsp.StatSN, sess->ExpStatSN);
2400 LOR_ERROR;
2402 sess->ExpStatSN += 1;
2403 if (logout_rsp.ExpCmdSN != sess->CmdSN) {
2404 iscsi_err(__FILE__, __LINE__,
2405 "Bad \"ExpCmdSN\": Got %u, needed %u\n",
2406 logout_rsp.ExpCmdSN, sess->CmdSN);
2407 LOR_ERROR;
2409 sess->MaxCmdSN = logout_rsp.MaxCmdSN;
2411 /* Callback */
2412 cmd->status = 0;
2413 iscsi_trace(TRACE_ISCSI_DEBUG,
2414 "LOGOUT_CMD_T done (cmd status %d, iscsi status %d)\n",
2415 cmd->status, logout_rsp.response);
2416 callback:
2417 if ((*cmd->callback)(cmd) != 0) {
2418 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2419 return -1;
2422 iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2423 iscsi_trace(TRACE_ISCSI_DEBUG, "* LOGOUT SUCCESSFUL *\n");
2424 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "CID", sess->cid);
2425 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20llu *\n", "ISID", sess->isid);
2426 iscsi_trace(TRACE_ISCSI_DEBUG, "* %20s:%20u *\n", "TSIH", sess->tsih);
2427 iscsi_trace(TRACE_ISCSI_DEBUG, "*********************************************\n");
2429 return 0;
2432 static int
2433 nop_out_i(initiator_cmd_t * cmd)
2435 uint8_t header[ISCSI_HEADER_LEN];
2436 iscsi_nop_out_args_t *nop_out;
2437 initiator_session_t *sess;
2438 int rc, length;
2440 nop_out = cmd->ptr;
2441 sess = g_target[(int)cmd->isid].sess;
2442 length = nop_out->length;
2443 if (nop_out->tag != 0xffffffff) {
2446 * Insert cmd into the hash table, keyed by
2447 * nop_out->tag. Upon receipt of the NOP_IN_T, the Rx
2448 * thread will retreive the cmd ptr using the tag from
2449 * the NOP_IN_T PDU. */
2451 if (hash_insert(&g_tag_hash, cmd, nop_out->tag) != 0) {
2452 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2453 return -1;
2456 /* Encapsulate and send NOP */
2458 nop_out->ExpStatSN = sess->ExpStatSN;
2459 /* nop_out->CmdSN = sess->CmdSN++; */
2460 nop_out->transfer_tag = 0xffffffff;
2461 if (iscsi_nop_out_encap(header, nop_out) != 0) {
2462 iscsi_err(__FILE__, __LINE__, "iscsi_nop_out_encap() failed\n");
2463 return -1;
2466 * We need to make a copy of nop_out->length and save in the
2467 * variable length. Otherwise, we may get a seg fault - as if
2468 * this is a NOP_OUT without ping, the Tx thread will issue
2469 * the callback function immediately after we return - thereby
2470 * de-allocating the NOP_OUT and initiator command structures.
2471 * */
2473 if ((rc = iscsi_sock_send_header_and_data(sess->sock, header,
2474 ISCSI_HEADER_LEN, nop_out->data, (unsigned)length,
2475 0)) != ISCSI_HEADER_LEN + length) {
2476 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed: got %d expected %d\n", rc, ISCSI_HEADER_LEN + length);
2477 return -1;
2479 cmd->status = 0;
2480 return 0;
2483 static int
2484 scsi_command_i(initiator_cmd_t * cmd)
2486 iscsi_scsi_cmd_args_t *scsi_cmd;
2487 uint8_t header[ISCSI_HEADER_LEN];
2488 uint64_t target;
2489 initiator_session_t *sess;
2490 iscsi_write_data_t data;
2491 struct iovec sg_singleton;
2492 struct iovec *sg, *sg_copy, *sg_copy_orig, *sg_which;
2493 int sg_len, sg_len_copy, sg_len_which;
2494 int fragment_flag;
2496 scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr;
2497 target = cmd->isid;
2498 sess = g_target[(int)target].sess;
2499 fragment_flag = 0;
2500 sg = sg_copy = sg_copy_orig = sg_which = NULL;
2501 sg_len = sg_len_copy = sg_len_which = 0;
2502 scsi_cmd->status = 0;
2504 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);
2506 if ((uint32_t)target > CONFIG_INITIATOR_NUM_TARGETS) {
2507 iscsi_err(__FILE__, __LINE__, "target %u\n",
2508 (uint32_t)target);
2509 NO_CLEANUP;
2510 return -1;
2513 /* Set and check scsi_cmd */
2514 if (scsi_cmd->trans_len > sess->sess_params.max_burst_length) {
2515 iscsi_err(__FILE__, __LINE__, "scsi_cmd->trans_len (%u) > MaxBurstLength (%u)\n",
2516 scsi_cmd->trans_len, sess->sess_params.max_burst_length);
2517 return -1;
2519 if (scsi_cmd->length > scsi_cmd->trans_len) {
2520 iscsi_err(__FILE__, __LINE__, "scsi_cmd->length (%u) > scsi_cmd->trans_len (%u)\n",
2521 scsi_cmd->length, scsi_cmd->trans_len);
2522 return -1;
2524 scsi_cmd->ExpStatSN = sess->ExpStatSN;
2525 scsi_cmd->CmdSN = sess->CmdSN;
2526 scsi_cmd->bytes_sent = scsi_cmd->bytes_recv = 0;
2528 /* Always use iovec for data */
2530 if (scsi_cmd->output) {
2531 if (scsi_cmd->send_sg_len) { /* Data already an iovec */
2532 sg = (struct iovec *)(void *)scsi_cmd->send_data;
2533 sg_len = scsi_cmd->send_sg_len;
2534 } else { /* Make iovec for data */
2535 sg_singleton.iov_base = scsi_cmd->send_data;
2536 sg_singleton.iov_len = scsi_cmd->trans_len;
2537 sg = &sg_singleton;
2538 sg_len = 1;
2542 * Insert cmd into the hash table, keyed by scsi_cmd->tag. The Rx
2543 * thread will
2545 /* retreive the cmd ptr using the tag from the response PDU. */
2547 if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
2548 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
2549 goto error;
2551 /* Send command PDU */
2553 if (scsi_cmd->output && sess->sess_params.immediate_data) {
2554 if (sess->sess_params.max_dataseg_len) {
2555 scsi_cmd->length = MIN(sess->sess_params.max_dataseg_len,
2556 scsi_cmd->trans_len);
2557 } else {
2558 scsi_cmd->length = scsi_cmd->trans_len;
2560 if (scsi_cmd->length == scsi_cmd->trans_len)
2561 scsi_cmd->final = 1;
2562 } else {
2563 scsi_cmd->length = 0;
2564 scsi_cmd->final = 1;
2566 if (iscsi_scsi_cmd_encap(header, scsi_cmd) != 0) {
2567 iscsi_err(__FILE__, __LINE__, "iscsi_scsi_cmd_encap() failed\n");
2568 goto error;
2571 * If we're sending any immediate data, we need to make a new
2572 * iovec that contains only the immediata data (a subset of
2573 * the original iovec). */
2574 iscsi_trace(TRACE_ISCSI_DEBUG, "sending command PDU with %u bytes immediate data\n", scsi_cmd->length);
2575 if (scsi_cmd->length && sess->sess_params.immediate_data) {
2576 if ((sg_copy = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
2577 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2578 goto error;
2580 fragment_flag++;
2581 sg_copy_orig = sg_copy;
2582 memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
2583 sg_len_copy = sg_len;
2584 if (modify_iov(&sg_copy, &sg_len_copy, 0, scsi_cmd->length) != 0) {
2585 iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
2586 goto error;
2588 if (scsi_cmd->ahs) {
2589 if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2590 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2591 goto error;
2593 if (iscsi_sock_msg(sess->sock, 1, (unsigned)scsi_cmd->ahs_len, scsi_cmd->ahs, 0) != scsi_cmd->ahs_len) {
2594 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2595 goto error;
2597 if ((unsigned)iscsi_sock_msg(sess->sock, 1, scsi_cmd->length, sg_copy, sg_len_copy) != scsi_cmd->length) {
2598 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2599 goto error;
2601 } else {
2602 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_copy, scsi_cmd->length, sg_len_copy)
2603 != ISCSI_HEADER_LEN + scsi_cmd->length) {
2604 iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
2605 goto error;
2608 scsi_cmd->bytes_sent += scsi_cmd->length;
2609 } else {
2610 if (iscsi_sock_msg(sess->sock, 1, ISCSI_HEADER_LEN, header, 0) != ISCSI_HEADER_LEN) {
2611 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2612 goto error;
2614 if (scsi_cmd->ahs_len) {
2615 if (iscsi_sock_msg(sess->sock, 1, (unsigned)scsi_cmd->ahs_len, scsi_cmd->ahs, 0) != scsi_cmd->ahs_len) {
2616 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2617 goto error;
2621 iscsi_trace(TRACE_ISCSI_DEBUG, "command PDU sent with %u bytes immediate data (%u bytes AHS)\n", scsi_cmd->length, scsi_cmd->ahs_len);
2624 * Send data PDUS if 1) we're not in R2T mode and 2) we
2625 * haven't sent everything as immediate data and 3) we have
2626 * not reached the first burst when sending immediate data
2628 if (scsi_cmd->output
2629 && (!sess->sess_params.initial_r2t)
2630 && (scsi_cmd->bytes_sent != scsi_cmd->trans_len)
2631 && ((!sess->sess_params.first_burst_length)
2632 || (scsi_cmd->bytes_sent < sess->sess_params.first_burst_length))) {
2634 uint32_t DataSN = 0;
2636 iscsi_trace(TRACE_ISCSI_DEBUG, "preparing to send %d bytes write data\n", scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2638 do {
2639 (void) memset(&data, 0x0, sizeof(data));
2642 * Take into account that MaxRecvPDULength and
2643 * FirstBurstLength could both be "0" (no limit)
2645 if (sess->sess_params.max_dataseg_len) {
2646 if (sess->sess_params.first_burst_length) {
2647 data.length = MIN_3(
2648 sess->sess_params.first_burst_length - scsi_cmd->bytes_sent,
2649 sess->sess_params.max_dataseg_len,
2650 scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2651 } else {
2652 data.length = MIN(
2653 sess->sess_params.max_dataseg_len,
2654 scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2656 } else {
2657 if (sess->sess_params.first_burst_length) {
2658 data.length = MIN(
2659 sess->sess_params.first_burst_length - scsi_cmd->bytes_sent,
2660 scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2661 } else {
2662 data.length = scsi_cmd->trans_len - scsi_cmd->bytes_sent;
2665 #define FRAG_CLEANUP {if (fragment_flag) iscsi_free_atomic(sg_copy);}
2667 if (data.length == 0) {
2668 iscsi_err(__FILE__, __LINE__,
2669 "Zero data.length\n");
2670 FRAG_CLEANUP;
2671 return -1;
2674 if (scsi_cmd->bytes_sent + data.length ==
2675 scsi_cmd->trans_len) {
2676 data.final = 1;
2678 data.tag = scsi_cmd->tag;
2679 data.transfer_tag = 0xffffffff;
2680 data.ExpStatSN = sess->ExpStatSN;
2681 data.DataSN = DataSN++;
2682 data.offset = scsi_cmd->bytes_sent;
2684 if (iscsi_write_data_encap(header, &data) != 0) {
2685 iscsi_err(__FILE__, __LINE__, "iscsi_write_data_encap() failed\n");
2686 goto error;
2688 if (data.length != scsi_cmd->trans_len) {
2691 * Make copy of iovec and modify with offset
2692 * and length
2695 if (!fragment_flag) {
2696 if ((sg_copy = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
2697 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
2698 goto error;
2700 sg_copy_orig = sg_copy;
2701 fragment_flag++;
2703 sg_copy = sg_copy_orig;
2704 memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
2705 sg_len_copy = sg_len;
2706 if (modify_iov(&sg_copy, &sg_len_copy, scsi_cmd->bytes_sent, data.length) != 0) {
2707 iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
2708 goto error;
2710 sg_which = sg_copy;
2711 sg_len_which = sg_len_copy;
2713 } else {
2716 * Data was not fragmented; use the original
2717 * iovec.
2720 sg_which = sg;
2721 sg_len_which = sg_len;
2724 iscsi_trace(TRACE_ISCSI_DEBUG, "sending write data PDU (offset %u, len %u, sg_len %u)\n",
2725 data.offset, data.length, sg_len_which);
2727 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_which, data.length, sg_len_which)
2728 != ISCSI_HEADER_LEN + data.length) {
2729 iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
2730 goto error;
2732 iscsi_trace(TRACE_ISCSI_DEBUG, "sent write data PDU (offset %u, len %u)\n", data.offset, data.length);
2733 scsi_cmd->bytes_sent += data.length;
2734 } while ((scsi_cmd->bytes_sent < scsi_cmd->trans_len)
2735 && ((scsi_cmd->bytes_sent < sess->sess_params.first_burst_length)
2736 || (!sess->sess_params.first_burst_length)));
2737 if (scsi_cmd->trans_len - scsi_cmd->bytes_sent) {
2738 iscsi_trace(TRACE_ISCSI_DEBUG, "REACHED FIRST BURST\n");
2740 iscsi_trace(TRACE_ISCSI_DEBUG, "successfully sent %u of %u bytes write data\n", scsi_cmd->bytes_sent, scsi_cmd->trans_len);
2742 if (scsi_cmd->output && (scsi_cmd->trans_len - scsi_cmd->bytes_sent)) {
2743 iscsi_trace(TRACE_ISCSI_DEBUG, "expecting R2T for remaining %u bytes write data\n", scsi_cmd->trans_len - scsi_cmd->bytes_sent);
2745 if (fragment_flag)
2746 iscsi_free_atomic(sg_copy_orig);
2747 sess->CmdSN++;
2749 return 0;
2751 error:
2752 if (fragment_flag)
2753 iscsi_free_atomic(sg_copy);
2754 return -1;
2757 static int
2758 reject_i(initiator_session_t * sess, uint8_t *header)
2760 initiator_cmd_t *cmd = NULL;
2761 iscsi_reject_t reject;
2762 uint8_t bad_header[ISCSI_HEADER_LEN];
2763 uint32_t tag;
2765 /* Get & check args */
2767 if (iscsi_reject_decap(header, &reject) != 0) {
2768 iscsi_err(__FILE__, __LINE__, "iscsi_reject_decap() failed\n");
2769 return -1;
2771 if (reject.length != ISCSI_HEADER_LEN) {
2772 iscsi_err(__FILE__, __LINE__, "reject.length %u\n",
2773 reject.length);
2774 NO_CLEANUP;
2775 return -1;
2778 /* Read bad header, extract tag, and get cmd from hash table */
2780 if (iscsi_sock_msg(sess->sock, 0, ISCSI_HEADER_LEN, bad_header, 0) != ISCSI_HEADER_LEN) {
2781 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2782 return -1;
2784 (void) memcpy(&tag, bad_header + 16, sizeof(tag));
2785 tag = ISCSI_NTOHL(tag);
2786 iscsi_err(__FILE__, __LINE__, "REJECT PDU: tag %#x (reason %#x)\n", tag, reject.reason);
2787 if (tag != 0xffffffff) {
2788 if ((cmd = hash_remove(&g_tag_hash, tag)) == NULL) {
2789 iscsi_trace(TRACE_ISCSI_DEBUG, "no cmd ptr associated with tag %#x\n", tag);
2790 } else {
2791 iscsi_trace(TRACE_ISCSI_DEBUG, "cmd %p associated with tag %#x\n", cmd, tag);
2792 ISCSI_LOCK(&sess->rx_worker.work_mutex, return -1);
2793 if (!cmd->tx_done)
2794 ISCSI_WAIT(&sess->rx_worker.work_cond, &sess->rx_worker.work_mutex, return -1);
2795 ISCSI_UNLOCK(&sess->rx_worker.work_mutex, return -1);
2797 } else {
2798 iscsi_err(__FILE__, __LINE__, "no command associated with tag %#x\n", tag);
2801 /* Execute callback to complete initiator_cmd_t */
2803 if (cmd) {
2804 cmd->status = -1;
2805 if (cmd->callback) {
2806 iscsi_trace(TRACE_ISCSI_DEBUG, "issuing callback for cmd associated with tag %#x\n", tag);
2807 if ((*cmd->callback)(cmd) != 0) {
2808 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2809 return -1;
2811 } else {
2812 iscsi_err(__FILE__, __LINE__, "no callback associated with tag %#x\n", tag);
2815 return 0;
2818 static int
2819 async_msg_i(initiator_session_t * sess, uint8_t *header)
2821 iscsi_async_msg_t msg;
2823 /* Get & check args */
2824 if (iscsi_amsg_decap(header, &msg) != 0) {
2825 iscsi_err(__FILE__, __LINE__, "iscsi_amsg_decap() failed\n");
2826 return -1;
2828 sess->CmdSN = msg.ExpCmdSN;
2829 sess->MaxCmdSN = msg.MaxCmdSN;
2830 sess->ExpStatSN = msg.StatSN + 1;
2832 /* Read Sense Data */
2833 if (msg.length) {
2834 uint8_t *sense_data = NULL;
2835 if ((sense_data = iscsi_malloc(msg.length)) == NULL) {
2836 iscsi_err(__FILE__, __LINE__, "iscsi_malloc() failed\n");
2837 return -1;
2839 iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes sense data \n", msg.length);
2840 if ((unsigned)iscsi_sock_msg(sess->sock, 0, msg.length, sense_data, 0) != msg.length) {
2841 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
2842 if (sense_data != NULL)
2843 iscsi_free(sense_data);
2844 return -1;
2846 iscsi_trace(TRACE_ISCSI_DEBUG, "read %d bytes sense data ok (currently discarding)\n", msg.length);
2847 if (sense_data != NULL)
2848 iscsi_free(sense_data);
2849 } else {
2850 iscsi_trace(TRACE_ISCSI_DEBUG, "no sense data available\n");
2853 switch (msg.AsyncEvent) {
2854 case 0:
2855 /* Ignore SCSI asyn messages for now */
2856 break;
2857 case 1:
2858 case 4:
2859 /* Ignore Parameter Negotiation. Send Logout */
2860 logout_phase_i(sess);
2861 /* FALLTHROUGH */
2862 case 2:
2863 case 3:
2864 if (iscsi_sock_shutdown(sess->sock, 1) != 0) {
2865 iscsi_err(__FILE__, __LINE__, "iscsi_sock_shutdown() failed\n");
2867 return -1;
2868 case 255:
2869 break;
2870 default:
2871 break;
2874 return 0;
2877 static int
2878 nop_in_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
2880 iscsi_nop_out_args_t *nop_out = NULL;
2881 iscsi_nop_in_args_t nop_in;
2882 uint8_t *ping_data = NULL;
2883 unsigned i;
2885 if (cmd) {
2886 nop_out = (iscsi_nop_out_args_t *) cmd->ptr;
2887 } else {
2888 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this NOP_IN\n");
2890 if (iscsi_nop_in_decap(header, &nop_in) != 0) {
2891 iscsi_err(__FILE__, __LINE__, "iscsi_nop_in() failed\n");
2892 return -1;
2894 if (cmd) {
2895 #if 0
2896 RETURN_NOT_EQUAL("nop_in.length", nop_in.length, nop_out->length, NO_CLEANUP, -1);
2897 #else
2898 if (nop_in.length != nop_out->length) {
2899 iscsi_err(__FILE__, __LINE__,
2900 "nop_in.length %u, nopout->length %u\n",
2901 nop_in.length, nop_out->length);
2902 NO_CLEANUP;
2903 return -1;
2905 #endif
2907 if (nop_in.length) {
2908 iscsi_trace(TRACE_ISCSI_DEBUG,
2909 "reading %d bytes ping data\n", nop_in.length);
2910 if ((ping_data = iscsi_malloc_atomic(nop_in.length)) == NULL) {
2911 iscsi_err(__FILE__, __LINE__,
2912 "iscsi_malloc_atomic() failed\n");
2913 return -1;
2915 #define NOI_CLEANUP {if (ping_data) iscsi_free_atomic(ping_data);}
2916 #define NOI_ERROR {NOI_CLEANUP; return -1;}
2917 if ((unsigned)iscsi_sock_msg(sess->sock, 0, nop_in.length,
2918 ping_data, 0) != nop_in.length) {
2919 iscsi_err(__FILE__, __LINE__,
2920 "iscsi_sock_msg() failed\n");
2921 NOI_ERROR;
2923 iscsi_trace(TRACE_ISCSI_DEBUG,
2924 "successfully read %d bytes ping data\n",
2925 nop_in.length);
2926 if (cmd) {
2927 for (i = 0; i < nop_in.length; i++) {
2928 if (nop_out->data[i] != ping_data[i]) {
2929 iscsi_err(__FILE__, __LINE__,
2930 "Bad ping data[%d]. "
2931 "Got %#x, expected %#x\n",
2932 i, ping_data[i],
2933 nop_out->data[i]);
2934 NOI_ERROR;
2940 /* Send ping response (if initiated by target) */
2941 if (nop_in.transfer_tag != 0xffffffff) {
2942 uint8_t nop_header[ISCSI_HEADER_LEN];
2943 iscsi_nop_out_args_t nop_out_args;
2945 iscsi_trace(TRACE_ISCSI_DEBUG,
2946 "sending %d byte ping response\n", nop_in.length);
2947 (void) memset(&nop_out_args, 0x0, sizeof(nop_out_args));
2948 nop_out_args.tag = 0xffffffff;
2949 nop_out_args.immediate = 0x40;
2950 nop_out_args.transfer_tag = nop_in.transfer_tag;
2951 nop_out_args.length = nop_in.length;
2952 nop_out_args.lun = nop_in.lun;
2953 nop_out_args.ExpStatSN = sess->ExpStatSN;
2954 nop_out_args.CmdSN = sess->CmdSN;
2955 if (iscsi_nop_out_encap(nop_header, &nop_out_args) != 0) {
2956 iscsi_err(__FILE__, __LINE__,
2957 "iscsi_nop_out_encap() failed\n");
2958 NOI_ERROR;
2960 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock,
2961 nop_header, nop_out_args.length, ping_data,
2962 nop_in.length, 0) != nop_in.length) {
2963 iscsi_err(__FILE__, __LINE__,
2964 "iscsi_sock_msg() failed\n");
2965 NOI_ERROR;
2967 iscsi_trace(TRACE_ISCSI_DEBUG,
2968 "successfully sent %d byte ping response\n",
2969 nop_in.length);
2971 NOI_CLEANUP;
2972 /* Check and update numbering */
2973 sess->ExpStatSN = nop_in.StatSN + 1;
2975 * RETURN_NOT_EQUAL("StatSN", nop_in.StatSN, sess->ExpStatSN++,
2976 * NO_CLEANUP, -1);
2978 sess->CmdSN = nop_in.ExpCmdSN;
2980 * RETURN_NOT_EQUAL("ExpCmdSN", nop_in.ExpCmdSN, sess->CmdSN,
2981 * NO_CLEANUP, -1);
2983 sess->MaxCmdSN = nop_in.MaxCmdSN;
2985 /* Callback */
2987 if (cmd) {
2988 cmd->status = 0;
2989 if (cmd->callback) {
2990 iscsi_trace(TRACE_ISCSI_DEBUG, "NOP_OUT_T done (cmd status %d)\n", cmd->status);
2991 if ((*cmd->callback)(cmd) != 0) {
2992 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
2993 return -1;
2995 } else {
2996 iscsi_trace(TRACE_ISCSI_DEBUG, "no callback associated with NOP_IN_T??\n");
2997 return -1;
3000 return 0;
3003 static int
3004 scsi_r2t_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3006 iscsi_r2t_t r2t;
3007 iscsi_scsi_cmd_args_t *scsi_cmd;
3008 iscsi_write_data_t data;
3009 uint32_t bytes_sent;
3010 uint32_t DataSN;
3011 struct iovec sg_singleton;
3012 struct iovec *sg, *sg_copy, *sg_copy_orig, *sg_which;
3013 int sg_len, sg_len_copy, sg_len_which;
3014 int fragment_flag;
3016 /* Make sure an initiator_cmd_t was specified, that it has a
3017 * callback function specified and that it also has a
3018 * iscsi_scsi_cmd_args_t associated with it. */
3020 if (cmd) {
3021 if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3022 iscsi_err(__FILE__, __LINE__, "no iscsi_scsi_cmd_args_t associated with this initiator_cmd_t??\n");
3023 return -1;
3024 } else if (cmd->callback == NULL) {
3025 iscsi_err(__FILE__, __LINE__, "no callback associated with this initiator_cmd_t??\n");
3026 return -1;
3028 } else {
3029 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this iscsi_r2t_t??\n");
3030 return -1;
3033 sg = sg_copy = sg_copy_orig = sg_which = NULL;
3034 sg_len = sg_len_copy = sg_len_which = 0;
3035 if (iscsi_r2t_decap(header, &r2t) != 0) {
3036 iscsi_err(__FILE__, __LINE__, "iscsi_r2t_decap() failed\n");
3037 return -1;
3040 /* Check args */
3041 if (r2t.length == 0) {
3042 iscsi_err(__FILE__, __LINE__, "Zero r2t.length\n");
3043 NO_CLEANUP;
3044 return -1;
3047 /* Check and update numbering */
3048 #if 0
3049 RETURN_NOT_EQUAL("StatSN", r2t.StatSN, sess->ExpStatSN, NO_CLEANUP, -1);
3050 RETURN_NOT_EQUAL("ExpCmdSN", r2t.ExpCmdSN, sess->CmdSN, NO_CLEANUP, -1);
3051 #else
3052 if (r2t.StatSN != sess->ExpStatSN) {
3053 iscsi_err(__FILE__, __LINE__,
3054 "r2t.StatSN %u, sess->ExpStatSN %u\n",
3055 r2t.StatSN, sess->ExpStatSN);
3056 NO_CLEANUP;
3057 return -1;
3059 if (r2t.ExpCmdSN != sess->CmdSN) {
3060 iscsi_err(__FILE__, __LINE__,
3061 "r2t.ExpCmdSN %u, sess->CmdSN %u\n",
3062 r2t.ExpCmdSN, sess->CmdSN);
3063 NO_CLEANUP;
3064 return -1;
3066 #endif
3067 sess->MaxCmdSN = r2t.MaxCmdSN;
3069 /* Send back requested data */
3070 iscsi_trace(TRACE_ISCSI_DEBUG,
3071 "sending %d bytes R2T write data (offset %u)\n",
3072 r2t.length, r2t.offset);
3073 if (scsi_cmd->send_sg_len) {
3074 sg = (struct iovec *)(void *)scsi_cmd->send_data;
3075 sg_len = scsi_cmd->send_sg_len;
3076 } else {
3077 sg_singleton.iov_base = scsi_cmd->send_data;
3078 sg_singleton.iov_len = scsi_cmd->trans_len;
3079 sg = &sg_singleton;
3080 sg_len = 1;
3082 fragment_flag = 0;
3083 bytes_sent = 0;
3084 DataSN = 0;
3085 #define FF_CLEANUP {if (fragment_flag) iscsi_free_atomic(sg_copy_orig);}
3086 do {
3087 (void) memset(&data, 0x0, sizeof(data));
3088 if (sess->sess_params.max_dataseg_len) {
3089 data.length = MIN(sess->sess_params.max_dataseg_len,
3090 r2t.length - bytes_sent);
3091 } else {
3092 data.length = r2t.length - bytes_sent;
3094 if (bytes_sent + data.length == r2t.length) {
3095 data.final = 1;
3097 data.tag = r2t.tag;
3098 data.transfer_tag = r2t.transfer_tag;
3099 data.ExpStatSN = sess->ExpStatSN;
3100 data.DataSN = DataSN++;
3101 data.offset = r2t.offset + bytes_sent;
3102 data.lun = scsi_cmd->lun;
3103 if (iscsi_write_data_encap(header, &data) != 0) {
3104 iscsi_err(__FILE__, __LINE__, "iscsi_write_data_encap() failed\n");
3105 FF_CLEANUP;
3106 return -1;
3108 if ((data.length < r2t.length) || (r2t.offset)) {
3109 if (data.length < r2t.length) {
3110 iscsi_trace(TRACE_ISCSI_DEBUG, "R2T data is being fragmented: sending %u bytes of %u requested\n",
3111 data.length, r2t.length);
3112 } else {
3113 iscsi_trace(TRACE_ISCSI_DEBUG, "R2T data starts at offset %u, desired length %u\n",
3114 r2t.offset, r2t.length);
3117 /* Allocate space for a copy of the original iovec */
3119 if (!fragment_flag) {
3120 if ((sg_copy_orig = iscsi_malloc_atomic(sg_len * sizeof(struct iovec))) == NULL) {
3121 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
3122 return -1;
3124 fragment_flag++;
3127 * Copy and modify original iovec with new offset and
3128 * length
3131 iscsi_trace(TRACE_ISCSI_DEBUG, "modifying original iovec with offset %u length %u\n",
3132 r2t.offset + bytes_sent, data.length);
3133 sg_copy = sg_copy_orig;
3134 sg_len_copy = sg_len;
3135 memcpy(sg_copy, sg, sizeof(struct iovec) * sg_len);
3136 if (modify_iov(&sg_copy, &sg_len_copy, r2t.offset + bytes_sent, data.length) != 0) {
3137 iscsi_err(__FILE__, __LINE__, "modify_iov() failed\n");
3138 FF_CLEANUP;
3139 return -1;
3141 sg_which = sg_copy;
3142 sg_len_which = sg_len_copy;
3143 } else {
3144 iscsi_trace(TRACE_ISCSI_DEBUG, "using original iovec for R2T transfer (offset %u, length %u)\n",
3145 r2t.offset, r2t.length);
3146 sg_which = sg;
3147 sg_len_which = sg_len;
3149 iscsi_trace(TRACE_ISCSI_DEBUG, "sending R2T write data PDU (offset %u, len %u, sg_len %u)\n",
3150 data.offset, data.length, sg_len_which);
3151 if ((unsigned)iscsi_sock_send_header_and_data(sess->sock, header, ISCSI_HEADER_LEN, sg_which, data.length, sg_len_which)
3152 != ISCSI_HEADER_LEN + data.length) {
3153 iscsi_err(__FILE__, __LINE__, "iscsi_sock_send_header_and_data() failed\n");
3154 FF_CLEANUP;
3155 return -1;
3157 iscsi_trace(TRACE_ISCSI_DEBUG, "sent write data PDU OK (offset %u, len %u)\n", data.offset, data.length);
3158 bytes_sent += data.length;
3159 scsi_cmd->bytes_sent += data.length;
3160 } while (bytes_sent < r2t.length);
3161 FF_CLEANUP;
3162 if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
3163 iscsi_err(__FILE__, __LINE__, "hash_insert() failed\n");
3164 return -1;
3166 return 0;
3169 static int
3170 scsi_response_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3172 iscsi_scsi_cmd_args_t *scsi_cmd;
3173 iscsi_scsi_rsp_t scsi_rsp;
3174 const char *errmsg;
3176 /* Make sure an initiator_cmd_t was specified, that it has a
3177 * callback function specified and that it also has a
3178 * iscsi_scsi_cmd_args_t associated with it. */
3180 if (cmd) {
3181 if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3182 iscsi_err(__FILE__, __LINE__, "no iscsi_scsi_cmd_args_t associated with this initiator_cmd_t??\n");
3183 return -1;
3184 } else if (cmd->callback == NULL) {
3185 iscsi_err(__FILE__, __LINE__, "no callback associated with this initiator_cmd_t??\n");
3186 return -1;
3188 } else {
3189 iscsi_err(__FILE__, __LINE__, "no initiator_cmd_t associated with this iscsi_scsi_rsp_t??\n");
3190 return -1;
3194 * Read SCSI response and check return args. Those marked
3195 * "FIX ME" are not yet implemented. */
3197 if (iscsi_scsi_rsp_decap(header, &scsi_rsp) != 0) {
3198 iscsi_err(__FILE__, __LINE__, "iscsi_scsi_rsp_decap() failed\n");
3199 return -1;
3201 #if 0
3202 RETURN_NOT_EQUAL("o bit (FIX ME)", scsi_rsp.bidi_overflow, 0, NO_CLEANUP, -1);
3203 RETURN_NOT_EQUAL("u bit (FIX ME)", scsi_rsp.bidi_underflow, 0, NO_CLEANUP, -1);
3204 RETURN_NOT_EQUAL("O bit (FIX ME)", scsi_rsp.overflow, 0, NO_CLEANUP, -1);
3205 RETURN_NOT_EQUAL("iSCSI Response (FIX ME)", scsi_rsp.response, 0, NO_CLEANUP, -1);
3206 RETURN_NOT_EQUAL("Tag", scsi_rsp.tag, scsi_cmd->tag, NO_CLEANUP, -1);
3207 RETURN_NOT_EQUAL("Bidi Residual Count", scsi_rsp.bidi_res_cnt, 0, NO_CLEANUP, -1);
3208 RETURN_NOT_EQUAL("StatSN", scsi_rsp.StatSN, sess->ExpStatSN, NO_CLEANUP, -1);
3209 #else
3210 errmsg = NULL;
3211 if (scsi_rsp.bidi_overflow != 0) {
3212 errmsg = "o bit (FIX ME)\n";
3213 } else if (scsi_rsp.bidi_underflow != 0) {
3214 errmsg = "u bit (FIX ME)\n";
3215 } else if (scsi_rsp.overflow != 0) {
3216 errmsg = "O bit (FIX ME)\n";
3217 } else if (scsi_rsp.response != 0) {
3218 errmsg = "Response (FIX ME)\n";
3219 } else if (scsi_rsp.tag != scsi_cmd->tag) {
3220 errmsg = "Tags don't match\n";
3221 } else if (scsi_rsp.bidi_res_cnt != 0) {
3222 errmsg = "Bidi Residual Count";
3223 } else if (scsi_rsp.StatSN != sess->ExpStatSN) {
3224 errmsg = "StatSN";
3226 if (errmsg) {
3227 iscsi_err(__FILE__, __LINE__, errmsg);
3228 NO_CLEANUP;
3229 return -1;
3231 #endif
3232 sess->ExpStatSN = scsi_rsp.StatSN + 1;
3234 if (sess->sess_params.max_dataseg_len &&
3235 scsi_rsp.length > sess->sess_params.max_dataseg_len) {
3236 iscsi_err(__FILE__, __LINE__,
3237 "scsi_rsp.length %u\n", scsi_rsp.length);
3238 NO_CLEANUP;
3239 return -1;
3241 if ((scsi_rsp.status == 0) && (scsi_rsp.length != 0)) {
3242 iscsi_err(__FILE__, __LINE__,
3243 "Unexpected DataSegmentLength %u "
3244 "with GOOD SCSI status\n", scsi_rsp.length);
3245 return -1;
3248 * Make sure all data was successfully transferred if command
3249 * completed successfully, otherwise read sense data. */
3251 if (scsi_rsp.status == 0) {
3252 if (scsi_cmd->output) {
3253 #if 0
3254 RETURN_NOT_EQUAL("scsi_cmd->bytes_sent", scsi_cmd->bytes_sent, scsi_cmd->trans_len, NO_CLEANUP, -1);
3255 #else
3256 if (scsi_cmd->bytes_sent != scsi_cmd->trans_len) {
3257 iscsi_err(__FILE__, __LINE__,
3258 "scsi_cmd->bytes_sent\n");
3259 NO_CLEANUP;
3260 return -1;
3262 #endif
3263 if (scsi_cmd->input) {
3265 #if 0
3266 RETURN_NOT_EQUAL("scsi_cmd->bytes_recv", scsi_cmd->bytes_recv, scsi_cmd->bidi_trans_len, NO_CLEANUP, -1);
3267 #else
3268 if (scsi_cmd->bytes_recv != scsi_cmd->bidi_trans_len) {
3269 iscsi_err(__FILE__, __LINE__,
3270 "scsi_cmd->bytes_recv\n");
3271 NO_CLEANUP;
3272 return -1;
3274 #endif
3276 } else if (scsi_cmd->input) {
3280 } else if (scsi_rsp.length) {
3281 uint8_t *sense_data = NULL;
3283 if ((sense_data = iscsi_malloc(scsi_rsp.length)) == NULL) {
3284 iscsi_err(__FILE__, __LINE__,
3285 "iscsi_malloc() failed\n");
3286 return -1;
3288 iscsi_err(__FILE__, __LINE__,
3289 "reading %d bytes sense data (recv_sg_len %u)\n",
3290 scsi_rsp.length, scsi_cmd->recv_sg_len);
3291 if ((unsigned)iscsi_sock_msg(sess->sock, 0, scsi_rsp.length,
3292 sense_data, 0) != scsi_rsp.length) {
3293 iscsi_err(__FILE__, __LINE__,
3294 "iscsi_sock_msg() failed\n");
3295 if (sense_data != NULL) {
3296 iscsi_free(sense_data);
3298 return -1;
3300 iscsi_err(__FILE__, __LINE__,
3301 "read %d bytes sense data ok (currently discarding)\n",
3302 scsi_rsp.length);
3303 if (sense_data != NULL) {
3304 iscsi_free(sense_data);
3306 } else {
3307 iscsi_trace(TRACE_ISCSI_DEBUG, "no sense data available\n");
3310 /* Check and update numbering */
3313 * RETURN_NOT_EQUAL("ExpCmdSN", scsi_rsp.ExpCmdSN, sess->CmdSN,
3314 * NO_CLEANUP, -1);
3316 sess->MaxCmdSN = scsi_rsp.MaxCmdSN;
3318 /* Set initiator_cmd_t status, iscsi_scsi_cmd_args_t status */
3319 /* and execute callback function */
3321 cmd->status = 0;
3322 scsi_cmd->status = scsi_rsp.status;
3323 iscsi_trace(TRACE_ISCSI_DEBUG,
3324 "iscsi_scsi_cmd_args_t done (cmd status %d, iscsi status %d, "
3325 "scsi status %d)\n",
3326 cmd->status, scsi_rsp.response, scsi_rsp.status);
3327 if ((*cmd->callback)(cmd) != 0) {
3328 iscsi_err(__FILE__, __LINE__, "callback() failed\n");
3329 return -1;
3332 return 0;
3335 static int
3336 scsi_read_data_i(initiator_session_t * sess, initiator_cmd_t * cmd, uint8_t *header)
3338 iscsi_scsi_cmd_args_t *scsi_cmd;
3339 iscsi_read_data_t data;
3340 const char *errmsg;
3341 int rc;
3343 iscsi_trace(TRACE_ISCSI_DEBUG, "processing read data\n");
3345 /* Make sure an initiator_cmd_t was specified, that it has a
3346 * callback function specified and that it also has a
3347 * iscsi_scsi_cmd_args_t associated with it. */
3349 if (cmd) {
3350 if (cmd->type != ISCSI_SCSI_CMD) {
3351 iscsi_err(__FILE__, __LINE__,
3352 "Invalid response from target for cmd "
3353 "type (%#x)\n", cmd->type);
3354 cmd->status = -1;
3355 if (cmd->callback) {
3356 (*cmd->callback)(cmd);
3358 return -1;
3360 if ((scsi_cmd = (iscsi_scsi_cmd_args_t *) cmd->ptr) == NULL) {
3361 iscsi_err(__FILE__, __LINE__,
3362 "no iscsi_scsi_cmd_args_t associated with "
3363 "this initiator_cmd_t??\n");
3364 return -1;
3365 } else if (cmd->callback == NULL) {
3366 iscsi_err(__FILE__, __LINE__,
3367 "no callback associated with this "
3368 "initiator_cmd_t??\n");
3369 return -1;
3371 } else {
3372 iscsi_err(__FILE__, __LINE__,
3373 "no initiator_cmd_t associated with this "
3374 "iscsi_read_data_t??\n");
3375 return -1;
3377 if (iscsi_read_data_decap(header, &data) != 0) {
3378 iscsi_err(__FILE__, __LINE__,
3379 "iscsi_scsi_rsp_decap() failed\n");
3380 return -1;
3383 /* Check args */
3384 #if 0
3385 RETURN_NOT_EQUAL("Overflow bit", data.overflow, 0, NO_CLEANUP, -1);
3386 RETURN_NOT_EQUAL("Underflow bit", data.underflow, 0, NO_CLEANUP, -1);
3387 RETURN_NOT_EQUAL("Tag", data.task_tag, scsi_cmd->tag, NO_CLEANUP, -1);
3388 RETURN_NOT_EQUAL("Residual Count", data.res_count, 0, NO_CLEANUP, -1);
3389 #else
3390 errmsg = NULL;
3391 if (data.overflow != 0) {
3392 errmsg = "Overflow bit";
3393 } else if (data.underflow != 0) {
3394 errmsg = "Underflow bit";
3395 } else if (data.task_tag != scsi_cmd->tag) {
3396 errmsg = "Tag";
3397 } else if (data.task_tag != scsi_cmd->tag) {
3398 errmsg = "Residual Count";
3400 if (errmsg) {
3401 iscsi_err(__FILE__, __LINE__, errmsg);
3402 NO_CLEANUP;
3403 return -1;
3405 #endif
3407 if (sess->sess_params.max_dataseg_len) {
3408 if (data.length > sess->sess_params.max_dataseg_len) {
3409 iscsi_err(__FILE__, __LINE__,
3410 "data.length %u\n", data.length);
3411 NO_CLEANUP;
3412 return -1;
3416 /* Check and update numbering */
3417 if (data.ExpCmdSN != sess->CmdSN) {
3418 iscsi_warn(__FILE__, __LINE__,
3419 "Bad \"ExpCmdSN\": Got %u expected %u.\n",
3420 data.ExpCmdSN, sess->CmdSN);
3422 sess->MaxCmdSN = data.MaxCmdSN;
3424 /* Need to optimize this section */
3426 if (scsi_cmd->recv_sg_len) {
3427 int sg_len = scsi_cmd->recv_sg_len;
3428 struct iovec *sg;
3429 struct iovec *sg_orig = NULL;
3430 char *sgp;
3431 uint32_t total_len, disp;
3432 int i;
3434 if (data.length != scsi_cmd->trans_len) {
3436 /* Make a copy of the iovec */
3438 sg_orig = sg = iscsi_malloc_atomic(sizeof(struct iovec)
3439 * sg_len);
3440 if (sg_orig == NULL) {
3441 iscsi_err(__FILE__, __LINE__,
3442 "iscsi_malloc_atomic() failed\n");
3443 return -1;
3446 (void) memcpy(sg, scsi_cmd->recv_data,
3447 sizeof(struct iovec) * sg_len);
3449 /* Find offset in iovecs */
3450 total_len = 0;
3451 disp = data.offset;
3452 for (i = 0; i < sg_len; i++) {
3453 total_len += sg[i].iov_len;
3454 if (total_len > data.offset) {
3455 break;
3457 disp -= sg[i].iov_len;
3459 sg[i].iov_len -= disp;
3460 sgp = sg[i].iov_base;
3461 sgp += disp;
3462 sg[i].iov_base = sgp;
3463 sg_len -= i;
3464 sg = &sg[i];
3466 /* Find last iovec needed for read */
3468 total_len = 0;
3469 for (i = 0; i < sg_len; i++) {
3470 total_len += sg[i].iov_len;
3471 if (total_len >= data.length) {
3472 break;
3475 sg[i].iov_len -= (total_len - data.length);
3476 sg_len = i + 1;
3477 } else {
3478 sg = (struct iovec *)(void *)scsi_cmd->recv_data;
3480 iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes into sg buffer (total offset %u)\n", data.length, data.offset);
3481 if ((rc = iscsi_sock_msg(sess->sock, 0, data.length, (uint8_t *)(void *) sg, sg_len)) != (int)data.length) {
3482 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed: got %u, expected %u\n", rc, data.length);
3483 if (sg_orig)
3484 iscsi_free_atomic(sg_orig);
3485 return -1;
3487 scsi_cmd->bytes_recv += data.length;
3488 if (sg_orig)
3489 iscsi_free_atomic(sg_orig);
3490 } else {
3491 if (data.length) {
3492 iscsi_trace(TRACE_ISCSI_DEBUG, "reading %d bytes into dest buffer (offset %u)\n", data.length, data.offset);
3493 if (iscsi_sock_msg(sess->sock, 0, data.length, scsi_cmd->recv_data + data.offset, 0) != (int)data.length) {
3494 iscsi_err(__FILE__, __LINE__, "iscsi_sock_msg() failed\n");
3495 return -1;
3497 scsi_cmd->bytes_recv += data.length;
3502 /* Check for status */
3504 if (data.S_bit) {
3505 iscsi_trace(TRACE_ISCSI_DEBUG,
3506 "received status with final PDU\n");
3507 #if 0
3508 RETURN_NOT_EQUAL("Final Bit", data.final, 1, NO_CLEANUP, -1);
3509 RETURN_NOT_EQUAL("StatSN", data.StatSN, sess->ExpStatSN++, NO_CLEANUP, -1);
3510 /* XXX - agc - increment in macro !!! */
3511 #else
3512 if (data.final != 1) {
3513 iscsi_err(__FILE__, __LINE__, "Final Bit");
3514 NO_CLEANUP;
3515 return -1;
3517 if (data.StatSN != sess->ExpStatSN++) {
3518 iscsi_err(__FILE__, __LINE__, "StatSN");
3519 NO_CLEANUP;
3520 return -1;
3522 #endif
3523 scsi_cmd->status = data.status = 0;
3524 cmd->status = 0;
3525 iscsi_trace(TRACE_ISCSI_DEBUG,
3526 "scsi op %#x done (tag %u, status %d)\n",
3527 scsi_cmd->cdb[0], scsi_cmd->tag, scsi_cmd->status);
3528 if ((*cmd->callback)(cmd) != 0) {
3529 iscsi_err(__FILE__, __LINE__,
3530 "callback() failed\n");
3531 return -1;
3533 } else {
3534 if (hash_insert(&g_tag_hash, cmd, scsi_cmd->tag) != 0) {
3535 iscsi_err(__FILE__, __LINE__,
3536 "hash_insert() failed\n");
3537 return -1;
3540 iscsi_trace(TRACE_ISCSI_DEBUG, "read data processed\n");
3541 return 0;
3544 int
3545 iscsi_initiator_info(char *ptr, int size, int len)
3547 initiator_session_t *sess;
3548 int i;
3550 ptr[0] = 0x0;
3551 len += snprintf(ptr, (size_t)(size - len),
3552 " %3s %30s %25s\n\n", "TID", "TargetName", "TargetAddress");
3553 for (i = 0; i < CONFIG_INITIATOR_NUM_TARGETS; i++) {
3554 len += snprintf(ptr + len, (size_t)(size - len),
3555 " %3i %30s %20s:%d (",
3556 i, g_target[i].TargetName,
3557 g_target[i].ip, g_target[i].port);
3558 if (g_target[i].has_session) {
3559 sess = g_target[i].sess;
3560 if (sess->state & INITIATOR_SESSION_STATE_INITIALIZING)
3561 len += snprintf(ptr + len,
3562 (size_t)(size - len), "%s",
3563 "initializing");
3564 if (sess->state & INITIATOR_SESSION_STATE_INITIALIZED)
3565 len += snprintf(ptr + len,
3566 (size_t)(size - len), "%s",
3567 "initialized");
3568 if (sess->state & INITIATOR_SESSION_STATE_CONNECTING)
3569 len += snprintf(ptr + len,
3570 (size_t)(size - len),
3571 "%s", "connecting");
3572 if (sess->state & INITIATOR_SESSION_STATE_CONNECTED)
3573 len += snprintf(ptr + len,
3574 (size_t)(size - len), "%s",
3575 "connected");
3576 if (sess->state & INITIATOR_SESSION_STATE_LOGGING_IN)
3577 len += snprintf(ptr + len,
3578 (size_t)(size - len), "%s",
3579 "logging in");
3580 if (sess->state &
3581 INITIATOR_SESSION_STATE_LOGGED_IN_NORMAL)
3582 len += snprintf(ptr + len,
3583 (size_t)(size - len), "%s",
3584 "Normal session");
3585 if (sess->state &
3586 INITIATOR_SESSION_STATE_LOGGED_IN_DISCOVERY)
3587 len += snprintf(ptr + len,
3588 (size_t)(size - len), "%s",
3589 "Discovery session");
3590 if (sess->state & INITIATOR_SESSION_STATE_LOGGING_OUT)
3591 len += snprintf(ptr + len,
3592 (size_t)(size - len), "%s",
3593 "logging out");
3594 if (sess->state & INITIATOR_SESSION_STATE_LOGGED_OUT)
3595 len += snprintf(ptr + len,
3596 (size_t)(size - len), "%s",
3597 "logged out");
3598 if (sess->state & INITIATOR_SESSION_STATE_DESTROYING)
3599 len += snprintf(ptr + len,
3600 (size_t)(size - len), "%s",
3601 "destroying");
3602 if (sess->tx_worker.state & ISCSI_WORKER_STATE_ERROR)
3603 len += snprintf(ptr + len,
3604 (size_t)(size - len), "%s",
3605 " **Tx Error** ");
3606 if (sess->rx_worker.state & ISCSI_WORKER_STATE_ERROR)
3607 len += snprintf(ptr + len,
3608 (size_t)(size - len), "%s",
3609 " **Rx Error** ");
3610 } else {
3611 len += snprintf(ptr + len, (size_t)(size - len), "%s",
3612 "No Session");
3614 len += snprintf(ptr + len, (size_t)(size - len), ")\n");
3616 return len;
3619 int
3620 iscsi_initiator_discover(char *host, uint64_t target, int lun)
3622 iscsi_nop_out_args_t discover_cmd;
3623 initiator_cmd_t cmd;
3625 cmd.type = ISCSI_NOP_OUT;
3626 cmd.ptr = &discover_cmd;
3627 cmd.isid = target;
3628 (void) strlcpy(cmd.targetname, host, sizeof(cmd.targetname));
3629 (void) memset(&discover_cmd, 0x0, sizeof(iscsi_nop_out_args_t));
3630 discover_cmd.length = 1;
3631 discover_cmd.data = (const uint8_t *) "";
3632 discover_cmd.lun = lun;
3633 discover_cmd.tag = 0xffffffff;
3634 if (initiator_command(&cmd) != 0) {
3635 iscsi_err(__FILE__, __LINE__, "initiator_command() failed\n");
3636 return -1;
3638 return 0;
3641 void
3642 get_target_info(uint64_t target, initiator_target_t *ip)
3644 (void) memcpy(ip, &g_target[(int)target], sizeof(*ip));
3647 int
3648 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)
3650 initiator_session_t *sess = NULL;
3652 #define INIT_CLEANUP {if (sess != NULL) iscsi_free_atomic(sess);}
3653 #define INIT_ERROR {INIT_CLEANUP; return -1;}
3655 USE_ARG(address_family);
3656 iscsi_trace(TRACE_ISCSI_DEBUG, "initializing initiator\n");
3657 iscsi_trace(TRACE_ISCSI_DEBUG, "target config filename to read from:%s\n", gfilename);
3658 if (get_target_config(hostname, port) != 0) {
3659 iscsi_err(__FILE__, __LINE__, "Error getting target configuration from config file\n");
3660 return -1;
3662 (void) strlcpy(g_target[0].iqnwanted, lun, sizeof(g_target[0].iqnwanted));
3663 g_initiator_state = 0;
3664 if (iscsi_queue_init(&g_session_q, CONFIG_INITIATOR_MAX_SESSIONS) != 0) {
3665 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3666 return -1;
3668 if ((sess = iscsi_malloc_atomic(sizeof(initiator_session_t))) == NULL) {
3669 iscsi_err(__FILE__, __LINE__, "iscsi_malloc_atomic() failed\n");
3670 return -1;
3672 if (iscsi_queue_insert(&g_session_q, sess) != 0) {
3673 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3674 INIT_CLEANUP;
3675 return -1;
3677 sess->sess_params.cred.user = strdup(user);
3678 sess->sess_params.auth_type = auth_type;
3679 sess->sess_params.mutual_auth = mutual_auth;
3680 sess->sess_params.digest_wanted = digest_type;
3681 iscsi_trace(TRACE_ISCSI_DEBUG, "%d free sessions available\n",
3682 CONFIG_INITIATOR_MAX_SESSIONS);
3684 g_tag = 0xabc123;
3685 if (hash_init(&g_tag_hash, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
3686 iscsi_err(__FILE__, __LINE__, "hash_init() failed\n");
3687 INIT_CLEANUP;
3688 return -1;
3690 iscsi_spin_init(&g_tag_spin);
3691 iscsi_trace(TRACE_ISCSI_DEBUG,
3692 "tag hash table initialized with queue depth %d\n",
3693 CONFIG_INITIATOR_QUEUE_DEPTH);
3696 * Start enqueue worker. This thread accepts scsi commands
3697 * from initiator_enqueue() and queues them onto one of the tx
3698 * worker queues.
3700 iscsi_trace(TRACE_ISCSI_DEBUG, "starting enqueue worker\n");
3701 if (iscsi_queue_init(&g_enqueue_q, CONFIG_INITIATOR_QUEUE_DEPTH) != 0) {
3702 iscsi_err(__FILE__, __LINE__, "iscsi_queue_init() failed\n");
3703 INIT_CLEANUP;
3704 return -1;
3706 iscsi_trace(TRACE_ISCSI_DEBUG, "about to initialize mutex\n");
3707 ISCSI_MUTEX_INIT(&g_enqueue_worker.work_mutex, INIT_ERROR);
3708 ISCSI_COND_INIT(&g_enqueue_worker.work_cond, INIT_ERROR);
3709 ISCSI_MUTEX_INIT(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3710 ISCSI_COND_INIT(&g_enqueue_worker.exit_cond, INIT_ERROR);
3711 ISCSI_LOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3713 iscsi_trace(TRACE_ISCSI_DEBUG, "spawning thread for enqueue worker\n");
3714 if (iscsi_thread_create(&g_enqueue_worker.thread,
3715 (void *) &enqueue_worker_proc, &g_enqueue_worker) != 0) {
3716 iscsi_err(__FILE__, __LINE__,
3717 "iscsi_threads_create() failed\n");
3718 INIT_CLEANUP;
3719 return -1;
3721 iscsi_trace(TRACE_ISCSI_DEBUG, "thread spawned, waiting for signal\n");
3722 ISCSI_WAIT(&g_enqueue_worker.exit_cond, &g_enqueue_worker.exit_mutex,
3723 INIT_ERROR);
3724 ISCSI_UNLOCK(&g_enqueue_worker.exit_mutex, INIT_ERROR);
3725 iscsi_trace(TRACE_ISCSI_DEBUG, "successfully started enqueue worker\n");
3727 iscsi_trace(TRACE_ISCSI_DEBUG, "initiator initialization complete\n");
3728 return 0;
3732 iscsi_initiator_set_defaults(iscsi_initiator_t *ini)
3734 char buf[32];
3736 /* set defaults */
3737 (void) memset(ini, 0x0, sizeof(*ini));
3738 iscsi_initiator_setvar(ini, "address family", "unspec");
3739 iscsi_initiator_setvar(ini, "digest type", "none");
3740 iscsi_initiator_setvar(ini, "auth type", "none");
3741 iscsi_initiator_setvar(ini, "mutual auth", "none");
3742 iscsi_initiator_setvar(ini, "target hostname", "localhost");
3743 (void) snprintf(buf, sizeof(buf), "%d", ISCSI_PORT);
3744 iscsi_initiator_setvar(ini, "target port", buf);
3745 return 1;
3748 /* check there's enough space in the arrays */
3749 static void
3750 size_arrays(iscsi_initiator_t *ini, unsigned needed)
3752 if (ini->size == 0) {
3753 /* only get here first time around */
3754 ini->size = needed;
3755 ini->name = calloc(sizeof(char *), needed);
3756 ini->value = calloc(sizeof(char *), needed);
3757 } else if (ini->c == ini->size) {
3758 /* only uses 'needed' when filled array */
3759 ini->size += needed;
3760 ini->name = realloc(ini->name, sizeof(char *) * needed);
3761 ini->value = realloc(ini->value, sizeof(char *) * needed);
3765 /* find the name in the array */
3766 static int
3767 findvar(iscsi_initiator_t *ini, const char *name)
3769 unsigned i;
3771 for (i = 0 ; i < ini->c && strcmp(ini->name[i], name) != 0; i++) {
3773 return (i == ini->c) ? -1 : (int)i;
3776 /* set a variable */
3778 iscsi_initiator_setvar(iscsi_initiator_t *ini, const char *name,
3779 const char *value)
3781 int i;
3783 if ((i = findvar(ini, name)) < 0) {
3784 /* add the element to the array */
3785 size_arrays(ini, ini->size + 15);
3786 ini->name[i = ini->c++] = strdup(name);
3787 } else {
3788 /* replace the element in the array */
3789 if (ini->value[i]) {
3790 (void) free(ini->value[i]);
3791 ini->value[i] = NULL;
3794 /* sanity checks for range of values would go here */
3795 ini->value[i] = strdup(value);
3796 return 1;
3799 /* get a variable's value (NULL if not set) */
3800 char *
3801 iscsi_initiator_getvar(iscsi_initiator_t *ini, const char *name)
3803 int i;
3805 return ((i = findvar(ini, name)) < 0) ? NULL : ini->value[i];