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#include <sys/scsi/impl/usmp.h>
2N/A
2N/A/*
2N/A * Pass usmp_cmd into ioctl
2N/A */
2N/Astatic HBA_STATUS
2N/ASendSMPPassThru(const char *devpath, void *reqframe, HBA_UINT32 *reqsize,
2N/A void *rspframe, HBA_UINT32 *rspsize) {
2N/A const char ROUTINE[] = "SendSMPPassThru";
2N/A int fd;
2N/A usmp_cmd_t ucmd_buf;
2N/A HBA_STATUS ret;
2N/A
2N/A bzero(&ucmd_buf, sizeof (ucmd_buf));
2N/A
2N/A ucmd_buf.usmp_req = (caddr_t)reqframe;
2N/A ucmd_buf.usmp_rsp = (caddr_t)rspframe;
2N/A ucmd_buf.usmp_reqsize = (size_t)(*reqsize);
2N/A ucmd_buf.usmp_rspsize = (size_t)(*rspsize);
2N/A ucmd_buf.usmp_timeout = SMP_DEFAULT_TIMEOUT;
2N/A
2N/A /*
2N/A * open smp device
2N/A */
2N/A
2N/A if ((fd = open(devpath, O_RDONLY | O_NONBLOCK)) == -1) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "open devpath %s failed due to %s",
2N/A devpath, strerror(errno));
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * send usmp command
2N/A */
2N/A if (ioctl(fd, USMPFUNC, &ucmd_buf) == -1) {
2N/A if ((errno == ETIME) || (errno == ETIMEDOUT) ||
2N/A (errno == EAGAIN)) {
2N/A ret = HBA_STATUS_ERROR_TRY_AGAIN;
2N/A } else if (errno == EBUSY) {
2N/A ret = HBA_STATUS_ERROR_BUSY;
2N/A } else {
2N/A ret = HBA_STATUS_ERROR;
2N/A }
2N/A log(LOG_DEBUG, ROUTINE, "ioctl:USMPFUNC failed due to %s",
2N/A strerror(errno));
2N/A (void) close(fd);
2N/A return (ret);
2N/A }
2N/A
2N/A (void) close(fd);
2N/A return (HBA_STATUS_OK);
2N/A}
2N/A
2N/A/*
2N/A * Send a USMP command to a remote SMP node
2N/A */
2N/AHBA_STATUS
2N/ASun_sasSendSMPPassThru(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
2N/A HBA_WWN destPortWWN, HBA_WWN domainPortWWN, void *pReqBuffer,
2N/A HBA_UINT32 ReqBufferSize, void *pRspBuffer, HBA_UINT32 *pRspBufferSize)
2N/A{
2N/A const char ROUTINE[] = "Sun_sasSendSMPPassThru";
2N/A HBA_STATUS status;
2N/A struct sun_sas_hba *hba_ptr;
2N/A int domainPortFound = 0;
2N/A int chkDomainPort = 0;
2N/A int hbaPortFound = 0;
2N/A struct sun_sas_port *hba_port_ptr, *hba_disco_port;
2N/A hrtime_t start, end;
2N/A double duration;
2N/A
2N/A start = gethrtime();
2N/A /* Validate the arguments */
2N/A if (pRspBuffer == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL response buffer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (pReqBuffer == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL sense buffer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (pRspBufferSize == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "NULL response size");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A
2N/A lock(&all_hbas_lock);
2N/A if ((hba_ptr = Retrieve_Sun_sasHandle(handle)) == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle);
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(&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
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(&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(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 if (chkDomainPort != 0) {
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(destPortWWN.wwn)) {
2N/A /* Does not match */
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * If matching targetPort does not support SMP protocal
2N/A * return error.
2N/A * comment it out for testing only
2N/A */
2N/A if ((hba_disco_port->port_attributes.\
2N/A PortSpecificAttribute.SASPort->PortProtocol &
2N/A HBA_SASPORTPROTOCOL_SMP) == 0) {
2N/A log(LOG_DEBUG, ROUTINE, "Input WWN %01611x\
2N/A does not support SMP protocol",
2N/A wwnConversion(hbaPortWWN.wwn));
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_INVALID_PROTOCOL_TYPE);
2N/A }
2N/A
2N/A /*
2N/A * SMP target port doesn't have any scsi info.
2N/A * - like /dev/rdsk/cxtxdxsx
2N/A * So we use OSDeviceName from port attributes.
2N/A * - like /dev/smp/expd[0-9]
2N/A */
2N/A status = SendSMPPassThru(
2N/A hba_disco_port->port_attributes.OSDeviceName,
2N/A pReqBuffer, &ReqBufferSize,
2N/A pRspBuffer, pRspBufferSize);
2N/A
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 if (chkDomainPort) {
2N/A unlock(&all_hbas_lock);
2N/A log(LOG_DEBUG, ROUTINE, "Unable to locate"
2N/A "requested SMP target port %16llx",
2N/A wwnConversion(destPortWWN.wwn));
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A }
2N/A }
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(hbaPortWWN.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"
2N/A "requested SMP target port %16llx",
2N/A wwnConversion(destPortWWN.wwn));
2N/A }
2N/A return (HBA_STATUS_ERROR_ILLEGAL_WWN);
2N/A}