xref: /freebsd/sys/dev/etherswitch/arswitch/arswitch.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*-
2  * Copyright (c) 2011-2012 Stefan Bethke.
3  * Copyright (c) 2012 Adrian Chadd.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/errno.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_arp.h>
44 #include <net/ethernet.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.h>
48 
49 #include <machine/bus.h>
50 #include <dev/iicbus/iic.h>
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/iicbus/iicbus.h>
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/etherswitch/mdio.h>
56 
57 #include <dev/etherswitch/etherswitch.h>
58 
59 #include <dev/etherswitch/arswitch/arswitchreg.h>
60 #include <dev/etherswitch/arswitch/arswitchvar.h>
61 #include <dev/etherswitch/arswitch/arswitch_reg.h>
62 #include <dev/etherswitch/arswitch/arswitch_phy.h>
63 #include <dev/etherswitch/arswitch/arswitch_vlans.h>
64 
65 #include <dev/etherswitch/arswitch/arswitch_7240.h>
66 #include <dev/etherswitch/arswitch/arswitch_8216.h>
67 #include <dev/etherswitch/arswitch/arswitch_8226.h>
68 #include <dev/etherswitch/arswitch/arswitch_8316.h>
69 #include <dev/etherswitch/arswitch/arswitch_9340.h>
70 
71 #include "mdio_if.h"
72 #include "miibus_if.h"
73 #include "etherswitch_if.h"
74 
75 #if	defined(DEBUG)
76 static SYSCTL_NODE(_debug, OID_AUTO, arswitch, CTLFLAG_RD, 0, "arswitch");
77 #endif
78 
79 static inline int arswitch_portforphy(int phy);
80 static void arswitch_tick(void *arg);
81 static int arswitch_ifmedia_upd(struct ifnet *);
82 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *);
83 
84 static int
85 arswitch_probe(device_t dev)
86 {
87 	struct arswitch_softc *sc;
88 	uint32_t id;
89 	char *chipname, desc[256];
90 
91 	sc = device_get_softc(dev);
92 	bzero(sc, sizeof(*sc));
93 	sc->page = -1;
94 
95 	/* AR7240 probe */
96 	if (ar7240_probe(dev) == 0) {
97 		chipname = "AR7240";
98 		sc->sc_switchtype = AR8X16_SWITCH_AR7240;
99 		sc->is_internal_switch = 1;
100 		id = 0;
101 		goto done;
102 	}
103 
104 	/* AR9340 probe */
105 	if (ar9340_probe(dev) == 0) {
106 		chipname = "AR9340";
107 		sc->sc_switchtype = AR8X16_SWITCH_AR9340;
108 		sc->is_internal_switch = 1;
109 		id = 0;
110 		goto done;
111 	}
112 
113 	/* AR8xxx probe */
114 	id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL);
115 	switch (id & (AR8X16_MASK_CTRL_VER_MASK | AR8X16_MASK_CTRL_REV_MASK)) {
116 	case 0x0101:
117 		chipname = "AR8216";
118 		sc->sc_switchtype = AR8X16_SWITCH_AR8216;
119 		break;
120 	case 0x0201:
121 		chipname = "AR8226";
122 		sc->sc_switchtype = AR8X16_SWITCH_AR8226;
123 		break;
124 	/* 0x0301 - AR8236 */
125 	case 0x1000:
126 	case 0x1001:
127 		chipname = "AR8316";
128 		sc->sc_switchtype = AR8X16_SWITCH_AR8316;
129 		break;
130 	default:
131 		chipname = NULL;
132 	}
133 
134 done:
135 
136 	DPRINTF(dev, "chipname=%s, id=%08x\n", chipname, id);
137 	if (chipname != NULL) {
138 		snprintf(desc, sizeof(desc),
139 		    "Atheros %s Ethernet Switch",
140 		    chipname);
141 		device_set_desc_copy(dev, desc);
142 		return (BUS_PROBE_DEFAULT);
143 	}
144 	return (ENXIO);
145 }
146 
147 static int
148 arswitch_attach_phys(struct arswitch_softc *sc)
149 {
150 	int phy, err = 0;
151 	char name[IFNAMSIZ];
152 
153 	/* PHYs need an interface, so we generate a dummy one */
154 	snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
155 	for (phy = 0; phy < sc->numphys; phy++) {
156 		sc->ifp[phy] = if_alloc(IFT_ETHER);
157 		sc->ifp[phy]->if_softc = sc;
158 		sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
159 		    IFF_DRV_RUNNING | IFF_SIMPLEX;
160 		sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
161 		bcopy(name, sc->ifname[phy], strlen(name)+1);
162 		if_initname(sc->ifp[phy], sc->ifname[phy],
163 		    arswitch_portforphy(phy));
164 		err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
165 		    arswitch_ifmedia_upd, arswitch_ifmedia_sts, \
166 		    BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
167 		DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
168 		    device_get_nameunit(sc->miibus[phy]),
169 		    sc->ifp[phy]->if_xname);
170 		if (err != 0) {
171 			device_printf(sc->sc_dev,
172 			    "attaching PHY %d failed\n",
173 			    phy);
174 		}
175 	}
176 	return (err);
177 }
178 
179 static int
180 arswitch_reset(device_t dev)
181 {
182 
183 	arswitch_writereg(dev, AR8X16_REG_MASK_CTRL,
184 	    AR8X16_MASK_CTRL_SOFT_RESET);
185 	DELAY(1000);
186 	if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) &
187 	    AR8X16_MASK_CTRL_SOFT_RESET) {
188 		device_printf(dev, "unable to reset switch\n");
189 		return (-1);
190 	}
191 	return (0);
192 }
193 
194 static int
195 arswitch_set_vlan_mode(struct arswitch_softc *sc, uint32_t mode)
196 {
197 
198 	/* Check for invalid modes. */
199 	if ((mode & sc->info.es_vlan_caps) != mode)
200 		return (EINVAL);
201 
202 	switch (mode) {
203 	case ETHERSWITCH_VLAN_DOT1Q:
204 		sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
205 		break;
206 	case ETHERSWITCH_VLAN_PORT:
207 		sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
208 		break;
209 	default:
210 		sc->vlan_mode = 0;
211 	};
212 
213 	/* Reset VLANs. */
214 	arswitch_reset_vlans(sc);
215 
216 	return (0);
217 }
218 
219 static void
220 arswitch_ports_init(struct arswitch_softc *sc)
221 {
222 	int port;
223 
224 	/* Port0 - CPU */
225 	arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(0),
226 	    (AR8X16_IS_SWITCH(sc, AR8216) ?
227 	    AR8X16_PORT_STS_SPEED_100 : AR8X16_PORT_STS_SPEED_1000) |
228 	    (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_RXFLOW) |
229 	    (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_TXFLOW) |
230 	    AR8X16_PORT_STS_RXMAC |
231 	    AR8X16_PORT_STS_TXMAC |
232 	    AR8X16_PORT_STS_DUPLEX);
233 	arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0),
234 	    arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0)) &
235 	    ~AR8X16_PORT_CTRL_HEADER);
236 
237 	for (port = 1; port <= sc->numphys; port++) {
238 		/* Set ports to auto negotiation. */
239 		arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(port),
240 		    AR8X16_PORT_STS_LINK_AUTO);
241 		arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port),
242 		    arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port)) &
243 		    ~AR8X16_PORT_CTRL_HEADER);
244 	}
245 }
246 
247 static int
248 arswitch_attach(device_t dev)
249 {
250 	struct arswitch_softc *sc;
251 	int err = 0;
252 
253 	sc = device_get_softc(dev);
254 
255 	/* sc->sc_switchtype is already decided in arswitch_probe() */
256 	sc->sc_dev = dev;
257 	mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF);
258 	sc->page = -1;
259 	strlcpy(sc->info.es_name, device_get_desc(dev),
260 	    sizeof(sc->info.es_name));
261 
262 	/*
263 	 * Attach switch related functions
264 	 */
265 	if (AR8X16_IS_SWITCH(sc, AR7240))
266 		ar7240_attach(sc);
267 	else if (AR8X16_IS_SWITCH(sc, AR9340))
268 		ar9340_attach(sc);
269 	else if (AR8X16_IS_SWITCH(sc, AR8216))
270 		ar8216_attach(sc);
271 	else if (AR8X16_IS_SWITCH(sc, AR8226))
272 		ar8226_attach(sc);
273 	else if (AR8X16_IS_SWITCH(sc, AR8316))
274 		ar8316_attach(sc);
275 	else
276 		return (ENXIO);
277 
278 	/* Common defaults. */
279 	sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */
280 
281 	/* XXX Defaults for externally connected AR8316 */
282 	sc->numphys = 4;
283 	sc->phy4cpu = 1;
284 	sc->is_rgmii = 1;
285 	sc->is_gmii = 0;
286 	sc->is_mii = 0;
287 
288 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
289 	    "numphys", &sc->numphys);
290 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
291 	    "phy4cpu", &sc->phy4cpu);
292 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
293 	    "is_rgmii", &sc->is_rgmii);
294 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
295 	    "is_gmii", &sc->is_gmii);
296 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
297 	    "is_mii", &sc->is_mii);
298 
299 	if (sc->numphys > AR8X16_NUM_PHYS)
300 		sc->numphys = AR8X16_NUM_PHYS;
301 
302 	/* Reset the switch. */
303 	if (arswitch_reset(dev))
304 		return (ENXIO);
305 
306 	err = sc->hal.arswitch_hw_setup(sc);
307 	if (err != 0)
308 		return (err);
309 
310 	err = sc->hal.arswitch_hw_global_setup(sc);
311 	if (err != 0)
312 		return (err);
313 
314 	/* Initialize the switch ports. */
315 	arswitch_ports_init(sc);
316 
317 	/*
318 	 * Attach the PHYs and complete the bus enumeration.
319 	 */
320 	err = arswitch_attach_phys(sc);
321 	if (err != 0)
322 		return (err);
323 
324 	/* Default to ingress filters off. */
325 	err = arswitch_set_vlan_mode(sc, 0);
326 	if (err != 0)
327 		return (err);
328 
329 	bus_generic_probe(dev);
330 	bus_enumerate_hinted_children(dev);
331 	err = bus_generic_attach(dev);
332 	if (err != 0)
333 		return (err);
334 
335 	callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
336 
337 	ARSWITCH_LOCK(sc);
338 	arswitch_tick(sc);
339 	ARSWITCH_UNLOCK(sc);
340 
341 	return (err);
342 }
343 
344 static int
345 arswitch_detach(device_t dev)
346 {
347 	struct arswitch_softc *sc = device_get_softc(dev);
348 	int i;
349 
350 	callout_drain(&sc->callout_tick);
351 
352 	for (i=0; i < sc->numphys; i++) {
353 		if (sc->miibus[i] != NULL)
354 			device_delete_child(dev, sc->miibus[i]);
355 		if (sc->ifp[i] != NULL)
356 			if_free(sc->ifp[i]);
357 		free(sc->ifname[i], M_DEVBUF);
358 	}
359 
360 	bus_generic_detach(dev);
361 	mtx_destroy(&sc->sc_mtx);
362 
363 	return (0);
364 }
365 
366 /*
367  * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to
368  * port 2, etc.
369  */
370 static inline int
371 arswitch_portforphy(int phy)
372 {
373 	return (phy+1);
374 }
375 
376 static inline struct mii_data *
377 arswitch_miiforport(struct arswitch_softc *sc, int port)
378 {
379 	int phy = port-1;
380 
381 	if (phy < 0 || phy >= sc->numphys)
382 		return (NULL);
383 	return (device_get_softc(sc->miibus[phy]));
384 }
385 
386 static inline struct ifnet *
387 arswitch_ifpforport(struct arswitch_softc *sc, int port)
388 {
389 	int phy = port-1;
390 
391 	if (phy < 0 || phy >= sc->numphys)
392 		return (NULL);
393 	return (sc->ifp[phy]);
394 }
395 
396 /*
397  * Convert port status to ifmedia.
398  */
399 static void
400 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
401 {
402 	*media_active = IFM_ETHER;
403 	*media_status = IFM_AVALID;
404 
405 	if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0)
406 		*media_status |= IFM_ACTIVE;
407 	else {
408 		*media_active |= IFM_NONE;
409 		return;
410 	}
411 	switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) {
412 	case AR8X16_PORT_STS_SPEED_10:
413 		*media_active |= IFM_10_T;
414 		break;
415 	case AR8X16_PORT_STS_SPEED_100:
416 		*media_active |= IFM_100_TX;
417 		break;
418 	case AR8X16_PORT_STS_SPEED_1000:
419 		*media_active |= IFM_1000_T;
420 		break;
421 	}
422 	if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0)
423 		*media_active |= IFM_FDX;
424 	else
425 		*media_active |= IFM_HDX;
426 	if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0)
427 		*media_active |= IFM_ETH_TXPAUSE;
428 	if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0)
429 		*media_active |= IFM_ETH_RXPAUSE;
430 }
431 
432 /*
433  * Poll the status for all PHYs.  We're using the switch port status because
434  * thats a lot quicker to read than talking to all the PHYs.  Care must be
435  * taken that the resulting ifmedia_active is identical to what the PHY will
436  * compute, or gratuitous link status changes will occur whenever the PHYs
437  * update function is called.
438  */
439 static void
440 arswitch_miipollstat(struct arswitch_softc *sc)
441 {
442 	int i;
443 	struct mii_data *mii;
444 	struct mii_softc *miisc;
445 	int portstatus;
446 
447 	ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
448 
449 	for (i = 0; i < sc->numphys; i++) {
450 		if (sc->miibus[i] == NULL)
451 			continue;
452 		mii = device_get_softc(sc->miibus[i]);
453 		portstatus = arswitch_readreg(sc->sc_dev,
454 		    AR8X16_REG_PORT_STS(arswitch_portforphy(i)));
455 #if 0
456 		DPRINTF(sc->sc_dev, "p[%d]=%b\n",
457 		    i,
458 		    portstatus,
459 		    "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7"
460 		    "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE");
461 #endif
462 		arswitch_update_ifmedia(portstatus, &mii->mii_media_status,
463 		    &mii->mii_media_active);
464 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
465 			if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
466 			    miisc->mii_inst)
467 				continue;
468 			mii_phy_update(miisc, MII_POLLSTAT);
469 		}
470 	}
471 }
472 
473 static void
474 arswitch_tick(void *arg)
475 {
476 	struct arswitch_softc *sc = arg;
477 
478 	arswitch_miipollstat(sc);
479 	callout_reset(&sc->callout_tick, hz, arswitch_tick, sc);
480 }
481 
482 static void
483 arswitch_lock(device_t dev)
484 {
485 	struct arswitch_softc *sc = device_get_softc(dev);
486 
487 	ARSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
488 	ARSWITCH_LOCK(sc);
489 }
490 
491 static void
492 arswitch_unlock(device_t dev)
493 {
494 	struct arswitch_softc *sc = device_get_softc(dev);
495 
496 	ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
497 	ARSWITCH_UNLOCK(sc);
498 }
499 
500 static etherswitch_info_t *
501 arswitch_getinfo(device_t dev)
502 {
503 	struct arswitch_softc *sc = device_get_softc(dev);
504 
505 	return (&sc->info);
506 }
507 
508 static int
509 arswitch_getport(device_t dev, etherswitch_port_t *p)
510 {
511 	struct arswitch_softc *sc;
512 	struct ifmediareq *ifmr;
513 	struct mii_data *mii;
514 	uint32_t reg;
515 	int err;
516 
517 	sc = device_get_softc(dev);
518 	if (p->es_port < 0 || p->es_port > sc->numphys)
519 		return (ENXIO);
520 
521 	ARSWITCH_LOCK(sc);
522 
523 	/* Retrieve the PVID. */
524 	arswitch_get_pvid(sc, p->es_port, &p->es_pvid);
525 
526 	/* Port flags. */
527 	reg = arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(p->es_port));
528 	if (reg & AR8X16_PORT_CTRL_DOUBLE_TAG)
529 		p->es_flags |= ETHERSWITCH_PORT_DOUBLE_TAG;
530 	reg >>= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
531 	if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD)
532 		p->es_flags |= ETHERSWITCH_PORT_ADDTAG;
533 	if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP)
534 		p->es_flags |= ETHERSWITCH_PORT_STRIPTAG;
535 	ARSWITCH_UNLOCK(sc);
536 
537 	mii = arswitch_miiforport(sc, p->es_port);
538 	if (p->es_port == 0) {
539 		/* fill in fixed values for CPU port */
540 		p->es_flags |= ETHERSWITCH_PORT_CPU;
541 		ifmr = &p->es_ifmr;
542 		ifmr->ifm_count = 0;
543 		ifmr->ifm_current = ifmr->ifm_active =
544 		    IFM_ETHER | IFM_1000_T | IFM_FDX;
545 		ifmr->ifm_mask = 0;
546 		ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
547 	} else if (mii != NULL) {
548 		err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
549 		    &mii->mii_media, SIOCGIFMEDIA);
550 		if (err)
551 			return (err);
552 	} else {
553 		return (ENXIO);
554 	}
555 	return (0);
556 }
557 
558 static int
559 arswitch_setport(device_t dev, etherswitch_port_t *p)
560 {
561 	int err;
562 	uint32_t reg;
563 	struct arswitch_softc *sc;
564 	struct ifmedia *ifm;
565 	struct mii_data *mii;
566 	struct ifnet *ifp;
567 
568 	sc = device_get_softc(dev);
569 	if (p->es_port < 0 || p->es_port > sc->numphys)
570 		return (ENXIO);
571 
572 	/* Port flags. */
573 	if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
574 
575 		ARSWITCH_LOCK(sc);
576 		/* Set the PVID. */
577 		if (p->es_pvid != 0)
578 			arswitch_set_pvid(sc, p->es_port, p->es_pvid);
579 
580 		/* Mutually exclusive. */
581 		if (p->es_flags & ETHERSWITCH_PORT_ADDTAG &&
582 		    p->es_flags & ETHERSWITCH_PORT_STRIPTAG) {
583 			ARSWITCH_UNLOCK(sc);
584 			return (EINVAL);
585 		}
586 
587 		reg = 0;
588 		if (p->es_flags & ETHERSWITCH_PORT_DOUBLE_TAG)
589 			reg |= AR8X16_PORT_CTRL_DOUBLE_TAG;
590 		if (p->es_flags & ETHERSWITCH_PORT_ADDTAG)
591 			reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD <<
592 			    AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
593 		if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG)
594 			reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP <<
595 			    AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
596 
597 		err = arswitch_modifyreg(sc->sc_dev,
598 		    AR8X16_REG_PORT_CTRL(p->es_port),
599 		    0x3 << AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT |
600 		    AR8X16_PORT_CTRL_DOUBLE_TAG, reg);
601 
602 		ARSWITCH_UNLOCK(sc);
603 		if (err)
604 			return (err);
605         }
606 
607 	/* Do not allow media changes on CPU port. */
608 	if (p->es_port == AR8X16_PORT_CPU)
609 		return (0);
610 
611 	mii = arswitch_miiforport(sc, p->es_port);
612 	if (mii == NULL)
613 		return (ENXIO);
614 
615 	ifp = arswitch_ifpforport(sc, p->es_port);
616 
617 	ifm = &mii->mii_media;
618 	return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
619 }
620 
621 static void
622 arswitch_statchg(device_t dev)
623 {
624 
625 	DPRINTF(dev, "%s\n", __func__);
626 }
627 
628 static int
629 arswitch_ifmedia_upd(struct ifnet *ifp)
630 {
631 	struct arswitch_softc *sc = ifp->if_softc;
632 	struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
633 
634 	if (mii == NULL)
635 		return (ENXIO);
636 	mii_mediachg(mii);
637 	return (0);
638 }
639 
640 static void
641 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
642 {
643 	struct arswitch_softc *sc = ifp->if_softc;
644 	struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
645 
646 	DPRINTF(sc->sc_dev, "%s\n", __func__);
647 
648 	if (mii == NULL)
649 		return;
650 	mii_pollstat(mii);
651 	ifmr->ifm_active = mii->mii_media_active;
652 	ifmr->ifm_status = mii->mii_media_status;
653 }
654 
655 static int
656 arswitch_getconf(device_t dev, etherswitch_conf_t *conf)
657 {
658 	struct arswitch_softc *sc;
659 
660 	sc = device_get_softc(dev);
661 
662 	/* Return the VLAN mode. */
663 	conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
664 	conf->vlan_mode = sc->vlan_mode;
665 
666 	return (0);
667 }
668 
669 static int
670 arswitch_setconf(device_t dev, etherswitch_conf_t *conf)
671 {
672 	struct arswitch_softc *sc;
673 	int err;
674 
675 	sc = device_get_softc(dev);
676 
677 	/* Set the VLAN mode. */
678 	if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) {
679 		err = arswitch_set_vlan_mode(sc, conf->vlan_mode);
680 		if (err != 0)
681 			return (err);
682 	}
683 
684 	return (0);
685 }
686 
687 static device_method_t arswitch_methods[] = {
688 	/* Device interface */
689 	DEVMETHOD(device_probe,		arswitch_probe),
690 	DEVMETHOD(device_attach,	arswitch_attach),
691 	DEVMETHOD(device_detach,	arswitch_detach),
692 
693 	/* bus interface */
694 	DEVMETHOD(bus_add_child,	device_add_child_ordered),
695 
696 	/* MII interface */
697 	DEVMETHOD(miibus_readreg,	arswitch_readphy),
698 	DEVMETHOD(miibus_writereg,	arswitch_writephy),
699 	DEVMETHOD(miibus_statchg,	arswitch_statchg),
700 
701 	/* MDIO interface */
702 	DEVMETHOD(mdio_readreg,		arswitch_readphy),
703 	DEVMETHOD(mdio_writereg,	arswitch_writephy),
704 
705 	/* etherswitch interface */
706 	DEVMETHOD(etherswitch_lock,	arswitch_lock),
707 	DEVMETHOD(etherswitch_unlock,	arswitch_unlock),
708 	DEVMETHOD(etherswitch_getinfo,	arswitch_getinfo),
709 	DEVMETHOD(etherswitch_readreg,	arswitch_readreg),
710 	DEVMETHOD(etherswitch_writereg,	arswitch_writereg),
711 	DEVMETHOD(etherswitch_readphyreg,	arswitch_readphy),
712 	DEVMETHOD(etherswitch_writephyreg,	arswitch_writephy),
713 	DEVMETHOD(etherswitch_getport,	arswitch_getport),
714 	DEVMETHOD(etherswitch_setport,	arswitch_setport),
715 	DEVMETHOD(etherswitch_getvgroup,	arswitch_getvgroup),
716 	DEVMETHOD(etherswitch_setvgroup,	arswitch_setvgroup),
717 	DEVMETHOD(etherswitch_getconf,	arswitch_getconf),
718 	DEVMETHOD(etherswitch_setconf,	arswitch_setconf),
719 
720 	DEVMETHOD_END
721 };
722 
723 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods,
724     sizeof(struct arswitch_softc));
725 static devclass_t arswitch_devclass;
726 
727 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0);
728 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0);
729 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0);
730 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0);
731 MODULE_VERSION(arswitch, 1);
732 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */
733 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */
734