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 * Combine uscsi command ans send it out via ioctl
2N/A */
2N/Astatic HBA_STATUS
2N/ASendScsiInquiry(const char *devpath, HBA_UINT8 cdb1, HBA_UINT8 cdb2,
2N/A void *responseBuffer, HBA_UINT32 *responseSize, HBA_UINT8 *scsiStatus,
2N/A void *senseBuffer, HBA_UINT32 *senseSize)
2N/A{
2N/A HBA_UINT32 status;
2N/A struct uscsi_cmd ucmd_buf;
2N/A union scsi_cdb cdb;
2N/A
2N/A bzero(&cdb, sizeof (cdb));
2N/A bzero(&ucmd_buf, sizeof (ucmd_buf));
2N/A bzero(senseBuffer, *senseSize);
2N/A bzero(responseBuffer, *responseSize);
2N/A
2N/A cdb.scc_cmd = SCMD_INQUIRY;
2N/A cdb.g0_addr1 = cdb2;
2N/A cdb.g0_addr2 = cdb1;
2N/A cdb.g0_count0 = *responseSize;
2N/A
2N/A ucmd_buf.uscsi_cdb = (char *)&cdb;
2N/A ucmd_buf.uscsi_cdblen = CDB_GROUP0;
2N/A ucmd_buf.uscsi_bufaddr = (caddr_t)responseBuffer;
2N/A ucmd_buf.uscsi_buflen = *responseSize;
2N/A ucmd_buf.uscsi_rqbuf = (caddr_t)senseBuffer;
2N/A ucmd_buf.uscsi_rqlen = *senseSize;
2N/A ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT | USCSI_RQENABLE;
2N/A
2N/A status = send_uscsi_cmd(devpath, &ucmd_buf);
2N/A *scsiStatus = ucmd_buf.uscsi_status;
2N/A return (status);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Send a SCSI inquiry to a remote WWN
2N/A */
2N/AHBA_STATUS
2N/ASun_sasScsiInquiry(HBA_HANDLE handle, HBA_WWN portWWN, HBA_WWN targetPortWWN,
2N/A HBA_WWN domainPortWWN, SMHBA_SCSILUN smhbaLUN, HBA_UINT8 cdb1,
2N/A HBA_UINT8 cdb2, void *responseBuffer, HBA_UINT32 *responseSize,
2N/A HBA_UINT8 *scsiStatus, void *senseBuffer, HBA_UINT32 *senseSize)
2N/A{
2N/A const char ROUTINE[] = "Sun_sasScsiInquiry";
2N/A HBA_STATUS status;
2N/A int index = 0;
2N/A int domainPortFound = 0;
2N/A int hbaPortFound = 0;
2N/A int chkDomainPort = 0;
2N/A struct sun_sas_hba *hba_ptr = NULL;
2N/A struct sun_sas_port *hba_port_ptr, *hba_disco_port;
2N/A struct ScsiEntryList *mapping_ptr;
2N/A hrtime_t start, end;
2N/A double duration;
2N/A HBA_SCSILUN hba_lun;
2N/A
2N/A start = gethrtime();
2N/A /* Validate the arguments */
2N/A if (responseBuffer == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL response buffer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (senseBuffer == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL sense buffer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (responseSize == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL response size");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (senseSize == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL sense size");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (scsiStatus == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL scsi status");
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 if ((hba_ptr = RetrieveHandle(index)) == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle);
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 /* Check for stale data */
2N/A status = verifyAdapter(hba_ptr);
2N/A if (status != HBA_STATUS_OK) {
2N/A log(LOG_DEBUG, ROUTINE, "Verify adapter failed");
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (status);
2N/A }
2N/A
2N/A /*
2N/A * We are not checking to see if our data is stale.
2N/A * By verifying this information here, we will take a big performance
2N/A * hit. This check will be done later only if the Inquiry ioctl fails
2N/A */
2N/A if (hba_ptr->device_path == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "HBA handle had NULL device path. \
2N/A Unable to send SCSI cmd");
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A if (wwnConversion(domainPortWWN.wwn))
2N/A chkDomainPort = 1;
2N/A
2N/A /* Determine which port to use */
2N/A for (hba_port_ptr = hba_ptr->first_port;
2N/A hba_port_ptr != NULL;
2N/A hba_port_ptr = hba_port_ptr->next) {
2N/A
2N/A if (hbaPortFound == 0) {
2N/A if (wwnConversion(hba_port_ptr->port_attributes.
2N/A PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
2N/A != wwnConversion(portWWN.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 if (chkDomainPort) {
2N/A if (hba_port_ptr->first_phy != NULL &&
2N/A wwnConversion(hba_port_ptr->first_phy->
2N/A phy.domainPortWWN.wwn) ==
2N/A wwnConversion(domainPortWWN.wwn)) {
2N/A domainPortFound = 1;
2N/A }
2N/A if (!(domainPortFound)) {
2N/A continue;
2N/A }
2N/A }
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
2N/A /*
2N/A * If discoveredPort is not given targetPort, just skip
2N/A */
2N/A if (wwnConversion(hba_disco_port->port_attributes.\
2N/A PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
2N/A != wwnConversion(targetPortWWN.wwn)) {
2N/A /* Does not match */
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * If discoveredPort is not a SAS/SATA port, it is not a
2N/A * target port
2N/A */
2N/A if ((hba_disco_port->port_attributes.PortType !=
2N/A HBA_PORTTYPE_SATADEVICE) &&
2N/A (hba_disco_port->port_attributes.PortType !=
2N/A HBA_PORTTYPE_SASDEVICE)) {
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A log(LOG_DEBUG, ROUTINE, "Target Port WWN "
2N/A "%016llx on handle %08lx is not a Target",
2N/A wwnConversion(targetPortWWN.wwn), handle);
2N/A return (HBA_STATUS_ERROR_NOT_A_TARGET);
2N/A }
2N/A
2N/A /*
2N/A * Iterating and matching is needed.
2N/A */
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 if (memcmp(
2N/A &mapping_ptr->entry.PortLun.TargetLun,
2N/A &smhbaLUN, sizeof (HBA_SCSILUN))
2N/A != 0) {
2N/A continue;
2N/A }
2N/A
2N/A status = SendScsiInquiry(
2N/A mapping_ptr->entry.ScsiId.OSDeviceName,
2N/A cdb1, cdb2,
2N/A responseBuffer, responseSize,
2N/A scsiStatus, senseBuffer,
2N/A senseSize);
2N/A
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A end = gethrtime();
2N/A duration = end - start;
2N/A duration /= HR_SECOND;
2N/A log(LOG_DEBUG, ROUTINE, "Took total\
2N/A of %.4f seconds", duration);
2N/A return (status);
2N/A }
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A (void *) memcpy(&hba_lun, &smhbaLUN,
2N/A sizeof (HBA_SCSILUN));
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate lun"
2N/A " %08lx for target %016llx on handle %08lx",
2N/A hba_lun, wwnConversion(targetPortWWN.wwn), handle);
2N/A return (HBA_STATUS_ERROR_INVALID_LUN);
2N/A }
2N/A if (chkDomainPort) {
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
2N/A "Port WWN %016llx on handle %08lx",
2N/A wwnConversion(targetPortWWN.wwn), handle);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A }
2N/A }
2N/A
2N/A unlock(&open_handles_lock);
2N/A unlock(&all_hbas_lock);
2N/A if (hbaPortFound == 0) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Unable to locate requested Port WWN %016llx on "
2N/A "handle %08lx", wwnConversion(portWWN.wwn), handle);
2N/A } else if (chkDomainPort && !domainPortFound) {
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate requested"
2N/A " domainPortWWN %016llx on handle %08lx",
2N/A wwnConversion(domainPortWWN.wwn), handle);
2N/A } else {
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
2N/A "Port WWN %016llx on handle %08lx",
2N/A wwnConversion(targetPortWWN.wwn), handle);
2N/A }
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A}