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#include <sun_sas.h>
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Retrieves the attributes for a specified port discovered in the network
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon KimHBA_STATUS
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon KimSun_sasGetDiscoveredPortAttributes(HBA_HANDLE handle,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim HBA_UINT32 port, HBA_UINT32 discoveredport,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim SMHBA_PORTATTRIBUTES *attributes) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim const char ROUTINE[] =
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Sun_sasGetDiscoveredPortAttributes";
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim HBA_STATUS status;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim HBA_STATUS ret = HBA_STATUS_OK;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim struct sun_sas_hba *hba_ptr;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim struct sun_sas_port *hba_port_ptr, *hba_disco_port;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim int index;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (attributes == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "NULL attributes argument. Handle %08lx, port %d, "
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "discovered port %d", handle, port, discoveredport);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR_ARG);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim lock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim index = RetrieveIndex(handle);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim lock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_ptr = RetrieveHandle(index);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_ptr == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Invalid handle %08lx.", handle);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR_INVALID_HANDLE);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Check for stale data */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim status = verifyAdapter(hba_ptr);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (status != HBA_STATUS_OK) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE, "Verify Adapter failed");
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (status);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_ptr->first_port == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* This is probably an internal failure of the library */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_ptr->device_path) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Internal failure: Adapter %s contains no port data",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_ptr->device_path);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Internal failure: Adapter at index %d contains no port "
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "data", hba_ptr->index);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim for (hba_port_ptr = hba_ptr->first_port;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_port_ptr->index == port) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim break;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_port_ptr == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Invalid port index %d for handle %08lx",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim port, handle);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* check to make sure there are devices attached to this port */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_port_ptr->first_attached_port != NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim for (hba_disco_port = hba_port_ptr->first_attached_port;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port != NULL;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port = hba_disco_port->next) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_disco_port->index == discoveredport) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim break;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (hba_disco_port == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Invalid discovered port index %d for hba port "
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "index %d on handle %08lx.",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim discoveredport, port, handle);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim ret = HBA_STATUS_ERROR_ILLEGAL_INDEX;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim attributes->PortType =
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port->port_attributes.PortType;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim attributes->PortState =
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port->port_attributes.PortState;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim (void) strlcpy(attributes->OSDeviceName,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port->port_attributes.OSDeviceName,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim sizeof (attributes->OSDeviceName));
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim (void) memcpy(attributes->PortSpecificAttribute.SASPort,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_disco_port->port_attributes.PortSpecificAttribute.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim SASPort, sizeof (struct SMHBA_SAS_Port));
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* No ports, so we can't possibly return anything */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "No discovered port on HBA port index %d for handle %08lx",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim port, handle);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim ret = HBA_STATUS_ERROR;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&open_handles_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (ret);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim}