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