2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A#include <sun_sas.h>
2N/A
2N/A/*
2N/A * Frees the HBA Library. Must be called after all HBA library functions
2N/A * to free all resources
2N/A */
2N/AHBA_STATUS Sun_sasFreeLibrary() {
2N/A HBA_STATUS status;
2N/A
2N/A lock(&all_hbas_lock);
2N/A
2N/A status = FreeHBA(global_hba_head);
2N/A
2N/A /* re-initialize all global variables */
2N/A global_hba_head = NULL;
2N/A hba_count = 0;
2N/A open_handle_index = 1;
2N/A unlock(&all_hbas_lock);
2N/A (void) mutex_destroy(&all_hbas_lock);
2N/A
2N/A /* free sysevent handle. */
2N/A if (gSysEventHandle != NULL)
2N/A sysevent_unbind_handle(gSysEventHandle);
2N/A
2N/A /* Reset our load count so we can be reloaded now */
2N/A loadCount = 0;
2N/A
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Internal routine to free up hba_ptr's (and all sub-structures)
2N/A */
2N/AHBA_STATUS FreeHBA(struct sun_sas_hba *hba) {
2N/A struct sun_sas_hba *hba_ptr = NULL;
2N/A struct sun_sas_hba *last_hba_ptr = NULL;
2N/A struct sun_sas_port *hba_port = NULL;
2N/A struct sun_sas_port *last_hba_port = NULL;
2N/A struct sun_sas_port *tgt_port = NULL;
2N/A struct sun_sas_port *last_tgt_port = NULL;
2N/A struct ScsiEntryList *scsi_info = NULL;
2N/A struct ScsiEntryList *last_scsi_info = NULL;
2N/A struct phy_info *phy_ptr = NULL;
2N/A struct phy_info *last_phy = NULL;
2N/A struct open_handle *open_handle = NULL;
2N/A struct open_handle *last_open_handle = NULL;
2N/A
2N/A last_hba_ptr = NULL;
2N/A /* walk through global_hba_head list freeing each handle */
2N/A for (hba_ptr = hba;
2N/A hba_ptr != NULL;
2N/A hba_ptr = hba_ptr->next) {
2N/A /* Free the nested structures (port and attached port) */
2N/A hba_port = hba_ptr->first_port;
2N/A while (hba_port != NULL) {
2N/A /* Free discovered port structure list. */
2N/A tgt_port = hba_port->first_attached_port;
2N/A while (tgt_port != NULL) {
2N/A /* Free target mapping data list first. */
2N/A scsi_info = tgt_port->scsiInfo;
2N/A while (scsi_info != NULL) {
2N/A last_scsi_info = scsi_info;
2N/A scsi_info = scsi_info->next;
2N/A free(last_scsi_info);
2N/A }
2N/A last_tgt_port = tgt_port;
2N/A tgt_port = tgt_port->next;
2N/A free(last_tgt_port->port_attributes.\
2N/A PortSpecificAttribute.SASPort);
2N/A free(last_tgt_port);
2N/A }
2N/A
2N/A phy_ptr = hba_port->first_phy;
2N/A while (phy_ptr != NULL) {
2N/A last_phy = phy_ptr;
2N/A phy_ptr = phy_ptr->next;
2N/A free(last_phy);
2N/A }
2N/A
2N/A last_hba_port = hba_port;
2N/A hba_port = hba_port->next;
2N/A free(last_hba_port->port_attributes.\
2N/A PortSpecificAttribute.SASPort);
2N/A free(last_hba_port);
2N/A }
2N/A
2N/A open_handle = hba_ptr->open_handles;
2N/A while (open_handle != NULL) {
2N/A last_open_handle = open_handle;
2N/A open_handle = open_handle->next;
2N/A free(last_open_handle);
2N/A }
2N/A /* Free up the top level HBA structure from the last spin */
2N/A if (last_hba_ptr != NULL) {
2N/A free(last_hba_ptr);
2N/A }
2N/A last_hba_ptr = hba_ptr;
2N/A }
2N/A if (last_hba_ptr != NULL) {
2N/A free(last_hba_ptr);
2N/A }
2N/A
2N/A return (HBA_STATUS_OK);
2N/A}