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 (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sun_sas.h>
2N/A
2N/A/*
2N/A * Retrieves the mapping between targets and OS SCSI information
2N/A */
2N/AHBA_STATUS
2N/ASun_sasGetTargetMapping(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
2N/A HBA_WWN domainPortWWN, SMHBA_TARGETMAPPING *mapping)
2N/A{
2N/A const char ROUTINE[] = "Sun_sasGetTargetMapping";
2N/A int i, index;
2N/A int hbaPortFound = 0;
2N/A int domainPortFound = 0;
2N/A uint_t total_entries = 0;
2N/A struct sun_sas_hba *hba_ptr;
2N/A struct sun_sas_port *hba_port_ptr, *hba_disco_port;
2N/A struct ScsiEntryList *mapping_ptr;
2N/A
2N/A if (mapping == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL mapping buffer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A
2N/A lock(&all_hbas_lock);
2N/A index = RetrieveIndex(handle);
2N/A lock(&open_handles_lock);
2N/A hba_ptr = RetrieveHandle(index);
2N/A if (hba_ptr == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx.", handle);
2N/A /* on error, need to set NumberOfEntries to 0 */
2N/A mapping->NumberOfEntries = 0;
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_INVALID_HANDLE);
2N/A }
2N/A
2N/A /*
2N/A * We should indicate an error if no domainPortWWN passed in.
2N/A */
2N/A if (wwnConversion(domainPortWWN.wwn) == 0) {
2N/A log(LOG_DEBUG, ROUTINE, "domainPortWWN must be provided");
2N/A mapping->NumberOfEntries = 0;
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A /*
2N/A * walk through the list of ports for this hba and count up the number
2N/A * of discovered ports on each hba port
2N/A */
2N/A i = 0;
2N/A for (hba_port_ptr = hba_ptr->first_port; hba_port_ptr != NULL;
2N/A hba_port_ptr = hba_port_ptr->next) {
2N/A if (hbaPortFound == 0) {
2N/A if (wwnConversion(hba_port_ptr->port_attributes.
2N/A PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
2N/A != wwnConversion(hbaPortWWN.wwn)) {
2N/A /*
2N/A * HBA ports under the same HBA may have
2N/A * different LocalSASAddress. We should loop
2N/A * through the HBA port list to find the
2N/A * matching HBA port.
2N/A */
2N/A continue;
2N/A } else {
2N/A hbaPortFound = 1;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Check whether the domainPortWWN matches.
2N/A */
2N/A if ((validateDomainAddress(hba_port_ptr, domainPortWWN))
2N/A != HBA_STATUS_OK) {
2N/A continue;
2N/A }
2N/A domainPortFound = 1;
2N/A
2N/A for (hba_disco_port = hba_port_ptr->first_attached_port;
2N/A hba_disco_port != NULL;
2N/A hba_disco_port = hba_disco_port->next) {
2N/A for (mapping_ptr = hba_disco_port->scsiInfo;
2N/A mapping_ptr != NULL;
2N/A mapping_ptr = mapping_ptr->next) {
2N/A /*
2N/A * Add the information as much as mapping
2N/A * can hold.
2N/A */
2N/A if (wwnConversion(domainPortWWN.wwn) !=
2N/A wwnConversion(mapping_ptr->entry.
2N/A PortLun.domainPortWWN.wwn)) {
2N/A continue;
2N/A }
2N/A
2N/A if (total_entries < mapping->NumberOfEntries) {
2N/A (void) memcpy(&mapping->entry[i].ScsiId,
2N/A &mapping_ptr->entry.ScsiId,
2N/A sizeof (SMHBA_SCSIID));
2N/A (void) memcpy(&mapping->entry[i].
2N/A PortLun, &mapping_ptr->entry.
2N/A PortLun, sizeof (SMHBA_PORTLUN));
2N/A (void) memcpy(&mapping->entry[i].LUID,
2N/A &mapping_ptr->entry.LUID,
2N/A sizeof (SMHBA_LUID));
2N/A i++;
2N/A }
2N/A total_entries++;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * check to make sure user has passed in an acceptable PortWWN for
2N/A * the given handle
2N/A */
2N/A if (hbaPortFound == 0) {
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
2N/A "HBA Port WWN %016llx on handle %08lx",
2N/A wwnConversion(hbaPortWWN.wwn), handle);
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A }
2N/A
2N/A if (domainPortFound == 0) {
2N/A log(LOG_DEBUG, ROUTINE, "No matching domain "
2N/A "port %016llx for port %016llx on handle "
2N/A "%08lx", wwnConversion(domainPortWWN.wwn),
2N/A wwnConversion(hbaPortWWN.wwn), handle);
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A }
2N/A
2N/A if (total_entries > mapping->NumberOfEntries) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "total entries: %d: mapping->NumberofEntries: %d.",
2N/A total_entries, mapping->NumberOfEntries);
2N/A mapping->NumberOfEntries = total_entries;
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_MORE_DATA);
2N/A }
2N/A
2N/A mapping->NumberOfEntries = total_entries;
2N/A
2N/A /* convert devpath to dev link */
2N/A convertDevpathToDevlink(mapping);
2N/A
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A
2N/A return (HBA_STATUS_OK);
2N/A}