16664 Update AMD microcode to 20240710
[illumos-gate.git] / usr / src / lib / sun_sas / common / Sun_sasFreeLibrary.c
blob78b2100e89f94f0cb179f815e46464277fde9ffa
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2019 Joyent, Inc.
30 #include <sun_sas.h>
33 * Frees the HBA Library. Must be called after all HBA library functions
34 * to free all resources
36 HBA_STATUS
37 Sun_sasFreeLibrary(void)
39 HBA_STATUS status;
41 lock(&all_hbas_lock);
43 status = FreeHBA(global_hba_head);
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 (void) mutex_destroy(&open_handles_lock);
53 /* free sysevent handle. */
54 if (gSysEventHandle != NULL)
55 sysevent_unbind_handle(gSysEventHandle);
57 /* Reset our load count so we can be reloaded now */
58 loadCount = 0;
60 return (status);
64 * Internal routine to free up hba_ptr's (and all sub-structures)
66 HBA_STATUS
67 FreeHBA(struct sun_sas_hba *hba)
69 struct sun_sas_hba *hba_ptr = NULL;
70 struct sun_sas_hba *last_hba_ptr = NULL;
71 struct sun_sas_port *hba_port = NULL;
72 struct sun_sas_port *last_hba_port = NULL;
73 struct sun_sas_port *tgt_port = NULL;
74 struct sun_sas_port *last_tgt_port = NULL;
75 struct ScsiEntryList *scsi_info = NULL;
76 struct ScsiEntryList *last_scsi_info = NULL;
77 struct phy_info *phy_ptr = NULL;
78 struct phy_info *last_phy = NULL;
79 struct open_handle *open_handle = NULL;
80 struct open_handle *last_open_handle = NULL;
82 last_hba_ptr = NULL;
83 /* walk through global_hba_head list freeing each handle */
84 for (hba_ptr = hba; hba_ptr != NULL; hba_ptr = hba_ptr->next) {
85 /* Free the nested structures (port and attached port) */
86 hba_port = hba_ptr->first_port;
87 while (hba_port != NULL) {
88 /* Free discovered port structure list. */
89 tgt_port = hba_port->first_attached_port;
90 while (tgt_port != NULL) {
91 /* Free target mapping data list first. */
92 scsi_info = tgt_port->scsiInfo;
93 while (scsi_info != NULL) {
94 last_scsi_info = scsi_info;
95 scsi_info = scsi_info->next;
96 free(last_scsi_info);
98 last_tgt_port = tgt_port;
99 tgt_port = tgt_port->next;
100 free(last_tgt_port->port_attributes.\
101 PortSpecificAttribute.SASPort);
102 free(last_tgt_port);
105 phy_ptr = hba_port->first_phy;
106 while (phy_ptr != NULL) {
107 last_phy = phy_ptr;
108 phy_ptr = phy_ptr->next;
109 free(last_phy);
112 last_hba_port = hba_port;
113 hba_port = hba_port->next;
114 free(last_hba_port->port_attributes.\
115 PortSpecificAttribute.SASPort);
116 free(last_hba_port);
119 open_handle = hba_ptr->open_handles;
120 while (open_handle != NULL) {
121 last_open_handle = open_handle;
122 open_handle = open_handle->next;
123 free(last_open_handle);
125 /* Free up the top level HBA structure from the last spin */
126 if (last_hba_ptr != NULL) {
127 free(last_hba_ptr);
129 last_hba_ptr = hba_ptr;
131 if (last_hba_ptr != NULL) {
132 free(last_hba_ptr);
135 return (HBA_STATUS_OK);