Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / sun_sas / common / Sun_sasFreeLibrary.c
blobcaffe52de105f0e1f4340da9df96a0d8775077b6
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.
28 #include <sun_sas.h>
31 * Frees the HBA Library. Must be called after all HBA library functions
32 * to free all resources
34 HBA_STATUS Sun_sasFreeLibrary() {
35 HBA_STATUS status;
37 lock(&all_hbas_lock);
39 status = FreeHBA(global_hba_head);
41 /* re-initialize all global variables */
42 global_hba_head = NULL;
43 hba_count = 0;
44 open_handle_index = 1;
45 unlock(&all_hbas_lock);
46 (void) mutex_destroy(&all_hbas_lock);
48 /* free sysevent handle. */
49 if (gSysEventHandle != NULL)
50 sysevent_unbind_handle(gSysEventHandle);
52 /* Reset our load count so we can be reloaded now */
53 loadCount = 0;
55 return (status);
59 * Internal routine to free up hba_ptr's (and all sub-structures)
61 HBA_STATUS FreeHBA(struct sun_sas_hba *hba) {
62 struct sun_sas_hba *hba_ptr = NULL;
63 struct sun_sas_hba *last_hba_ptr = NULL;
64 struct sun_sas_port *hba_port = NULL;
65 struct sun_sas_port *last_hba_port = NULL;
66 struct sun_sas_port *tgt_port = NULL;
67 struct sun_sas_port *last_tgt_port = NULL;
68 struct ScsiEntryList *scsi_info = NULL;
69 struct ScsiEntryList *last_scsi_info = NULL;
70 struct phy_info *phy_ptr = NULL;
71 struct phy_info *last_phy = NULL;
72 struct open_handle *open_handle = NULL;
73 struct open_handle *last_open_handle = NULL;
75 last_hba_ptr = NULL;
76 /* walk through global_hba_head list freeing each handle */
77 for (hba_ptr = hba;
78 hba_ptr != NULL;
79 hba_ptr = hba_ptr->next) {
80 /* Free the nested structures (port and attached port) */
81 hba_port = hba_ptr->first_port;
82 while (hba_port != NULL) {
83 /* Free discovered port structure list. */
84 tgt_port = hba_port->first_attached_port;
85 while (tgt_port != NULL) {
86 /* Free target mapping data list first. */
87 scsi_info = tgt_port->scsiInfo;
88 while (scsi_info != NULL) {
89 last_scsi_info = scsi_info;
90 scsi_info = scsi_info->next;
91 free(last_scsi_info);
93 last_tgt_port = tgt_port;
94 tgt_port = tgt_port->next;
95 free(last_tgt_port->port_attributes.\
96 PortSpecificAttribute.SASPort);
97 free(last_tgt_port);
100 phy_ptr = hba_port->first_phy;
101 while (phy_ptr != NULL) {
102 last_phy = phy_ptr;
103 phy_ptr = phy_ptr->next;
104 free(last_phy);
107 last_hba_port = hba_port;
108 hba_port = hba_port->next;
109 free(last_hba_port->port_attributes.\
110 PortSpecificAttribute.SASPort);
111 free(last_hba_port);
114 open_handle = hba_ptr->open_handles;
115 while (open_handle != NULL) {
116 last_open_handle = open_handle;
117 open_handle = open_handle->next;
118 free(last_open_handle);
120 /* Free up the top level HBA structure from the last spin */
121 if (last_hba_ptr != NULL) {
122 free(last_hba_ptr);
124 last_hba_ptr = hba_ptr;
126 if (last_hba_ptr != NULL) {
127 free(last_hba_ptr);
130 return (HBA_STATUS_OK);