xref: /illumos-gate/usr/src/uts/common/io/softmac/softmac_stat.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/kstat.h>
30 #include <sys/mac.h>
31 #include <sys/dls.h>
32 #include <sys/softmac_impl.h>
33 
34 typedef struct i_softmac_stat_info_s {
35 	uint_t		ssi_stat;
36 	char		*ssi_name;
37 	char		*ssi_alias;
38 } i_softmac_stat_info_t;
39 
40 /*
41  * Must be the same order as mac_driver_stat.
42  */
43 static i_softmac_stat_info_t	i_softmac_driver_si[] = {
44 	{ MAC_STAT_IFSPEED,	"ifspeed", 	"link_speed"	},
45 	{ MAC_STAT_MULTIRCV,	"multircv",	NULL		},
46 	{ MAC_STAT_BRDCSTRCV,	"brdcstrcv", 	NULL		},
47 	{ MAC_STAT_MULTIXMT,	"multixmt",	NULL		},
48 	{ MAC_STAT_BRDCSTXMT,	"brdcstxmt",	NULL		},
49 	{ MAC_STAT_NORCVBUF,	"norcvbuf",	"rx_no_buf"	},
50 	{ MAC_STAT_IERRORS,	"ierrors",	NULL		},
51 	{ MAC_STAT_UNKNOWNS,	"unknowns",	NULL		},
52 	{ MAC_STAT_NOXMTBUF,	"noxmtbuf",	"No Txpkt "	},
53 	{ MAC_STAT_OERRORS,	"oerrors",	NULL		},
54 	{ MAC_STAT_COLLISIONS,	"collisions",	NULL		},
55 	{ MAC_STAT_RBYTES,	"rbytes64",	"rbytes"	},
56 	{ MAC_STAT_IPACKETS,	"ipackets64",	"ipackets"	},
57 	{ MAC_STAT_OBYTES,	"obytes64",	"obytes"	},
58 	{ MAC_STAT_OPACKETS,	"opackets64",	"opackets"	},
59 	{ MAC_STAT_UNDERFLOWS,	"uflo",		NULL		},
60 	{ MAC_STAT_OVERFLOWS,	"oflo",		NULL		}
61 };
62 
63 /*
64  * Must be the same order as ether_stat.
65  */
66 static i_softmac_stat_info_t	i_softmac_ether_si[] = {
67 	{ ETHER_STAT_ALIGN_ERRORS,	"align_errors",
68 	    "alignment_err" },
69 	{ ETHER_STAT_FCS_ERRORS, 	"fcs_errors",		"crc_err" },
70 	{ ETHER_STAT_FIRST_COLLISIONS,	"first_collisions",	NULL },
71 	{ ETHER_STAT_MULTI_COLLISIONS,	"multi_collisions",	NULL },
72 	{ ETHER_STAT_SQE_ERRORS,	"sqe_errors",		NULL },
73 	{ ETHER_STAT_DEFER_XMTS,	"defer_xmts",		NULL },
74 	{ ETHER_STAT_TX_LATE_COLLISIONS, "tx_late_collisions",
75 	    "late_collisions" },
76 	{ ETHER_STAT_EX_COLLISIONS,	"ex_collisions",
77 	    "excessive_collisions" },
78 	{ ETHER_STAT_MACXMT_ERRORS,	"macxmt_errors",	NULL },
79 	{ ETHER_STAT_CARRIER_ERRORS,	"carrier_errors",	NULL },
80 	{ ETHER_STAT_TOOLONG_ERRORS,	"toolong_errors", 	"length_err" },
81 	{ ETHER_STAT_MACRCV_ERRORS, 	"macrcv_errors",
82 	    "Rx Error Count" },
83 
84 	{ ETHER_STAT_XCVR_ADDR,		"xcvr_addr",		NULL },
85 	{ ETHER_STAT_XCVR_ID,		"xcvr_id",		NULL },
86 	{ ETHER_STAT_XCVR_INUSE,	"xcvr_inuse",		NULL },
87 
88 	{ ETHER_STAT_CAP_1000FDX,	"cap_1000fdx",		NULL },
89 	{ ETHER_STAT_CAP_1000HDX,	"cap_1000hdx",		NULL },
90 	{ ETHER_STAT_CAP_100FDX,	"cap_100fdx",		NULL },
91 	{ ETHER_STAT_CAP_100HDX,	"cap_100hdx",		NULL },
92 	{ ETHER_STAT_CAP_10FDX,		"cap_10fdx",		NULL },
93 	{ ETHER_STAT_CAP_10HDX,		"cap_10hdx",		NULL },
94 	{ ETHER_STAT_CAP_ASMPAUSE,	"cap_asmpause",		NULL },
95 	{ ETHER_STAT_CAP_PAUSE,		"cap_pause",		NULL },
96 	{ ETHER_STAT_CAP_AUTONEG,	"cap_autoneg",		NULL },
97 
98 	{ ETHER_STAT_ADV_CAP_1000FDX,	"adv_cap_1000fdx",	NULL },
99 	{ ETHER_STAT_ADV_CAP_1000HDX,	"adv_cap_1000hdx",	NULL },
100 	{ ETHER_STAT_ADV_CAP_100FDX,	"adv_cap_100fdx",	NULL },
101 	{ ETHER_STAT_ADV_CAP_100HDX,	"adv_cap_100hdx",	NULL },
102 	{ ETHER_STAT_ADV_CAP_10FDX,	"adv_cap_10fdx",	NULL },
103 	{ ETHER_STAT_ADV_CAP_10HDX,	"adv_cap_10hdx",	NULL },
104 	{ ETHER_STAT_ADV_CAP_ASMPAUSE,	"adv_cap_asmpause",	NULL },
105 	{ ETHER_STAT_ADV_CAP_PAUSE,	"adv_cap_pause", 	NULL },
106 	{ ETHER_STAT_ADV_CAP_AUTONEG,	"adv_cap_autoneg", 	NULL },
107 
108 	{ ETHER_STAT_LP_CAP_1000FDX,	"lp_cap_1000fdx",	NULL },
109 	{ ETHER_STAT_LP_CAP_1000HDX,	"lp_cap_1000hdx",	NULL },
110 	{ ETHER_STAT_LP_CAP_100FDX,	"lp_cap_100fdx",	NULL },
111 	{ ETHER_STAT_LP_CAP_100HDX,	"lp_cap_100hdx",	NULL },
112 	{ ETHER_STAT_LP_CAP_10FDX,	"lp_cap_10fdx",		NULL },
113 	{ ETHER_STAT_LP_CAP_10HDX,	"lp_cap_10hdx",		NULL },
114 	{ ETHER_STAT_LP_CAP_ASMPAUSE,	"lp_cap_asmpause",	NULL },
115 	{ ETHER_STAT_LP_CAP_PAUSE,	"lp_cap_pause",		NULL },
116 	{ ETHER_STAT_LP_CAP_AUTONEG,	"lp_cap_autoneg",	NULL },
117 
118 	{ ETHER_STAT_LINK_ASMPAUSE,	"link_asmpause",	NULL },
119 	{ ETHER_STAT_LINK_PAUSE,	"link_pause",		NULL },
120 	{ ETHER_STAT_LINK_AUTONEG,	"link_autoneg", 	NULL },
121 	{ ETHER_STAT_LINK_DUPLEX,	"link_duplex",		"duplex" },
122 
123 	{ ETHER_STAT_TOOSHORT_ERRORS,	"runt_errors",		NULL },
124 	{ ETHER_STAT_CAP_REMFAULT,	"cap_rem_fault",	NULL },
125 	{ ETHER_STAT_ADV_REMFAULT,	"adv_rem_fault",	NULL },
126 	{ ETHER_STAT_LP_REMFAULT,	"lp_rem_fault",		NULL },
127 
128 	{ ETHER_STAT_JABBER_ERRORS,	"jabber_errors",	NULL },
129 	{ ETHER_STAT_CAP_100T4,		"cap_100T4",		NULL },
130 	{ ETHER_STAT_ADV_CAP_100T4,	"adv_cap_100T4",	NULL },
131 	{ ETHER_STAT_LP_CAP_100T4,	"lp_cap_100T4", 	NULL }
132 };
133 
134 static kstat_t	*softmac_hold_dev_kstat(softmac_t *);
135 static void	softmac_rele_dev_kstat(kstat_t *);
136 static int	softmac_get_kstat(kstat_t *, char *, uint64_t *);
137 
138 static kstat_t *
139 softmac_hold_dev_kstat(softmac_t *softmac)
140 {
141 	char		drv[MAXLINKNAMELEN];
142 	uint_t		ppa;
143 	kstat_t		*ksp;
144 
145 	if (ddi_parse(softmac->smac_devname, drv, &ppa) != DDI_SUCCESS)
146 		return (NULL);
147 
148 	/*
149 	 * Find the kstat by the module name and the instance number.
150 	 */
151 	ksp = kstat_hold_byname(drv, ppa, softmac->smac_devname, ALL_ZONES);
152 	if (ksp != NULL) {
153 		KSTAT_ENTER(ksp);
154 
155 		if ((ksp->ks_data != NULL) &&
156 		    (ksp->ks_type == KSTAT_TYPE_NAMED)) {
157 			/*
158 			 * Update the kstat to get the latest statistics.
159 			 */
160 			if (KSTAT_UPDATE(ksp, KSTAT_READ) == 0)
161 				return (ksp);
162 		}
163 
164 		KSTAT_EXIT(ksp);
165 		kstat_rele(ksp);
166 	}
167 	return (NULL);
168 }
169 
170 static void
171 softmac_rele_dev_kstat(kstat_t *ksp)
172 {
173 	KSTAT_EXIT(ksp);
174 	kstat_rele(ksp);
175 }
176 
177 /*
178  * The kstat needs to be held when calling this function.
179  */
180 static int
181 softmac_get_kstat(kstat_t *ksp, char *name, uint64_t *valp)
182 {
183 	kstat_named_t	*knp;
184 	int		i;
185 	int		ret = ENOTSUP;
186 
187 	if (name == NULL)
188 		return (ret);
189 
190 	/*
191 	 * Search the kstat with the given name.
192 	 */
193 	for (i = 0, knp = KSTAT_NAMED_PTR(ksp); i < ksp->ks_ndata; i++, knp++) {
194 		if (strcmp(knp->name, name) == 0) {
195 			switch (knp->data_type) {
196 			case KSTAT_DATA_INT32:
197 			case KSTAT_DATA_UINT32:
198 				*valp = (uint64_t)(knp->value.ui32);
199 				ret = 0;
200 				break;
201 			case KSTAT_DATA_INT64:
202 			case KSTAT_DATA_UINT64:
203 				*valp = knp->value.ui64;
204 				ret = 0;
205 				break;
206 #ifdef _LP64
207 			case KSTAT_DATA_LONG:
208 			case KSTAT_DATA_ULONG:
209 				*valp = (uint64_t)knp->value.ul;
210 				ret = 0;
211 				break;
212 #endif
213 			case KSTAT_DATA_CHAR:
214 				if (strcmp(name, "duplex") != 0)
215 					break;
216 				if (strncmp(knp->value.c, "full", 4) == 0)
217 					*valp = LINK_DUPLEX_FULL;
218 				else if (strncmp(knp->value.c, "half", 4) == 0)
219 					*valp = LINK_DUPLEX_HALF;
220 				else
221 					*valp = LINK_DUPLEX_UNKNOWN;
222 				ret = 0;
223 				break;
224 			}
225 			break;
226 		}
227 	}
228 
229 	return (ret);
230 }
231 
232 int
233 softmac_m_stat(void *arg, uint_t stat, uint64_t *val)
234 {
235 	softmac_t	*softmac = arg;
236 	kstat_t		*ksp;
237 	uint_t		index;
238 	int		ret;
239 
240 	if ((ksp = softmac_hold_dev_kstat(softmac)) == NULL)
241 		return (ENOTSUP);
242 
243 	if (IS_MAC_STAT(stat)) {
244 		index = stat - MAC_STAT_MIN;
245 		if ((ret = softmac_get_kstat(ksp,
246 		    i_softmac_driver_si[index].ssi_name, val)) != 0) {
247 			ret = softmac_get_kstat(ksp,
248 			    i_softmac_driver_si[index].ssi_alias, val);
249 		}
250 	} else {
251 		ASSERT(IS_MACTYPE_STAT(stat));
252 		index = stat - MACTYPE_STAT_MIN;
253 
254 		switch (softmac->smac_media) {
255 		case DL_ETHER:
256 			if ((ret = softmac_get_kstat(ksp,
257 			    i_softmac_ether_si[index].ssi_name, val)) != 0) {
258 				ret = softmac_get_kstat(ksp,
259 				    i_softmac_ether_si[index].ssi_alias, val);
260 			}
261 			break;
262 		default:
263 			ret = ENOTSUP;
264 			break;
265 		}
266 	}
267 
268 	softmac_rele_dev_kstat(ksp);
269 	return (ret);
270 }
271