xref: /titanic_41/usr/src/uts/common/io/softmac/softmac_stat.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/kstat.h>
28 #include <sys/mac.h>
29 #include <sys/dls.h>
30 #include <sys/softmac_impl.h>
31 
32 typedef struct i_softmac_stat_info_s {
33 	uint_t		ssi_stat;
34 	char		*ssi_name;
35 	char		*ssi_alias;
36 } i_softmac_stat_info_t;
37 
38 /*
39  * Must be the same order as mac_driver_stat.
40  */
41 static i_softmac_stat_info_t	i_softmac_driver_si[] = {
42 	{ MAC_STAT_IFSPEED,	"ifspeed", 	"link_speed"	},
43 	{ MAC_STAT_MULTIRCV,	"multircv",	NULL		},
44 	{ MAC_STAT_BRDCSTRCV,	"brdcstrcv", 	NULL		},
45 	{ MAC_STAT_MULTIXMT,	"multixmt",	NULL		},
46 	{ MAC_STAT_BRDCSTXMT,	"brdcstxmt",	NULL		},
47 	{ MAC_STAT_NORCVBUF,	"norcvbuf",	"rx_no_buf"	},
48 	{ MAC_STAT_IERRORS,	"ierrors",	NULL		},
49 	{ MAC_STAT_UNKNOWNS,	"unknowns",	NULL		},
50 	{ MAC_STAT_NOXMTBUF,	"noxmtbuf",	"No Txpkt "	},
51 	{ MAC_STAT_OERRORS,	"oerrors",	NULL		},
52 	{ MAC_STAT_COLLISIONS,	"collisions",	NULL		},
53 	{ MAC_STAT_RBYTES,	"rbytes64",	"rbytes"	},
54 	{ MAC_STAT_IPACKETS,	"ipackets64",	"ipackets"	},
55 	{ MAC_STAT_OBYTES,	"obytes64",	"obytes"	},
56 	{ MAC_STAT_OPACKETS,	"opackets64",	"opackets"	},
57 	{ MAC_STAT_UNDERFLOWS,	"uflo",		NULL		},
58 	{ MAC_STAT_OVERFLOWS,	"oflo",		NULL		}
59 };
60 
61 #define	SOFTMAC_DRIVER_SI_SZ						\
62 	(sizeof (i_softmac_driver_si) / sizeof (i_softmac_driver_si[0]))
63 
64 /*
65  * Must be the same order as ether_stat.
66  */
67 static i_softmac_stat_info_t	i_softmac_ether_si[] = {
68 	{ ETHER_STAT_ALIGN_ERRORS,	"align_errors",
69 	    "alignment_err" },
70 	{ ETHER_STAT_FCS_ERRORS, 	"fcs_errors",		"crc_err" },
71 	{ ETHER_STAT_FIRST_COLLISIONS,	"first_collisions",	NULL },
72 	{ ETHER_STAT_MULTI_COLLISIONS,	"multi_collisions",	NULL },
73 	{ ETHER_STAT_SQE_ERRORS,	"sqe_errors",		NULL },
74 	{ ETHER_STAT_DEFER_XMTS,	"defer_xmts",		NULL },
75 	{ ETHER_STAT_TX_LATE_COLLISIONS, "tx_late_collisions",
76 	    "late_collisions" },
77 	{ ETHER_STAT_EX_COLLISIONS,	"ex_collisions",
78 	    "excessive_collisions" },
79 	{ ETHER_STAT_MACXMT_ERRORS,	"macxmt_errors",	NULL },
80 	{ ETHER_STAT_CARRIER_ERRORS,	"carrier_errors",	NULL },
81 	{ ETHER_STAT_TOOLONG_ERRORS,	"toolong_errors", 	"length_err" },
82 	{ ETHER_STAT_MACRCV_ERRORS, 	"macrcv_errors",
83 	    "Rx Error Count" },
84 
85 	{ ETHER_STAT_XCVR_ADDR,		"xcvr_addr",		NULL },
86 	{ ETHER_STAT_XCVR_ID,		"xcvr_id",		NULL },
87 	{ ETHER_STAT_XCVR_INUSE,	"xcvr_inuse",		NULL },
88 
89 	{ ETHER_STAT_CAP_1000FDX,	"cap_1000fdx",		NULL },
90 	{ ETHER_STAT_CAP_1000HDX,	"cap_1000hdx",		NULL },
91 	{ ETHER_STAT_CAP_100FDX,	"cap_100fdx",		NULL },
92 	{ ETHER_STAT_CAP_100HDX,	"cap_100hdx",		NULL },
93 	{ ETHER_STAT_CAP_10FDX,		"cap_10fdx",		NULL },
94 	{ ETHER_STAT_CAP_10HDX,		"cap_10hdx",		NULL },
95 	{ ETHER_STAT_CAP_ASMPAUSE,	"cap_asmpause",		NULL },
96 	{ ETHER_STAT_CAP_PAUSE,		"cap_pause",		NULL },
97 	{ ETHER_STAT_CAP_AUTONEG,	"cap_autoneg",		NULL },
98 
99 	{ ETHER_STAT_ADV_CAP_1000FDX,	"adv_cap_1000fdx",	NULL },
100 	{ ETHER_STAT_ADV_CAP_1000HDX,	"adv_cap_1000hdx",	NULL },
101 	{ ETHER_STAT_ADV_CAP_100FDX,	"adv_cap_100fdx",	NULL },
102 	{ ETHER_STAT_ADV_CAP_100HDX,	"adv_cap_100hdx",	NULL },
103 	{ ETHER_STAT_ADV_CAP_10FDX,	"adv_cap_10fdx",	NULL },
104 	{ ETHER_STAT_ADV_CAP_10HDX,	"adv_cap_10hdx",	NULL },
105 	{ ETHER_STAT_ADV_CAP_ASMPAUSE,	"adv_cap_asmpause",	NULL },
106 	{ ETHER_STAT_ADV_CAP_PAUSE,	"adv_cap_pause", 	NULL },
107 	{ ETHER_STAT_ADV_CAP_AUTONEG,	"adv_cap_autoneg", 	NULL },
108 
109 	{ ETHER_STAT_LP_CAP_1000FDX,	"lp_cap_1000fdx",	NULL },
110 	{ ETHER_STAT_LP_CAP_1000HDX,	"lp_cap_1000hdx",	NULL },
111 	{ ETHER_STAT_LP_CAP_100FDX,	"lp_cap_100fdx",	NULL },
112 	{ ETHER_STAT_LP_CAP_100HDX,	"lp_cap_100hdx",	NULL },
113 	{ ETHER_STAT_LP_CAP_10FDX,	"lp_cap_10fdx",		NULL },
114 	{ ETHER_STAT_LP_CAP_10HDX,	"lp_cap_10hdx",		NULL },
115 	{ ETHER_STAT_LP_CAP_ASMPAUSE,	"lp_cap_asmpause",	NULL },
116 	{ ETHER_STAT_LP_CAP_PAUSE,	"lp_cap_pause",		NULL },
117 	{ ETHER_STAT_LP_CAP_AUTONEG,	"lp_cap_autoneg",	NULL },
118 
119 	{ ETHER_STAT_LINK_ASMPAUSE,	"link_asmpause",	NULL },
120 	{ ETHER_STAT_LINK_PAUSE,	"link_pause",		NULL },
121 	{ ETHER_STAT_LINK_AUTONEG,	"link_autoneg", 	NULL },
122 	{ ETHER_STAT_LINK_DUPLEX,	"link_duplex",		"duplex" },
123 
124 	{ ETHER_STAT_TOOSHORT_ERRORS,	"runt_errors",		NULL },
125 	{ ETHER_STAT_CAP_REMFAULT,	"cap_rem_fault",	NULL },
126 	{ ETHER_STAT_ADV_REMFAULT,	"adv_rem_fault",	NULL },
127 	{ ETHER_STAT_LP_REMFAULT,	"lp_rem_fault",		NULL },
128 
129 	{ ETHER_STAT_JABBER_ERRORS,	"jabber_errors",	NULL },
130 	{ ETHER_STAT_CAP_100T4,		"cap_100T4",		NULL },
131 	{ ETHER_STAT_ADV_CAP_100T4,	"adv_cap_100T4",	NULL },
132 	{ ETHER_STAT_LP_CAP_100T4,	"lp_cap_100T4", 	NULL },
133 
134 	{ ETHER_STAT_CAP_10GFDX,	"cap_10gfdx",		NULL },
135 	{ ETHER_STAT_ADV_CAP_10GFDX,	"adv_cap_10gfdx",	NULL },
136 	{ ETHER_STAT_LP_CAP_1000FDX,	"lp_cap_10gfdx",	NULL }
137 };
138 
139 #define	SOFTMAC_ETHER_SI_SZ						\
140 	(sizeof (i_softmac_ether_si) / sizeof (i_softmac_ether_si[0]))
141 
142 static kstat_t	*softmac_hold_dev_kstat(softmac_t *);
143 static void	softmac_rele_dev_kstat(kstat_t *);
144 static int	softmac_get_kstat(kstat_t *, char *, uint64_t *);
145 
146 static kstat_t *
147 softmac_hold_dev_kstat(softmac_t *softmac)
148 {
149 	char		drv[MAXLINKNAMELEN];
150 	uint_t		ppa;
151 	kstat_t		*ksp;
152 
153 	if (ddi_parse(softmac->smac_devname, drv, &ppa) != DDI_SUCCESS)
154 		return (NULL);
155 
156 	/*
157 	 * Find the kstat by the module name and the instance number.
158 	 */
159 	ksp = kstat_hold_byname(drv, ppa, softmac->smac_devname, ALL_ZONES);
160 	if (ksp != NULL) {
161 		KSTAT_ENTER(ksp);
162 
163 		if ((ksp->ks_data != NULL) &&
164 		    (ksp->ks_type == KSTAT_TYPE_NAMED)) {
165 			/*
166 			 * Update the kstat to get the latest statistics.
167 			 */
168 			if (KSTAT_UPDATE(ksp, KSTAT_READ) == 0)
169 				return (ksp);
170 		}
171 
172 		KSTAT_EXIT(ksp);
173 		kstat_rele(ksp);
174 	}
175 	return (NULL);
176 }
177 
178 static void
179 softmac_rele_dev_kstat(kstat_t *ksp)
180 {
181 	KSTAT_EXIT(ksp);
182 	kstat_rele(ksp);
183 }
184 
185 /*
186  * The kstat needs to be held when calling this function.
187  */
188 static int
189 softmac_get_kstat(kstat_t *ksp, char *name, uint64_t *valp)
190 {
191 	kstat_named_t	*knp;
192 	int		i;
193 	int		ret = ENOTSUP;
194 
195 	if (name == NULL)
196 		return (ret);
197 
198 	/*
199 	 * Search the kstat with the given name.
200 	 */
201 	for (i = 0, knp = KSTAT_NAMED_PTR(ksp); i < ksp->ks_ndata; i++, knp++) {
202 		if (strcmp(knp->name, name) == 0) {
203 			switch (knp->data_type) {
204 			case KSTAT_DATA_INT32:
205 			case KSTAT_DATA_UINT32:
206 				*valp = (uint64_t)(knp->value.ui32);
207 				ret = 0;
208 				break;
209 			case KSTAT_DATA_INT64:
210 			case KSTAT_DATA_UINT64:
211 				*valp = knp->value.ui64;
212 				ret = 0;
213 				break;
214 #ifdef _LP64
215 			case KSTAT_DATA_LONG:
216 			case KSTAT_DATA_ULONG:
217 				*valp = (uint64_t)knp->value.ul;
218 				ret = 0;
219 				break;
220 #endif
221 			case KSTAT_DATA_CHAR:
222 				if (strcmp(name, "duplex") != 0)
223 					break;
224 				if (strncmp(knp->value.c, "full", 4) == 0)
225 					*valp = LINK_DUPLEX_FULL;
226 				else if (strncmp(knp->value.c, "half", 4) == 0)
227 					*valp = LINK_DUPLEX_HALF;
228 				else
229 					*valp = LINK_DUPLEX_UNKNOWN;
230 				ret = 0;
231 				break;
232 			}
233 			break;
234 		}
235 	}
236 
237 	return (ret);
238 }
239 
240 int
241 softmac_m_stat(void *arg, uint_t stat, uint64_t *val)
242 {
243 	softmac_t	*softmac = arg;
244 	kstat_t		*ksp;
245 	uint_t		index;
246 	int		ret;
247 
248 	if ((ksp = softmac_hold_dev_kstat(softmac)) == NULL)
249 		return (ENOTSUP);
250 
251 	if (IS_MAC_STAT(stat)) {
252 		i_softmac_stat_info_t *ssip = NULL;
253 
254 		for (index = 0; index < SOFTMAC_DRIVER_SI_SZ; index++) {
255 			if (stat == i_softmac_driver_si[index].ssi_stat) {
256 				ssip = &i_softmac_driver_si[index];
257 				break;
258 			}
259 		}
260 
261 		if (ssip == NULL) {
262 			ret = ENOTSUP;
263 		} else {
264 			if ((ret = softmac_get_kstat(ksp, ssip->ssi_name,
265 			    val)) != 0)
266 				ret = softmac_get_kstat(ksp, ssip->ssi_alias,
267 				    val);
268 		}
269 	} else {
270 		ASSERT(IS_MACTYPE_STAT(stat));
271 
272 		switch (softmac->smac_media) {
273 		case DL_ETHER: {
274 			i_softmac_stat_info_t *ssip = NULL;
275 
276 			for (index = 0; index < SOFTMAC_ETHER_SI_SZ; index++) {
277 				if (stat ==
278 				    i_softmac_ether_si[index].ssi_stat) {
279 					ssip = &i_softmac_ether_si[index];
280 					break;
281 				}
282 			}
283 
284 			if (ssip == NULL) {
285 				ret = ENOTSUP;
286 			} else {
287 				if ((ret = softmac_get_kstat(ksp,
288 				    ssip->ssi_name, val)) != 0)
289 					ret = softmac_get_kstat(ksp,
290 					    ssip->ssi_alias, val);
291 			}
292 
293 			break;
294 		}
295 		default:
296 			ret = ENOTSUP;
297 			break;
298 		}
299 	}
300 
301 	softmac_rele_dev_kstat(ksp);
302 	return (ret);
303 }
304