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