xref: /freebsd/sys/dev/etherswitch/etherswitch.c (revision a05a680469a7ac77b195021fed74e3aa58152dd7)
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 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/sx.h>
38 #include <sys/systm.h>
39 #include <sys/uio.h>
40 
41 #include <net/if.h>
42 
43 #include <dev/etherswitch/etherswitch.h>
44 
45 #include "etherswitch_if.h"
46 
47 struct etherswitch_softc {
48 	device_t sc_dev;
49 	struct cdev *sc_devnode;
50 };
51 
52 static int etherswitch_probe(device_t);
53 static int etherswitch_attach(device_t);
54 static int etherswitch_detach(device_t);
55 static void etherswitch_identify(driver_t *driver, device_t parent);
56 
57 static device_method_t etherswitch_methods[] = {
58 	/* device interface */
59 	DEVMETHOD(device_identify,	etherswitch_identify),
60 	DEVMETHOD(device_probe,		etherswitch_probe),
61 	DEVMETHOD(device_attach,	etherswitch_attach),
62 	DEVMETHOD(device_detach,	etherswitch_detach),
63 
64 	DEVMETHOD_END
65 };
66 
67 driver_t etherswitch_driver = {
68 	"etherswitch",
69 	etherswitch_methods,
70 	sizeof(struct etherswitch_softc),
71 };
72 
73 static	d_ioctl_t	etherswitchioctl;
74 
75 static struct cdevsw etherswitch_cdevsw = {
76 	.d_version =	D_VERSION,
77 	.d_flags =	D_TRACKCLOSE,
78 	.d_ioctl =	etherswitchioctl,
79 	.d_name =	"etherswitch",
80 };
81 
82 static void
83 etherswitch_identify(driver_t *driver, device_t parent)
84 {
85 	if (device_find_child(parent, "etherswitch", -1) == NULL)
86 		BUS_ADD_CHILD(parent, 0, "etherswitch", DEVICE_UNIT_ANY);
87 }
88 
89 static int
90 etherswitch_probe(device_t dev)
91 {
92 	device_set_desc(dev, "Switch controller");
93 
94 	return (0);
95 }
96 
97 static int
98 etherswitch_attach(device_t dev)
99 {
100 	int err;
101 	struct etherswitch_softc *sc;
102 	struct make_dev_args devargs;
103 
104 	sc = device_get_softc(dev);
105 	sc->sc_dev = dev;
106 	make_dev_args_init(&devargs);
107 	devargs.mda_devsw = &etherswitch_cdevsw;
108 	devargs.mda_uid = UID_ROOT;
109 	devargs.mda_gid = GID_WHEEL;
110 	devargs.mda_mode = 0600;
111 	devargs.mda_si_drv1 = sc;
112 	err = make_dev_s(&devargs, &sc->sc_devnode, "etherswitch%d",
113 	    device_get_unit(dev));
114 	if (err != 0) {
115 		device_printf(dev, "failed to create character device\n");
116 		return (ENXIO);
117 	}
118 
119 	return (0);
120 }
121 
122 static int
123 etherswitch_detach(device_t dev)
124 {
125 	struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev);
126 
127 	if (sc->sc_devnode)
128 		destroy_dev(sc->sc_devnode);
129 
130 	return (0);
131 }
132 
133 static int
134 etherswitchioctl(struct cdev *cdev, u_long cmd, caddr_t data, int flags, struct thread *td)
135 {
136 	struct etherswitch_softc *sc = cdev->si_drv1;
137 	device_t dev = sc->sc_dev;
138 	device_t etherswitch = device_get_parent(dev);
139 	etherswitch_conf_t conf;
140 	etherswitch_info_t *info;
141 	etherswitch_reg_t *reg;
142 	etherswitch_phyreg_t *phyreg;
143 	etherswitch_portid_t *portid;
144 	int error = 0;
145 
146 	switch (cmd) {
147 	case IOETHERSWITCHGETINFO:
148 		info = ETHERSWITCH_GETINFO(etherswitch);
149 		bcopy(info, data, sizeof(etherswitch_info_t));
150 		break;
151 
152 	case IOETHERSWITCHGETREG:
153 		reg = (etherswitch_reg_t *)data;
154 		ETHERSWITCH_LOCK(etherswitch);
155 		reg->val = ETHERSWITCH_READREG(etherswitch, reg->reg);
156 		ETHERSWITCH_UNLOCK(etherswitch);
157 		break;
158 
159 	case IOETHERSWITCHSETREG:
160 		reg = (etherswitch_reg_t *)data;
161 		ETHERSWITCH_LOCK(etherswitch);
162 		error = ETHERSWITCH_WRITEREG(etherswitch, reg->reg, reg->val);
163 		ETHERSWITCH_UNLOCK(etherswitch);
164 		break;
165 
166 	case IOETHERSWITCHGETPORT:
167 		error = ETHERSWITCH_GETPORT(etherswitch, (etherswitch_port_t *)data);
168 		break;
169 
170 	case IOETHERSWITCHSETPORT:
171 		error = ETHERSWITCH_SETPORT(etherswitch, (etherswitch_port_t *)data);
172 		break;
173 
174 	case IOETHERSWITCHGETVLANGROUP:
175 		error = ETHERSWITCH_GETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
176 		break;
177 
178 	case IOETHERSWITCHSETVLANGROUP:
179 		error = ETHERSWITCH_SETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
180 		break;
181 
182 	case IOETHERSWITCHGETPHYREG:
183 		phyreg = (etherswitch_phyreg_t *)data;
184 		phyreg->val = ETHERSWITCH_READPHYREG(etherswitch, phyreg->phy, phyreg->reg);
185 		break;
186 
187 	case IOETHERSWITCHSETPHYREG:
188 		phyreg = (etherswitch_phyreg_t *)data;
189 		error = ETHERSWITCH_WRITEPHYREG(etherswitch, phyreg->phy, phyreg->reg, phyreg->val);
190 		break;
191 
192 	case IOETHERSWITCHGETCONF:
193 		bzero(&conf, sizeof(etherswitch_conf_t));
194 		error = ETHERSWITCH_GETCONF(etherswitch, &conf);
195 		bcopy(&conf, data, sizeof(etherswitch_conf_t));
196 		break;
197 
198 	case IOETHERSWITCHSETCONF:
199 		error = ETHERSWITCH_SETCONF(etherswitch, (etherswitch_conf_t *)data);
200 		break;
201 
202 	case IOETHERSWITCHFLUSHALL:
203 		error = ETHERSWITCH_FLUSH_ALL(etherswitch);
204 		break;
205 
206 	case IOETHERSWITCHFLUSHPORT:
207 		portid = (etherswitch_portid_t *)data;
208 		error = ETHERSWITCH_FLUSH_PORT(etherswitch, portid->es_port);
209 		break;
210 
211 	case IOETHERSWITCHGETTABLE:
212 		error = ETHERSWITCH_FETCH_TABLE(etherswitch, (void *) data);
213 		break;
214 
215 	case IOETHERSWITCHGETTABLEENTRY:
216 		error = ETHERSWITCH_FETCH_TABLE_ENTRY(etherswitch, (void *) data);
217 		break;
218 
219 	default:
220 		error = ENOTTY;
221 	}
222 
223 	return (error);
224 }
225 
226 MODULE_VERSION(etherswitch, 1);
227