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