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