1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Data-Link Services Module
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/sysmacros.h>
35*0Sstevel@tonic-gate #include <sys/atomic.h>
36*0Sstevel@tonic-gate #include <sys/ght.h>
37*0Sstevel@tonic-gate #include <sys/kstat.h>
38*0Sstevel@tonic-gate #include <sys/vlan.h>
39*0Sstevel@tonic-gate #include <sys/mac.h>
40*0Sstevel@tonic-gate #include <sys/ctype.h>
41*0Sstevel@tonic-gate #include <sys/dls.h>
42*0Sstevel@tonic-gate #include <sys/dls_impl.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate typedef struct i_dls_stat_info_s {
45*0Sstevel@tonic-gate 	enum mac_stat	dsi_stat;
46*0Sstevel@tonic-gate 	char		*dsi_name;
47*0Sstevel@tonic-gate 	uint_t		dsi_type;
48*0Sstevel@tonic-gate } i_mac_stat_info_t;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static i_mac_stat_info_t	i_dls_si[] = {
51*0Sstevel@tonic-gate 	{ MAC_STAT_IFSPEED, "ifspeed", KSTAT_DATA_UINT64 },
52*0Sstevel@tonic-gate 	{ MAC_STAT_MULTIRCV, "multircv", KSTAT_DATA_UINT32 },
53*0Sstevel@tonic-gate 	{ MAC_STAT_BRDCSTRCV, "brdcstrcv", KSTAT_DATA_UINT32 },
54*0Sstevel@tonic-gate 	{ MAC_STAT_MULTIXMT, "multixmt", KSTAT_DATA_UINT32 },
55*0Sstevel@tonic-gate 	{ MAC_STAT_BRDCSTXMT, "brdcstxmt", KSTAT_DATA_UINT32 },
56*0Sstevel@tonic-gate 	{ MAC_STAT_NORCVBUF, "norcvbuf", KSTAT_DATA_UINT32 },
57*0Sstevel@tonic-gate 	{ MAC_STAT_IERRORS, "ierrors", KSTAT_DATA_UINT32 },
58*0Sstevel@tonic-gate 	{ MAC_STAT_NOXMTBUF, "noxmtbuf", KSTAT_DATA_UINT32 },
59*0Sstevel@tonic-gate 	{ MAC_STAT_OERRORS, "oerrors", KSTAT_DATA_UINT32 },
60*0Sstevel@tonic-gate 	{ MAC_STAT_COLLISIONS, "collisions", KSTAT_DATA_UINT32 },
61*0Sstevel@tonic-gate 	{ MAC_STAT_RBYTES, "rbytes", KSTAT_DATA_UINT32 },
62*0Sstevel@tonic-gate 	{ MAC_STAT_IPACKETS, "ipackets", KSTAT_DATA_UINT32 },
63*0Sstevel@tonic-gate 	{ MAC_STAT_OBYTES, "obytes", KSTAT_DATA_UINT32 },
64*0Sstevel@tonic-gate 	{ MAC_STAT_OPACKETS, "opackets", KSTAT_DATA_UINT32 },
65*0Sstevel@tonic-gate 	{ MAC_STAT_RBYTES, "rbytes64", KSTAT_DATA_UINT64 },
66*0Sstevel@tonic-gate 	{ MAC_STAT_IPACKETS, "ipackets64", KSTAT_DATA_UINT64 },
67*0Sstevel@tonic-gate 	{ MAC_STAT_OBYTES, "obytes64", KSTAT_DATA_UINT64 },
68*0Sstevel@tonic-gate 	{ MAC_STAT_OPACKETS, "opackets64", KSTAT_DATA_UINT64 }
69*0Sstevel@tonic-gate };
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #define	STAT_INFO_COUNT	(sizeof (i_dls_si) / sizeof (i_dls_si[0]))
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Private functions.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int
78*0Sstevel@tonic-gate i_dls_stat_update(kstat_t *ksp, int rw)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	dls_vlan_t	*dvp = ksp->ks_private;
81*0Sstevel@tonic-gate 	dls_link_t	*dlp = dvp->dv_dlp;
82*0Sstevel@tonic-gate 	kstat_named_t	*knp;
83*0Sstevel@tonic-gate 	uint_t		i;
84*0Sstevel@tonic-gate 	uint64_t	val;
85*0Sstevel@tonic-gate 	int		err;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (rw != KSTAT_READ)
88*0Sstevel@tonic-gate 		return (EACCES);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	if ((err = dls_mac_hold(dlp)) != 0)
91*0Sstevel@tonic-gate 		return (err);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	knp = (kstat_named_t *)ksp->ks_data;
94*0Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
95*0Sstevel@tonic-gate 		if (!(dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat]))
96*0Sstevel@tonic-gate 			continue;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		val = mac_stat_get(dlp->dl_mh, i_dls_si[i].dsi_stat);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		switch (i_dls_si[i].dsi_type) {
101*0Sstevel@tonic-gate 		case KSTAT_DATA_UINT64:
102*0Sstevel@tonic-gate 			knp->value.ui64 = val;
103*0Sstevel@tonic-gate 			break;
104*0Sstevel@tonic-gate 		case KSTAT_DATA_UINT32:
105*0Sstevel@tonic-gate 			knp->value.ui32 = (uint32_t)val;
106*0Sstevel@tonic-gate 			break;
107*0Sstevel@tonic-gate 		default:
108*0Sstevel@tonic-gate 			ASSERT(B_FALSE);
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		knp++;
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	knp->value.ui32 = dlp->dl_unknowns;
115*0Sstevel@tonic-gate 	dls_mac_rele(dlp);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (0);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate  * Exported functions.
122*0Sstevel@tonic-gate  */
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate void
125*0Sstevel@tonic-gate dls_stat_create(dls_vlan_t *dvp)
126*0Sstevel@tonic-gate {
127*0Sstevel@tonic-gate 	dls_link_t		*dlp = dvp->dv_dlp;
128*0Sstevel@tonic-gate 	char			module[IFNAMSIZ];
129*0Sstevel@tonic-gate 	uint_t			instance;
130*0Sstevel@tonic-gate 	kstat_t			*ksp;
131*0Sstevel@tonic-gate 	kstat_named_t		*knp;
132*0Sstevel@tonic-gate 	uint_t			i;
133*0Sstevel@tonic-gate 	uint_t			count;
134*0Sstevel@tonic-gate 	int			err;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (dls_mac_hold(dlp) != 0)
137*0Sstevel@tonic-gate 		return;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	count = 0;
140*0Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
141*0Sstevel@tonic-gate 		if (dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat])
142*0Sstevel@tonic-gate 			count++;
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	err = ddi_parse(dvp->dv_name, module, &instance);
146*0Sstevel@tonic-gate 	ASSERT(err == DDI_SUCCESS);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if ((ksp = kstat_create(module, instance, NULL, "net",
149*0Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, count + 1, 0)) == NULL)
150*0Sstevel@tonic-gate 		goto done;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	ksp->ks_update = i_dls_stat_update;
153*0Sstevel@tonic-gate 	ksp->ks_private = (void *)dvp;
154*0Sstevel@tonic-gate 	dvp->dv_ksp = ksp;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	knp = (kstat_named_t *)ksp->ks_data;
157*0Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
158*0Sstevel@tonic-gate 		if (!(dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat]))
159*0Sstevel@tonic-gate 			continue;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		kstat_named_init(knp, i_dls_si[i].dsi_name,
162*0Sstevel@tonic-gate 		    i_dls_si[i].dsi_type);
163*0Sstevel@tonic-gate 		knp++;
164*0Sstevel@tonic-gate 		--count;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	ASSERT(count == 0);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	kstat_named_init(knp, "unknowns", KSTAT_DATA_UINT32);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	kstat_install(ksp);
171*0Sstevel@tonic-gate done:
172*0Sstevel@tonic-gate 	dls_mac_rele(dlp);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate void
176*0Sstevel@tonic-gate dls_stat_destroy(dls_vlan_t *dvp)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	kstat_delete(dvp->dv_ksp);
179*0Sstevel@tonic-gate 	dvp->dv_ksp = NULL;
180*0Sstevel@tonic-gate }
181