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 
62 #include <dev/etherswitch/arswitch/arswitch_7240.h>
63 #include <dev/etherswitch/arswitch/arswitch_8216.h>
64 #include <dev/etherswitch/arswitch/arswitch_8226.h>
65 #include <dev/etherswitch/arswitch/arswitch_8316.h>
66 
67 #include "mdio_if.h"
68 #include "miibus_if.h"
69 #include "etherswitch_if.h"
70 
71 #if	defined(DEBUG)
72 static SYSCTL_NODE(_debug, OID_AUTO, arswitch, CTLFLAG_RD, 0, "arswitch");
73 #endif
74 
75 static inline int arswitch_portforphy(int phy);
76 static void arswitch_tick(void *arg);
77 static int arswitch_ifmedia_upd(struct ifnet *);
78 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *);
79 
80 static void
81 arswitch_identify(driver_t *driver, device_t parent)
82 {
83 	device_t child;
84 
85 	if (device_find_child(parent, driver->name, -1) == NULL) {
86 		child = BUS_ADD_CHILD(parent, 0, driver->name, -1);
87 	}
88 }
89 
90 static int
91 arswitch_probe(device_t dev)
92 {
93 	struct arswitch_softc *sc;
94 	uint32_t id;
95 	char *chipname, desc[256];
96 
97 	sc = device_get_softc(dev);
98 	bzero(sc, sizeof(*sc));
99 	sc->page = -1;
100 
101 	/* AR7240 probe */
102 	if (ar7240_probe(dev) == 0) {
103 		chipname = "AR7240";
104 		sc->sc_switchtype = AR8X16_SWITCH_AR7240;
105 		goto done;
106 	}
107 
108 	/* AR8xxx probe */
109 	id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL);
110 	switch ((id & AR8X16_MASK_CTRL_VER_MASK) >>
111 	    AR8X16_MASK_CTRL_VER_SHIFT) {
112 	case 1:
113 		chipname = "AR8216";
114 		sc->sc_switchtype = AR8X16_SWITCH_AR8216;
115 		break;
116 	case 2:
117 		chipname = "AR8226";
118 		sc->sc_switchtype = AR8X16_SWITCH_AR8226;
119 		break;
120 	case 16:
121 		chipname = "AR8316";
122 		sc->sc_switchtype = AR8X16_SWITCH_AR8316;
123 		break;
124 	default:
125 		chipname = NULL;
126 	}
127 
128 done:
129 	DPRINTF(dev, "chipname=%s, rev=%02x\n", chipname,
130 	    id & AR8X16_MASK_CTRL_REV_MASK);
131 	if (chipname != NULL) {
132 		snprintf(desc, sizeof(desc),
133 		    "Atheros %s Ethernet Switch",
134 		    chipname);
135 		device_set_desc_copy(dev, desc);
136 		return (BUS_PROBE_DEFAULT);
137 	}
138 	return (ENXIO);
139 }
140 
141 static int
142 arswitch_attach_phys(struct arswitch_softc *sc)
143 {
144 	int phy, err = 0;
145 	char name[IFNAMSIZ];
146 
147 	/* PHYs need an interface, so we generate a dummy one */
148 	snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
149 	for (phy = 0; phy < sc->numphys; phy++) {
150 		sc->ifp[phy] = if_alloc(IFT_ETHER);
151 		sc->ifp[phy]->if_softc = sc;
152 		sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
153 		    IFF_DRV_RUNNING | IFF_SIMPLEX;
154 		sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
155 		bcopy(name, sc->ifname[phy], strlen(name)+1);
156 		if_initname(sc->ifp[phy], sc->ifname[phy],
157 		    arswitch_portforphy(phy));
158 		err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
159 		    arswitch_ifmedia_upd, arswitch_ifmedia_sts, \
160 		    BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
161 		DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
162 		    device_get_nameunit(sc->miibus[phy]),
163 		    sc->ifp[phy]->if_xname);
164 		if (err != 0) {
165 			device_printf(sc->sc_dev,
166 			    "attaching PHY %d failed\n",
167 			    phy);
168 		}
169 	}
170 	return (err);
171 }
172 
173 static int
174 arswitch_attach(device_t dev)
175 {
176 	struct arswitch_softc *sc;
177 	int err = 0;
178 
179 	sc = device_get_softc(dev);
180 
181 	/* sc->sc_switchtype is already decided in arswitch_probe() */
182 	sc->sc_dev = dev;
183 	mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF);
184 	sc->page = -1;
185 	strlcpy(sc->info.es_name, device_get_desc(dev),
186 	    sizeof(sc->info.es_name));
187 
188 	/*
189 	 * Attach switch related functions
190 	 */
191 	if (AR8X16_IS_SWITCH(sc, AR7240))
192 		ar7240_attach(sc);
193 	else if (AR8X16_IS_SWITCH(sc, AR8216))
194 		ar8216_attach(sc);
195 	else if (AR8X16_IS_SWITCH(sc, AR8226))
196 		ar8226_attach(sc);
197 	else if (AR8X16_IS_SWITCH(sc, AR8316))
198 		ar8316_attach(sc);
199 	else
200 		return (ENXIO);
201 
202 	/*
203 	 * XXX these two should be part of the switch attach function
204 	 */
205 	sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */
206 	sc->info.es_nvlangroups = 16;
207 
208 	/* XXX Defaults for externally connected AR8316 */
209 	sc->numphys = 4;
210 	sc->phy4cpu = 1;
211 	sc->is_rgmii = 1;
212 	sc->is_gmii = 0;
213 
214 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
215 	    "numphys", &sc->numphys);
216 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
217 	    "phy4cpu", &sc->phy4cpu);
218 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
219 	    "is_rgmii", &sc->is_rgmii);
220 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
221 	    "is_gmii", &sc->is_gmii);
222 
223 #ifdef NOTYET
224 	arswitch_writereg(dev, AR8X16_REG_MASK_CTRL,
225 	    AR8X16_MASK_CTRL_SOFT_RESET);
226 	DELAY(1000);
227 	if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) &
228 	    AR8X16_MASK_CTRL_SOFT_RESET) {
229 		device_printf(dev, "unable to reset switch\n");
230 		return (ENXIO);
231 	}
232 	arswitch_modifyreg(dev, AR8X16_REG_GLOBAL_CTRL,
233 	    AR8X16_FLOOD_MASK_BCAST_TO_CPU,
234 	    AR8X16_FLOOD_MASK_BCAST_TO_CPU);
235 #endif
236 
237 	err = sc->hal.arswitch_hw_setup(sc);
238 	if (err != 0)
239 		return (err);
240 
241 	err = sc->hal.arswitch_hw_global_setup(sc);
242 	if (err != 0)
243 		return (err);
244 
245 	/*
246 	 * Attach the PHYs and complete the bus enumeration.
247 	 */
248 	err = arswitch_attach_phys(sc);
249 	if (err != 0)
250 		return (err);
251 
252 	bus_generic_probe(dev);
253 	bus_enumerate_hinted_children(dev);
254 	err = bus_generic_attach(dev);
255 	if (err != 0)
256 		return (err);
257 
258 	callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
259 	arswitch_tick(sc);
260 
261 	return (err);
262 }
263 
264 static int
265 arswitch_detach(device_t dev)
266 {
267 	struct arswitch_softc *sc = device_get_softc(dev);
268 	int i;
269 
270 	callout_drain(&sc->callout_tick);
271 
272 	for (i=0; i < sc->numphys; i++) {
273 		if (sc->miibus[i] != NULL)
274 			device_delete_child(dev, sc->miibus[i]);
275 		if (sc->ifp[i] != NULL)
276 			if_free(sc->ifp[i]);
277 		free(sc->ifname[i], M_DEVBUF);
278 	}
279 
280 	bus_generic_detach(dev);
281 	mtx_destroy(&sc->sc_mtx);
282 
283 	return (0);
284 }
285 
286 /*
287  * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to
288  * port 2, etc.
289  */
290 static inline int
291 arswitch_portforphy(int phy)
292 {
293 	return (phy+1);
294 }
295 
296 static inline struct mii_data *
297 arswitch_miiforport(struct arswitch_softc *sc, int port)
298 {
299 	int phy = port-1;
300 
301 	if (phy < 0 || phy >= sc->numphys)
302 		return (NULL);
303 	return (device_get_softc(sc->miibus[phy]));
304 }
305 
306 static inline struct ifnet *
307 arswitch_ifpforport(struct arswitch_softc *sc, int port)
308 {
309 	int phy = port-1;
310 
311 	if (phy < 0 || phy >= sc->numphys)
312 		return (NULL);
313 	return (sc->ifp[phy]);
314 }
315 
316 /*
317  * Convert port status to ifmedia.
318  */
319 static void
320 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
321 {
322 	*media_active = IFM_ETHER;
323 	*media_status = IFM_AVALID;
324 
325 	if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0)
326 		*media_status |= IFM_ACTIVE;
327 	else {
328 		*media_active |= IFM_NONE;
329 		return;
330 	}
331 	switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) {
332 	case AR8X16_PORT_STS_SPEED_10:
333 		*media_active |= IFM_10_T;
334 		break;
335 	case AR8X16_PORT_STS_SPEED_100:
336 		*media_active |= IFM_100_TX;
337 		break;
338 	case AR8X16_PORT_STS_SPEED_1000:
339 		*media_active |= IFM_1000_T;
340 		break;
341 	}
342 	if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0)
343 		*media_active |= IFM_FDX;
344 	else
345 		*media_active |= IFM_HDX;
346 	if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0)
347 		*media_active |= IFM_ETH_TXPAUSE;
348 	if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0)
349 		*media_active |= IFM_ETH_RXPAUSE;
350 }
351 
352 /*
353  * Poll the status for all PHYs.  We're using the switch port status because
354  * thats a lot quicker to read than talking to all the PHYs.  Care must be
355  * taken that the resulting ifmedia_active is identical to what the PHY will
356  * compute, or gratuitous link status changes will occur whenever the PHYs
357  * update function is called.
358  */
359 static void
360 arswitch_miipollstat(struct arswitch_softc *sc)
361 {
362 	int i;
363 	struct mii_data *mii;
364 	struct mii_softc *miisc;
365 	int portstatus;
366 
367 	for (i = 0; i < sc->numphys; i++) {
368 		if (sc->miibus[i] == NULL)
369 			continue;
370 		mii = device_get_softc(sc->miibus[i]);
371 		portstatus = arswitch_readreg(sc->sc_dev,
372 		    AR8X16_REG_PORT_STS(arswitch_portforphy(i)));
373 #if 0
374 		DPRINTF(sc->sc_dev, "p[%d]=%b\n",
375 		    arge_portforphy(i),
376 		    portstatus,
377 		    "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7"
378 		    "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE");
379 #endif
380 		arswitch_update_ifmedia(portstatus, &mii->mii_media_status,
381 		    &mii->mii_media_active);
382 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
383 			if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
384 			    miisc->mii_inst)
385 				continue;
386 			mii_phy_update(miisc, MII_POLLSTAT);
387 		}
388 	}
389 }
390 
391 static void
392 arswitch_tick(void *arg)
393 {
394 	struct arswitch_softc *sc = arg;
395 
396 	arswitch_miipollstat(sc);
397 	callout_reset(&sc->callout_tick, hz, arswitch_tick, sc);
398 }
399 
400 static etherswitch_info_t *
401 arswitch_getinfo(device_t dev)
402 {
403 	struct arswitch_softc *sc = device_get_softc(dev);
404 
405 	return (&sc->info);
406 }
407 
408 static int
409 arswitch_getport(device_t dev, etherswitch_port_t *p)
410 {
411 	struct arswitch_softc *sc = device_get_softc(dev);
412 	struct mii_data *mii;
413 	struct ifmediareq *ifmr = &p->es_ifmr;
414 	int err;
415 
416 	if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PORTS)
417 		return (ENXIO);
418 	p->es_vlangroup = 0;
419 
420 	mii = arswitch_miiforport(sc, p->es_port);
421 	if (p->es_port == 0) {
422 		/* fill in fixed values for CPU port */
423 		ifmr->ifm_count = 0;
424 		ifmr->ifm_current = ifmr->ifm_active =
425 		    IFM_ETHER | IFM_1000_T | IFM_FDX;
426 		ifmr->ifm_mask = 0;
427 		ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
428 	} else if (mii != NULL) {
429 		err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
430 		    &mii->mii_media, SIOCGIFMEDIA);
431 		if (err)
432 			return (err);
433 	} else {
434 		return (ENXIO);
435 	}
436 	return (0);
437 }
438 
439 /*
440  * XXX doesn't yet work?
441  */
442 static int
443 arswitch_setport(device_t dev, etherswitch_port_t *p)
444 {
445 	int err;
446 	struct arswitch_softc *sc;
447 	struct ifmedia *ifm;
448 	struct mii_data *mii;
449 	struct ifnet *ifp;
450 
451 	/*
452 	 * XXX check the sc numphys, or the #define ?
453 	 */
454 	if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PHYS)
455 		return (ENXIO);
456 
457 	sc = device_get_softc(dev);
458 
459 	/*
460 	 * XXX TODO: don't set the CPU port?
461 	 */
462 
463 	mii = arswitch_miiforport(sc, p->es_port);
464 	if (mii == NULL)
465 		return (ENXIO);
466 
467 	ifp = arswitch_ifpforport(sc, p->es_port);
468 
469 	ifm = &mii->mii_media;
470 	err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
471 	return (err);
472 }
473 
474 static int
475 arswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
476 {
477 
478 	/* XXX not implemented yet */
479 	vg->es_vid = 0;
480 	vg->es_member_ports = 0;
481 	vg->es_untagged_ports = 0;
482 	vg->es_fid = 0;
483 	return (0);
484 }
485 
486 static int
487 arswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
488 {
489 
490 	/* XXX not implemented yet */
491 	return (0);
492 }
493 
494 static void
495 arswitch_statchg(device_t dev)
496 {
497 
498 	DPRINTF(dev, "%s\n", __func__);
499 }
500 
501 static int
502 arswitch_ifmedia_upd(struct ifnet *ifp)
503 {
504 	struct arswitch_softc *sc = ifp->if_softc;
505 	struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
506 
507 	if (mii == NULL)
508 		return (ENXIO);
509 	mii_mediachg(mii);
510 	return (0);
511 }
512 
513 static void
514 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
515 {
516 	struct arswitch_softc *sc = ifp->if_softc;
517 	struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
518 
519 	DPRINTF(sc->sc_dev, "%s\n", __func__);
520 
521 	if (mii == NULL)
522 		return;
523 	mii_pollstat(mii);
524 	ifmr->ifm_active = mii->mii_media_active;
525 	ifmr->ifm_status = mii->mii_media_status;
526 }
527 
528 static device_method_t arswitch_methods[] = {
529 	/* Device interface */
530 	DEVMETHOD(device_identify,	arswitch_identify),
531 	DEVMETHOD(device_probe,		arswitch_probe),
532 	DEVMETHOD(device_attach,	arswitch_attach),
533 	DEVMETHOD(device_detach,	arswitch_detach),
534 
535 	/* bus interface */
536 	DEVMETHOD(bus_add_child,	device_add_child_ordered),
537 
538 	/* MII interface */
539 	DEVMETHOD(miibus_readreg,	arswitch_readphy),
540 	DEVMETHOD(miibus_writereg,	arswitch_writephy),
541 	DEVMETHOD(miibus_statchg,	arswitch_statchg),
542 
543 	/* MDIO interface */
544 	DEVMETHOD(mdio_readreg,		arswitch_readphy),
545 	DEVMETHOD(mdio_writereg,	arswitch_writephy),
546 
547 	/* etherswitch interface */
548 	DEVMETHOD(etherswitch_getinfo,	arswitch_getinfo),
549 	DEVMETHOD(etherswitch_readreg,	arswitch_readreg),
550 	DEVMETHOD(etherswitch_writereg,	arswitch_writereg),
551 	DEVMETHOD(etherswitch_readphyreg,	arswitch_readphy),
552 	DEVMETHOD(etherswitch_writephyreg,	arswitch_writephy),
553 	DEVMETHOD(etherswitch_getport,	arswitch_getport),
554 	DEVMETHOD(etherswitch_setport,	arswitch_setport),
555 	DEVMETHOD(etherswitch_getvgroup,	arswitch_getvgroup),
556 	DEVMETHOD(etherswitch_setvgroup,	arswitch_setvgroup),
557 
558 	DEVMETHOD_END
559 };
560 
561 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods,
562     sizeof(struct arswitch_softc));
563 static devclass_t arswitch_devclass;
564 
565 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0);
566 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0);
567 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0);
568 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0);
569 MODULE_VERSION(arswitch, 1);
570 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */
571 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */
572