xref: /illumos-gate/usr/src/lib/sun_sas/common/Sun_sasFreeLibrary.c (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
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  * Copyright 2019 Joyent, Inc.
28  */
29 
30 #include <sun_sas.h>
31 
32 /*
33  * Frees the HBA Library.  Must be called after all HBA library functions
34  * to free all resources
35  */
36 HBA_STATUS
37 Sun_sasFreeLibrary(void)
38 {
39 	HBA_STATUS status;
40 
41 	lock(&all_hbas_lock);
42 
43 	status = FreeHBA(global_hba_head);
44 
45 	/* re-initialize all global variables */
46 	global_hba_head = NULL;
47 	hba_count = 0;
48 	open_handle_index = 1;
49 	unlock(&all_hbas_lock);
50 	(void) mutex_destroy(&all_hbas_lock);
51 
52 	/* free sysevent handle. */
53 	if (gSysEventHandle != NULL)
54 		sysevent_unbind_handle(gSysEventHandle);
55 
56 	/* Reset our load count so we can be reloaded now */
57 	loadCount = 0;
58 
59 	return (status);
60 }
61 
62 /*
63  * Internal routine to free up hba_ptr's (and all sub-structures)
64  */
65 HBA_STATUS
66 FreeHBA(struct sun_sas_hba *hba)
67 {
68 	struct sun_sas_hba	*hba_ptr = NULL;
69 	struct sun_sas_hba	*last_hba_ptr = NULL;
70 	struct sun_sas_port	*hba_port = NULL;
71 	struct sun_sas_port	*last_hba_port = NULL;
72 	struct sun_sas_port	*tgt_port = NULL;
73 	struct sun_sas_port	*last_tgt_port = NULL;
74 	struct ScsiEntryList	*scsi_info = NULL;
75 	struct ScsiEntryList	*last_scsi_info = NULL;
76 	struct phy_info		*phy_ptr = NULL;
77 	struct phy_info		*last_phy = NULL;
78 	struct open_handle	*open_handle = NULL;
79 	struct open_handle	*last_open_handle = NULL;
80 
81 	last_hba_ptr = NULL;
82 	/* walk through global_hba_head list freeing each handle */
83 	for (hba_ptr = hba; hba_ptr != NULL; hba_ptr = hba_ptr->next) {
84 		/* Free the nested structures (port and attached port) */
85 		hba_port = hba_ptr->first_port;
86 		while (hba_port != NULL) {
87 			/* Free discovered port structure list. */
88 			tgt_port = hba_port->first_attached_port;
89 			while (tgt_port != NULL) {
90 				/* Free target mapping data list first. */
91 				scsi_info = tgt_port->scsiInfo;
92 				while (scsi_info != NULL) {
93 					last_scsi_info = scsi_info;
94 					scsi_info = scsi_info->next;
95 					free(last_scsi_info);
96 				}
97 				last_tgt_port = tgt_port;
98 				tgt_port = tgt_port->next;
99 				free(last_tgt_port->port_attributes.\
100 				    PortSpecificAttribute.SASPort);
101 				free(last_tgt_port);
102 			}
103 
104 			phy_ptr = hba_port->first_phy;
105 			while (phy_ptr != NULL) {
106 				last_phy = phy_ptr;
107 				phy_ptr = phy_ptr->next;
108 				free(last_phy);
109 			}
110 
111 			last_hba_port = hba_port;
112 			hba_port = hba_port->next;
113 			free(last_hba_port->port_attributes.\
114 			    PortSpecificAttribute.SASPort);
115 			free(last_hba_port);
116 		}
117 
118 		open_handle = hba_ptr->open_handles;
119 		while (open_handle != NULL) {
120 			last_open_handle = open_handle;
121 			open_handle = open_handle->next;
122 			free(last_open_handle);
123 		}
124 		/* Free up the top level HBA structure from the last spin */
125 		if (last_hba_ptr != NULL) {
126 			free(last_hba_ptr);
127 		}
128 		last_hba_ptr = hba_ptr;
129 	}
130 	if (last_hba_ptr != NULL) {
131 		free(last_hba_ptr);
132 	}
133 
134 	return (HBA_STATUS_OK);
135 }
136