xref: /freebsd/sys/dev/etherswitch/etherswitch.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011-2012 Stefan Bethke.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sx.h>
40 #include <sys/systm.h>
41 #include <sys/uio.h>
42 
43 #include <net/if.h>
44 
45 #include <dev/etherswitch/etherswitch.h>
46 
47 #include "etherswitch_if.h"
48 
49 struct etherswitch_softc {
50 	device_t sc_dev;
51 	struct cdev *sc_devnode;
52 };
53 
54 static int etherswitch_probe(device_t);
55 static int etherswitch_attach(device_t);
56 static int etherswitch_detach(device_t);
57 static void etherswitch_identify(driver_t *driver, device_t parent);
58 
59 static device_method_t etherswitch_methods[] = {
60 	/* device interface */
61 	DEVMETHOD(device_identify,	etherswitch_identify),
62 	DEVMETHOD(device_probe,		etherswitch_probe),
63 	DEVMETHOD(device_attach,	etherswitch_attach),
64 	DEVMETHOD(device_detach,	etherswitch_detach),
65 
66 	DEVMETHOD_END
67 };
68 
69 driver_t etherswitch_driver = {
70 	"etherswitch",
71 	etherswitch_methods,
72 	sizeof(struct etherswitch_softc),
73 };
74 
75 static	d_ioctl_t	etherswitchioctl;
76 
77 static struct cdevsw etherswitch_cdevsw = {
78 	.d_version =	D_VERSION,
79 	.d_flags =	D_TRACKCLOSE,
80 	.d_ioctl =	etherswitchioctl,
81 	.d_name =	"etherswitch",
82 };
83 
84 static void
85 etherswitch_identify(driver_t *driver, device_t parent)
86 {
87 	if (device_find_child(parent, "etherswitch", -1) == NULL)
88 		BUS_ADD_CHILD(parent, 0, "etherswitch", -1);
89 }
90 
91 static int
92 etherswitch_probe(device_t dev)
93 {
94 	device_set_desc(dev, "Switch controller");
95 
96 	return (0);
97 }
98 
99 static int
100 etherswitch_attach(device_t dev)
101 {
102 	int err;
103 	struct etherswitch_softc *sc;
104 	struct make_dev_args devargs;
105 
106 	sc = device_get_softc(dev);
107 	sc->sc_dev = dev;
108 	make_dev_args_init(&devargs);
109 	devargs.mda_devsw = &etherswitch_cdevsw;
110 	devargs.mda_uid = UID_ROOT;
111 	devargs.mda_gid = GID_WHEEL;
112 	devargs.mda_mode = 0600;
113 	devargs.mda_si_drv1 = sc;
114 	err = make_dev_s(&devargs, &sc->sc_devnode, "etherswitch%d",
115 	    device_get_unit(dev));
116 	if (err != 0) {
117 		device_printf(dev, "failed to create character device\n");
118 		return (ENXIO);
119 	}
120 
121 	return (0);
122 }
123 
124 static int
125 etherswitch_detach(device_t dev)
126 {
127 	struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev);
128 
129 	if (sc->sc_devnode)
130 		destroy_dev(sc->sc_devnode);
131 
132 	return (0);
133 }
134 
135 static int
136 etherswitchioctl(struct cdev *cdev, u_long cmd, caddr_t data, int flags, struct thread *td)
137 {
138 	struct etherswitch_softc *sc = cdev->si_drv1;
139 	device_t dev = sc->sc_dev;
140 	device_t etherswitch = device_get_parent(dev);
141 	etherswitch_conf_t conf;
142 	etherswitch_info_t *info;
143 	etherswitch_reg_t *reg;
144 	etherswitch_phyreg_t *phyreg;
145 	etherswitch_portid_t *portid;
146 	int error = 0;
147 
148 	switch (cmd) {
149 	case IOETHERSWITCHGETINFO:
150 		info = ETHERSWITCH_GETINFO(etherswitch);
151 		bcopy(info, data, sizeof(etherswitch_info_t));
152 		break;
153 
154 	case IOETHERSWITCHGETREG:
155 		reg = (etherswitch_reg_t *)data;
156 		ETHERSWITCH_LOCK(etherswitch);
157 		reg->val = ETHERSWITCH_READREG(etherswitch, reg->reg);
158 		ETHERSWITCH_UNLOCK(etherswitch);
159 		break;
160 
161 	case IOETHERSWITCHSETREG:
162 		reg = (etherswitch_reg_t *)data;
163 		ETHERSWITCH_LOCK(etherswitch);
164 		error = ETHERSWITCH_WRITEREG(etherswitch, reg->reg, reg->val);
165 		ETHERSWITCH_UNLOCK(etherswitch);
166 		break;
167 
168 	case IOETHERSWITCHGETPORT:
169 		error = ETHERSWITCH_GETPORT(etherswitch, (etherswitch_port_t *)data);
170 		break;
171 
172 	case IOETHERSWITCHSETPORT:
173 		error = ETHERSWITCH_SETPORT(etherswitch, (etherswitch_port_t *)data);
174 		break;
175 
176 	case IOETHERSWITCHGETVLANGROUP:
177 		error = ETHERSWITCH_GETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
178 		break;
179 
180 	case IOETHERSWITCHSETVLANGROUP:
181 		error = ETHERSWITCH_SETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
182 		break;
183 
184 	case IOETHERSWITCHGETPHYREG:
185 		phyreg = (etherswitch_phyreg_t *)data;
186 		phyreg->val = ETHERSWITCH_READPHYREG(etherswitch, phyreg->phy, phyreg->reg);
187 		break;
188 
189 	case IOETHERSWITCHSETPHYREG:
190 		phyreg = (etherswitch_phyreg_t *)data;
191 		error = ETHERSWITCH_WRITEPHYREG(etherswitch, phyreg->phy, phyreg->reg, phyreg->val);
192 		break;
193 
194 	case IOETHERSWITCHGETCONF:
195 		bzero(&conf, sizeof(etherswitch_conf_t));
196 		error = ETHERSWITCH_GETCONF(etherswitch, &conf);
197 		bcopy(&conf, data, sizeof(etherswitch_conf_t));
198 		break;
199 
200 	case IOETHERSWITCHSETCONF:
201 		error = ETHERSWITCH_SETCONF(etherswitch, (etherswitch_conf_t *)data);
202 		break;
203 
204 	case IOETHERSWITCHFLUSHALL:
205 		error = ETHERSWITCH_FLUSH_ALL(etherswitch);
206 		break;
207 
208 	case IOETHERSWITCHFLUSHPORT:
209 		portid = (etherswitch_portid_t *)data;
210 		error = ETHERSWITCH_FLUSH_PORT(etherswitch, portid->es_port);
211 		break;
212 
213 	case IOETHERSWITCHGETTABLE:
214 		error = ETHERSWITCH_FETCH_TABLE(etherswitch, (void *) data);
215 		break;
216 
217 	case IOETHERSWITCHGETTABLEENTRY:
218 		error = ETHERSWITCH_FETCH_TABLE_ENTRY(etherswitch, (void *) data);
219 		break;
220 
221 	default:
222 		error = ENOTTY;
223 	}
224 
225 	return (error);
226 }
227 
228 MODULE_VERSION(etherswitch, 1);
229