1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * iSCSI connection interfaces
26  */
27 
28 #define	ISCSI_ICS_NAMES
29 #include "iscsi.h"
30 #include "persistent.h"
31 #include <sys/bootprops.h>
32 
33 extern ib_boot_prop_t   *iscsiboot_prop;
34 
35 static void iscsi_client_notify_task(void *cn_task_void);
36 
37 static void iscsi_conn_flush_active_cmds(iscsi_conn_t *icp);
38 
39 #define	SHUTDOWN_TIMEOUT	180 /* seconds */
40 
41 extern int modrootloaded;
42 
43 boolean_t iscsi_conn_logging = B_FALSE;
44 
45 /*
46  * +--------------------------------------------------------------------+
47  * | External Connection Interfaces					|
48  * +--------------------------------------------------------------------+
49  */
50 
51 /*
52  * iscsi_conn_create - This creates an iscsi connection structure and
53  * associates it with a session structure.  The session's sess_conn_list_rwlock
54  * should be held as a writer before calling this function.
55  */
56 iscsi_status_t
57 iscsi_conn_create(struct sockaddr *addr, iscsi_sess_t *isp, iscsi_conn_t **icpp)
58 {
59 	iscsi_conn_t	*icp	= NULL;
60 	char		th_name[ISCSI_TH_MAX_NAME_LEN];
61 
62 	/* See if this connection already exists */
63 	for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) {
64 
65 		/*
66 		 * Compare the ioctl information to see if
67 		 * its a match for this connection.  (This
68 		 * is done by making sure the IPs are of
69 		 * the same size and then they are the
70 		 * same value.
71 		 */
72 		if (bcmp(&icp->conn_base_addr, addr,
73 		    SIZEOF_SOCKADDR(addr)) == 0) {
74 			/* It's a match, record this connection */
75 			break;
76 		}
77 	}
78 
79 	/* If icp is found return it */
80 	if (icp != NULL) {
81 		*icpp = icp;
82 		return (ISCSI_STATUS_SUCCESS);
83 	}
84 
85 	/* We are creating the connection, allocate, and setup */
86 	icp = (iscsi_conn_t *)kmem_zalloc(sizeof (iscsi_conn_t), KM_SLEEP);
87 
88 	/*
89 	 * Setup connection
90 	 */
91 	icp->conn_sig			= ISCSI_SIG_CONN;
92 	icp->conn_state			= ISCSI_CONN_STATE_FREE;
93 	mutex_init(&icp->conn_state_mutex, NULL, MUTEX_DRIVER, NULL);
94 	cv_init(&icp->conn_state_change, NULL, CV_DRIVER, NULL);
95 	mutex_init(&icp->conn_login_mutex, NULL, MUTEX_DRIVER, NULL);
96 	cv_init(&icp->conn_login_cv, NULL, CV_DRIVER, NULL);
97 	icp->conn_state_destroy		= B_FALSE;
98 	idm_sm_audit_init(&icp->conn_state_audit);
99 	icp->conn_sess			= isp;
100 
101 	mutex_enter(&iscsi_oid_mutex);
102 	icp->conn_oid = iscsi_oid++;
103 	mutex_exit(&iscsi_oid_mutex);
104 
105 	/*
106 	 * IDM CN taskq
107 	 */
108 
109 	if (snprintf(th_name, sizeof (th_name) - 1,
110 	    ISCSI_CONN_CN_TASKQ_NAME_FORMAT,
111 	    icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid,
112 	    icp->conn_oid) >= sizeof (th_name)) {
113 		cv_destroy(&icp->conn_state_change);
114 		mutex_destroy(&icp->conn_state_mutex);
115 		kmem_free(icp, sizeof (iscsi_conn_t));
116 		*icpp = NULL;
117 		return (ISCSI_STATUS_INTERNAL_ERROR);
118 	}
119 
120 	icp->conn_cn_taskq =
121 	    ddi_taskq_create(icp->conn_sess->sess_hba->hba_dip, th_name, 1,
122 	    TASKQ_DEFAULTPRI, 0);
123 	if (icp->conn_cn_taskq == NULL) {
124 		cv_destroy(&icp->conn_state_change);
125 		mutex_destroy(&icp->conn_state_mutex);
126 		kmem_free(icp, sizeof (iscsi_conn_t));
127 		*icpp = NULL;
128 		return (ISCSI_STATUS_INTERNAL_ERROR);
129 	}
130 
131 	/* Creation of the transfer thread */
132 	if (snprintf(th_name, sizeof (th_name) - 1, ISCSI_CONN_TXTH_NAME_FORMAT,
133 	    icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid,
134 	    icp->conn_oid) >= sizeof (th_name)) {
135 		cv_destroy(&icp->conn_state_change);
136 		mutex_destroy(&icp->conn_state_mutex);
137 		kmem_free(icp, sizeof (iscsi_conn_t));
138 		ddi_taskq_destroy(icp->conn_cn_taskq);
139 		*icpp = NULL;
140 		return (ISCSI_STATUS_INTERNAL_ERROR);
141 	}
142 
143 	icp->conn_tx_thread = iscsi_thread_create(isp->sess_hba->hba_dip,
144 	    th_name, iscsi_tx_thread, icp);
145 
146 	/* setup connection queues */
147 	iscsi_init_queue(&icp->conn_queue_active);
148 	iscsi_init_queue(&icp->conn_queue_idm_aborting);
149 
150 	bcopy(addr, &icp->conn_base_addr, sizeof (icp->conn_base_addr));
151 
152 	/* Add new connection to the session connection list */
153 	icp->conn_cid = isp->sess_conn_next_cid++;
154 	if (isp->sess_conn_list == NULL) {
155 		isp->sess_conn_list = isp->sess_conn_list_last_ptr = icp;
156 	} else {
157 		isp->sess_conn_list_last_ptr->conn_next = icp;
158 		isp->sess_conn_list_last_ptr = icp;
159 	}
160 
161 	KSTAT_INC_SESS_CNTR_CONN(isp);
162 	(void) iscsi_conn_kstat_init(icp);
163 
164 	*icpp = icp;
165 
166 	return (ISCSI_STATUS_SUCCESS);
167 }
168 
169 /*
170  * iscsi_conn_online - This attempts to take a connection from
171  * ISCSI_CONN_STATE_FREE to ISCSI_CONN_STATE_LOGGED_IN.
172  */
173 iscsi_status_t
174 iscsi_conn_online(iscsi_conn_t *icp)
175 {
176 	iscsi_task_t	*itp;
177 	iscsi_status_t	rval;
178 
179 	ASSERT(icp != NULL);
180 	ASSERT(mutex_owned(&icp->conn_state_mutex));
181 	ASSERT(icp->conn_state == ISCSI_CONN_STATE_FREE);
182 
183 	/*
184 	 * If we are attempting to connect then for the purposes of the
185 	 * other initiator code we are effectively in ISCSI_CONN_STATE_IN_LOGIN.
186 	 */
187 	iscsi_conn_update_state_locked(icp, ISCSI_CONN_STATE_IN_LOGIN);
188 	mutex_exit(&icp->conn_state_mutex);
189 
190 	/*
191 	 * Sync base connection information before login
192 	 * A login redirection might have shifted the
193 	 * current information from the base.
194 	 */
195 	bcopy(&icp->conn_base_addr, &icp->conn_curr_addr,
196 	    sizeof (icp->conn_curr_addr));
197 
198 	itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
199 	ASSERT(itp != NULL);
200 
201 	itp->t_arg = icp;
202 	itp->t_blocking = B_TRUE;
203 	rval = iscsi_login_start(itp);
204 	kmem_free(itp, sizeof (iscsi_task_t));
205 
206 	mutex_enter(&icp->conn_state_mutex);
207 
208 	return (rval);
209 }
210 
211 /*
212  * iscsi_conn_offline - This attempts to take a connection from
213  * any state to ISCSI_CONN_STATE_FREE.
214  */
215 iscsi_status_t
216 iscsi_conn_offline(iscsi_conn_t *icp)
217 {
218 	clock_t		delay;
219 
220 	ASSERT(icp != NULL);
221 
222 	/*
223 	 * We can only destroy a connection if its either in
224 	 * a state of FREE or LOGGED.  The other states are
225 	 * transitionary and its unsafe to perform actions
226 	 * on the connection in those states.  Set a flag
227 	 * on the connection to influence the transitions
228 	 * to quickly complete.  Then wait for a state
229 	 * transition.
230 	 *
231 	 * ISCSI_CONN_STATE_LOGGED_IN is set immediately at the
232 	 * start of CN_NOTIFY_FFP processing. icp->conn_state_ffp
233 	 * is set to true at the end of ffp processing, at which
234 	 * point any session updates are complete.  We don't
235 	 * want to start offlining the connection before we're
236 	 * done completing the FFP processing since this might
237 	 * interrupt the discovery process.
238 	 */
239 	delay = ddi_get_lbolt() + SEC_TO_TICK(SHUTDOWN_TIMEOUT);
240 	mutex_enter(&icp->conn_state_mutex);
241 	icp->conn_state_destroy = B_TRUE;
242 	while ((((icp->conn_state != ISCSI_CONN_STATE_FREE) &&
243 	    (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN)) ||
244 	    ((icp->conn_state == ISCSI_CONN_STATE_LOGGED_IN) &&
245 	    !icp->conn_state_ffp)) &&
246 	    (ddi_get_lbolt() < delay)) {
247 		/* wait for transition */
248 		(void) cv_timedwait(&icp->conn_state_change,
249 		    &icp->conn_state_mutex, delay);
250 	}
251 
252 	switch (icp->conn_state) {
253 	case ISCSI_CONN_STATE_FREE:
254 		break;
255 	case ISCSI_CONN_STATE_LOGGED_IN:
256 		if (icp->conn_state_ffp)
257 			(void) iscsi_handle_logout(icp);
258 		else {
259 			icp->conn_state_destroy = B_FALSE;
260 			mutex_exit(&icp->conn_state_mutex);
261 			return (ISCSI_STATUS_INTERNAL_ERROR);
262 		}
263 		break;
264 	case ISCSI_CONN_STATE_IN_LOGIN:
265 	case ISCSI_CONN_STATE_IN_LOGOUT:
266 	case ISCSI_CONN_STATE_FAILED:
267 	case ISCSI_CONN_STATE_POLLING:
268 	default:
269 		icp->conn_state_destroy = B_FALSE;
270 		mutex_exit(&icp->conn_state_mutex);
271 		return (ISCSI_STATUS_INTERNAL_ERROR);
272 	}
273 	mutex_exit(&icp->conn_state_mutex);
274 
275 	return (ISCSI_STATUS_SUCCESS);
276 }
277 
278 /*
279  * iscsi_conn_destroy - This destroys an iscsi connection structure
280  * and de-associates it with the session.  The connection should
281  * already been in the ISCSI_CONN_STATE_FREE when attempting this
282  * operation.
283  */
284 iscsi_status_t
285 iscsi_conn_destroy(iscsi_conn_t *icp)
286 {
287 	iscsi_sess_t	*isp;
288 	iscsi_conn_t	*t_icp;
289 
290 	ASSERT(icp != NULL);
291 	isp = icp->conn_sess;
292 	ASSERT(isp != NULL);
293 
294 	if (icp->conn_state != ISCSI_CONN_STATE_FREE) {
295 		return (ISCSI_STATUS_INTERNAL_ERROR);
296 	}
297 
298 	/* Destroy transfer thread */
299 	iscsi_thread_destroy(icp->conn_tx_thread);
300 	ddi_taskq_destroy(icp->conn_cn_taskq);
301 
302 	/* Terminate connection queues */
303 	iscsi_destroy_queue(&icp->conn_queue_idm_aborting);
304 	iscsi_destroy_queue(&icp->conn_queue_active);
305 
306 	cv_destroy(&icp->conn_login_cv);
307 	mutex_destroy(&icp->conn_login_mutex);
308 	cv_destroy(&icp->conn_state_change);
309 	mutex_destroy(&icp->conn_state_mutex);
310 
311 	/*
312 	 * Remove connection from sessions linked list.
313 	 */
314 	if (isp->sess_conn_list == icp) {
315 		/* connection first item in list */
316 		isp->sess_conn_list = icp->conn_next;
317 		/*
318 		 * check if this is also the last item in the list
319 		 */
320 		if (isp->sess_conn_list_last_ptr == icp) {
321 			isp->sess_conn_list_last_ptr = NULL;
322 		}
323 	} else {
324 		/*
325 		 * search session list for icp pointing
326 		 * to connection being removed.  Then
327 		 * update that connections next pointer.
328 		 */
329 		t_icp = isp->sess_conn_list;
330 		while (t_icp->conn_next != NULL) {
331 			if (t_icp->conn_next == icp) {
332 				break;
333 			}
334 			t_icp = t_icp->conn_next;
335 		}
336 		if (t_icp->conn_next == icp) {
337 			t_icp->conn_next = icp->conn_next;
338 			/*
339 			 * if this is the last connection in the list
340 			 * update the last_ptr to point to t_icp
341 			 */
342 			if (isp->sess_conn_list_last_ptr == icp) {
343 				isp->sess_conn_list_last_ptr = t_icp;
344 			}
345 		} else {
346 			/* couldn't find session */
347 			ASSERT(FALSE);
348 		}
349 	}
350 
351 	/* Free this Connections Data */
352 	iscsi_conn_kstat_term(icp);
353 	kmem_free(icp, sizeof (iscsi_conn_t));
354 
355 	return (ISCSI_STATUS_SUCCESS);
356 }
357 
358 
359 /*
360  * iscsi_conn_set_login_min_max - set min/max login window
361  *
362  * Used to set the min and max login window.  Input values
363  * are in seconds.
364  */
365 void
366 iscsi_conn_set_login_min_max(iscsi_conn_t *icp, int min, int max)
367 {
368 	ASSERT(icp != NULL);
369 
370 	icp->conn_login_min = ddi_get_lbolt() + SEC_TO_TICK(min);
371 	icp->conn_login_max = ddi_get_lbolt() + SEC_TO_TICK(max);
372 }
373 
374 
375 /*
376  * Process the idm notifications
377  */
378 idm_status_t
379 iscsi_client_notify(idm_conn_t *ic, idm_client_notify_t icn, uintptr_t data)
380 {
381 	iscsi_cn_task_t		*cn;
382 	iscsi_conn_t		*icp = ic->ic_handle;
383 	iscsi_sess_t		*isp;
384 
385 	/*
386 	 * Don't access icp if the notification is CN_CONNECT_DESTROY
387 	 * since icp may have already been freed.
388 	 *
389 	 * In particular, we cannot audit the CN_CONNECT_DESTROY event.
390 	 *
391 	 * Handle a few cases immediately, the rest in a task queue.
392 	 */
393 	switch (icn) {
394 	case CN_CONNECT_FAIL:
395 	case CN_LOGIN_FAIL:
396 		/*
397 		 * Wakeup any thread waiting for login stuff to happen.
398 		 */
399 		ASSERT(icp != NULL);
400 
401 		mutex_enter(&icp->conn_state_mutex);
402 		idm_sm_audit_event(&icp->conn_state_audit,
403 		    SAS_ISCSI_CONN, icp->conn_state, icn, data);
404 		mutex_exit(&icp->conn_state_mutex);
405 		iscsi_login_update_state(icp, LOGIN_ERROR);
406 		return (IDM_STATUS_SUCCESS);
407 
408 	case CN_READY_FOR_LOGIN:
409 		idm_conn_hold(ic); /* Released in CN_CONNECT_LOST */
410 		ASSERT(icp != NULL);
411 
412 		mutex_enter(&icp->conn_state_mutex);
413 		idm_sm_audit_event(&icp->conn_state_audit,
414 		    SAS_ISCSI_CONN, icp->conn_state, icn, data);
415 		icp->conn_state_idm_connected = B_TRUE;
416 		cv_broadcast(&icp->conn_state_change);
417 		mutex_exit(&icp->conn_state_mutex);
418 
419 		iscsi_login_update_state(icp, LOGIN_READY);
420 		return (IDM_STATUS_SUCCESS);
421 
422 	case CN_CONNECT_DESTROY:
423 		/*
424 		 * We released any dependecies we had on this object in
425 		 * either CN_LOGIN_FAIL or CN_CONNECT_LOST so we just need
426 		 * to destroy the IDM connection now.
427 		 */
428 		idm_ini_conn_destroy(ic);
429 		return (IDM_STATUS_SUCCESS);
430 	}
431 
432 	ASSERT(icp != NULL);
433 	mutex_enter(&icp->conn_state_mutex);
434 	idm_sm_audit_event(&icp->conn_state_audit,
435 	    SAS_ISCSI_CONN, icp->conn_state, icn, data);
436 	mutex_exit(&icp->conn_state_mutex);
437 	isp = icp->conn_sess;
438 
439 	/*
440 	 * Dispatch notifications to the taskq since they often require
441 	 * long blocking operations.  In the case of CN_CONNECT_DESTROY
442 	 * we actually just want to destroy the connection which we
443 	 * can't do in the IDM taskq context.
444 	 */
445 	cn = kmem_alloc(sizeof (*cn), KM_SLEEP);
446 
447 	cn->ct_ic = ic;
448 	cn->ct_icn = icn;
449 	cn->ct_data = data;
450 
451 	idm_conn_hold(ic);
452 
453 	if (ddi_taskq_dispatch(icp->conn_cn_taskq,
454 	    iscsi_client_notify_task, cn, DDI_SLEEP) != DDI_SUCCESS) {
455 		idm_conn_rele(ic);
456 		cmn_err(CE_WARN, "iscsi connection(%u) failure - "
457 		    "unable to schedule notify task", icp->conn_oid);
458 		iscsi_conn_update_state(icp, ISCSI_CONN_STATE_FREE);
459 		mutex_enter(&isp->sess_state_mutex);
460 		iscsi_sess_state_machine(isp,
461 		    ISCSI_SESS_EVENT_N6);
462 		mutex_exit(&isp->sess_state_mutex);
463 	}
464 
465 	return (IDM_STATUS_SUCCESS);
466 }
467 
468 static void
469 iscsi_client_notify_task(void *cn_task_void)
470 {
471 	iscsi_cn_task_t		*cn_task = cn_task_void;
472 	iscsi_conn_t		*icp;
473 	iscsi_sess_t		*isp;
474 	idm_conn_t		*ic;
475 	idm_client_notify_t	icn;
476 	uintptr_t		data;
477 	idm_ffp_disable_t	disable_type;
478 	boolean_t		in_login;
479 
480 	ic = cn_task->ct_ic;
481 	icn = cn_task->ct_icn;
482 	data = cn_task->ct_data;
483 
484 	icp = ic->ic_handle;
485 	ASSERT(icp != NULL);
486 	isp = icp->conn_sess;
487 
488 	switch (icn) {
489 	case CN_FFP_ENABLED:
490 		mutex_enter(&icp->conn_state_mutex);
491 		icp->conn_async_logout = B_FALSE;
492 		icp->conn_state_ffp = B_TRUE;
493 		cv_broadcast(&icp->conn_state_change);
494 		mutex_exit(&icp->conn_state_mutex);
495 
496 		/*
497 		 * This logic assumes that the IDM login-snooping code
498 		 * and the initiator login code will agree to go when
499 		 * the connection is in FFP or final error received.
500 		 * The reason we do this is that we don't want to process
501 		 * CN_FFP_DISABLED until CN_FFP_ENABLED has been full handled.
502 		 */
503 		mutex_enter(&icp->conn_login_mutex);
504 		while ((icp->conn_login_state != LOGIN_FFP) &&
505 		    (icp->conn_login_state != LOGIN_ERROR)) {
506 			cv_wait(&icp->conn_login_cv, &icp->conn_login_mutex);
507 		}
508 		mutex_exit(&icp->conn_login_mutex);
509 		break;
510 	case CN_FFP_DISABLED:
511 		disable_type = (idm_ffp_disable_t)data;
512 
513 		mutex_enter(&icp->conn_state_mutex);
514 		switch (disable_type) {
515 		case FD_SESS_LOGOUT:
516 		case FD_CONN_LOGOUT:
517 			if (icp->conn_async_logout) {
518 				/*
519 				 * Our logout was in response to an
520 				 * async logout request so treat this
521 				 * like a connection failure (we will
522 				 * try to re-establish the connection)
523 				 */
524 				iscsi_conn_update_state_locked(icp,
525 				    ISCSI_CONN_STATE_FAILED);
526 			} else {
527 				/*
528 				 * Logout due to to user config change,
529 				 * we will not try to re-establish
530 				 * the connection.
531 				 */
532 				iscsi_conn_update_state_locked(icp,
533 				    ISCSI_CONN_STATE_IN_LOGOUT);
534 				/*
535 				 * Hold off generating the ISCSI_SESS_EVENT_N3
536 				 * event until we get the CN_CONNECT_LOST
537 				 * notification.  This matches the pre-IDM
538 				 * implementation better.
539 				 */
540 			}
541 			break;
542 
543 		case FD_CONN_FAIL:
544 		default:
545 			if (icp->conn_state == ISCSI_CONN_STATE_IN_LOGIN) {
546 				iscsi_conn_update_state_locked(icp,
547 				    ISCSI_CONN_STATE_FREE);
548 			} else {
549 				iscsi_conn_update_state_locked(icp,
550 				    ISCSI_CONN_STATE_FAILED);
551 			}
552 			break;
553 		}
554 
555 		icp->conn_state_ffp = B_FALSE;
556 		cv_broadcast(&icp->conn_state_change);
557 		mutex_exit(&icp->conn_state_mutex);
558 
559 		break;
560 	case CN_CONNECT_LOST:
561 		/*
562 		 * We only care about CN_CONNECT_LOST if we've logged in.  IDM
563 		 * sends a flag as the data payload to indicate whether we
564 		 * were trying to login.  The CN_LOGIN_FAIL notification
565 		 * gives us what we need to know for login failures and
566 		 * otherwise we will need to keep a bunch of state to know
567 		 * what CN_CONNECT_LOST means to us.
568 		 */
569 		in_login = (boolean_t)data;
570 		if (in_login ||
571 		    (icp->conn_prev_state == ISCSI_CONN_STATE_IN_LOGIN)) {
572 			mutex_enter(&icp->conn_state_mutex);
573 
574 			icp->conn_state_idm_connected = B_FALSE;
575 			cv_broadcast(&icp->conn_state_change);
576 			mutex_exit(&icp->conn_state_mutex);
577 
578 			/* Release connect hold from CN_READY_FOR_LOGIN */
579 			idm_conn_rele(ic);
580 			break;
581 		}
582 
583 		/* Any remaining commands are never going to finish */
584 		iscsi_conn_flush_active_cmds(icp);
585 
586 		/*
587 		 * The connection is no longer active so cleanup any
588 		 * references to the connection and release any holds so
589 		 * that IDM can finish cleanup.
590 		 */
591 		mutex_enter(&icp->conn_state_mutex);
592 		if (icp->conn_state != ISCSI_CONN_STATE_FAILED) {
593 
594 			mutex_enter(&isp->sess_state_mutex);
595 			iscsi_sess_state_machine(isp, ISCSI_SESS_EVENT_N3);
596 			mutex_exit(&isp->sess_state_mutex);
597 
598 			iscsi_conn_update_state_locked(icp,
599 			    ISCSI_CONN_STATE_FREE);
600 		} else {
601 
602 			mutex_enter(&isp->sess_state_mutex);
603 			iscsi_sess_state_machine(isp,
604 			    ISCSI_SESS_EVENT_N5);
605 			mutex_exit(&isp->sess_state_mutex);
606 
607 			/*
608 			 * If session type is NORMAL, try to reestablish the
609 			 * connection.
610 			 */
611 			if (isp->sess_type == ISCSI_SESS_TYPE_NORMAL) {
612 				iscsi_conn_retry(isp, icp);
613 			} else {
614 
615 				mutex_enter(&isp->sess_state_mutex);
616 				iscsi_sess_state_machine(isp,
617 				    ISCSI_SESS_EVENT_N6);
618 				mutex_exit(&isp->sess_state_mutex);
619 
620 				iscsi_conn_update_state_locked(icp,
621 				    ISCSI_CONN_STATE_FREE);
622 			}
623 		}
624 
625 		(void) iscsi_thread_stop(icp->conn_tx_thread);
626 
627 		icp->conn_state_idm_connected = B_FALSE;
628 		cv_broadcast(&icp->conn_state_change);
629 		mutex_exit(&icp->conn_state_mutex);
630 
631 		/* Release connect hold from CN_READY_FOR_LOGIN */
632 		idm_conn_rele(ic);
633 		break;
634 	default:
635 		ISCSI_CONN_LOG(CE_WARN,
636 		    "iscsi_client_notify: unknown notification: "
637 		    "%x: NOT IMPLEMENTED YET: icp: %p ic: %p ",
638 		    icn, (void *)icp, (void *)ic);
639 		break;
640 	}
641 	/* free the task notify structure we allocated in iscsi_client_notify */
642 	kmem_free(cn_task, sizeof (*cn_task));
643 
644 	/* Release the hold we acquired in iscsi_client_notify */
645 	idm_conn_rele(ic);
646 }
647 
648 /*
649  * iscsi_conn_sync_params - used to update connection parameters
650  *
651  * Used to update connection parameters with current configured
652  * parameters in the persistent store.  This should be called
653  * before starting to make a new iscsi connection in iscsi_login.
654  */
655 iscsi_status_t
656 iscsi_conn_sync_params(iscsi_conn_t *icp)
657 {
658 	iscsi_sess_t		*isp;
659 	iscsi_hba_t		*ihp;
660 	int			param_id;
661 	persistent_param_t	pp;
662 	persistent_tunable_param_t	ptp;
663 	iscsi_config_sess_t	*ics;
664 	int			idx, size;
665 	char			*name;
666 
667 	ASSERT(icp != NULL);
668 	ASSERT((icp->conn_state == ISCSI_CONN_STATE_IN_LOGIN) ||
669 	    (icp->conn_state == ISCSI_CONN_STATE_FAILED) ||
670 	    (icp->conn_state == ISCSI_CONN_STATE_POLLING));
671 	isp = icp->conn_sess;
672 	ASSERT(isp != NULL);
673 	ihp = isp->sess_hba;
674 	ASSERT(ihp != NULL);
675 
676 	/*
677 	 * Check if someone is trying to destroy this
678 	 * connection.  If so fail the sync request,
679 	 * as a method of fast fail.
680 	 */
681 	if (icp->conn_state_destroy == B_TRUE) {
682 		return (ISCSI_STATUS_SHUTDOWN);
683 	}
684 
685 	bzero(&pp, sizeof (pp));
686 
687 	/* First get a copy of the HBA params */
688 	bcopy(&ihp->hba_params, &icp->conn_params,
689 	    sizeof (iscsi_login_params_t));
690 	bcopy(&ihp->hba_tunable_params, &icp->conn_tunable_params,
691 	    sizeof (iscsi_tunable_params_t));
692 
693 	/*
694 	 * Now we need to get the session configured
695 	 * values from the persistent store and apply
696 	 * them to our connection.
697 	 */
698 	(void) persistent_param_get((char *)isp->sess_name, &pp);
699 	for (param_id = 0; param_id < ISCSI_NUM_LOGIN_PARAM;
700 	    param_id++) {
701 		if (iscsiboot_prop && modrootloaded &&
702 		    !iscsi_chk_bootlun_mpxio(ihp) && isp->sess_boot) {
703 			/*
704 			 * iscsi boot with mpxio disabled
705 			 * while iscsi booting target's parameter overriden
706 			 * do no update target's parameters.
707 			 */
708 			if (pp.p_bitmap) {
709 				cmn_err(CE_NOTE, "Adopting "
710 				    " default login parameters in"
711 				    " boot session as MPxIO is disabled");
712 			}
713 			break;
714 		}
715 		if (pp.p_bitmap & (1 << param_id)) {
716 
717 			switch (param_id) {
718 			/*
719 			 * Boolean parameters
720 			 */
721 			case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER:
722 				icp->conn_params.data_pdu_in_order =
723 				    pp.p_params.data_pdu_in_order;
724 				break;
725 			case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA:
726 				icp->conn_params.immediate_data =
727 				    pp.p_params.immediate_data;
728 				break;
729 			case ISCSI_LOGIN_PARAM_INITIAL_R2T:
730 				icp->conn_params.initial_r2t =
731 				    pp.p_params.initial_r2t;
732 				break;
733 			case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER:
734 				icp->conn_params.data_pdu_in_order =
735 				    pp.p_params.data_pdu_in_order;
736 				break;
737 			/*
738 			 * Integer parameters
739 			 */
740 			case ISCSI_LOGIN_PARAM_HEADER_DIGEST:
741 				icp->conn_params.header_digest =
742 				    pp.p_params.header_digest;
743 				break;
744 			case ISCSI_LOGIN_PARAM_DATA_DIGEST:
745 				icp->conn_params.data_digest =
746 				    pp.p_params.data_digest;
747 				break;
748 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN:
749 				icp->conn_params.default_time_to_retain =
750 				    pp.p_params.default_time_to_retain;
751 				break;
752 			case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT:
753 				icp->conn_params.default_time_to_wait =
754 				    pp.p_params.default_time_to_wait;
755 				break;
756 			case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH:
757 				icp->conn_params.max_recv_data_seg_len =
758 				    pp.p_params.max_recv_data_seg_len;
759 				break;
760 			case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH:
761 				icp->conn_params.first_burst_length =
762 				    pp.p_params.first_burst_length;
763 				break;
764 			case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH:
765 				icp->conn_params.max_burst_length =
766 				    pp.p_params.max_burst_length;
767 				break;
768 
769 			/*
770 			 * Integer parameters which currently are unsettable
771 			 */
772 			case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS:
773 				/* FALLTHRU */
774 			case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T:
775 				/* FALLTHRU */
776 			case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL:
777 				/* FALLTHRU */
778 			default:
779 				break;
780 			}
781 		}
782 	}
783 
784 	if (persistent_get_tunable_param((char *)isp->sess_name, &ptp) ==
785 	    B_TRUE) {
786 		if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_RX_TIMEOUT_VALUE) {
787 			icp->conn_tunable_params.recv_login_rsp_timeout =
788 			    ptp.p_params.recv_login_rsp_timeout;
789 		}
790 		if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_CONN_LOGIN_MAX) {
791 			icp->conn_tunable_params.conn_login_max =
792 			    ptp.p_params.conn_login_max;
793 		}
794 		if (ptp.p_bitmap & ISCSI_TUNABLE_PARAM_LOGIN_POLLING_DELAY) {
795 			icp->conn_tunable_params.polling_login_delay =
796 			    ptp.p_params.polling_login_delay;
797 		}
798 	}
799 
800 	/* Skip binding checks on discovery sessions */
801 	if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) {
802 		return (ISCSI_STATUS_SUCCESS);
803 	}
804 
805 	/*
806 	 * Now we need to get the current optional connection
807 	 * binding information.
808 	 */
809 	/* setup initial buffer for configured session information */
810 	size = sizeof (*ics);
811 	ics = kmem_zalloc(size, KM_SLEEP);
812 	ics->ics_in = 1;
813 
814 	/* get configured sessions information */
815 	name = (char *)isp->sess_name;
816 	if (persistent_get_config_session(name, ics) == B_FALSE) {
817 		/*
818 		 * If we were unable to get target level information
819 		 * then check the initiator level information.
820 		 */
821 		name = (char *)isp->sess_hba->hba_name;
822 		if (persistent_get_config_session(name, ics) == B_FALSE) {
823 			/*
824 			 * No hba information is found.  So assume default
825 			 * one session unbound behavior.
826 			 */
827 			ics->ics_out = 1;
828 			ics->ics_bound = B_FALSE;
829 		}
830 	}
831 
832 	if (iscsiboot_prop && (ics->ics_out > 1) && isp->sess_boot &&
833 	    !iscsi_chk_bootlun_mpxio(ihp)) {
834 		/*
835 		 * iscsi booting session with mpxio disabled,
836 		 * no need set multiple sessions for booting session
837 		 */
838 		ics->ics_out = 1;
839 		ics->ics_bound = B_FALSE;
840 		cmn_err(CE_NOTE, "MPxIO is disabled,"
841 		    " no need to configure multiple boot sessions");
842 	}
843 
844 	/*
845 	 * Check to make sure this session is still a configured
846 	 * session.  The user might have decreased the session
847 	 * count. (NOTE: byte 5 of the sess_isid is the session
848 	 * count (via MS/T).  This counter starts at 0.)
849 	 */
850 
851 
852 	idx = isp->sess_isid[5];
853 
854 	if (iscsiboot_prop && (idx == ISCSI_MAX_CONFIG_SESSIONS)) {
855 		/*
856 		 * This is temporary session for boot session propose
857 		 * no need to bound IP for this session
858 		 */
859 		icp->conn_bound = B_FALSE;
860 		kmem_free(ics, sizeof (iscsi_config_sess_t));
861 		return (ISCSI_STATUS_SUCCESS);
862 	}
863 
864 	if (ics->ics_out <= idx) {
865 		/*
866 		 * No longer a configured session.  Return a
867 		 * failure so we don't attempt to relogin.
868 		 */
869 		return (ISCSI_STATUS_SHUTDOWN);
870 	}
871 
872 	/*
873 	 * If sessions are unbound set this information on
874 	 * the connection and return success.
875 	 */
876 	if (ics->ics_bound == B_FALSE) {
877 		icp->conn_bound = B_FALSE;
878 		kmem_free(ics, sizeof (iscsi_config_sess_t));
879 		return (ISCSI_STATUS_SUCCESS);
880 	}
881 
882 	/*
883 	 * Since the sessions are bound we need to find the matching
884 	 * binding information for the session's isid.  If this
885 	 * session's isid is > 0 then we need to get more configured
886 	 * session information to find the binding info.
887 	 */
888 	if (idx > 0) {
889 		int ics_out;
890 
891 		ics_out = ics->ics_out;
892 		/* record new size and free last buffer */
893 		size = ISCSI_SESSION_CONFIG_SIZE(ics_out);
894 		kmem_free(ics, sizeof (*ics));
895 
896 		/* allocate new buffer */
897 		ics = kmem_zalloc(size, KM_SLEEP);
898 		ics->ics_in = ics_out;
899 
900 		/* get configured sessions information */
901 		if (persistent_get_config_session(name, ics) != B_TRUE) {
902 			cmn_err(CE_NOTE, "iscsi session(%d) - "
903 			    "unable to get configured session information\n",
904 			    isp->sess_oid);
905 			kmem_free(ics, size);
906 			return (ISCSI_STATUS_SHUTDOWN);
907 		}
908 	}
909 
910 	/* Copy correct binding information to the connection */
911 	icp->conn_bound = B_TRUE;
912 	if (ics->ics_bindings[idx].i_insize == sizeof (struct in_addr)) {
913 		bcopy(&ics->ics_bindings[idx].i_addr.in4,
914 		    &icp->conn_bound_addr.sin4.sin_addr.s_addr,
915 		    sizeof (struct in_addr));
916 		icp->conn_bound_addr.sin4.sin_family = AF_INET;
917 	} else {
918 		bcopy(&ics->ics_bindings[idx].i_addr.in6,
919 		    &icp->conn_bound_addr.sin6.sin6_addr.s6_addr,
920 		    sizeof (struct in6_addr));
921 		icp->conn_bound_addr.sin6.sin6_family = AF_INET6;
922 	}
923 
924 	kmem_free(ics, size);
925 
926 	return (ISCSI_STATUS_SUCCESS);
927 }
928 
929 /*
930  * +--------------------------------------------------------------------+
931  * | Internal Connection Interfaces					|
932  * +--------------------------------------------------------------------+
933  */
934 
935 /*
936  * iscsi_conn_flush_active_cmds - flush all active icmdps
937  *	for a connection.
938  */
939 static void
940 iscsi_conn_flush_active_cmds(iscsi_conn_t *icp)
941 {
942 	iscsi_cmd_t	*icmdp;
943 	iscsi_sess_t	*isp;
944 	boolean_t	lock_held = B_FALSE;
945 
946 	ASSERT(icp != NULL);
947 	isp = icp->conn_sess;
948 	ASSERT(isp != NULL);
949 
950 	if (mutex_owned(&icp->conn_queue_active.mutex)) {
951 		lock_held = B_TRUE;
952 	} else {
953 		mutex_enter(&icp->conn_queue_active.mutex);
954 	}
955 
956 	/* Flush active queue */
957 	icmdp = icp->conn_queue_active.head;
958 	while (icmdp != NULL) {
959 
960 		mutex_enter(&icmdp->cmd_mutex);
961 		if (icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) {
962 			icmdp->cmd_un.scsi.pkt_stat |= STAT_ABORTED;
963 		}
964 		mutex_exit(&icmdp->cmd_mutex);
965 
966 		iscsi_cmd_state_machine(icmdp,
967 		    ISCSI_CMD_EVENT_E7, isp);
968 		icmdp = icp->conn_queue_active.head;
969 	}
970 
971 	/* Wait for active queue to drain */
972 	while (icp->conn_queue_active.count) {
973 		mutex_exit(&icp->conn_queue_active.mutex);
974 		delay(drv_usectohz(100000));
975 		mutex_enter(&icp->conn_queue_active.mutex);
976 	}
977 
978 	if (lock_held == B_FALSE) {
979 		mutex_exit(&icp->conn_queue_active.mutex);
980 	}
981 
982 	/* Wait for IDM abort queue to drain (if necessary) */
983 	mutex_enter(&icp->conn_queue_idm_aborting.mutex);
984 	while (icp->conn_queue_idm_aborting.count) {
985 		mutex_exit(&icp->conn_queue_idm_aborting.mutex);
986 		delay(drv_usectohz(100000));
987 		mutex_enter(&icp->conn_queue_idm_aborting.mutex);
988 	}
989 	mutex_exit(&icp->conn_queue_idm_aborting.mutex);
990 }
991 
992 /*
993  * iscsi_conn_retry - retry connect/login
994  */
995 void
996 iscsi_conn_retry(iscsi_sess_t *isp, iscsi_conn_t *icp)
997 {
998 	iscsi_task_t *itp;
999 
1000 	ASSERT(isp != NULL);
1001 	ASSERT(icp != NULL);
1002 
1003 	/* set login min/max time values */
1004 	iscsi_conn_set_login_min_max(icp,
1005 	    ISCSI_CONN_DEFAULT_LOGIN_MIN,
1006 	    icp->conn_tunable_params.conn_login_max);
1007 
1008 	ISCSI_CONN_LOG(CE_NOTE, "DEBUG: iscsi_conn_retry: icp: %p icp: %p ",
1009 	    (void *)icp,
1010 	    (void *)icp->conn_ic);
1011 
1012 	/*
1013 	 * Sync base connection information before login.
1014 	 * A login redirection might have shifted the
1015 	 * current information from the base.
1016 	 */
1017 	bcopy(&icp->conn_base_addr, &icp->conn_curr_addr,
1018 	    sizeof (icp->conn_curr_addr));
1019 
1020 	/* schedule login task */
1021 	itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
1022 	itp->t_arg = icp;
1023 	itp->t_blocking = B_FALSE;
1024 	if (ddi_taskq_dispatch(isp->sess_taskq,
1025 	    (void(*)())iscsi_login_start, itp, DDI_SLEEP) !=
1026 	    DDI_SUCCESS) {
1027 		kmem_free(itp, sizeof (iscsi_task_t));
1028 		cmn_err(CE_WARN, "iscsi connection(%u) failure - "
1029 		    "unable to schedule login task", icp->conn_oid);
1030 
1031 		iscsi_conn_update_state(icp, ISCSI_CONN_STATE_FREE);
1032 		mutex_enter(&isp->sess_state_mutex);
1033 		iscsi_sess_state_machine(isp,
1034 		    ISCSI_SESS_EVENT_N6);
1035 		mutex_exit(&isp->sess_state_mutex);
1036 	}
1037 }
1038 
1039 void
1040 iscsi_conn_update_state(iscsi_conn_t *icp, iscsi_conn_state_t
1041 			    next_state)
1042 {
1043 	mutex_enter(&icp->conn_state_mutex);
1044 	(void) iscsi_conn_update_state_locked(icp, next_state);
1045 	mutex_exit(&icp->conn_state_mutex);
1046 }
1047 
1048 void
1049 iscsi_conn_update_state_locked(iscsi_conn_t *icp,
1050 	    iscsi_conn_state_t next_state)
1051 {
1052 	ASSERT(mutex_owned(&icp->conn_state_mutex));
1053 	next_state = (next_state > ISCSI_CONN_STATE_MAX) ?
1054 	    ISCSI_CONN_STATE_MAX : next_state;
1055 	idm_sm_audit_state_change(&icp->conn_state_audit,
1056 	    SAS_ISCSI_CONN, icp->conn_state, next_state);
1057 	switch (next_state) {
1058 	case ISCSI_CONN_STATE_FREE:
1059 	case ISCSI_CONN_STATE_IN_LOGIN:
1060 	case ISCSI_CONN_STATE_LOGGED_IN:
1061 	case ISCSI_CONN_STATE_IN_LOGOUT:
1062 	case ISCSI_CONN_STATE_FAILED:
1063 	case ISCSI_CONN_STATE_POLLING:
1064 		ISCSI_CONN_LOG(CE_NOTE,
1065 		    "iscsi_conn_update_state conn %p %s(%d) -> %s(%d)",
1066 		    (void *)icp,
1067 		    iscsi_ics_name[icp->conn_state], icp->conn_state,
1068 		    iscsi_ics_name[next_state], next_state);
1069 		icp->conn_prev_state = icp->conn_state;
1070 		icp->conn_state = next_state;
1071 		cv_broadcast(&icp->conn_state_change);
1072 		break;
1073 	default:
1074 		cmn_err(CE_WARN, "Update state found illegal state: %x "
1075 		    "prev_state: %x", next_state, icp->conn_prev_state);
1076 		ASSERT(0);
1077 	}
1078 }
1079