xref: /illumos-gate/usr/src/uts/common/io/aggr/aggr_dev.c (revision 92a0208178405fef708b0283ffcaa02fbc3468ff)
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 /*
27  * IEEE 802.3ad Link Aggregation.
28  */
29 
30 #include <sys/conf.h>
31 #include <sys/modctl.h>
32 #include <sys/aggr.h>
33 #include <sys/aggr_impl.h>
34 
35 /* module description */
36 #define	AGGR_LINKINFO	"Link Aggregation MAC"
37 
38 /* device info ptr, only one for instance 0 */
39 dev_info_t *aggr_dip = NULL;
40 
41 static int aggr_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
42 static int aggr_attach(dev_info_t *, ddi_attach_cmd_t);
43 static int aggr_detach(dev_info_t *, ddi_detach_cmd_t);
44 
45 static struct cb_ops aggr_cb_ops = {
46 	nulldev,		/* open */
47 	nulldev,		/* close */
48 	nulldev,		/* strategy */
49 	nulldev,		/* print */
50 	nodev,			/* dump */
51 	nodev,			/* read */
52 	nodev,			/* write */
53 	nodev,			/* ioctl */
54 	nodev,			/* devmap */
55 	nodev,			/* mmap */
56 	nodev,			/* segmap */
57 	nochpoll,		/* poll */
58 	ddi_prop_op,		/* cb_prop_op */
59 	0,			/* streamtab  */
60 	D_MP			/* Driver compatibility flag */
61 };
62 
63 static struct dev_ops aggr_dev_ops = {
64 	DEVO_REV,		/* devo_rev */
65 	0,			/* refcnt */
66 	aggr_getinfo,		/* get_dev_info */
67 	nulldev,		/* identify */
68 	nulldev,		/* probe */
69 	aggr_attach,		/* attach */
70 	aggr_detach,		/* detach */
71 	nodev,			/* reset */
72 	&aggr_cb_ops,		/* driver operations */
73 	NULL,			/* bus operations */
74 	nodev			/* dev power */
75 };
76 
77 static struct modldrv aggr_modldrv = {
78 	&mod_driverops,		/* Type of module.  This one is a driver */
79 	AGGR_LINKINFO,		/* short description */
80 	&aggr_dev_ops		/* driver specific ops */
81 };
82 
83 static struct modlinkage modlinkage = {
84 	MODREV_1,
85 	&aggr_modldrv,
86 	NULL
87 };
88 
89 int
90 _init(void)
91 {
92 	int	err;
93 
94 	mac_init_ops(&aggr_dev_ops, "aggr");
95 	if ((err = mod_install(&modlinkage)) != 0)
96 		mac_fini_ops(&aggr_dev_ops);
97 	return (err);
98 }
99 
100 int
101 _fini(void)
102 {
103 	int	err;
104 
105 	if ((err = mod_remove(&modlinkage)) == 0)
106 		mac_fini_ops(&aggr_dev_ops);
107 	return (err);
108 }
109 
110 int
111 _info(struct modinfo *modinfop)
112 {
113 	return (mod_info(&modlinkage, modinfop));
114 }
115 
116 /*ARGSUSED*/
117 static int
118 aggr_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
119     void **result)
120 {
121 	switch (infocmd) {
122 	case DDI_INFO_DEVT2DEVINFO:
123 		*result = aggr_dip;
124 		return (DDI_SUCCESS);
125 	case DDI_INFO_DEVT2INSTANCE:
126 		*result = 0;
127 		return (DDI_SUCCESS);
128 	}
129 	return (DDI_FAILURE);
130 }
131 
132 static int
133 aggr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
134 {
135 	switch (cmd) {
136 	case DDI_ATTACH:
137 		if (ddi_get_instance(dip) != 0) {
138 			/* we only allow instance 0 to attach */
139 			return (DDI_FAILURE);
140 		}
141 		if (aggr_ioc_init() != 0)
142 			return (DDI_FAILURE);
143 		aggr_dip = dip;
144 		aggr_port_init();
145 		aggr_grp_init();
146 		aggr_lacp_init();
147 		return (DDI_SUCCESS);
148 
149 	case DDI_RESUME:
150 		return (DDI_SUCCESS);
151 
152 	default:
153 		return (DDI_FAILURE);
154 	}
155 }
156 
157 /*ARGSUSED*/
158 static int
159 aggr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
160 {
161 	switch (cmd) {
162 	case DDI_DETACH:
163 		if (aggr_grp_count() > 0)
164 			return (DDI_FAILURE);
165 
166 		aggr_dip = NULL;
167 		aggr_port_fini();
168 		aggr_grp_fini();
169 		aggr_lacp_fini();
170 		aggr_ioc_fini();
171 		return (DDI_SUCCESS);
172 
173 	case DDI_SUSPEND:
174 		return (DDI_SUCCESS);
175 
176 	default:
177 		return (DDI_FAILURE);
178 	}
179 }
180