1d62bc4baSyz /*
2d62bc4baSyz  * CDDL HEADER START
3d62bc4baSyz  *
4d62bc4baSyz  * The contents of this file are subject to the terms of the
5d62bc4baSyz  * Common Development and Distribution License (the "License").
6d62bc4baSyz  * You may not use this file except in compliance with the License.
7d62bc4baSyz  *
8d62bc4baSyz  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d62bc4baSyz  * or http://www.opensolaris.org/os/licensing.
10d62bc4baSyz  * See the License for the specific language governing permissions
11d62bc4baSyz  * and limitations under the License.
12d62bc4baSyz  *
13d62bc4baSyz  * When distributing Covered Code, include this CDDL HEADER in each
14d62bc4baSyz  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d62bc4baSyz  * If applicable, add the following below this CDDL HEADER, with the
16d62bc4baSyz  * fields enclosed by brackets "[]" replaced with your own identifying
17d62bc4baSyz  * information: Portions Copyright [yyyy] [name of copyright owner]
18d62bc4baSyz  *
19d62bc4baSyz  * CDDL HEADER END
20d62bc4baSyz  */
21d62bc4baSyz /*
22aca118b7Slucy wang - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23d62bc4baSyz  * Use is subject to license terms.
243bc4925dSGarrett D'Amore  *
253bc4925dSGarrett D'Amore  * Copyright 2015 Garrett D'Amore <garret@damore.org>
26*3d75a287SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
27d62bc4baSyz  */
28d62bc4baSyz 
29d62bc4baSyz #include <sys/types.h>
30d62bc4baSyz #include <sys/kstat.h>
31d62bc4baSyz #include <sys/mac.h>
32d62bc4baSyz #include <sys/dls.h>
33d62bc4baSyz #include <sys/softmac_impl.h>
34d62bc4baSyz 
35d62bc4baSyz typedef struct i_softmac_stat_info_s {
36d62bc4baSyz 	uint_t		ssi_stat;
37d62bc4baSyz 	char		*ssi_name;
38d62bc4baSyz 	char		*ssi_alias;
39d62bc4baSyz } i_softmac_stat_info_t;
40d62bc4baSyz 
41d62bc4baSyz /*
42d62bc4baSyz  * Must be the same order as mac_driver_stat.
43d62bc4baSyz  */
44d62bc4baSyz static i_softmac_stat_info_t	i_softmac_driver_si[] = {
45d62bc4baSyz 	{ MAC_STAT_IFSPEED,	"ifspeed", 	"link_speed"	},
46d62bc4baSyz 	{ MAC_STAT_MULTIRCV,	"multircv",	NULL		},
47d62bc4baSyz 	{ MAC_STAT_BRDCSTRCV,	"brdcstrcv", 	NULL		},
48d62bc4baSyz 	{ MAC_STAT_MULTIXMT,	"multixmt",	NULL		},
49d62bc4baSyz 	{ MAC_STAT_BRDCSTXMT,	"brdcstxmt",	NULL		},
50d62bc4baSyz 	{ MAC_STAT_NORCVBUF,	"norcvbuf",	"rx_no_buf"	},
51d62bc4baSyz 	{ MAC_STAT_IERRORS,	"ierrors",	NULL		},
52d62bc4baSyz 	{ MAC_STAT_UNKNOWNS,	"unknowns",	NULL		},
53d62bc4baSyz 	{ MAC_STAT_NOXMTBUF,	"noxmtbuf",	"No Txpkt "	},
54d62bc4baSyz 	{ MAC_STAT_OERRORS,	"oerrors",	NULL		},
55d62bc4baSyz 	{ MAC_STAT_COLLISIONS,	"collisions",	NULL		},
56d62bc4baSyz 	{ MAC_STAT_RBYTES,	"rbytes64",	"rbytes"	},
57d62bc4baSyz 	{ MAC_STAT_IPACKETS,	"ipackets64",	"ipackets"	},
58d62bc4baSyz 	{ MAC_STAT_OBYTES,	"obytes64",	"obytes"	},
59d62bc4baSyz 	{ MAC_STAT_OPACKETS,	"opackets64",	"opackets"	},
60d62bc4baSyz 	{ MAC_STAT_UNDERFLOWS,	"uflo",		NULL		},
61d62bc4baSyz 	{ MAC_STAT_OVERFLOWS,	"oflo",		NULL		}
62d62bc4baSyz };
63d62bc4baSyz 
64aca118b7Slucy wang - Sun Microsystems - Beijing China #define	SOFTMAC_DRIVER_SI_SZ						\
65aca118b7Slucy wang - Sun Microsystems - Beijing China 	(sizeof (i_softmac_driver_si) / sizeof (i_softmac_driver_si[0]))
66aca118b7Slucy wang - Sun Microsystems - Beijing China 
67d62bc4baSyz /*
68d62bc4baSyz  * Must be the same order as ether_stat.
69d62bc4baSyz  */
70d62bc4baSyz static i_softmac_stat_info_t	i_softmac_ether_si[] = {
71d62bc4baSyz 	{ ETHER_STAT_ALIGN_ERRORS,	"align_errors",
72d62bc4baSyz 	    "alignment_err" },
73d62bc4baSyz 	{ ETHER_STAT_FCS_ERRORS, 	"fcs_errors",		"crc_err" },
74d62bc4baSyz 	{ ETHER_STAT_FIRST_COLLISIONS,	"first_collisions",	NULL },
75d62bc4baSyz 	{ ETHER_STAT_MULTI_COLLISIONS,	"multi_collisions",	NULL },
76d62bc4baSyz 	{ ETHER_STAT_SQE_ERRORS,	"sqe_errors",		NULL },
77d62bc4baSyz 	{ ETHER_STAT_DEFER_XMTS,	"defer_xmts",		NULL },
78d62bc4baSyz 	{ ETHER_STAT_TX_LATE_COLLISIONS, "tx_late_collisions",
79d62bc4baSyz 	    "late_collisions" },
80d62bc4baSyz 	{ ETHER_STAT_EX_COLLISIONS,	"ex_collisions",
81d62bc4baSyz 	    "excessive_collisions" },
82d62bc4baSyz 	{ ETHER_STAT_MACXMT_ERRORS,	"macxmt_errors",	NULL },
83d62bc4baSyz 	{ ETHER_STAT_CARRIER_ERRORS,	"carrier_errors",	NULL },
84d62bc4baSyz 	{ ETHER_STAT_TOOLONG_ERRORS,	"toolong_errors", 	"length_err" },
85d62bc4baSyz 	{ ETHER_STAT_MACRCV_ERRORS, 	"macrcv_errors",
86d62bc4baSyz 	    "Rx Error Count" },
87d62bc4baSyz 
88d62bc4baSyz 	{ ETHER_STAT_XCVR_ADDR,		"xcvr_addr",		NULL },
89d62bc4baSyz 	{ ETHER_STAT_XCVR_ID,		"xcvr_id",		NULL },
90d62bc4baSyz 	{ ETHER_STAT_XCVR_INUSE,	"xcvr_inuse",		NULL },
91d62bc4baSyz 
92d62bc4baSyz 	{ ETHER_STAT_CAP_1000FDX,	"cap_1000fdx",		NULL },
93d62bc4baSyz 	{ ETHER_STAT_CAP_1000HDX,	"cap_1000hdx",		NULL },
94d62bc4baSyz 	{ ETHER_STAT_CAP_100FDX,	"cap_100fdx",		NULL },
95d62bc4baSyz 	{ ETHER_STAT_CAP_100HDX,	"cap_100hdx",		NULL },
96d62bc4baSyz 	{ ETHER_STAT_CAP_10FDX,		"cap_10fdx",		NULL },
97d62bc4baSyz 	{ ETHER_STAT_CAP_10HDX,		"cap_10hdx",		NULL },
98d62bc4baSyz 	{ ETHER_STAT_CAP_ASMPAUSE,	"cap_asmpause",		NULL },
99d62bc4baSyz 	{ ETHER_STAT_CAP_PAUSE,		"cap_pause",		NULL },
100d62bc4baSyz 	{ ETHER_STAT_CAP_AUTONEG,	"cap_autoneg",		NULL },
101d62bc4baSyz 
102d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_1000FDX,	"adv_cap_1000fdx",	NULL },
103d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_1000HDX,	"adv_cap_1000hdx",	NULL },
104d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_100FDX,	"adv_cap_100fdx",	NULL },
105d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_100HDX,	"adv_cap_100hdx",	NULL },
106d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_10FDX,	"adv_cap_10fdx",	NULL },
107d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_10HDX,	"adv_cap_10hdx",	NULL },
108d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_ASMPAUSE,	"adv_cap_asmpause",	NULL },
109d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_PAUSE,	"adv_cap_pause", 	NULL },
110d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_AUTONEG,	"adv_cap_autoneg", 	NULL },
111d62bc4baSyz 
112d62bc4baSyz 	{ ETHER_STAT_LP_CAP_1000FDX,	"lp_cap_1000fdx",	NULL },
113d62bc4baSyz 	{ ETHER_STAT_LP_CAP_1000HDX,	"lp_cap_1000hdx",	NULL },
114d62bc4baSyz 	{ ETHER_STAT_LP_CAP_100FDX,	"lp_cap_100fdx",	NULL },
115d62bc4baSyz 	{ ETHER_STAT_LP_CAP_100HDX,	"lp_cap_100hdx",	NULL },
116d62bc4baSyz 	{ ETHER_STAT_LP_CAP_10FDX,	"lp_cap_10fdx",		NULL },
117d62bc4baSyz 	{ ETHER_STAT_LP_CAP_10HDX,	"lp_cap_10hdx",		NULL },
118d62bc4baSyz 	{ ETHER_STAT_LP_CAP_ASMPAUSE,	"lp_cap_asmpause",	NULL },
119d62bc4baSyz 	{ ETHER_STAT_LP_CAP_PAUSE,	"lp_cap_pause",		NULL },
120d62bc4baSyz 	{ ETHER_STAT_LP_CAP_AUTONEG,	"lp_cap_autoneg",	NULL },
121d62bc4baSyz 
122d62bc4baSyz 	{ ETHER_STAT_LINK_ASMPAUSE,	"link_asmpause",	NULL },
123d62bc4baSyz 	{ ETHER_STAT_LINK_PAUSE,	"link_pause",		NULL },
124d62bc4baSyz 	{ ETHER_STAT_LINK_AUTONEG,	"link_autoneg", 	NULL },
125d62bc4baSyz 	{ ETHER_STAT_LINK_DUPLEX,	"link_duplex",		"duplex" },
126d62bc4baSyz 
127d62bc4baSyz 	{ ETHER_STAT_TOOSHORT_ERRORS,	"runt_errors",		NULL },
128d62bc4baSyz 	{ ETHER_STAT_CAP_REMFAULT,	"cap_rem_fault",	NULL },
129d62bc4baSyz 	{ ETHER_STAT_ADV_REMFAULT,	"adv_rem_fault",	NULL },
130d62bc4baSyz 	{ ETHER_STAT_LP_REMFAULT,	"lp_rem_fault",		NULL },
131d62bc4baSyz 
132d62bc4baSyz 	{ ETHER_STAT_JABBER_ERRORS,	"jabber_errors",	NULL },
133d62bc4baSyz 	{ ETHER_STAT_CAP_100T4,		"cap_100T4",		NULL },
134d62bc4baSyz 	{ ETHER_STAT_ADV_CAP_100T4,	"adv_cap_100T4",	NULL },
1353bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_100T4,	"lp_cap_100T4",		NULL },
136aca118b7Slucy wang - Sun Microsystems - Beijing China 
137aca118b7Slucy wang - Sun Microsystems - Beijing China 	{ ETHER_STAT_CAP_10GFDX,	"cap_10gfdx",		NULL },
138aca118b7Slucy wang - Sun Microsystems - Beijing China 	{ ETHER_STAT_ADV_CAP_10GFDX,	"adv_cap_10gfdx",	NULL },
1393bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_1000FDX,	"lp_cap_10gfdx",	NULL },
1403bc4925dSGarrett D'Amore 
1413bc4925dSGarrett D'Amore 	{ ETHER_STAT_CAP_40GFDX,	"cap_40gfdx",		NULL },
1423bc4925dSGarrett D'Amore 	{ ETHER_STAT_ADV_CAP_40GFDX,	"adv_cap_40gfdx",	NULL },
1433bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_40GFDX,	"lp_cap_40gfdx",	NULL },
1443bc4925dSGarrett D'Amore 
1453bc4925dSGarrett D'Amore 	{ ETHER_STAT_CAP_100GFDX,	"cap_100gfdx",		NULL },
1463bc4925dSGarrett D'Amore 	{ ETHER_STAT_ADV_CAP_100GFDX,	"adv_cap_100gfdx",	NULL },
1473bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_100GFDX,	"lp_cap_100gfdx",	NULL },
1483bc4925dSGarrett D'Amore 
1493bc4925dSGarrett D'Amore 	{ ETHER_STAT_CAP_2500FDX,	"cap_2500fdx",		NULL },
1503bc4925dSGarrett D'Amore 	{ ETHER_STAT_ADV_CAP_2500FDX,	"adv_cap_2500fdx",	NULL },
1513bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_2500FDX,	"lp_cap_2500fdx",	NULL },
1523bc4925dSGarrett D'Amore 
1533bc4925dSGarrett D'Amore 	{ ETHER_STAT_CAP_5000FDX,	"cap_5000fdx",		NULL },
1543bc4925dSGarrett D'Amore 	{ ETHER_STAT_ADV_CAP_5000FDX,	"adv_cap_5000fdx",	NULL },
1553bc4925dSGarrett D'Amore 	{ ETHER_STAT_LP_CAP_5000FDX,	"lp_cap_5000fdx",	NULL },
156*3d75a287SRobert Mustacchi 
157*3d75a287SRobert Mustacchi 	{ ETHER_STAT_CAP_25GFDX,	"cap_25gfdx",		NULL },
158*3d75a287SRobert Mustacchi 	{ ETHER_STAT_ADV_CAP_25GFDX,	"adv_cap_25gfdx",	NULL },
159*3d75a287SRobert Mustacchi 	{ ETHER_STAT_LP_CAP_25GFDX,	"lp_cap_25gfdx",	NULL },
160*3d75a287SRobert Mustacchi 
161*3d75a287SRobert Mustacchi 	{ ETHER_STAT_CAP_50GFDX,	"cap_50gfdx",		NULL },
162*3d75a287SRobert Mustacchi 	{ ETHER_STAT_ADV_CAP_50GFDX,	"adv_cap_50gfdx",	NULL },
163*3d75a287SRobert Mustacchi 	{ ETHER_STAT_LP_CAP_50GFDX,	"lp_cap_50gfdx",	NULL },
164d62bc4baSyz };
165d62bc4baSyz 
166aca118b7Slucy wang - Sun Microsystems - Beijing China #define	SOFTMAC_ETHER_SI_SZ						\
167aca118b7Slucy wang - Sun Microsystems - Beijing China 	(sizeof (i_softmac_ether_si) / sizeof (i_softmac_ether_si[0]))
168aca118b7Slucy wang - Sun Microsystems - Beijing China 
169d62bc4baSyz static kstat_t	*softmac_hold_dev_kstat(softmac_t *);
170d62bc4baSyz static void	softmac_rele_dev_kstat(kstat_t *);
171d62bc4baSyz static int	softmac_get_kstat(kstat_t *, char *, uint64_t *);
172d62bc4baSyz 
173d62bc4baSyz static kstat_t *
softmac_hold_dev_kstat(softmac_t * softmac)174d62bc4baSyz softmac_hold_dev_kstat(softmac_t *softmac)
175d62bc4baSyz {
176d62bc4baSyz 	char		drv[MAXLINKNAMELEN];
177d62bc4baSyz 	uint_t		ppa;
178d62bc4baSyz 	kstat_t		*ksp;
179d62bc4baSyz 
180d62bc4baSyz 	if (ddi_parse(softmac->smac_devname, drv, &ppa) != DDI_SUCCESS)
181d62bc4baSyz 		return (NULL);
182d62bc4baSyz 
183d62bc4baSyz 	/*
184d62bc4baSyz 	 * Find the kstat by the module name and the instance number.
185d62bc4baSyz 	 */
186d62bc4baSyz 	ksp = kstat_hold_byname(drv, ppa, softmac->smac_devname, ALL_ZONES);
187d62bc4baSyz 	if (ksp != NULL) {
188d62bc4baSyz 		KSTAT_ENTER(ksp);
189d62bc4baSyz 
190d62bc4baSyz 		if ((ksp->ks_data != NULL) &&
191d62bc4baSyz 		    (ksp->ks_type == KSTAT_TYPE_NAMED)) {
192d62bc4baSyz 			/*
193d62bc4baSyz 			 * Update the kstat to get the latest statistics.
194d62bc4baSyz 			 */
195d62bc4baSyz 			if (KSTAT_UPDATE(ksp, KSTAT_READ) == 0)
196d62bc4baSyz 				return (ksp);
197d62bc4baSyz 		}
198d62bc4baSyz 
199d62bc4baSyz 		KSTAT_EXIT(ksp);
200d62bc4baSyz 		kstat_rele(ksp);
201d62bc4baSyz 	}
202d62bc4baSyz 	return (NULL);
203d62bc4baSyz }
204d62bc4baSyz 
205d62bc4baSyz static void
softmac_rele_dev_kstat(kstat_t * ksp)206d62bc4baSyz softmac_rele_dev_kstat(kstat_t *ksp)
207d62bc4baSyz {
208d62bc4baSyz 	KSTAT_EXIT(ksp);
209d62bc4baSyz 	kstat_rele(ksp);
210d62bc4baSyz }
211d62bc4baSyz 
212d62bc4baSyz /*
213d62bc4baSyz  * The kstat needs to be held when calling this function.
214d62bc4baSyz  */
215d62bc4baSyz static int
softmac_get_kstat(kstat_t * ksp,char * name,uint64_t * valp)216d62bc4baSyz softmac_get_kstat(kstat_t *ksp, char *name, uint64_t *valp)
217d62bc4baSyz {
218d62bc4baSyz 	kstat_named_t	*knp;
219d62bc4baSyz 	int		i;
220d62bc4baSyz 	int		ret = ENOTSUP;
221d62bc4baSyz 
222d62bc4baSyz 	if (name == NULL)
223d62bc4baSyz 		return (ret);
224d62bc4baSyz 
225d62bc4baSyz 	/*
226d62bc4baSyz 	 * Search the kstat with the given name.
227d62bc4baSyz 	 */
228