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 (void) mutex_destroy(&open_handles_lock); 52 53 /* free sysevent handle. */ 54 if (gSysEventHandle != NULL) 55 sysevent_unbind_handle(gSysEventHandle); 56 57 /* Reset our load count so we can be reloaded now */ 58 loadCount = 0; 59 60 return (status); 61 } 62 63 /* 64 * Internal routine to free up hba_ptr's (and all sub-structures) 65 */ 66 HBA_STATUS 67 FreeHBA(struct sun_sas_hba *hba) 68 { 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; 81 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); 97 } 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); 103 } 104 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); 110 } 111 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); 117 } 118 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); 124 } 125 /* Free up the top level HBA structure from the last spin */ 126 if (last_hba_ptr != NULL) { 127 free(last_hba_ptr); 128 } 129 last_hba_ptr = hba_ptr; 130 } 131 if (last_hba_ptr != NULL) { 132 free(last_hba_ptr); 133 } 134 135 return (HBA_STATUS_OK); 136 } 137