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 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <kstat.h>
2N/A#include <sun_sas.h>
2N/A
2N/A/*
2N/A * Retrieves the statistics for a specified port.phy on an adapter
2N/A */
2N/AHBA_STATUS Sun_sasGetPhyStatistics(HBA_HANDLE handle,
2N/A HBA_UINT32 port, HBA_UINT32 phy, SMHBA_PHYSTATISTICS *pStatistics) {
2N/A const char ROUTINE[] = "Sun_sasGetPhyStatistics";
2N/A HBA_STATUS status = HBA_STATUS_OK;
2N/A struct sun_sas_hba *hba_ptr;
2N/A struct sun_sas_port *hba_port_ptr;
2N/A struct phy_info *phy_ptr;
2N/A PSMHBA_SASPHYSTATISTICS psas;
2N/A kstat_ctl_t *kc;
2N/A kstat_t *ksp;
2N/A kstat_named_t *kname;
2N/A char *charptr, path[MAXPATHLEN + 1];
2N/A char *driver_name, kstat_name[256];
2N/A di_node_t node;
2N/A int instance = 0;
2N/A int i;
2N/A uint64_t iport_wwn;
2N/A
2N/A /* Validate the arguments */
2N/A if (pStatistics == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "NULL Phy Statistics buffer of phyIndex: %08lx", phy);
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A psas = pStatistics->SASPhyStatistics;
2N/A if (psas == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "NULL SAS Phy Statistics buffer of phyIndex: %08lx", phy);
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A
2N/A lock(&all_hbas_lock);
2N/A
2N/A if ((hba_ptr = Retrieve_Sun_sasHandle(handle)) == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Invalid HBA handler %08lx of phyIndex: %08lx",
2N/A handle, phy);
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,
2N/A "Verify Adapter failed for phyIndex: %08lx", phy);
2N/A unlock(&all_hbas_lock);
2N/A return (status);
2N/A }
2N/A
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 if (hba_port_ptr->index == port) {
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (hba_port_ptr == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Invalid port index of phyIndex: %08lx", phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
2N/A }
2N/A
2N/A if (phy >= hba_port_ptr->port_attributes.PortSpecificAttribute.
2N/A SASPort->NumberofPhys) {
2N/A log(LOG_DEBUG, ROUTINE, "Invalid phy index %08lx", phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
2N/A }
2N/A
2N/A /* We need to find out the phy identifier. */
2N/A for (phy_ptr = hba_port_ptr->first_phy;
2N/A phy_ptr != NULL;
2N/A phy_ptr = phy_ptr->next) {
2N/A if (phy == phy_ptr->index)
2N/A break;
2N/A }
2N/A
2N/A if (phy_ptr == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "Invalid phy index %08lx", phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
2N/A }
2N/A
2N/A /*
2N/A * for statistics that are not supported, its bits should all be
2N/A * set to -1
2N/A */
2N/A (void) memset(pStatistics->SASPhyStatistics, 0xff,
2N/A sizeof (SMHBA_SASPHYSTATISTICS));
2N/A
2N/A
2N/A /* First, we need the deivce path to locate the devinfo node. */
2N/A (void *) strlcpy(path, hba_port_ptr->device_path,
2N/A sizeof (path));
2N/A charptr = strrchr(path, ':');
2N/A if (charptr) {
2N/A *charptr = '\0';
2N/A }
2N/A
2N/A errno = 0;
2N/A
2N/A (void *) memset(kstat_name, 0, sizeof (kstat_name));
2N/A node = di_init(path, DINFOCPYONE);
2N/A if (node == DI_NODE_NIL) {
2N/A di_fini(node);
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Unable to take devinfo snapshot on HBA \"%s\" "
2N/A "for phyIndex: %08lx due to %s",
2N/A path, phy, strerror(errno));
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * Then we could fetch the instance number and driver name of this
2N/A * device.
2N/A */
2N/A instance = di_instance(node);
2N/A if (instance == -1) {
2N/A di_fini(node);
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "An instance number has not been assigned to the "
2N/A "device \"%s\" when get phyIndex: %08lx", path, phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A driver_name = di_driver_name(node);
2N/A if (driver_name == NULL) {
2N/A di_fini(node);
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "No driver bound to this device \"%s\" "
2N/A "when get phyIndex: %08lx",
2N/A path, phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A di_fini(node);
2N/A
2N/A iport_wwn = wwnConversion(hba_port_ptr->port_attributes.\
2N/A PortSpecificAttribute.SASPort->LocalSASAddress.wwn);
2N/A
2N/A /*
2N/A * Construct the kstat name here.
2N/A */
2N/A (void) snprintf(kstat_name, sizeof (kstat_name), "%s.%016llx.%u.%u",
2N/A driver_name, iport_wwn, instance, phy_ptr->phy.PhyIdentifier);
2N/A
2N/A /* retrieve all the statistics from kstat. */
2N/A kc = kstat_open();
2N/A if (kc == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "kstat_open failed due to \"%s\" of phyIndex: %08lx",
2N/A strerror(errno), phy);
2N/A unlock(&all_hbas_lock);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A ksp = kstat_lookup(kc, NULL, -1, kstat_name);
2N/A if (ksp == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "No matching kstat name found for \"%s\" "
2N/A "of phyIndex: %08lx",
2N/A kstat_name, phy);
2N/A unlock(&all_hbas_lock);
2N/A (void) kstat_close(kc);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A /* Found the phy we're looking for. */
2N/A if (kstat_read(kc, ksp, NULL) == -1) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "error reading kstat data due to \"%s\" "
2N/A "of phyIndex: %08lx",
2N/A strerror(errno), phy);
2N/A unlock(&all_hbas_lock);
2N/A (void) kstat_close(kc);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A kname = (kstat_named_t *)ksp->ks_data;
2N/A for (i = 0; i < ksp->ks_ndata; i++, kname++) {
2N/A if (strcmp(kname->name,
2N/A "SecondsSinceLastReset") == 0) {
2N/A psas->SecondsSinceLastReset = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "TxFrames") == 0) {
2N/A psas->TxFrames = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "RxFrames") == 0) {
2N/A psas->RxFrames = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "TxWords") == 0) {
2N/A psas->TxWords = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "RxWords") == 0) {
2N/A psas->RxWords = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "InvalidDwordCount") == 0) {
2N/A psas->InvalidDwordCount = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "RunningDisparityErrorCount") == 0) {
2N/A psas->RunningDisparityErrorCount = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "LossofDwordSyncCount") == 0) {
2N/A psas->LossofDwordSyncCount = kname->value.ull;
2N/A continue;
2N/A }
2N/A if (strcmp(kname->name, "PhyResetProblemCount") == 0) {
2N/A psas->PhyResetProblemCount = kname->value.ull;
2N/A }
2N/A }
2N/A unlock(&all_hbas_lock);
2N/A (void) kstat_close(kc);
2N/A
2N/A return (HBA_STATUS_OK);
2N/A}