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