xref: /onnv-gate/usr/src/lib/sun_sas/common/Sun_sasGetPhyStatistics.c (revision 10652:9d0aff74d6fd)
1*10652SHyon.Kim@Sun.COM /*
2*10652SHyon.Kim@Sun.COM  * CDDL HEADER START
3*10652SHyon.Kim@Sun.COM  *
4*10652SHyon.Kim@Sun.COM  * The contents of this file are subject to the terms of the
5*10652SHyon.Kim@Sun.COM  * Common Development and Distribution License (the "License").
6*10652SHyon.Kim@Sun.COM  * You may not use this file except in compliance with the License.
7*10652SHyon.Kim@Sun.COM  *
8*10652SHyon.Kim@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10652SHyon.Kim@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10652SHyon.Kim@Sun.COM  * See the License for the specific language governing permissions
11*10652SHyon.Kim@Sun.COM  * and limitations under the License.
12*10652SHyon.Kim@Sun.COM  *
13*10652SHyon.Kim@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10652SHyon.Kim@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10652SHyon.Kim@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10652SHyon.Kim@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10652SHyon.Kim@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10652SHyon.Kim@Sun.COM  *
19*10652SHyon.Kim@Sun.COM  * CDDL HEADER END
20*10652SHyon.Kim@Sun.COM  */
21*10652SHyon.Kim@Sun.COM 
22*10652SHyon.Kim@Sun.COM /*
23*10652SHyon.Kim@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*10652SHyon.Kim@Sun.COM  * Use is subject to license terms.
25*10652SHyon.Kim@Sun.COM  */
26*10652SHyon.Kim@Sun.COM 
27*10652SHyon.Kim@Sun.COM #include <kstat.h>
28*10652SHyon.Kim@Sun.COM #include <sun_sas.h>
29*10652SHyon.Kim@Sun.COM 
30*10652SHyon.Kim@Sun.COM /*
31*10652SHyon.Kim@Sun.COM  * Retrieves the statistics for a specified port.phy on an adapter
32*10652SHyon.Kim@Sun.COM  */
Sun_sasGetPhyStatistics(HBA_HANDLE handle,HBA_UINT32 port,HBA_UINT32 phy,SMHBA_PHYSTATISTICS * pStatistics)33*10652SHyon.Kim@Sun.COM HBA_STATUS Sun_sasGetPhyStatistics(HBA_HANDLE handle,
34*10652SHyon.Kim@Sun.COM     HBA_UINT32 port, HBA_UINT32 phy, SMHBA_PHYSTATISTICS *pStatistics) {
35*10652SHyon.Kim@Sun.COM 	const char	ROUTINE[] = "Sun_sasGetPhyStatistics";
36*10652SHyon.Kim@Sun.COM 	HBA_STATUS		status = HBA_STATUS_OK;
37*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	*hba_ptr;
38*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*hba_port_ptr;
39*10652SHyon.Kim@Sun.COM 	struct phy_info		*phy_ptr;
40*10652SHyon.Kim@Sun.COM 	PSMHBA_SASPHYSTATISTICS	psas;
41*10652SHyon.Kim@Sun.COM 	kstat_ctl_t		*kc;
42*10652SHyon.Kim@Sun.COM 	kstat_t			*ksp;
43*10652SHyon.Kim@Sun.COM 	kstat_named_t		*kname;
44*10652SHyon.Kim@Sun.COM 	char			*charptr, path[MAXPATHLEN + 1];
45*10652SHyon.Kim@Sun.COM 	char			*driver_name, kstat_name[256];
46*10652SHyon.Kim@Sun.COM 	di_node_t		node;
47*10652SHyon.Kim@Sun.COM 	int			instance = 0;
48*10652SHyon.Kim@Sun.COM 	int			i;
49*10652SHyon.Kim@Sun.COM 	uint64_t		iport_wwn;
50*10652SHyon.Kim@Sun.COM 
51*10652SHyon.Kim@Sun.COM 	/* Validate the arguments */
52*10652SHyon.Kim@Sun.COM 	if (pStatistics == NULL) {
53*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
54*10652SHyon.Kim@Sun.COM 		    "NULL Phy Statistics buffer of phyIndex: %08lx", phy);
55*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ARG);
56*10652SHyon.Kim@Sun.COM 	}
57*10652SHyon.Kim@Sun.COM 	psas = pStatistics->SASPhyStatistics;
58*10652SHyon.Kim@Sun.COM 	if (psas == NULL) {
59*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
60*10652SHyon.Kim@Sun.COM 		    "NULL SAS Phy Statistics buffer of phyIndex: %08lx", phy);
61*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ARG);
62*10652SHyon.Kim@Sun.COM 	}
63*10652SHyon.Kim@Sun.COM 
64*10652SHyon.Kim@Sun.COM 	lock(&all_hbas_lock);
65*10652SHyon.Kim@Sun.COM 
66*10652SHyon.Kim@Sun.COM 	if ((hba_ptr = Retrieve_Sun_sasHandle(handle)) == NULL) {
67*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
68*10652SHyon.Kim@Sun.COM 		    "Invalid HBA handler %08lx of phyIndex: %08lx",
69*10652SHyon.Kim@Sun.COM 		    handle, phy);
70*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
71*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_INVALID_HANDLE);
72*10652SHyon.Kim@Sun.COM 	}
73*10652SHyon.Kim@Sun.COM 
74*10652SHyon.Kim@Sun.COM 	/* Check for stale data */
75*10652SHyon.Kim@Sun.COM 	status = verifyAdapter(hba_ptr);
76*10652SHyon.Kim@Sun.COM 	if (status != HBA_STATUS_OK) {
77*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
78*10652SHyon.Kim@Sun.COM 		    "Verify Adapter failed for phyIndex: %08lx", phy);
79*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
80*10652SHyon.Kim@Sun.COM 		return (status);
81*10652SHyon.Kim@Sun.COM 	}
82*10652SHyon.Kim@Sun.COM 
83*10652SHyon.Kim@Sun.COM 	for (hba_port_ptr = hba_ptr->first_port;
84*10652SHyon.Kim@Sun.COM 	    hba_port_ptr != NULL;
85*10652SHyon.Kim@Sun.COM 	    hba_port_ptr = hba_port_ptr->next) {
86*10652SHyon.Kim@Sun.COM 		if (hba_port_ptr->index == port) {
87*10652SHyon.Kim@Sun.COM 			break;
88*10652SHyon.Kim@Sun.COM 		}
89*10652SHyon.Kim@Sun.COM 	}
90*10652SHyon.Kim@Sun.COM 
91*10652SHyon.Kim@Sun.COM 	if (hba_port_ptr == NULL) {
92*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
93*10652SHyon.Kim@Sun.COM 		    "Invalid port index of phyIndex: %08lx", phy);
94*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
95*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
96*10652SHyon.Kim@Sun.COM 	}
97*10652SHyon.Kim@Sun.COM 
98*10652SHyon.Kim@Sun.COM 	if (phy >= hba_port_ptr->port_attributes.PortSpecificAttribute.
99*10652SHyon.Kim@Sun.COM 	    SASPort->NumberofPhys) {
100*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "Invalid phy index %08lx", phy);
101*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
102*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
103*10652SHyon.Kim@Sun.COM 	}
104*10652SHyon.Kim@Sun.COM 
105*10652SHyon.Kim@Sun.COM 	/* We need to find out the phy identifier. */
106*10652SHyon.Kim@Sun.COM 	for (phy_ptr = hba_port_ptr->first_phy;
107*10652SHyon.Kim@Sun.COM 	    phy_ptr != NULL;
108*10652SHyon.Kim@Sun.COM 	    phy_ptr = phy_ptr->next) {
109*10652SHyon.Kim@Sun.COM 		if (phy == phy_ptr->index)
110*10652SHyon.Kim@Sun.COM 			break;
111*10652SHyon.Kim@Sun.COM 	}
112*10652SHyon.Kim@Sun.COM 
113*10652SHyon.Kim@Sun.COM 	if (phy_ptr == NULL) {
114*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "Invalid phy index %08lx", phy);
115*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
116*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
117*10652SHyon.Kim@Sun.COM 	}
118*10652SHyon.Kim@Sun.COM 
119*10652SHyon.Kim@Sun.COM 	/*
120*10652SHyon.Kim@Sun.COM 	 * for statistics that are not supported, its bits should all be
121*10652SHyon.Kim@Sun.COM 	 * set to -1
122*10652SHyon.Kim@Sun.COM 	 */
123*10652SHyon.Kim@Sun.COM 	(void) memset(pStatistics->SASPhyStatistics, 0xff,
124*10652SHyon.Kim@Sun.COM 	    sizeof (SMHBA_SASPHYSTATISTICS));
125*10652SHyon.Kim@Sun.COM 
126*10652SHyon.Kim@Sun.COM 
127*10652SHyon.Kim@Sun.COM 	/* First, we need the deivce path to locate the devinfo node. */
128*10652SHyon.Kim@Sun.COM 	(void *) strlcpy(path, hba_port_ptr->device_path,
129*10652SHyon.Kim@Sun.COM 	    sizeof (path));
130*10652SHyon.Kim@Sun.COM 	charptr = strrchr(path, ':');
131*10652SHyon.Kim@Sun.COM 	if (charptr) {
132*10652SHyon.Kim@Sun.COM 		*charptr = '\0';
133*10652SHyon.Kim@Sun.COM 	}
134*10652SHyon.Kim@Sun.COM 
135*10652SHyon.Kim@Sun.COM 	errno = 0;
136*10652SHyon.Kim@Sun.COM 
137*10652SHyon.Kim@Sun.COM 	(void *) memset(kstat_name, 0, sizeof (kstat_name));
138*10652SHyon.Kim@Sun.COM 	node = di_init(path, DINFOCPYONE);
139*10652SHyon.Kim@Sun.COM 	if (node == DI_NODE_NIL) {
140*10652SHyon.Kim@Sun.COM 		di_fini(node);
141*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
142*10652SHyon.Kim@Sun.COM 		    "Unable to take devinfo snapshot on HBA \"%s\" "
143*10652SHyon.Kim@Sun.COM 		    "for phyIndex: %08lx due to %s",
144*10652SHyon.Kim@Sun.COM 		    path, phy, strerror(errno));
145*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
146*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
147*10652SHyon.Kim@Sun.COM 	}
148*10652SHyon.Kim@Sun.COM 
149*10652SHyon.Kim@Sun.COM 	/*
150*10652SHyon.Kim@Sun.COM 	 * Then we could fetch the instance number and driver name of this
151*10652SHyon.Kim@Sun.COM 	 * device.
152*10652SHyon.Kim@Sun.COM 	 */
153*10652SHyon.Kim@Sun.COM 	instance = di_instance(node);
154*10652SHyon.Kim@Sun.COM 	if (instance == -1) {
155*10652SHyon.Kim@Sun.COM 		di_fini(node);
156*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
157*10652SHyon.Kim@Sun.COM 		    "An instance number has not been assigned to the "
158*10652SHyon.Kim@Sun.COM 		    "device \"%s\" when get phyIndex: %08lx", path, phy);
159*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
160*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
161*10652SHyon.Kim@Sun.COM 	}
162*10652SHyon.Kim@Sun.COM 
163*10652SHyon.Kim@Sun.COM 	driver_name = di_driver_name(node);
164*10652SHyon.Kim@Sun.COM 	if (driver_name == NULL) {
165*10652SHyon.Kim@Sun.COM 		di_fini(node);
166*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
167*10652SHyon.Kim@Sun.COM 		    "No driver bound to this device \"%s\" "
168*10652SHyon.Kim@Sun.COM 		    "when get phyIndex: %08lx",
169*10652SHyon.Kim@Sun.COM 		    path, phy);
170*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
171*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
172*10652SHyon.Kim@Sun.COM 	}
173*10652SHyon.Kim@Sun.COM 
174*10652SHyon.Kim@Sun.COM 	di_fini(node);
175*10652SHyon.Kim@Sun.COM 
176*10652SHyon.Kim@Sun.COM 	iport_wwn = wwnConversion(hba_port_ptr->port_attributes.\
177*10652SHyon.Kim@Sun.COM 	    PortSpecificAttribute.SASPort->LocalSASAddress.wwn);
178*10652SHyon.Kim@Sun.COM 
179*10652SHyon.Kim@Sun.COM 	/*
180*10652SHyon.Kim@Sun.COM 	 * Construct the kstat name here.
181*10652SHyon.Kim@Sun.COM 	 */
182*10652SHyon.Kim@Sun.COM 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s.%016llx.%u.%u",
183*10652SHyon.Kim@Sun.COM 	    driver_name, iport_wwn, instance, phy_ptr->phy.PhyIdentifier);
184*10652SHyon.Kim@Sun.COM 
185*10652SHyon.Kim@Sun.COM 	/* retrieve all the statistics from kstat. */
186*10652SHyon.Kim@Sun.COM 	kc = kstat_open();
187*10652SHyon.Kim@Sun.COM 	if (kc == NULL) {
188*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
189*10652SHyon.Kim@Sun.COM 		    "kstat_open failed due to \"%s\" of phyIndex: %08lx",
190*10652SHyon.Kim@Sun.COM 		    strerror(errno), phy);
191*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
192*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
193*10652SHyon.Kim@Sun.COM 	}
194*10652SHyon.Kim@Sun.COM 	ksp = kstat_lookup(kc, NULL, -1, kstat_name);
195*10652SHyon.Kim@Sun.COM 	if (ksp == NULL) {
196*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
197*10652SHyon.Kim@Sun.COM 		    "No matching kstat name found for \"%s\" "
198*10652SHyon.Kim@Sun.COM 		    "of phyIndex: %08lx",
199*10652SHyon.Kim@Sun.COM 		    kstat_name, phy);
200*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
201*10652SHyon.Kim@Sun.COM 		(void) kstat_close(kc);
202*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
203*10652SHyon.Kim@Sun.COM 	}
204*10652SHyon.Kim@Sun.COM 	/* Found the phy we're looking for. */
205*10652SHyon.Kim@Sun.COM 	if (kstat_read(kc, ksp, NULL) == -1) {
206*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
207*10652SHyon.Kim@Sun.COM 		    "error reading kstat data due to \"%s\" "
208*10652SHyon.Kim@Sun.COM 		    "of phyIndex: %08lx",
209*10652SHyon.Kim@Sun.COM 		    strerror(errno), phy);
210*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
211*10652SHyon.Kim@Sun.COM 		(void) kstat_close(kc);
212*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
213*10652SHyon.Kim@Sun.COM 	}
214*10652SHyon.Kim@Sun.COM 
215*10652SHyon.Kim@Sun.COM 	kname = (kstat_named_t *)ksp->ks_data;
216*10652SHyon.Kim@Sun.COM 	for (i = 0; i < ksp->ks_ndata; i++, kname++) {
217*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name,
218*10652SHyon.Kim@Sun.COM 		    "SecondsSinceLastReset") == 0) {
219*10652SHyon.Kim@Sun.COM 		    psas->SecondsSinceLastReset = kname->value.ull;
220*10652SHyon.Kim@Sun.COM 			continue;
221*10652SHyon.Kim@Sun.COM 		}
222*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "TxFrames") == 0) {
223*10652SHyon.Kim@Sun.COM 			psas->TxFrames = kname->value.ull;
224*10652SHyon.Kim@Sun.COM 			continue;
225*10652SHyon.Kim@Sun.COM 		}
226*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "RxFrames") == 0) {
227*10652SHyon.Kim@Sun.COM 			psas->RxFrames = kname->value.ull;
228*10652SHyon.Kim@Sun.COM 			continue;
229*10652SHyon.Kim@Sun.COM 		}
230*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "TxWords") == 0) {
231*10652SHyon.Kim@Sun.COM 			psas->TxWords = kname->value.ull;
232*10652SHyon.Kim@Sun.COM 			continue;
233*10652SHyon.Kim@Sun.COM 		}
234*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "RxWords") == 0) {
235*10652SHyon.Kim@Sun.COM 			psas->RxWords = kname->value.ull;
236*10652SHyon.Kim@Sun.COM 			continue;
237*10652SHyon.Kim@Sun.COM 		}
238*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "InvalidDwordCount") == 0) {
239*10652SHyon.Kim@Sun.COM 			psas->InvalidDwordCount = kname->value.ull;
240*10652SHyon.Kim@Sun.COM 			continue;
241*10652SHyon.Kim@Sun.COM 		}
242*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "RunningDisparityErrorCount") == 0) {
243*10652SHyon.Kim@Sun.COM 			psas->RunningDisparityErrorCount = kname->value.ull;
244*10652SHyon.Kim@Sun.COM 			continue;
245*10652SHyon.Kim@Sun.COM 		}
246*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "LossofDwordSyncCount") == 0) {
247*10652SHyon.Kim@Sun.COM 			psas->LossofDwordSyncCount = kname->value.ull;
248*10652SHyon.Kim@Sun.COM 			continue;
249*10652SHyon.Kim@Sun.COM 		}
250*10652SHyon.Kim@Sun.COM 		if (strcmp(kname->name, "PhyResetProblemCount") == 0) {
251*10652SHyon.Kim@Sun.COM 			psas->PhyResetProblemCount = kname->value.ull;
252*10652SHyon.Kim@Sun.COM 		}
253*10652SHyon.Kim@Sun.COM 	}
254*10652SHyon.Kim@Sun.COM 	unlock(&all_hbas_lock);
255*10652SHyon.Kim@Sun.COM 	(void) kstat_close(kc);
256*10652SHyon.Kim@Sun.COM 
257*10652SHyon.Kim@Sun.COM 	return (HBA_STATUS_OK);
258*10652SHyon.Kim@Sun.COM }
259