]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/etherswitch/e6000sw/e6000sw.c
MFV r349454:
[FreeBSD/FreeBSD.git] / sys / dev / etherswitch / e6000sw / e6000sw.c
1 /*-
2  * Copyright (c) 2015 Semihalf
3  * Copyright (c) 2015 Stormshield
4  * Copyright (c) 2018-2019, Rubicon Communications, LLC (Netgate)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40
41 #include <net/if.h>
42 #include <net/if_media.h>
43 #include <net/if_types.h>
44
45 #include <dev/etherswitch/etherswitch.h>
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
48
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51
52 #include "e6000swreg.h"
53 #include "etherswitch_if.h"
54 #include "miibus_if.h"
55 #include "mdio_if.h"
56
57 MALLOC_DECLARE(M_E6000SW);
58 MALLOC_DEFINE(M_E6000SW, "e6000sw", "e6000sw switch");
59
60 #define E6000SW_LOCK(_sc)               sx_xlock(&(_sc)->sx)
61 #define E6000SW_UNLOCK(_sc)             sx_unlock(&(_sc)->sx)
62 #define E6000SW_LOCK_ASSERT(_sc, _what) sx_assert(&(_sc)->sx, (_what))
63 #define E6000SW_TRYLOCK(_sc)            sx_tryxlock(&(_sc)->sx)
64 #define E6000SW_WAITREADY(_sc, _reg, _bit)                              \
65     e6000sw_waitready((_sc), REG_GLOBAL, (_reg), (_bit))
66 #define E6000SW_WAITREADY2(_sc, _reg, _bit)                             \
67     e6000sw_waitready((_sc), REG_GLOBAL2, (_reg), (_bit))
68 #define MDIO_READ(dev, addr, reg)                                       \
69     MDIO_READREG(device_get_parent(dev), (addr), (reg))
70 #define MDIO_WRITE(dev, addr, reg, val)                                 \
71     MDIO_WRITEREG(device_get_parent(dev), (addr), (reg), (val))
72
73
74 typedef struct e6000sw_softc {
75         device_t                dev;
76         phandle_t               node;
77
78         struct sx               sx;
79         struct ifnet            *ifp[E6000SW_MAX_PORTS];
80         char                    *ifname[E6000SW_MAX_PORTS];
81         device_t                miibus[E6000SW_MAX_PORTS];
82         struct proc             *kproc;
83
84         int                     vlans[E6000SW_NUM_VLANS];
85         uint32_t                swid;
86         uint32_t                vlan_mode;
87         uint32_t                cpuports_mask;
88         uint32_t                fixed_mask;
89         uint32_t                fixed25_mask;
90         uint32_t                ports_mask;
91         int                     phy_base;
92         int                     sw_addr;
93         int                     num_ports;
94 } e6000sw_softc_t;
95
96 static etherswitch_info_t etherswitch_info = {
97         .es_nports =            0,
98         .es_nvlangroups =       0,
99         .es_vlan_caps =         ETHERSWITCH_VLAN_PORT | ETHERSWITCH_VLAN_DOT1Q,
100         .es_name =              "Marvell 6000 series switch"
101 };
102
103 static void e6000sw_identify(driver_t *, device_t);
104 static int e6000sw_probe(device_t);
105 static int e6000sw_parse_fixed_link(e6000sw_softc_t *, phandle_t, uint32_t);
106 static int e6000sw_parse_ethernet(e6000sw_softc_t *, phandle_t, uint32_t);
107 static int e6000sw_attach(device_t);
108 static int e6000sw_detach(device_t);
109 static int e6000sw_readphy(device_t, int, int);
110 static int e6000sw_writephy(device_t, int, int, int);
111 static etherswitch_info_t* e6000sw_getinfo(device_t);
112 static int e6000sw_getconf(device_t, etherswitch_conf_t *);
113 static int e6000sw_setconf(device_t, etherswitch_conf_t *);
114 static void e6000sw_lock(device_t);
115 static void e6000sw_unlock(device_t);
116 static int e6000sw_getport(device_t, etherswitch_port_t *);
117 static int e6000sw_setport(device_t, etherswitch_port_t *);
118 static int e6000sw_set_vlan_mode(e6000sw_softc_t *, uint32_t);
119 static int e6000sw_readreg_wrapper(device_t, int);
120 static int e6000sw_writereg_wrapper(device_t, int, int);
121 static int e6000sw_readphy_wrapper(device_t, int, int);
122 static int e6000sw_writephy_wrapper(device_t, int, int, int);
123 static int e6000sw_getvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
124 static int e6000sw_setvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
125 static int e6000sw_setvgroup(device_t, etherswitch_vlangroup_t *);
126 static int e6000sw_getvgroup(device_t, etherswitch_vlangroup_t *);
127 static void e6000sw_setup(device_t, e6000sw_softc_t *);
128 static void e6000sw_tick(void *);
129 static void e6000sw_set_atustat(device_t, e6000sw_softc_t *, int, int);
130 static int e6000sw_atu_flush(device_t, e6000sw_softc_t *, int);
131 static int e6000sw_vtu_flush(e6000sw_softc_t *);
132 static int e6000sw_vtu_update(e6000sw_softc_t *, int, int, int, int, int);
133 static __inline void e6000sw_writereg(e6000sw_softc_t *, int, int, int);
134 static __inline uint32_t e6000sw_readreg(e6000sw_softc_t *, int, int);
135 static int e6000sw_ifmedia_upd(struct ifnet *);
136 static void e6000sw_ifmedia_sts(struct ifnet *, struct ifmediareq *);
137 static int e6000sw_atu_mac_table(device_t, e6000sw_softc_t *, struct atu_opt *,
138     int);
139 static int e6000sw_get_pvid(e6000sw_softc_t *, int, int *);
140 static void e6000sw_set_pvid(e6000sw_softc_t *, int, int);
141 static __inline bool e6000sw_is_cpuport(e6000sw_softc_t *, int);
142 static __inline bool e6000sw_is_fixedport(e6000sw_softc_t *, int);
143 static __inline bool e6000sw_is_fixed25port(e6000sw_softc_t *, int);
144 static __inline bool e6000sw_is_phyport(e6000sw_softc_t *, int);
145 static __inline bool e6000sw_is_portenabled(e6000sw_softc_t *, int);
146 static __inline struct mii_data *e6000sw_miiforphy(e6000sw_softc_t *,
147     unsigned int);
148
149 static device_method_t e6000sw_methods[] = {
150         /* device interface */
151         DEVMETHOD(device_identify,              e6000sw_identify),
152         DEVMETHOD(device_probe,                 e6000sw_probe),
153         DEVMETHOD(device_attach,                e6000sw_attach),
154         DEVMETHOD(device_detach,                e6000sw_detach),
155
156         /* bus interface */
157         DEVMETHOD(bus_add_child,                device_add_child_ordered),
158
159         /* mii interface */
160         DEVMETHOD(miibus_readreg,               e6000sw_readphy),
161         DEVMETHOD(miibus_writereg,              e6000sw_writephy),
162
163         /* etherswitch interface */
164         DEVMETHOD(etherswitch_getinfo,          e6000sw_getinfo),
165         DEVMETHOD(etherswitch_getconf,          e6000sw_getconf),
166         DEVMETHOD(etherswitch_setconf,          e6000sw_setconf),
167         DEVMETHOD(etherswitch_lock,             e6000sw_lock),
168         DEVMETHOD(etherswitch_unlock,           e6000sw_unlock),
169         DEVMETHOD(etherswitch_getport,          e6000sw_getport),
170         DEVMETHOD(etherswitch_setport,          e6000sw_setport),
171         DEVMETHOD(etherswitch_readreg,          e6000sw_readreg_wrapper),
172         DEVMETHOD(etherswitch_writereg,         e6000sw_writereg_wrapper),
173         DEVMETHOD(etherswitch_readphyreg,       e6000sw_readphy_wrapper),
174         DEVMETHOD(etherswitch_writephyreg,      e6000sw_writephy_wrapper),
175         DEVMETHOD(etherswitch_setvgroup,        e6000sw_setvgroup_wrapper),
176         DEVMETHOD(etherswitch_getvgroup,        e6000sw_getvgroup_wrapper),
177
178         DEVMETHOD_END
179 };
180
181 static devclass_t e6000sw_devclass;
182
183 DEFINE_CLASS_0(e6000sw, e6000sw_driver, e6000sw_methods,
184     sizeof(e6000sw_softc_t));
185
186 DRIVER_MODULE(e6000sw, mdio, e6000sw_driver, e6000sw_devclass, 0, 0);
187 DRIVER_MODULE(etherswitch, e6000sw, etherswitch_driver, etherswitch_devclass, 0,
188     0);
189 DRIVER_MODULE(miibus, e6000sw, miibus_driver, miibus_devclass, 0, 0);
190 MODULE_DEPEND(e6000sw, mdio, 1, 1, 1);
191
192
193 static void
194 e6000sw_identify(driver_t *driver, device_t parent)
195 {
196
197         if (device_find_child(parent, "e6000sw", -1) == NULL)
198                 BUS_ADD_CHILD(parent, 0, "e6000sw", -1);
199 }
200
201 static int
202 e6000sw_probe(device_t dev)
203 {
204         e6000sw_softc_t *sc;
205         const char *description;
206         phandle_t switch_node;
207
208         switch_node = ofw_bus_find_compatible(OF_finddevice("/"),
209             "marvell,mv88e6085");
210
211         if (switch_node == 0)
212                 return (ENXIO);
213
214         if (bootverbose)
215                 device_printf(dev, "Found switch_node: 0x%x\n", switch_node);
216
217         sc = device_get_softc(dev);
218         sc->dev = dev;
219         sc->node = switch_node;
220
221         if (OF_getencprop(sc->node, "reg", &sc->sw_addr,
222             sizeof(sc->sw_addr)) < 0)
223                 return (ENXIO);
224         if (sc->sw_addr < 0 || sc->sw_addr > 32)
225                 return (ENXIO);
226
227         /*
228          * Create temporary lock, just to satisfy assertions,
229          * when obtaining the switch ID. Destroy immediately afterwards.
230          */
231         sx_init(&sc->sx, "e6000sw_tmp");
232         E6000SW_LOCK(sc);
233         sc->swid = e6000sw_readreg(sc, REG_PORT(0), SWITCH_ID) & 0xfff0;
234         E6000SW_UNLOCK(sc);
235         sx_destroy(&sc->sx);
236
237         switch (sc->swid) {
238         case MV88E6141:
239                 description = "Marvell 88E6141";
240                 sc->phy_base = 0x10;
241                 sc->num_ports = 6;
242                 break;
243         case MV88E6341:
244                 description = "Marvell 88E6341";
245                 sc->phy_base = 0x10;
246                 sc->num_ports = 6;
247                 break;
248         case MV88E6352:
249                 description = "Marvell 88E6352";
250                 sc->num_ports = 7;
251                 break;
252         case MV88E6172:
253                 description = "Marvell 88E6172";
254                 sc->num_ports = 7;
255                 break;
256         case MV88E6176:
257                 description = "Marvell 88E6176";
258                 sc->num_ports = 7;
259                 break;
260         default:
261                 device_printf(dev, "Unrecognized device, id 0x%x.\n", sc->swid);
262                 return (ENXIO);
263         }
264
265         device_set_desc(dev, description);
266
267         return (BUS_PROBE_DEFAULT);
268 }
269
270 static int
271 e6000sw_parse_fixed_link(e6000sw_softc_t *sc, phandle_t node, uint32_t port)
272 {
273         int speed;
274         phandle_t fixed_link;
275
276         fixed_link = ofw_bus_find_child(node, "fixed-link");
277
278         if (fixed_link != 0) {
279                 sc->fixed_mask |= (1 << port);
280
281                 if (OF_getencprop(fixed_link, "speed", &speed, sizeof(speed))> 0) {
282                         if (speed == 2500 &&
283                             (MVSWITCH(sc, MV88E6141) ||
284                              MVSWITCH(sc, MV88E6341)))
285                                 sc->fixed25_mask |= (1 << port);
286                 } else {
287                     device_printf(sc->dev,
288                             "Port %d has a fixed-link node without a speed "
289                             "property\n", port);
290
291                     return (ENXIO);
292                 }
293         }
294
295         return (0);
296 }
297
298 static int
299 e6000sw_parse_ethernet(e6000sw_softc_t *sc, phandle_t port_handle, uint32_t port) {
300         phandle_t switch_eth, switch_eth_handle;
301
302         if (OF_getencprop(port_handle, "ethernet", (void*)&switch_eth_handle,
303             sizeof(switch_eth_handle)) > 0) {
304                 if (switch_eth_handle > 0) {
305                         switch_eth = OF_node_from_xref(switch_eth_handle);
306
307                         device_printf(sc->dev, "CPU port at %d\n", port);
308                         sc->cpuports_mask |= (1 << port);
309
310                         return (e6000sw_parse_fixed_link(sc, switch_eth, port));
311                 } else
312                         device_printf(sc->dev,
313                                 "Port %d has ethernet property but it points "
314                                 "to an invalid location\n", port);
315         }
316
317         return (0);
318 }
319
320 static int
321 e6000sw_parse_child_fdt(e6000sw_softc_t *sc, phandle_t child, int *pport)
322 {
323         uint32_t port;
324
325         if (pport == NULL)
326                 return (ENXIO);
327
328         if (OF_getencprop(child, "reg", (void *)&port, sizeof(port)) < 0)
329                 return (ENXIO);
330         if (port >= sc->num_ports)
331                 return (ENXIO);
332         *pport = port;
333
334         if (e6000sw_parse_fixed_link(sc, child, port) != 0)
335                 return (ENXIO);
336
337         if (e6000sw_parse_ethernet(sc, child, port) != 0)
338                 return (ENXIO);
339
340         if ((sc->fixed_mask & (1 << port)) != 0)
341                 device_printf(sc->dev, "fixed port at %d\n", port);
342         else
343                 device_printf(sc->dev, "PHY at port %d\n", port);
344
345         return (0);
346 }
347
348 static int
349 e6000sw_init_interface(e6000sw_softc_t *sc, int port)
350 {
351         char name[IFNAMSIZ];
352
353         snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->dev));
354
355         sc->ifp[port] = if_alloc(IFT_ETHER);
356         if (sc->ifp[port] == NULL)
357                 return (ENOMEM);
358         sc->ifp[port]->if_softc = sc;
359         sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
360             IFF_DRV_RUNNING | IFF_SIMPLEX;
361         sc->ifname[port] = malloc(strlen(name) + 1, M_E6000SW, M_NOWAIT);
362         if (sc->ifname[port] == NULL) {
363                 if_free(sc->ifp[port]);
364                 return (ENOMEM);
365         }
366         memcpy(sc->ifname[port], name, strlen(name) + 1);
367         if_initname(sc->ifp[port], sc->ifname[port], port);
368
369         return (0);
370 }
371
372 static int
373 e6000sw_attach_miibus(e6000sw_softc_t *sc, int port)
374 {
375         int err;
376
377         err = mii_attach(sc->dev, &sc->miibus[port], sc->ifp[port],
378             e6000sw_ifmedia_upd, e6000sw_ifmedia_sts, BMSR_DEFCAPMASK,
379             port + sc->phy_base, MII_OFFSET_ANY, 0);
380         if (err != 0)
381                 return (err);
382
383         return (0);
384 }
385
386 static int
387 e6000sw_attach(device_t dev)
388 {
389         e6000sw_softc_t *sc;
390         phandle_t child, ports;
391         int err, port;
392         uint32_t reg;
393
394         err = 0;
395         sc = device_get_softc(dev);
396
397         /*
398          * According to the Linux source code, all of the Switch IDs we support
399          * are multi_chip capable, and should go into multi-chip mode if the
400          * sw_addr != 0.
401          */
402         if (MVSWITCH_MULTICHIP(sc))
403                 device_printf(dev, "multi-chip addressing mode (%#x)\n",
404                     sc->sw_addr);
405         else
406                 device_printf(dev, "single-chip addressing mode\n");
407
408         sx_init(&sc->sx, "e6000sw");
409
410         E6000SW_LOCK(sc);
411         e6000sw_setup(dev, sc);
412
413         ports = ofw_bus_find_child(sc->node, "ports");
414
415         if (ports == 0) {
416                 device_printf(dev, "failed to parse DTS: no ports found for "
417                     "switch\n");
418                 return (ENXIO);
419         }
420
421         for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
422                 err = e6000sw_parse_child_fdt(sc, child, &port);
423                 if (err != 0) {
424                         device_printf(sc->dev, "failed to parse DTS\n");
425                         goto out_fail;
426                 }
427
428                 /* Port is in use. */
429                 sc->ports_mask |= (1 << port);
430
431                 err = e6000sw_init_interface(sc, port);
432                 if (err != 0) {
433                         device_printf(sc->dev, "failed to init interface\n");
434                         goto out_fail;
435                 }
436
437                 if (e6000sw_is_fixedport(sc, port)) {
438                         /* Link must be down to change speed force value. */
439                         reg = e6000sw_readreg(sc, REG_PORT(port), PSC_CONTROL);
440                         reg &= ~PSC_CONTROL_LINK_UP;
441                         reg |= PSC_CONTROL_FORCED_LINK;
442                         e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
443
444                         /*
445                          * Force speed, full-duplex, EEE off and flow-control
446                          * on.
447                          */
448                         reg &= ~(PSC_CONTROL_SPD2500 | PSC_CONTROL_ALT_SPD |
449                             PSC_CONTROL_FORCED_EEE);
450                         if (e6000sw_is_fixed25port(sc, port))
451                                 reg |= PSC_CONTROL_SPD2500;
452                         else
453                                 reg |= PSC_CONTROL_SPD1000;
454                         reg |= PSC_CONTROL_FORCED_DPX | PSC_CONTROL_FULLDPX |
455                             PSC_CONTROL_FORCED_LINK | PSC_CONTROL_LINK_UP |
456                             PSC_CONTROL_FORCED_FC | PSC_CONTROL_FC_ON |
457                             PSC_CONTROL_FORCED_SPD;
458                         if (MVSWITCH(sc, MV88E6141) || MVSWITCH(sc, MV88E6341))
459                             reg |= PSC_CONTROL_FORCED_EEE;
460                         e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
461                 }
462
463                 /* Don't attach miibus at CPU/fixed ports */
464                 if (!e6000sw_is_phyport(sc, port))
465                         continue;
466
467                 err = e6000sw_attach_miibus(sc, port);
468                 if (err != 0) {
469                         device_printf(sc->dev, "failed to attach miibus\n");
470                         goto out_fail;
471                 }
472         }
473
474         etherswitch_info.es_nports = sc->num_ports;
475
476         /* Default to port vlan. */
477         e6000sw_set_vlan_mode(sc, ETHERSWITCH_VLAN_PORT);
478
479         reg = e6000sw_readreg(sc, REG_GLOBAL, SWITCH_GLOBAL_STATUS);
480         if (reg & SWITCH_GLOBAL_STATUS_IR)
481                 device_printf(dev, "switch is ready.\n");
482         E6000SW_UNLOCK(sc);
483
484         bus_generic_probe(dev);
485         bus_generic_attach(dev);
486
487         kproc_create(e6000sw_tick, sc, &sc->kproc, 0, 0, "e6000sw tick kproc");
488
489         return (0);
490
491 out_fail:
492         E6000SW_UNLOCK(sc);
493         e6000sw_detach(dev);
494
495         return (err);
496 }
497
498 static int
499 e6000sw_waitready(e6000sw_softc_t *sc, uint32_t phy, uint32_t reg,
500     uint32_t busybit)
501 {
502         int i;
503
504         for (i = 0; i < E6000SW_RETRIES; i++) {
505                 if ((e6000sw_readreg(sc, phy, reg) & busybit) == 0)
506                         return (0);
507                 DELAY(1);
508         }
509
510         return (1);
511 }
512
513 /*
514  * PHY registers are paged. Put page index in reg 22 (accessible from every
515  * page), then access specific register.
516  */
517 static int
518 e6000sw_readphy(device_t dev, int phy, int reg)
519 {
520         e6000sw_softc_t *sc;
521         uint32_t val;
522
523         sc = device_get_softc(dev);
524         if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
525                 device_printf(dev, "Wrong register address.\n");
526                 return (EINVAL);
527         }
528
529         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
530         if (E6000SW_WAITREADY2(sc, SMI_PHY_CMD_REG, SMI_CMD_BUSY)) {
531                 device_printf(dev, "Timeout while waiting for switch\n");
532                 return (ETIMEDOUT);
533         }
534
535         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG,
536             SMI_CMD_OP_C22_READ | (reg & SMI_CMD_REG_ADDR_MASK) |
537             ((phy << SMI_CMD_DEV_ADDR) & SMI_CMD_DEV_ADDR_MASK));
538         if (E6000SW_WAITREADY2(sc, SMI_PHY_CMD_REG, SMI_CMD_BUSY)) {
539                 device_printf(dev, "Timeout while waiting for switch\n");
540                 return (ETIMEDOUT);
541         }
542
543         val = e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG);
544
545         return (val & PHY_DATA_MASK);
546 }
547
548 static int
549 e6000sw_writephy(device_t dev, int phy, int reg, int data)
550 {
551         e6000sw_softc_t *sc;
552
553         sc = device_get_softc(dev);
554         if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
555                 device_printf(dev, "Wrong register address.\n");
556                 return (EINVAL);
557         }
558
559         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
560         if (E6000SW_WAITREADY2(sc, SMI_PHY_CMD_REG, SMI_CMD_BUSY)) {
561                 device_printf(dev, "Timeout while waiting for switch\n");
562                 return (ETIMEDOUT);
563         }
564
565         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG,
566             data & PHY_DATA_MASK);
567         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG,
568             SMI_CMD_OP_C22_WRITE | (reg & SMI_CMD_REG_ADDR_MASK) |
569             ((phy << SMI_CMD_DEV_ADDR) & SMI_CMD_DEV_ADDR_MASK));
570
571         return (0);
572 }
573
574 static int
575 e6000sw_detach(device_t dev)
576 {
577         int phy;
578         e6000sw_softc_t *sc;
579
580         sc = device_get_softc(dev);
581         bus_generic_detach(dev);
582         sx_destroy(&sc->sx);
583         for (phy = 0; phy < sc->num_ports; phy++) {
584                 if (sc->miibus[phy] != NULL)
585                         device_delete_child(dev, sc->miibus[phy]);
586                 if (sc->ifp[phy] != NULL)
587                         if_free(sc->ifp[phy]);
588                 if (sc->ifname[phy] != NULL)
589                         free(sc->ifname[phy], M_E6000SW);
590         }
591
592         return (0);
593 }
594
595 static etherswitch_info_t*
596 e6000sw_getinfo(device_t dev)
597 {
598
599         return (&etherswitch_info);
600 }
601
602 static int
603 e6000sw_getconf(device_t dev, etherswitch_conf_t *conf)
604 {
605         struct e6000sw_softc *sc;
606
607         /* Return the VLAN mode. */
608         sc = device_get_softc(dev);
609         conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
610         conf->vlan_mode = sc->vlan_mode;
611
612         return (0);
613 }
614
615 static int
616 e6000sw_setconf(device_t dev, etherswitch_conf_t *conf)
617 {
618         struct e6000sw_softc *sc;
619
620         /* Set the VLAN mode. */
621         sc = device_get_softc(dev);
622         if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) {
623                 E6000SW_LOCK(sc);
624                 e6000sw_set_vlan_mode(sc, conf->vlan_mode);
625                 E6000SW_UNLOCK(sc);
626         }
627
628         return (0);
629 }
630
631 static void
632 e6000sw_lock(device_t dev)
633 {
634         struct e6000sw_softc *sc;
635
636         sc = device_get_softc(dev);
637
638         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
639         E6000SW_LOCK(sc);
640 }
641
642 static void
643 e6000sw_unlock(device_t dev)
644 {
645         struct e6000sw_softc *sc;
646
647         sc = device_get_softc(dev);
648
649         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
650         E6000SW_UNLOCK(sc);
651 }
652
653 static int
654 e6000sw_getport(device_t dev, etherswitch_port_t *p)
655 {
656         struct mii_data *mii;
657         int err;
658         struct ifmediareq *ifmr;
659         uint32_t reg;
660
661         e6000sw_softc_t *sc = device_get_softc(dev);
662         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
663
664         if (p->es_port >= sc->num_ports || p->es_port < 0)
665                 return (EINVAL);
666         if (!e6000sw_is_portenabled(sc, p->es_port))
667                 return (0);
668
669         E6000SW_LOCK(sc);
670         e6000sw_get_pvid(sc, p->es_port, &p->es_pvid);
671
672         /* Port flags. */
673         reg = e6000sw_readreg(sc, REG_PORT(p->es_port), PORT_CONTROL2);
674         if (reg & PORT_CONTROL2_DISC_TAGGED)
675                 p->es_flags |= ETHERSWITCH_PORT_DROPTAGGED;
676         if (reg & PORT_CONTROL2_DISC_UNTAGGED)
677                 p->es_flags |= ETHERSWITCH_PORT_DROPUNTAGGED;
678
679         err = 0;
680         if (e6000sw_is_fixedport(sc, p->es_port)) {
681                 if (e6000sw_is_cpuport(sc, p->es_port))
682                         p->es_flags |= ETHERSWITCH_PORT_CPU;
683                 ifmr = &p->es_ifmr;
684                 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
685                 ifmr->ifm_count = 0;
686                 if (e6000sw_is_fixed25port(sc, p->es_port))
687                         ifmr->ifm_active = IFM_2500_T;
688                 else
689                         ifmr->ifm_active = IFM_1000_T;
690                 ifmr->ifm_active |= IFM_ETHER | IFM_FDX;
691                 ifmr->ifm_current = ifmr->ifm_active;
692                 ifmr->ifm_mask = 0;
693         } else {
694                 mii = e6000sw_miiforphy(sc, p->es_port);
695                 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
696                     &mii->mii_media, SIOCGIFMEDIA);
697         }
698         E6000SW_UNLOCK(sc);
699
700         return (err);
701 }
702
703 static int
704 e6000sw_setport(device_t dev, etherswitch_port_t *p)
705 {
706         e6000sw_softc_t *sc;
707         int err;
708         struct mii_data *mii;
709         uint32_t reg;
710
711         sc = device_get_softc(dev);
712         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
713
714         if (p->es_port >= sc->num_ports || p->es_port < 0)
715                 return (EINVAL);
716         if (!e6000sw_is_portenabled(sc, p->es_port))
717                 return (0);
718
719         /* Port flags. */
720         reg = e6000sw_readreg(sc, REG_PORT(p->es_port), PORT_CONTROL2);
721         if (p->es_flags & ETHERSWITCH_PORT_DROPTAGGED)
722                 reg |= PORT_CONTROL2_DISC_TAGGED;
723         else
724                 reg &= ~PORT_CONTROL2_DISC_TAGGED;
725         if (p->es_flags & ETHERSWITCH_PORT_DROPUNTAGGED)
726                 reg |= PORT_CONTROL2_DISC_UNTAGGED;
727         else
728                 reg &= ~PORT_CONTROL2_DISC_UNTAGGED;
729         e6000sw_writereg(sc, REG_PORT(p->es_port), PORT_CONTROL2, reg);
730
731         err = 0;
732         E6000SW_LOCK(sc);
733         if (p->es_pvid != 0)
734                 e6000sw_set_pvid(sc, p->es_port, p->es_pvid);
735         if (e6000sw_is_phyport(sc, p->es_port)) {
736                 mii = e6000sw_miiforphy(sc, p->es_port);
737                 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, &mii->mii_media,
738                     SIOCSIFMEDIA);
739         }
740         E6000SW_UNLOCK(sc);
741
742         return (err);
743 }
744
745 static __inline void
746 e6000sw_port_vlan_assign(e6000sw_softc_t *sc, int port, uint32_t fid,
747     uint32_t members)
748 {
749         uint32_t reg;
750
751         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
752         reg &= ~(PORT_MASK(sc) | PORT_VLAN_MAP_FID_MASK);
753         reg |= members & PORT_MASK(sc) & ~(1 << port);
754         reg |= (fid << PORT_VLAN_MAP_FID) & PORT_VLAN_MAP_FID_MASK;
755         e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
756         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL1);
757         reg &= ~PORT_CONTROL1_FID_MASK;
758         reg |= (fid >> 4) & PORT_CONTROL1_FID_MASK;
759         e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL1, reg);
760 }
761
762 static int
763 e6000sw_init_vlan(struct e6000sw_softc *sc)
764 {
765         int i, port, ret;
766         uint32_t members;
767
768         /* Disable all ports */
769         for (port = 0; port < sc->num_ports; port++) {
770                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
771                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
772                     (ret & ~PORT_CONTROL_ENABLE));
773         }
774
775         /* Flush VTU. */
776         e6000sw_vtu_flush(sc);
777
778         for (port = 0; port < sc->num_ports; port++) {
779                 /* Reset the egress and frame mode. */
780                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
781                 ret &= ~(PORT_CONTROL_EGRESS | PORT_CONTROL_FRAME);
782                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL, ret);
783
784                 /* Set the the 802.1q mode. */
785                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL2);
786                 ret &= ~PORT_CONTROL2_DOT1Q;
787                 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q)
788                         ret |= PORT_CONTROL2_DOT1Q;
789                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL2, ret);
790         }
791
792         for (port = 0; port < sc->num_ports; port++) {
793                 if (!e6000sw_is_portenabled(sc, port))
794                         continue;
795
796                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
797
798                 /* Set port priority */
799                 ret &= ~PORT_VID_PRIORITY_MASK;
800
801                 /* Set VID map */
802                 ret &= ~PORT_VID_DEF_VID_MASK;
803                 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q)
804                         ret |= 1;
805                 else
806                         ret |= (port + 1);
807                 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
808         }
809
810         /* Assign the member ports to each origin port. */
811         for (port = 0; port < sc->num_ports; port++) {
812                 members = 0;
813                 if (e6000sw_is_portenabled(sc, port)) {
814                         for (i = 0; i < sc->num_ports; i++) {
815                                 if (i == port || !e6000sw_is_portenabled(sc, i))
816                                         continue;
817                                 members |= (1 << i);
818                         }
819                 }
820                 /* Default to FID 0. */
821                 e6000sw_port_vlan_assign(sc, port, 0, members);
822         }
823
824         /* Reset internal VLAN table. */
825         for (i = 0; i < nitems(sc->vlans); i++)
826                 sc->vlans[i] = 0;
827
828         /* Create default VLAN (1). */
829         if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
830                 sc->vlans[0] = 1;
831                 e6000sw_vtu_update(sc, 0, sc->vlans[0], 1, 0, sc->ports_mask);
832         }
833
834         /* Enable all ports */
835         for (port = 0; port < sc->num_ports; port++) {
836                 if (!e6000sw_is_portenabled(sc, port))
837                         continue;
838                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
839                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
840                     (ret | PORT_CONTROL_ENABLE));
841         }
842
843         return (0);
844 }
845
846 static int
847 e6000sw_set_vlan_mode(struct e6000sw_softc *sc, uint32_t mode)
848 {
849
850         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
851         switch (mode) {
852         case ETHERSWITCH_VLAN_PORT:
853                 sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
854                 etherswitch_info.es_nvlangroups = sc->num_ports;
855                 return (e6000sw_init_vlan(sc));
856                 break;
857         case ETHERSWITCH_VLAN_DOT1Q:
858                 sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
859                 etherswitch_info.es_nvlangroups = E6000SW_NUM_VLANS;
860                 return (e6000sw_init_vlan(sc));
861                 break;
862         default:
863                 return (EINVAL);
864         }
865 }
866
867 /*
868  * Registers in this switch are divided into sections, specified in
869  * documentation. So as to access any of them, section index and reg index
870  * is necessary. etherswitchcfg uses only one variable, so indexes were
871  * compressed into addr_reg: 32 * section_index + reg_index.
872  */
873 static int
874 e6000sw_readreg_wrapper(device_t dev, int addr_reg)
875 {
876
877         if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
878             (addr_reg < (REG_PORT(0) * 32))) {
879                 device_printf(dev, "Wrong register address.\n");
880                 return (EINVAL);
881         }
882
883         return (e6000sw_readreg(device_get_softc(dev), addr_reg / 32,
884             addr_reg % 32));
885 }
886
887 static int
888 e6000sw_writereg_wrapper(device_t dev, int addr_reg, int val)
889 {
890
891         if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
892             (addr_reg < (REG_PORT(0) * 32))) {
893                 device_printf(dev, "Wrong register address.\n");
894                 return (EINVAL);
895         }
896         e6000sw_writereg(device_get_softc(dev), addr_reg / 5,
897             addr_reg % 32, val);
898
899         return (0);
900 }
901
902 /*
903  * These wrappers are necessary because PHY accesses from etherswitchcfg
904  * need to be synchronized with locks, while miibus PHY accesses do not.
905  */
906 static int
907 e6000sw_readphy_wrapper(device_t dev, int phy, int reg)
908 {
909         e6000sw_softc_t *sc;
910         int ret;
911
912         sc = device_get_softc(dev);
913         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
914
915         E6000SW_LOCK(sc);
916         ret = e6000sw_readphy(dev, phy, reg);
917         E6000SW_UNLOCK(sc);
918
919         return (ret);
920 }
921
922 static int
923 e6000sw_writephy_wrapper(device_t dev, int phy, int reg, int data)
924 {
925         e6000sw_softc_t *sc;
926         int ret;
927
928         sc = device_get_softc(dev);
929         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
930
931         E6000SW_LOCK(sc);
932         ret = e6000sw_writephy(dev, phy, reg, data);
933         E6000SW_UNLOCK(sc);
934
935         return (ret);
936 }
937
938 /*
939  * setvgroup/getvgroup called from etherswitchfcg need to be locked,
940  * while internal calls do not.
941  */
942 static int
943 e6000sw_setvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
944 {
945         e6000sw_softc_t *sc;
946         int ret;
947
948         sc = device_get_softc(dev);
949         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
950
951         E6000SW_LOCK(sc);
952         ret = e6000sw_setvgroup(dev, vg);
953         E6000SW_UNLOCK(sc);
954
955         return (ret);
956 }
957
958 static int
959 e6000sw_getvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
960 {
961         e6000sw_softc_t *sc;
962         int ret;
963
964         sc = device_get_softc(dev);
965         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
966
967         E6000SW_LOCK(sc);
968         ret = e6000sw_getvgroup(dev, vg);
969         E6000SW_UNLOCK(sc);
970
971         return (ret);
972 }
973
974 static int
975 e6000sw_set_port_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
976 {
977         uint32_t port;
978
979         port = vg->es_vlangroup;
980         if (port > sc->num_ports)
981                 return (EINVAL);
982
983         if (vg->es_member_ports != vg->es_untagged_ports) {
984                 device_printf(sc->dev, "Tagged ports not supported.\n");
985                 return (EINVAL);
986         }
987
988         e6000sw_port_vlan_assign(sc, port, 0, vg->es_untagged_ports);
989         vg->es_vid = port | ETHERSWITCH_VID_VALID;
990
991         return (0);
992 }
993
994 static int
995 e6000sw_set_dot1q_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
996 {
997         int i, vlan;
998
999         vlan = vg->es_vid & ETHERSWITCH_VID_MASK;
1000
1001         /* Set VLAN to '0' removes it from table. */
1002         if (vlan == 0) {
1003                 e6000sw_vtu_update(sc, VTU_PURGE,
1004                     sc->vlans[vg->es_vlangroup], 0, 0, 0);
1005                 sc->vlans[vg->es_vlangroup] = 0;
1006                 return (0);
1007         }
1008
1009         /* Is this VLAN already in table ? */
1010         for (i = 0; i < etherswitch_info.es_nvlangroups; i++)
1011                 if (i != vg->es_vlangroup && vlan == sc->vlans[i])
1012                         return (EINVAL);
1013
1014         sc->vlans[vg->es_vlangroup] = vlan;
1015         e6000sw_vtu_update(sc, 0, vlan, vg->es_vlangroup + 1,
1016             vg->es_member_ports & sc->ports_mask,
1017             vg->es_untagged_ports & sc->ports_mask);
1018
1019         return (0);
1020 }
1021
1022 static int
1023 e6000sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
1024 {
1025         e6000sw_softc_t *sc;
1026
1027         sc = device_get_softc(dev);
1028         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
1029
1030         if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT)
1031                 return (e6000sw_set_port_vlan(sc, vg));
1032         else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q)
1033                 return (e6000sw_set_dot1q_vlan(sc, vg));
1034
1035         return (EINVAL);
1036 }
1037
1038 static int
1039 e6000sw_get_port_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
1040 {
1041         uint32_t port, reg;
1042
1043         port = vg->es_vlangroup;
1044         if (port > sc->num_ports)
1045                 return (EINVAL);
1046
1047         if (!e6000sw_is_portenabled(sc, port)) {
1048                 vg->es_vid = port;
1049                 return (0);
1050         }
1051
1052         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
1053         vg->es_untagged_ports = vg->es_member_ports = reg & PORT_MASK(sc);
1054         vg->es_vid = port | ETHERSWITCH_VID_VALID;
1055         vg->es_fid = (reg & PORT_VLAN_MAP_FID_MASK) >> PORT_VLAN_MAP_FID;
1056         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL1);
1057         vg->es_fid |= (reg & PORT_CONTROL1_FID_MASK) << 4;
1058
1059         return (0);
1060 }
1061
1062 static int
1063 e6000sw_get_dot1q_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
1064 {
1065         int i, port;
1066         uint32_t reg;
1067
1068         vg->es_fid = 0;
1069         vg->es_vid = sc->vlans[vg->es_vlangroup];
1070         vg->es_untagged_ports = vg->es_member_ports = 0;
1071         if (vg->es_vid == 0)
1072                 return (0);
1073
1074         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1075                 device_printf(sc->dev, "VTU unit is busy, cannot access\n");
1076                 return (EBUSY);
1077         }
1078
1079         e6000sw_writereg(sc, REG_GLOBAL, VTU_VID, vg->es_vid - 1);
1080
1081         reg = e6000sw_readreg(sc, REG_GLOBAL, VTU_OPERATION);
1082         reg &= ~VTU_OP_MASK;
1083         reg |= VTU_GET_NEXT | VTU_BUSY;
1084         e6000sw_writereg(sc, REG_GLOBAL, VTU_OPERATION, reg);
1085         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1086                 device_printf(sc->dev, "Timeout while reading\n");
1087                 return (EBUSY);
1088         }
1089
1090         reg = e6000sw_readreg(sc, REG_GLOBAL, VTU_VID);
1091         if (reg == VTU_VID_MASK || (reg & VTU_VID_VALID) == 0)
1092                 return (EINVAL);
1093         if ((reg & VTU_VID_MASK) != vg->es_vid)
1094                 return (EINVAL);
1095
1096         vg->es_vid |= ETHERSWITCH_VID_VALID;
1097         reg = e6000sw_readreg(sc, REG_GLOBAL, VTU_DATA);
1098         for (i = 0; i < sc->num_ports; i++) {
1099                 if (i == VTU_PPREG(sc))
1100                         reg = e6000sw_readreg(sc, REG_GLOBAL, VTU_DATA2);
1101                 port = (reg >> VTU_PORT(sc, i)) & VTU_PORT_MASK;
1102                 if (port == VTU_PORT_UNTAGGED) {
1103                         vg->es_untagged_ports |= (1 << i);
1104                         vg->es_member_ports |= (1 << i);
1105                 } else if (port == VTU_PORT_TAGGED)
1106                         vg->es_member_ports |= (1 << i);
1107         }
1108
1109         return (0);
1110 }
1111
1112 static int
1113 e6000sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
1114 {
1115         e6000sw_softc_t *sc;
1116
1117         sc = device_get_softc(dev);
1118         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
1119
1120         if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT)
1121                 return (e6000sw_get_port_vlan(sc, vg));
1122         else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q)
1123                 return (e6000sw_get_dot1q_vlan(sc, vg));
1124
1125         return (EINVAL);
1126 }
1127
1128 static __inline struct mii_data*
1129 e6000sw_miiforphy(e6000sw_softc_t *sc, unsigned int phy)
1130 {
1131
1132         if (!e6000sw_is_phyport(sc, phy))
1133                 return (NULL);
1134
1135         return (device_get_softc(sc->miibus[phy]));
1136 }
1137
1138 static int
1139 e6000sw_ifmedia_upd(struct ifnet *ifp)
1140 {
1141         e6000sw_softc_t *sc;
1142         struct mii_data *mii;
1143
1144         sc = ifp->if_softc;
1145         mii = e6000sw_miiforphy(sc, ifp->if_dunit);
1146         if (mii == NULL)
1147                 return (ENXIO);
1148         mii_mediachg(mii);
1149
1150         return (0);
1151 }
1152
1153 static void
1154 e6000sw_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1155 {
1156         e6000sw_softc_t *sc;
1157         struct mii_data *mii;
1158
1159         sc = ifp->if_softc;
1160         mii = e6000sw_miiforphy(sc, ifp->if_dunit);
1161
1162         if (mii == NULL)
1163                 return;
1164
1165         mii_pollstat(mii);
1166         ifmr->ifm_active = mii->mii_media_active;
1167         ifmr->ifm_status = mii->mii_media_status;
1168 }
1169
1170 static int
1171 e6000sw_smi_waitready(e6000sw_softc_t *sc, int phy)
1172 {
1173         int i;
1174
1175         for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
1176                 if ((MDIO_READ(sc->dev, phy, SMI_CMD) & SMI_CMD_BUSY) == 0)
1177                         return (0);
1178                 DELAY(1);
1179         }
1180
1181         return (1);
1182 }
1183
1184 static __inline uint32_t
1185 e6000sw_readreg(e6000sw_softc_t *sc, int addr, int reg)
1186 {
1187
1188         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
1189
1190         if (!MVSWITCH_MULTICHIP(sc))
1191                 return (MDIO_READ(sc->dev, addr, reg) & 0xffff);
1192
1193         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
1194                 printf("e6000sw: readreg timeout\n");
1195                 return (0xffff);
1196         }
1197         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
1198             SMI_CMD_OP_C22_READ | (reg & SMI_CMD_REG_ADDR_MASK) |
1199             ((addr << SMI_CMD_DEV_ADDR) & SMI_CMD_DEV_ADDR_MASK));
1200         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
1201                 printf("e6000sw: readreg timeout\n");
1202                 return (0xffff);
1203         }
1204
1205         return (MDIO_READ(sc->dev, sc->sw_addr, SMI_DATA) & 0xffff);
1206 }
1207
1208 static __inline void
1209 e6000sw_writereg(e6000sw_softc_t *sc, int addr, int reg, int val)
1210 {
1211
1212         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
1213
1214         if (!MVSWITCH_MULTICHIP(sc)) {
1215                 MDIO_WRITE(sc->dev, addr, reg, val);
1216                 return;
1217         }
1218
1219         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
1220                 printf("e6000sw: readreg timeout\n");
1221                 return;
1222         }
1223         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_DATA, val);
1224         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
1225             SMI_CMD_OP_C22_WRITE | (reg & SMI_CMD_REG_ADDR_MASK) |
1226             ((addr << SMI_CMD_DEV_ADDR) & SMI_CMD_DEV_ADDR_MASK));
1227 }
1228
1229 static __inline bool
1230 e6000sw_is_cpuport(e6000sw_softc_t *sc, int port)
1231 {
1232
1233         return ((sc->cpuports_mask & (1 << port)) ? true : false);
1234 }
1235
1236 static __inline bool
1237 e6000sw_is_fixedport(e6000sw_softc_t *sc, int port)
1238 {
1239
1240         return ((sc->fixed_mask & (1 << port)) ? true : false);
1241 }
1242
1243 static __inline bool
1244 e6000sw_is_fixed25port(e6000sw_softc_t *sc, int port)
1245 {
1246
1247         return ((sc->fixed25_mask & (1 << port)) ? true : false);
1248 }
1249
1250 static __inline bool
1251 e6000sw_is_phyport(e6000sw_softc_t *sc, int port)
1252 {
1253         uint32_t phy_mask;
1254         phy_mask = ~(sc->fixed_mask | sc->cpuports_mask);
1255
1256         return ((phy_mask & (1 << port)) ? true : false);
1257 }
1258
1259 static __inline bool
1260 e6000sw_is_portenabled(e6000sw_softc_t *sc, int port)
1261 {
1262
1263         return ((sc->ports_mask & (1 << port)) ? true : false);
1264 }
1265
1266 static __inline void
1267 e6000sw_set_pvid(e6000sw_softc_t *sc, int port, int pvid)
1268 {
1269         uint32_t reg;
1270
1271         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1272         reg &= ~PORT_VID_DEF_VID_MASK;
1273         reg |= (pvid & PORT_VID_DEF_VID_MASK);
1274         e6000sw_writereg(sc, REG_PORT(port), PORT_VID, reg);
1275 }
1276
1277 static __inline int
1278 e6000sw_get_pvid(e6000sw_softc_t *sc, int port, int *pvid)
1279 {
1280
1281         if (pvid == NULL)
1282                 return (ENXIO);
1283
1284         *pvid = e6000sw_readreg(sc, REG_PORT(port), PORT_VID) &
1285             PORT_VID_DEF_VID_MASK;
1286
1287         return (0);
1288 }
1289
1290 /*
1291  * Convert port status to ifmedia.
1292  */
1293 static void
1294 e6000sw_update_ifmedia(uint16_t portstatus, u_int *media_status, u_int *media_active)
1295 {
1296         *media_active = IFM_ETHER;
1297         *media_status = IFM_AVALID;
1298
1299         if ((portstatus & PORT_STATUS_LINK_MASK) != 0)
1300                 *media_status |= IFM_ACTIVE;
1301         else {
1302                 *media_active |= IFM_NONE;
1303                 return;
1304         }
1305
1306         switch (portstatus & PORT_STATUS_SPEED_MASK) {
1307         case PORT_STATUS_SPEED_10:
1308                 *media_active |= IFM_10_T;
1309                 break;
1310         case PORT_STATUS_SPEED_100:
1311                 *media_active |= IFM_100_TX;
1312                 break;
1313         case PORT_STATUS_SPEED_1000:
1314                 *media_active |= IFM_1000_T;
1315                 break;
1316         }
1317
1318         if ((portstatus & PORT_STATUS_DUPLEX_MASK) == 0)
1319                 *media_active |= IFM_FDX;
1320         else
1321                 *media_active |= IFM_HDX;
1322 }
1323
1324 static void
1325 e6000sw_tick(void *arg)
1326 {
1327         e6000sw_softc_t *sc;
1328         struct mii_data *mii;
1329         struct mii_softc *miisc;
1330         uint16_t portstatus;
1331         int port;
1332
1333         sc = arg;
1334
1335         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
1336
1337         for (;;) {
1338                 E6000SW_LOCK(sc);
1339                 for (port = 0; port < sc->num_ports; port++) {
1340                         /* Tick only on PHY ports */
1341                         if (!e6000sw_is_portenabled(sc, port) ||
1342                             !e6000sw_is_phyport(sc, port))
1343                                 continue;
1344
1345                         mii = e6000sw_miiforphy(sc, port);
1346                         if (mii == NULL)
1347                                 continue;
1348
1349                         portstatus = e6000sw_readreg(sc, REG_PORT(port),
1350                             PORT_STATUS);
1351
1352                         e6000sw_update_ifmedia(portstatus,
1353                             &mii->mii_media_status, &mii->mii_media_active);
1354
1355                         LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1356                                 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media)
1357                                     != miisc->mii_inst)
1358                                         continue;
1359                                 mii_phy_update(miisc, MII_POLLSTAT);
1360                         }
1361                 }
1362                 E6000SW_UNLOCK(sc);
1363                 pause("e6000sw tick", 1000);
1364         }
1365 }
1366
1367 static void
1368 e6000sw_setup(device_t dev, e6000sw_softc_t *sc)
1369 {
1370         uint32_t atu_ctrl;
1371
1372         /* Set aging time. */
1373         atu_ctrl = e6000sw_readreg(sc, REG_GLOBAL, ATU_CONTROL);
1374         atu_ctrl &= ~ATU_CONTROL_AGETIME_MASK;
1375         atu_ctrl |= E6000SW_DEFAULT_AGETIME << ATU_CONTROL_AGETIME;
1376         e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL, atu_ctrl);
1377
1378         /* Send all with specific mac address to cpu port */
1379         e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_2x, MGMT_EN_ALL);
1380         e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_0x, MGMT_EN_ALL);
1381
1382         /* Disable Remote Management */
1383         e6000sw_writereg(sc, REG_GLOBAL, SWITCH_GLOBAL_CONTROL2, 0);
1384
1385         /* Disable loopback filter and flow control messages */
1386         e6000sw_writereg(sc, REG_GLOBAL2, SWITCH_MGMT,
1387             SWITCH_MGMT_PRI_MASK |
1388             (1 << SWITCH_MGMT_RSVD2CPU) |
1389             SWITCH_MGMT_FC_PRI_MASK |
1390             (1 << SWITCH_MGMT_FORCEFLOW));
1391
1392         e6000sw_atu_flush(dev, sc, NO_OPERATION);
1393         e6000sw_atu_mac_table(dev, sc, NULL, NO_OPERATION);
1394         e6000sw_set_atustat(dev, sc, 0, COUNT_ALL);
1395 }
1396
1397 static void
1398 e6000sw_set_atustat(device_t dev, e6000sw_softc_t *sc, int bin, int flag)
1399 {
1400         uint16_t ret;
1401
1402         ret = e6000sw_readreg(sc, REG_GLOBAL2, ATU_STATS);
1403         e6000sw_writereg(sc, REG_GLOBAL2, ATU_STATS, (bin << ATU_STATS_BIN ) |
1404             (flag << ATU_STATS_FLAG));
1405 }
1406
1407 static int
1408 e6000sw_atu_mac_table(device_t dev, e6000sw_softc_t *sc, struct atu_opt *atu,
1409     int flag)
1410 {
1411         uint16_t ret_opt;
1412         uint16_t ret_data;
1413
1414         if (flag == NO_OPERATION)
1415                 return (0);
1416         else if ((flag & (LOAD_FROM_FIB | PURGE_FROM_FIB | GET_NEXT_IN_FIB |
1417             GET_VIOLATION_DATA | CLEAR_VIOLATION_DATA)) == 0) {
1418                 device_printf(dev, "Wrong Opcode for ATU operation\n");
1419                 return (EINVAL);
1420         }
1421
1422         if (E6000SW_WAITREADY(sc, ATU_OPERATION, ATU_UNIT_BUSY)) {
1423                 device_printf(dev, "ATU unit is busy, cannot access\n");
1424                 return (EBUSY);
1425         }
1426
1427         ret_opt = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1428         if (flag & LOAD_FROM_FIB) {
1429                 ret_data = e6000sw_readreg(sc, REG_GLOBAL, ATU_DATA);
1430                 e6000sw_writereg(sc, REG_GLOBAL2, ATU_DATA, (ret_data &
1431                     ~ENTRY_STATE));
1432         }
1433         e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR01, atu->mac_01);
1434         e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR23, atu->mac_23);
1435         e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR45, atu->mac_45);
1436         e6000sw_writereg(sc, REG_GLOBAL, ATU_FID, atu->fid);
1437
1438         e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION,
1439             (ret_opt | ATU_UNIT_BUSY | flag));
1440
1441         if (E6000SW_WAITREADY(sc, ATU_OPERATION, ATU_UNIT_BUSY))
1442                 device_printf(dev, "Timeout while waiting ATU\n");
1443         else if (flag & GET_NEXT_IN_FIB) {
1444                 atu->mac_01 = e6000sw_readreg(sc, REG_GLOBAL,
1445                     ATU_MAC_ADDR01);
1446                 atu->mac_23 = e6000sw_readreg(sc, REG_GLOBAL,
1447                     ATU_MAC_ADDR23);
1448                 atu->mac_45 = e6000sw_readreg(sc, REG_GLOBAL,
1449                     ATU_MAC_ADDR45);
1450         }
1451
1452         return (0);
1453 }
1454
1455 static int
1456 e6000sw_atu_flush(device_t dev, e6000sw_softc_t *sc, int flag)
1457 {
1458         uint32_t reg;
1459
1460         if (flag == NO_OPERATION)
1461                 return (0);
1462
1463         if (E6000SW_WAITREADY(sc, ATU_OPERATION, ATU_UNIT_BUSY)) {
1464                 device_printf(dev, "ATU unit is busy, cannot access\n");
1465                 return (EBUSY);
1466         }
1467         reg = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1468         e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION,
1469             (reg | ATU_UNIT_BUSY | flag));
1470         if (E6000SW_WAITREADY(sc, ATU_OPERATION, ATU_UNIT_BUSY))
1471                 device_printf(dev, "Timeout while flushing ATU\n");
1472
1473         return (0);
1474 }
1475
1476 static int
1477 e6000sw_vtu_flush(e6000sw_softc_t *sc)
1478 {
1479
1480         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1481                 device_printf(sc->dev, "VTU unit is busy, cannot access\n");
1482                 return (EBUSY);
1483         }
1484
1485         e6000sw_writereg(sc, REG_GLOBAL, VTU_OPERATION, VTU_FLUSH | VTU_BUSY);
1486         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1487                 device_printf(sc->dev, "Timeout while flushing VTU\n");
1488                 return (ETIMEDOUT);
1489         }
1490
1491         return (0);
1492 }
1493
1494 static int
1495 e6000sw_vtu_update(e6000sw_softc_t *sc, int purge, int vid, int fid,
1496     int members, int untagged)
1497 {
1498         int i, op;
1499         uint32_t data[2];
1500
1501         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1502                 device_printf(sc->dev, "VTU unit is busy, cannot access\n");
1503                 return (EBUSY);
1504         }
1505
1506         *data = (vid & VTU_VID_MASK);
1507         if (purge == 0)
1508                 *data |= VTU_VID_VALID;
1509         e6000sw_writereg(sc, REG_GLOBAL, VTU_VID, *data);
1510
1511         if (purge == 0) {
1512                 data[0] = 0;
1513                 data[1] = 0;
1514                 for (i = 0; i < sc->num_ports; i++) {
1515                         if ((untagged & (1 << i)) != 0)
1516                                 data[i / VTU_PPREG(sc)] |=
1517                                     VTU_PORT_UNTAGGED << VTU_PORT(sc, i);
1518                         else if ((members & (1 << i)) != 0)
1519                                 data[i / VTU_PPREG(sc)] |=
1520                                     VTU_PORT_TAGGED << VTU_PORT(sc, i);
1521                         else
1522                                 data[i / VTU_PPREG(sc)] |=
1523                                     VTU_PORT_DISCARD << VTU_PORT(sc, i);
1524                 }
1525                 e6000sw_writereg(sc, REG_GLOBAL, VTU_DATA, data[0]);
1526                 e6000sw_writereg(sc, REG_GLOBAL, VTU_DATA2, data[1]);
1527                 e6000sw_writereg(sc, REG_GLOBAL, VTU_FID,
1528                     fid & VTU_FID_MASK(sc));
1529                 op = VTU_LOAD;
1530         } else
1531                 op = VTU_PURGE;
1532
1533         e6000sw_writereg(sc, REG_GLOBAL, VTU_OPERATION, op | VTU_BUSY);
1534         if (E6000SW_WAITREADY(sc, VTU_OPERATION, VTU_BUSY)) {
1535                 device_printf(sc->dev, "Timeout while flushing VTU\n");
1536                 return (ETIMEDOUT);
1537         }
1538
1539         return (0);
1540 }