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