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