xref: /onnv-gate/usr/src/lib/sun_sas/common/Sun_sasFreeLibrary.c (revision 10652:9d0aff74d6fd)
1*10652SHyon.Kim@Sun.COM /*
2*10652SHyon.Kim@Sun.COM  * CDDL HEADER START
3*10652SHyon.Kim@Sun.COM  *
4*10652SHyon.Kim@Sun.COM  * The contents of this file are subject to the terms of the
5*10652SHyon.Kim@Sun.COM  * Common Development and Distribution License (the "License").
6*10652SHyon.Kim@Sun.COM  * You may not use this file except in compliance with the License.
7*10652SHyon.Kim@Sun.COM  *
8*10652SHyon.Kim@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10652SHyon.Kim@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10652SHyon.Kim@Sun.COM  * See the License for the specific language governing permissions
11*10652SHyon.Kim@Sun.COM  * and limitations under the License.
12*10652SHyon.Kim@Sun.COM  *
13*10652SHyon.Kim@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10652SHyon.Kim@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10652SHyon.Kim@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10652SHyon.Kim@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10652SHyon.Kim@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10652SHyon.Kim@Sun.COM  *
19*10652SHyon.Kim@Sun.COM  * CDDL HEADER END
20*10652SHyon.Kim@Sun.COM  */
21*10652SHyon.Kim@Sun.COM 
22*10652SHyon.Kim@Sun.COM /*
23*10652SHyon.Kim@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*10652SHyon.Kim@Sun.COM  * Use is subject to license terms.
25*10652SHyon.Kim@Sun.COM  */
26*10652SHyon.Kim@Sun.COM 
27*10652SHyon.Kim@Sun.COM 
28*10652SHyon.Kim@Sun.COM #include <sun_sas.h>
29*10652SHyon.Kim@Sun.COM 
30*10652SHyon.Kim@Sun.COM /*
31*10652SHyon.Kim@Sun.COM  * Frees the HBA Library.  Must be called after all HBA library functions
32*10652SHyon.Kim@Sun.COM  * to free all resources
33*10652SHyon.Kim@Sun.COM  */
Sun_sasFreeLibrary()34*10652SHyon.Kim@Sun.COM HBA_STATUS Sun_sasFreeLibrary() {
35*10652SHyon.Kim@Sun.COM 	HBA_STATUS 	status;
36*10652SHyon.Kim@Sun.COM 
37*10652SHyon.Kim@Sun.COM 	lock(&all_hbas_lock);
38*10652SHyon.Kim@Sun.COM 
39*10652SHyon.Kim@Sun.COM 	status = FreeHBA(global_hba_head);
40*10652SHyon.Kim@Sun.COM 
41*10652SHyon.Kim@Sun.COM 	/* re-initialize all global variables */
42*10652SHyon.Kim@Sun.COM 	global_hba_head = NULL;
43*10652SHyon.Kim@Sun.COM 	hba_count = 0;
44*10652SHyon.Kim@Sun.COM 	open_handle_index = 1;
45*10652SHyon.Kim@Sun.COM 	unlock(&all_hbas_lock);
46*10652SHyon.Kim@Sun.COM 	(void) mutex_destroy(&all_hbas_lock);
47*10652SHyon.Kim@Sun.COM 
48*10652SHyon.Kim@Sun.COM 	/* free sysevent handle. */
49*10652SHyon.Kim@Sun.COM 	if (gSysEventHandle != NULL)
50*10652SHyon.Kim@Sun.COM 		sysevent_unbind_handle(gSysEventHandle);
51*10652SHyon.Kim@Sun.COM 
52*10652SHyon.Kim@Sun.COM 	/* Reset our load count so we can be reloaded now */
53*10652SHyon.Kim@Sun.COM 	loadCount = 0;
54*10652SHyon.Kim@Sun.COM 
55*10652SHyon.Kim@Sun.COM 	return (status);
56*10652SHyon.Kim@Sun.COM }
57*10652SHyon.Kim@Sun.COM 
58*10652SHyon.Kim@Sun.COM /*
59*10652SHyon.Kim@Sun.COM  * Internal routine to free up hba_ptr's (and all sub-structures)
60*10652SHyon.Kim@Sun.COM  */
FreeHBA(struct sun_sas_hba * hba)61*10652SHyon.Kim@Sun.COM HBA_STATUS FreeHBA(struct sun_sas_hba *hba) {
62*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	*hba_ptr = NULL;
63*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	*last_hba_ptr = NULL;
64*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*hba_port = NULL;
65*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*last_hba_port = NULL;
66*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*tgt_port = NULL;
67*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*last_tgt_port = NULL;
68*10652SHyon.Kim@Sun.COM 	struct ScsiEntryList	*scsi_info = NULL;
69*10652SHyon.Kim@Sun.COM 	struct ScsiEntryList	*last_scsi_info = NULL;
70*10652SHyon.Kim@Sun.COM 	struct phy_info		*phy_ptr = NULL;
71*10652SHyon.Kim@Sun.COM 	struct phy_info		*last_phy = NULL;
72*10652SHyon.Kim@Sun.COM 	struct open_handle	*open_handle = NULL;
73*10652SHyon.Kim@Sun.COM 	struct open_handle	*last_open_handle = NULL;
74*10652SHyon.Kim@Sun.COM 
75*10652SHyon.Kim@Sun.COM 	last_hba_ptr = NULL;
76*10652SHyon.Kim@Sun.COM 	/* walk through global_hba_head list freeing each handle */
77*10652SHyon.Kim@Sun.COM 	for (hba_ptr = hba;
78*10652SHyon.Kim@Sun.COM 	    hba_ptr != NULL;
79*10652SHyon.Kim@Sun.COM 	    hba_ptr = hba_ptr->next) {
80*10652SHyon.Kim@Sun.COM 		/* Free the nested structures (port and attached port) */
81*10652SHyon.Kim@Sun.COM 		hba_port = hba_ptr->first_port;
82*10652SHyon.Kim@Sun.COM 		while (hba_port != NULL) {
83*10652SHyon.Kim@Sun.COM 		    /* Free discovered port structure list. */
84*10652SHyon.Kim@Sun.COM 		    tgt_port = hba_port->first_attached_port;
85*10652SHyon.Kim@Sun.COM 		    while (tgt_port != NULL) {
86*10652SHyon.Kim@Sun.COM 			    /* Free target mapping data list first. */
87*10652SHyon.Kim@Sun.COM 			    scsi_info = tgt_port->scsiInfo;
88*10652SHyon.Kim@Sun.COM 			    while (scsi_info != NULL) {
89*10652SHyon.Kim@Sun.COM 				    last_scsi_info = scsi_info;
90*10652SHyon.Kim@Sun.COM 				    scsi_info = scsi_info->next;
91*10652SHyon.Kim@Sun.COM 				    free(last_scsi_info);
92*10652SHyon.Kim@Sun.COM 			    }
93*10652SHyon.Kim@Sun.COM 			last_tgt_port = tgt_port;
94*10652SHyon.Kim@Sun.COM 			tgt_port = tgt_port->next;
95*10652SHyon.Kim@Sun.COM 			free(last_tgt_port->port_attributes.\
96*10652SHyon.Kim@Sun.COM 			    PortSpecificAttribute.SASPort);
97*10652SHyon.Kim@Sun.COM 			free(last_tgt_port);
98*10652SHyon.Kim@Sun.COM 		    }
99*10652SHyon.Kim@Sun.COM 
100*10652SHyon.Kim@Sun.COM 		    phy_ptr = hba_port->first_phy;
101*10652SHyon.Kim@Sun.COM 		    while (phy_ptr != NULL) {
102*10652SHyon.Kim@Sun.COM 			last_phy = phy_ptr;
103*10652SHyon.Kim@Sun.COM 			phy_ptr = phy_ptr->next;
104*10652SHyon.Kim@Sun.COM 			free(last_phy);
105*10652SHyon.Kim@Sun.COM 		    }
106*10652SHyon.Kim@Sun.COM 
107*10652SHyon.Kim@Sun.COM 		    last_hba_port = hba_port;
108*10652SHyon.Kim@Sun.COM 		    hba_port = hba_port->next;
109*10652SHyon.Kim@Sun.COM 		    free(last_hba_port->port_attributes.\
110*10652SHyon.Kim@Sun.COM 			PortSpecificAttribute.SASPort);
111*10652SHyon.Kim@Sun.COM 		    free(last_hba_port);
112*10652SHyon.Kim@Sun.COM 		}
113*10652SHyon.Kim@Sun.COM 
114*10652SHyon.Kim@Sun.COM 		open_handle = hba_ptr->open_handles;
115*10652SHyon.Kim@Sun.COM 		while (open_handle != NULL) {
116*10652SHyon.Kim@Sun.COM 		    last_open_handle = open_handle;
117*10652SHyon.Kim@Sun.COM 		    open_handle = open_handle->next;
118*10652SHyon.Kim@Sun.COM 		    free(last_open_handle);
119*10652SHyon.Kim@Sun.COM 		}
120*10652SHyon.Kim@Sun.COM 		/* Free up the top level HBA structure from the last spin */
121*10652SHyon.Kim@Sun.COM 		if (last_hba_ptr != NULL) {
122*10652SHyon.Kim@Sun.COM 		    free(last_hba_ptr);
123*10652SHyon.Kim@Sun.COM 		}
124*10652SHyon.Kim@Sun.COM 		last_hba_ptr = hba_ptr;
125*10652SHyon.Kim@Sun.COM 	}
126*10652SHyon.Kim@Sun.COM 	if (last_hba_ptr != NULL) {
127*10652SHyon.Kim@Sun.COM 	    free(last_hba_ptr);
128*10652SHyon.Kim@Sun.COM 	}
129*10652SHyon.Kim@Sun.COM 
130*10652SHyon.Kim@Sun.COM 	return (HBA_STATUS_OK);
131*10652SHyon.Kim@Sun.COM }
132