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/*
23 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#include    <sun_sas.h>
28
29/*
30 * Closes an adapter
31 *
32 * the handle is removed from the open_handles list
33 */
34void
35Sun_sasCloseAdapter(HBA_HANDLE handle)
36{
37	const char			ROUTINE[] = "Sun_sasCloseAdapter";
38	struct open_handle		*open_handle_ptr, *open_handle_prev_ptr;
39	int				found = 0;
40
41	if (global_hba_head == NULL) {
42		log(LOG_DEBUG, ROUTINE,
43		    "Attempted to close an invalid handle %08lx. "
44		    "There are no hba handles loaded in the VSL.",
45		    handle);
46		return;
47	}
48
49	/* Removing handle from open_handles; */
50	lock(&open_handles_lock);
51	if (global_hba_head->open_handles == NULL) {
52		/* check to see if there are any open global_hba_head */
53		log(LOG_DEBUG, ROUTINE,
54		    "Attempted to close an invalid handle %08lx. "
55		    "There are no open handles in the VSL.",
56		    handle);
57	} else if (global_hba_head->open_handles->next == NULL) {
58		/* there is only one handle open */
59		if (global_hba_head->open_handles->handle == handle) {
60			free(global_hba_head->open_handles);
61			global_hba_head->open_handles = NULL;
62		} else {
63			log(LOG_DEBUG, ROUTINE,
64			    "Attempted to close an invalid handle %08lx. "
65			    "Unable to find handle to close.", handle);
66		}
67	} else {	/* there is more than one handle open */
68		open_handle_ptr = global_hba_head->open_handles;
69		if (open_handle_ptr->handle == handle) {
70			global_hba_head->open_handles = open_handle_ptr->next;
71			free(open_handle_ptr);
72		} else {
73			for (open_handle_ptr = open_handle_ptr->next,
74			    open_handle_prev_ptr =
75			    global_hba_head->open_handles;
76			    open_handle_ptr != NULL;
77			    open_handle_ptr = open_handle_ptr->next) {
78				if (open_handle_ptr->handle == handle) {
79					open_handle_prev_ptr->next =
80					    open_handle_ptr->next;
81					free(open_handle_ptr);
82					found = 1;
83					break;
84				} else {
85					open_handle_prev_ptr =
86					    open_handle_prev_ptr->next;
87				}
88			}
89			if (found == 0) {
90				log(LOG_DEBUG, ROUTINE,
91				    "Attempted to close an invalid handle "
92				    "%08lx.  Unable to find handle to close.",
93				    handle);
94			}
95		}
96	}
97
98	unlock(&open_handles_lock);
99}
100