]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/etherswitch/e6000sw/e6000sw.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / sys / dev / etherswitch / e6000sw / e6000sw.c
1 /*-
2  * Copyright (c) 2015 Semihalf
3  * Copyright (c) 2015 Stormshield
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/module.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/if_types.h>
43
44 #include <dev/etherswitch/etherswitch.h>
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
50
51 #include "e6000swreg.h"
52 #include "etherswitch_if.h"
53 #include "miibus_if.h"
54 #include "mdio_if.h"
55
56 MALLOC_DECLARE(M_E6000SW);
57 MALLOC_DEFINE(M_E6000SW, "e6000sw", "e6000sw switch");
58
59 #define E6000SW_LOCK(_sc)               sx_xlock(&(_sc)->sx)
60 #define E6000SW_UNLOCK(_sc)             sx_unlock(&(_sc)->sx)
61 #define E6000SW_LOCK_ASSERT(_sc, _what) sx_assert(&(_sc)->sx, (_what))
62 #define E6000SW_TRYLOCK(_sc)            sx_tryxlock(&(_sc)->sx)
63
64 typedef struct e6000sw_softc {
65         device_t                dev;
66         phandle_t               node;
67
68         struct sx               sx;
69         struct ifnet            *ifp[E6000SW_MAX_PORTS];
70         char                    *ifname[E6000SW_MAX_PORTS];
71         device_t                miibus[E6000SW_MAX_PORTS];
72         struct proc             *kproc;
73
74         uint32_t                swid;
75         uint32_t                cpuports_mask;
76         uint32_t                fixed_mask;
77         uint32_t                fixed25_mask;
78         uint32_t                ports_mask;
79         int                     phy_base;
80         int                     sw_addr;
81         int                     num_ports;
82         boolean_t               multi_chip;
83
84         int                     vid[E6000SW_NUM_VGROUPS];
85         int                     members[E6000SW_NUM_VGROUPS];
86         int                     vgroup[E6000SW_MAX_PORTS];
87 } e6000sw_softc_t;
88
89 static etherswitch_info_t etherswitch_info = {
90         .es_nports =            0,
91         .es_nvlangroups =       E6000SW_NUM_VGROUPS,
92         .es_vlan_caps =         ETHERSWITCH_VLAN_PORT,
93         .es_name =              "Marvell 6000 series switch"
94 };
95
96 static void e6000sw_identify(driver_t *, device_t);
97 static int e6000sw_probe(device_t);
98 static int e6000sw_attach(device_t);
99 static int e6000sw_detach(device_t);
100 static int e6000sw_readphy(device_t, int, int);
101 static int e6000sw_writephy(device_t, int, int, int);
102 static etherswitch_info_t* e6000sw_getinfo(device_t);
103 static int e6000sw_getconf(device_t, etherswitch_conf_t *);
104 static void e6000sw_lock(device_t);
105 static void e6000sw_unlock(device_t);
106 static int e6000sw_getport(device_t, etherswitch_port_t *);
107 static int e6000sw_setport(device_t, etherswitch_port_t *);
108 static int e6000sw_readreg_wrapper(device_t, int);
109 static int e6000sw_writereg_wrapper(device_t, int, int);
110 static int e6000sw_readphy_wrapper(device_t, int, int);
111 static int e6000sw_writephy_wrapper(device_t, int, int, int);
112 static int e6000sw_getvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
113 static int e6000sw_setvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
114 static int e6000sw_setvgroup(device_t, etherswitch_vlangroup_t *);
115 static int e6000sw_getvgroup(device_t, etherswitch_vlangroup_t *);
116 static void e6000sw_setup(device_t, e6000sw_softc_t *);
117 static void e6000sw_port_vlan_conf(e6000sw_softc_t *);
118 static void e6000sw_tick(void *);
119 static void e6000sw_set_atustat(device_t, e6000sw_softc_t *, int, int);
120 static int e6000sw_atu_flush(device_t, e6000sw_softc_t *, int);
121 static __inline void e6000sw_writereg(e6000sw_softc_t *, int, int, int);
122 static __inline uint32_t e6000sw_readreg(e6000sw_softc_t *, int, int);
123 static int e6000sw_ifmedia_upd(struct ifnet *);
124 static void e6000sw_ifmedia_sts(struct ifnet *, struct ifmediareq *);
125 static int e6000sw_atu_mac_table(device_t, e6000sw_softc_t *, struct atu_opt *,
126     int);
127 static int e6000sw_get_pvid(e6000sw_softc_t *, int, int *);
128 static int e6000sw_set_pvid(e6000sw_softc_t *, int, int);
129 static __inline bool e6000sw_is_cpuport(e6000sw_softc_t *, int);
130 static __inline bool e6000sw_is_fixedport(e6000sw_softc_t *, int);
131 static __inline bool e6000sw_is_fixed25port(e6000sw_softc_t *, int);
132 static __inline bool e6000sw_is_phyport(e6000sw_softc_t *, int);
133 static __inline bool e6000sw_is_portenabled(e6000sw_softc_t *, int);
134 static __inline struct mii_data *e6000sw_miiforphy(e6000sw_softc_t *,
135     unsigned int);
136
137 static device_method_t e6000sw_methods[] = {
138         /* device interface */
139         DEVMETHOD(device_identify,              e6000sw_identify),
140         DEVMETHOD(device_probe,                 e6000sw_probe),
141         DEVMETHOD(device_attach,                e6000sw_attach),
142         DEVMETHOD(device_detach,                e6000sw_detach),
143
144         /* bus interface */
145         DEVMETHOD(bus_add_child,                device_add_child_ordered),
146
147         /* mii interface */
148         DEVMETHOD(miibus_readreg,               e6000sw_readphy),
149         DEVMETHOD(miibus_writereg,              e6000sw_writephy),
150
151         /* etherswitch interface */
152         DEVMETHOD(etherswitch_getinfo,          e6000sw_getinfo),
153         DEVMETHOD(etherswitch_getconf,          e6000sw_getconf),
154         DEVMETHOD(etherswitch_lock,             e6000sw_lock),
155         DEVMETHOD(etherswitch_unlock,           e6000sw_unlock),
156         DEVMETHOD(etherswitch_getport,          e6000sw_getport),
157         DEVMETHOD(etherswitch_setport,          e6000sw_setport),
158         DEVMETHOD(etherswitch_readreg,          e6000sw_readreg_wrapper),
159         DEVMETHOD(etherswitch_writereg,         e6000sw_writereg_wrapper),
160         DEVMETHOD(etherswitch_readphyreg,       e6000sw_readphy_wrapper),
161         DEVMETHOD(etherswitch_writephyreg,      e6000sw_writephy_wrapper),
162         DEVMETHOD(etherswitch_setvgroup,        e6000sw_setvgroup_wrapper),
163         DEVMETHOD(etherswitch_getvgroup,        e6000sw_getvgroup_wrapper),
164
165         DEVMETHOD_END
166 };
167
168 static devclass_t e6000sw_devclass;
169
170 DEFINE_CLASS_0(e6000sw, e6000sw_driver, e6000sw_methods,
171     sizeof(e6000sw_softc_t));
172
173 DRIVER_MODULE(e6000sw, mdio, e6000sw_driver, e6000sw_devclass, 0, 0);
174 DRIVER_MODULE(etherswitch, e6000sw, etherswitch_driver, etherswitch_devclass, 0,
175     0);
176 DRIVER_MODULE(miibus, e6000sw, miibus_driver, miibus_devclass, 0, 0);
177 MODULE_DEPEND(e6000sw, mdio, 1, 1, 1);
178
179 #define SMI_CMD                 0
180 #define SMI_CMD_BUSY            (1 << 15)
181 #define SMI_CMD_OP_READ         ((2 << 10) | SMI_CMD_BUSY | (1 << 12))
182 #define SMI_CMD_OP_WRITE        ((1 << 10) | SMI_CMD_BUSY | (1 << 12))
183 #define SMI_DATA                1
184
185 #define MDIO_READ(dev, addr, reg)                                       \
186         MDIO_READREG(device_get_parent(dev), (addr), (reg))
187 #define MDIO_WRITE(dev, addr, reg, val)                                 \
188         MDIO_WRITEREG(device_get_parent(dev), (addr), (reg), (val))
189
190 static void
191 e6000sw_identify(driver_t *driver, device_t parent)
192 {
193
194         if (device_find_child(parent, "e6000sw", -1) == NULL)
195                 BUS_ADD_CHILD(parent, 0, "e6000sw", -1);
196 }
197
198 static int
199 e6000sw_probe(device_t dev)
200 {
201         e6000sw_softc_t *sc;
202         const char *description;
203         phandle_t dsa_node, switch_node;
204
205         dsa_node = fdt_find_compatible(OF_finddevice("/"),
206             "marvell,dsa", 0);
207         switch_node = OF_child(dsa_node);
208
209         if (switch_node == 0)
210                 return (ENXIO);
211
212         sc = device_get_softc(dev);
213         sc->dev = dev;
214         sc->node = switch_node;
215
216         if (OF_getencprop(sc->node, "reg", &sc->sw_addr,
217             sizeof(sc->sw_addr)) < 0)
218                 return (ENXIO);
219         if (sc->sw_addr != 0 && (sc->sw_addr % 2) == 0)
220                 sc->multi_chip = true;
221
222         /*
223          * Create temporary lock, just to satisfy assertions,
224          * when obtaining the switch ID. Destroy immediately afterwards.
225          */
226         sx_init(&sc->sx, "e6000sw_tmp");
227         E6000SW_LOCK(sc);
228         sc->swid = e6000sw_readreg(sc, REG_PORT(0), SWITCH_ID) & 0xfff0;
229         E6000SW_UNLOCK(sc);
230         sx_destroy(&sc->sx);
231
232         switch (sc->swid) {
233         case MV88E6141:
234                 description = "Marvell 88E6141";
235                 sc->phy_base = 0x10;
236                 sc->num_ports = 6;
237                 break;
238         case MV88E6341:
239                 description = "Marvell 88E6341";
240                 sc->phy_base = 0x10;
241                 sc->num_ports = 6;
242                 break;
243         case MV88E6352:
244                 description = "Marvell 88E6352";
245                 sc->num_ports = 7;
246                 break;
247         case MV88E6172:
248                 description = "Marvell 88E6172";
249                 sc->num_ports = 7;
250                 break;
251         case MV88E6176:
252                 description = "Marvell 88E6176";
253                 sc->num_ports = 7;
254                 break;
255         default:
256                 device_printf(dev, "Unrecognized device, id 0x%x.\n", sc->swid);
257                 return (ENXIO);
258         }
259
260         device_set_desc(dev, description);
261
262         return (BUS_PROBE_DEFAULT);
263 }
264
265 static int
266 e6000sw_parse_child_fdt(e6000sw_softc_t *sc, phandle_t child, int *pport,
267     int *pvlangroup)
268 {
269         char *name, *portlabel;
270         int speed;
271         phandle_t fixed_link;
272         uint32_t port, vlangroup;
273
274         if (pport == NULL || pvlangroup == NULL)
275                 return (ENXIO);
276
277         if (OF_getencprop(child, "reg", (void *)&port, sizeof(port)) < 0)
278                 return (ENXIO);
279         if (port >= sc->num_ports)
280                 return (ENXIO);
281         *pport = port;
282
283         if (OF_getencprop(child, "vlangroup", (void *)&vlangroup,
284             sizeof(vlangroup)) > 0) {
285                 if (vlangroup >= E6000SW_NUM_VGROUPS)
286                         return (ENXIO);
287                 *pvlangroup = vlangroup;
288         } else {
289                 *pvlangroup = -1;
290         }
291
292         if (OF_getprop_alloc(child, "label", 1, (void **)&portlabel) > 0) {
293                 if (strncmp(portlabel, "cpu", 3) == 0) {
294                         device_printf(sc->dev, "CPU port at %d\n", port);
295                         sc->cpuports_mask |= (1 << port);
296                         sc->fixed_mask |= (1 << port);
297                 }
298                 free(portlabel, M_OFWPROP);
299         }
300
301         fixed_link = OF_child(child);
302         if (fixed_link != 0 &&
303             OF_getprop_alloc(fixed_link, "name", 1, (void **)&name) > 0) {
304                 if (strncmp(name, "fixed-link", 10) == 0) {
305                         /* Assume defaults: 1g - full-duplex. */
306                         sc->fixed_mask |= (1 << port);
307                         if (OF_getencprop(fixed_link, "speed", &speed,
308                              sizeof(speed)) > 0) {
309                                 if (speed == 2500 &&
310                                     (MVSWITCH(sc, MV88E6141) ||
311                                      MVSWITCH(sc, MV88E6341))) {
312                                         sc->fixed25_mask |= (1 << port);
313                                 }
314                         }
315                 }
316                 free(name, M_OFWPROP);
317         }
318         if ((sc->fixed_mask & (1 << port)) != 0)
319                 device_printf(sc->dev, "fixed port at %d\n", port);
320         else
321                 device_printf(sc->dev, "PHY at port %d\n", port);
322
323         return (0);
324 }
325
326 static int
327 e6000sw_init_interface(e6000sw_softc_t *sc, int port)
328 {
329         char name[IFNAMSIZ];
330
331         snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->dev));
332
333         sc->ifp[port] = if_alloc(IFT_ETHER);
334         if (sc->ifp[port] == NULL)
335                 return (ENOMEM);
336         sc->ifp[port]->if_softc = sc;
337         sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
338             IFF_DRV_RUNNING | IFF_SIMPLEX;
339         sc->ifname[port] = malloc(strlen(name) + 1, M_E6000SW, M_NOWAIT);
340         if (sc->ifname[port] == NULL) {
341                 if_free(sc->ifp[port]);
342                 return (ENOMEM);
343         }
344         memcpy(sc->ifname[port], name, strlen(name) + 1);
345         if_initname(sc->ifp[port], sc->ifname[port], port);
346
347         return (0);
348 }
349
350 static int
351 e6000sw_attach_miibus(e6000sw_softc_t *sc, int port)
352 {
353         int err;
354
355         err = mii_attach(sc->dev, &sc->miibus[port], sc->ifp[port],
356             e6000sw_ifmedia_upd, e6000sw_ifmedia_sts, BMSR_DEFCAPMASK,
357             port + sc->phy_base, MII_OFFSET_ANY, 0);
358         if (err != 0)
359                 return (err);
360
361         return (0);
362 }
363
364 static int
365 e6000sw_attach(device_t dev)
366 {
367         etherswitch_vlangroup_t vg;
368         e6000sw_softc_t *sc;
369         phandle_t child;
370         int err, port, vlangroup;
371         int member_ports[E6000SW_NUM_VGROUPS];
372         uint32_t reg;
373
374         err = 0;
375         sc = device_get_softc(dev);
376
377         if (sc->multi_chip)
378                 device_printf(dev, "multi-chip addressing mode\n");
379         else
380                 device_printf(dev, "single-chip addressing mode\n");
381
382         sx_init(&sc->sx, "e6000sw");
383
384         E6000SW_LOCK(sc);
385         e6000sw_setup(dev, sc);
386         bzero(member_ports, sizeof(member_ports));
387
388         for (child = OF_child(sc->node); child != 0; child = OF_peer(child)) {
389                 err = e6000sw_parse_child_fdt(sc, child, &port, &vlangroup);
390                 if (err != 0) {
391                         device_printf(sc->dev, "failed to parse DTS\n");
392                         goto out_fail;
393                 }
394
395                 if (vlangroup != -1)
396                         member_ports[vlangroup] |= (1 << port);
397
398                 /* Port is in use. */
399                 sc->ports_mask |= (1 << port);
400
401                 err = e6000sw_init_interface(sc, port);
402                 if (err != 0) {
403                         device_printf(sc->dev, "failed to init interface\n");
404                         goto out_fail;
405                 }
406
407                 if (e6000sw_is_fixedport(sc, port)) {
408                         /* Link must be down to change speed force value. */
409                         reg = e6000sw_readreg(sc, REG_PORT(port), PSC_CONTROL);
410                         reg &= ~PSC_CONTROL_LINK_UP;
411                         reg |= PSC_CONTROL_FORCED_LINK;
412                         e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
413
414                         /*
415                          * Force speed, full-duplex, EEE off and flow-control
416                          * on.
417                          */
418                         if (e6000sw_is_fixed25port(sc, port))
419                                 reg = PSC_CONTROL_SPD2500;
420                         else
421                                 reg = PSC_CONTROL_SPD1000;
422                         reg |= PSC_CONTROL_FORCED_DPX | PSC_CONTROL_FULLDPX |
423                             PSC_CONTROL_FORCED_LINK | PSC_CONTROL_LINK_UP |
424                             PSC_CONTROL_FORCED_FC | PSC_CONTROL_FC_ON |
425                             PSC_CONTROL_FORCED_SPD;
426                         if (MVSWITCH(sc, MV88E6141) || MVSWITCH(sc, MV88E6341))
427                             reg |= PSC_CONTROL_FORCED_EEE;
428                         e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
429                 }
430
431                 /* Don't attach miibus at CPU/fixed ports */
432                 if (!e6000sw_is_phyport(sc, port))
433                         continue;
434
435                 err = e6000sw_attach_miibus(sc, port);
436                 if (err != 0) {
437                         device_printf(sc->dev, "failed to attach miibus\n");
438                         goto out_fail;
439                 }
440         }
441
442         etherswitch_info.es_nports = sc->num_ports;
443         for (port = 0; port < sc->num_ports; port++)
444                 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
445
446         /* Set VLAN configuration */
447         e6000sw_port_vlan_conf(sc);
448
449         /* Set vlangroups */
450         for (vlangroup = 0; vlangroup < E6000SW_NUM_VGROUPS; vlangroup++)
451                 if (member_ports[vlangroup] != 0) {
452                         vg.es_vlangroup = vg.es_vid = vlangroup;
453                         vg.es_member_ports = vg.es_untagged_ports =
454                             member_ports[vlangroup];
455                         e6000sw_setvgroup(dev, &vg);
456                 }
457
458         E6000SW_UNLOCK(sc);
459
460         bus_generic_probe(dev);
461         bus_generic_attach(dev);
462
463         kproc_create(e6000sw_tick, sc, &sc->kproc, 0, 0, "e6000sw tick kproc");
464
465         return (0);
466
467 out_fail:
468         E6000SW_UNLOCK(sc);
469         e6000sw_detach(dev);
470
471         return (err);
472 }
473
474 static __inline int
475 e6000sw_poll_done(e6000sw_softc_t *sc)
476 {
477         int i;
478
479         for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
480
481                 if ((e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG) &
482                     (1 << PHY_CMD_SMI_BUSY)) == 0)
483                         return (0);
484
485                 pause("e6000sw PHY poll", hz/1000);
486         }
487
488         return (ETIMEDOUT);
489 }
490
491 /*
492  * PHY registers are paged. Put page index in reg 22 (accessible from every
493  * page), then access specific register.
494  */
495 static int
496 e6000sw_readphy(device_t dev, int phy, int reg)
497 {
498         e6000sw_softc_t *sc;
499         uint32_t val;
500         int err;
501
502         sc = device_get_softc(dev);
503         if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
504                 device_printf(dev, "Wrong register address.\n");
505                 return (EINVAL);
506         }
507
508         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
509
510         err = e6000sw_poll_done(sc);
511         if (err != 0) {
512                 device_printf(dev, "Timeout while waiting for switch\n");
513                 return (err);
514         }
515
516         val = 1 << PHY_CMD_SMI_BUSY;
517         val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
518         val |= PHY_CMD_OPCODE_READ << PHY_CMD_OPCODE;
519         val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
520         val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
521         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
522
523         err = e6000sw_poll_done(sc);
524         if (err != 0) {
525                 device_printf(dev, "Timeout while waiting for switch\n");
526                 return (err);
527         }
528
529         val = e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG);
530
531         return (val & PHY_DATA_MASK);
532 }
533
534 static int
535 e6000sw_writephy(device_t dev, int phy, int reg, int data)
536 {
537         e6000sw_softc_t *sc;
538         uint32_t val;
539         int err;
540
541         sc = device_get_softc(dev);
542         if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
543                 device_printf(dev, "Wrong register address.\n");
544                 return (EINVAL);
545         }
546
547         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
548
549         err = e6000sw_poll_done(sc);
550         if (err != 0) {
551                 device_printf(dev, "Timeout while waiting for switch\n");
552                 return (err);
553         }
554
555         val = 1 << PHY_CMD_SMI_BUSY;
556         val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
557         val |= PHY_CMD_OPCODE_WRITE << PHY_CMD_OPCODE;
558         val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
559         val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
560         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG,
561             data & PHY_DATA_MASK);
562         e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
563
564         err = e6000sw_poll_done(sc);
565         if (err != 0)
566                 device_printf(dev, "Timeout while waiting for switch\n");
567
568         return (err);
569 }
570
571 static int
572 e6000sw_detach(device_t dev)
573 {
574         int phy;
575         e6000sw_softc_t *sc;
576
577         sc = device_get_softc(dev);
578         bus_generic_detach(dev);
579         sx_destroy(&sc->sx);
580         for (phy = 0; phy < sc->num_ports; phy++) {
581                 if (sc->miibus[phy] != NULL)
582                         device_delete_child(dev, sc->miibus[phy]);
583                 if (sc->ifp[phy] != NULL)
584                         if_free(sc->ifp[phy]);
585                 if (sc->ifname[phy] != NULL)
586                         free(sc->ifname[phy], M_E6000SW);
587         }
588
589         return (0);
590 }
591
592 static etherswitch_info_t*
593 e6000sw_getinfo(device_t dev)
594 {
595
596         return (&etherswitch_info);
597 }
598
599 static int
600 e6000sw_getconf(device_t dev __unused, etherswitch_conf_t *conf)
601 {
602
603         /* Return the VLAN mode. */
604         conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
605         conf->vlan_mode = ETHERSWITCH_VLAN_PORT;
606
607         return (0);
608 }
609
610 static void
611 e6000sw_lock(device_t dev)
612 {
613         struct e6000sw_softc *sc;
614
615         sc = device_get_softc(dev);
616
617         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
618         E6000SW_LOCK(sc);
619 }
620
621 static void
622 e6000sw_unlock(device_t dev)
623 {
624         struct e6000sw_softc *sc;
625
626         sc = device_get_softc(dev);
627
628         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
629         E6000SW_UNLOCK(sc);
630 }
631
632 static int
633 e6000sw_getport(device_t dev, etherswitch_port_t *p)
634 {
635         struct mii_data *mii;
636         int err;
637         struct ifmediareq *ifmr;
638
639         e6000sw_softc_t *sc = device_get_softc(dev);
640         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
641
642         if (p->es_port >= sc->num_ports || p->es_port < 0)
643                 return (EINVAL);
644         if (!e6000sw_is_portenabled(sc, p->es_port))
645                 return (0);
646
647         err = 0;
648         E6000SW_LOCK(sc);
649         e6000sw_get_pvid(sc, p->es_port, &p->es_pvid);
650
651         if (e6000sw_is_fixedport(sc, p->es_port)) {
652                 if (e6000sw_is_cpuport(sc, p->es_port))
653                         p->es_flags |= ETHERSWITCH_PORT_CPU;
654                 ifmr = &p->es_ifmr;
655                 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
656                 ifmr->ifm_count = 0;
657                 if (e6000sw_is_fixed25port(sc, p->es_port))
658                         ifmr->ifm_active = IFM_2500_T;
659                 else
660                         ifmr->ifm_active = IFM_1000_T;
661                 ifmr->ifm_active |= IFM_ETHER | IFM_FDX;
662                 ifmr->ifm_current = ifmr->ifm_active;
663                 ifmr->ifm_mask = 0;
664         } else {
665                 mii = e6000sw_miiforphy(sc, p->es_port);
666                 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
667                     &mii->mii_media, SIOCGIFMEDIA);
668         }
669         E6000SW_UNLOCK(sc);
670
671         return (err);
672 }
673
674 static int
675 e6000sw_setport(device_t dev, etherswitch_port_t *p)
676 {
677         e6000sw_softc_t *sc;
678         int err;
679         struct mii_data *mii;
680
681         sc = device_get_softc(dev);
682         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
683
684         if (p->es_port >= sc->num_ports || p->es_port < 0)
685                 return (EINVAL);
686         if (!e6000sw_is_portenabled(sc, p->es_port))
687                 return (0);
688
689         err = 0;
690         E6000SW_LOCK(sc);
691         if (p->es_pvid != 0)
692                 e6000sw_set_pvid(sc, p->es_port, p->es_pvid);
693         if (e6000sw_is_phyport(sc, p->es_port)) {
694                 mii = e6000sw_miiforphy(sc, p->es_port);
695                 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, &mii->mii_media,
696                     SIOCSIFMEDIA);
697         }
698         E6000SW_UNLOCK(sc);
699
700         return (err);
701 }
702
703 /*
704  * Registers in this switch are divided into sections, specified in
705  * documentation. So as to access any of them, section index and reg index
706  * is necessary. etherswitchcfg uses only one variable, so indexes were
707  * compressed into addr_reg: 32 * section_index + reg_index.
708  */
709 static int
710 e6000sw_readreg_wrapper(device_t dev, int addr_reg)
711 {
712
713         if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
714             (addr_reg < (REG_PORT(0) * 32))) {
715                 device_printf(dev, "Wrong register address.\n");
716                 return (EINVAL);
717         }
718
719         return (e6000sw_readreg(device_get_softc(dev), addr_reg / 32,
720             addr_reg % 32));
721 }
722
723 static int
724 e6000sw_writereg_wrapper(device_t dev, int addr_reg, int val)
725 {
726
727         if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
728             (addr_reg < (REG_PORT(0) * 32))) {
729                 device_printf(dev, "Wrong register address.\n");
730                 return (EINVAL);
731         }
732         e6000sw_writereg(device_get_softc(dev), addr_reg / 5,
733             addr_reg % 32, val);
734
735         return (0);
736 }
737
738 /*
739  * These wrappers are necessary because PHY accesses from etherswitchcfg
740  * need to be synchronized with locks, while miibus PHY accesses do not.
741  */
742 static int
743 e6000sw_readphy_wrapper(device_t dev, int phy, int reg)
744 {
745         e6000sw_softc_t *sc;
746         int ret;
747
748         sc = device_get_softc(dev);
749         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
750
751         E6000SW_LOCK(sc);
752         ret = e6000sw_readphy(dev, phy, reg);
753         E6000SW_UNLOCK(sc);
754
755         return (ret);
756 }
757
758 static int
759 e6000sw_writephy_wrapper(device_t dev, int phy, int reg, int data)
760 {
761         e6000sw_softc_t *sc;
762         int ret;
763
764         sc = device_get_softc(dev);
765         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
766
767         E6000SW_LOCK(sc);
768         ret = e6000sw_writephy(dev, phy, reg, data);
769         E6000SW_UNLOCK(sc);
770
771         return (ret);
772 }
773
774 /*
775  * setvgroup/getvgroup called from etherswitchfcg need to be locked,
776  * while internal calls do not.
777  */
778 static int
779 e6000sw_setvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
780 {
781         e6000sw_softc_t *sc;
782         int ret;
783
784         sc = device_get_softc(dev);
785         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
786
787         E6000SW_LOCK(sc);
788         ret = e6000sw_setvgroup(dev, vg);
789         E6000SW_UNLOCK(sc);
790
791         return (ret);
792 }
793
794 static int
795 e6000sw_getvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
796 {
797         e6000sw_softc_t *sc;
798         int ret;
799
800         sc = device_get_softc(dev);
801         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
802
803         E6000SW_LOCK(sc);
804         ret = e6000sw_getvgroup(dev, vg);
805         E6000SW_UNLOCK(sc);
806
807         return (ret);
808 }
809
810 static __inline void
811 e6000sw_flush_port(e6000sw_softc_t *sc, int port)
812 {
813         uint32_t reg;
814
815         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
816         reg &= ~PORT_VLAN_MAP_TABLE_MASK;
817         reg &= ~PORT_VLAN_MAP_FID_MASK;
818         e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
819         if (sc->vgroup[port] != E6000SW_PORT_NO_VGROUP) {
820                 /*
821                  * If port belonged somewhere, owner-group
822                  * should have its entry removed.
823                  */
824                 sc->members[sc->vgroup[port]] &= ~(1 << port);
825                 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
826         }
827 }
828
829 static __inline void
830 e6000sw_port_assign_vgroup(e6000sw_softc_t *sc, int port, int fid, int vgroup,
831     int members)
832 {
833         uint32_t reg;
834
835         reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
836         reg &= ~PORT_VLAN_MAP_TABLE_MASK;
837         reg &= ~PORT_VLAN_MAP_FID_MASK;
838         reg |= members & ~(1 << port);
839         reg |= (fid << PORT_VLAN_MAP_FID) & PORT_VLAN_MAP_FID_MASK;
840         e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
841         sc->vgroup[port] = vgroup;
842 }
843
844 static int
845 e6000sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
846 {
847         e6000sw_softc_t *sc;
848         int port, fid;
849
850         sc = device_get_softc(dev);
851         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
852
853         if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
854                 return (EINVAL);
855         if (vg->es_member_ports != vg->es_untagged_ports) {
856                 device_printf(dev, "Tagged ports not supported.\n");
857                 return (EINVAL);
858         }
859
860         vg->es_untagged_ports &= PORT_VLAN_MAP_TABLE_MASK;
861         fid = vg->es_vlangroup + 1;
862         for (port = 0; port < sc->num_ports; port++) {
863                 if ((sc->members[sc->vgroup[port]] & (1 << port)))
864                         e6000sw_flush_port(sc, port);
865                 if (vg->es_untagged_ports & (1 << port))
866                         e6000sw_port_assign_vgroup(sc, port, fid,
867                             vg->es_vlangroup, vg->es_untagged_ports);
868         }
869         sc->vid[vg->es_vlangroup] = vg->es_vid;
870         sc->members[vg->es_vlangroup] = vg->es_untagged_ports;
871
872         return (0);
873 }
874
875 static int
876 e6000sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
877 {
878         e6000sw_softc_t *sc;
879
880         sc = device_get_softc(dev);
881         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
882
883         if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
884                 return (EINVAL);
885         vg->es_untagged_ports = vg->es_member_ports =
886             sc->members[vg->es_vlangroup];
887         if (vg->es_untagged_ports != 0)
888                 vg->es_vid = ETHERSWITCH_VID_VALID;
889
890         return (0);
891 }
892
893 static __inline struct mii_data*
894 e6000sw_miiforphy(e6000sw_softc_t *sc, unsigned int phy)
895 {
896
897         if (!e6000sw_is_phyport(sc, phy))
898                 return (NULL);
899
900         return (device_get_softc(sc->miibus[phy]));
901 }
902
903 static int
904 e6000sw_ifmedia_upd(struct ifnet *ifp)
905 {
906         e6000sw_softc_t *sc;
907         struct mii_data *mii;
908
909         sc = ifp->if_softc;
910         mii = e6000sw_miiforphy(sc, ifp->if_dunit);
911         if (mii == NULL)
912                 return (ENXIO);
913         mii_mediachg(mii);
914
915         return (0);
916 }
917
918 static void
919 e6000sw_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
920 {
921         e6000sw_softc_t *sc;
922         struct mii_data *mii;
923
924         sc = ifp->if_softc;
925         mii = e6000sw_miiforphy(sc, ifp->if_dunit);
926
927         if (mii == NULL)
928                 return;
929
930         mii_pollstat(mii);
931         ifmr->ifm_active = mii->mii_media_active;
932         ifmr->ifm_status = mii->mii_media_status;
933 }
934
935 static int
936 e6000sw_smi_waitready(e6000sw_softc_t *sc, int phy)
937 {
938         int i;
939
940         for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
941                 if ((MDIO_READ(sc->dev, phy, SMI_CMD) & SMI_CMD_BUSY) == 0)
942                         return (0);
943                 DELAY(1);
944         }
945
946         return (1);
947 }
948
949 static __inline uint32_t
950 e6000sw_readreg(e6000sw_softc_t *sc, int addr, int reg)
951 {
952
953         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
954
955         if (!sc->multi_chip)
956                 return (MDIO_READ(sc->dev, addr, reg) & 0xffff);
957
958         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
959                 printf("e6000sw: readreg timeout\n");
960                 return (0xffff);
961         }
962         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
963             SMI_CMD_OP_READ | (addr << 5) | reg);
964         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
965                 printf("e6000sw: readreg timeout\n");
966                 return (0xffff);
967         }
968
969         return (MDIO_READ(sc->dev, sc->sw_addr, SMI_DATA) & 0xffff);
970 }
971
972 static __inline void
973 e6000sw_writereg(e6000sw_softc_t *sc, int addr, int reg, int val)
974 {
975
976         E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
977
978         if (!sc->multi_chip) {
979                 MDIO_WRITE(sc->dev, addr, reg, val);
980                 return;
981         }
982
983         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
984                 printf("e6000sw: readreg timeout\n");
985                 return;
986         }
987         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_DATA, val);
988         MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
989             SMI_CMD_OP_WRITE | (addr << 5) | reg);
990         if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
991                 printf("e6000sw: readreg timeout\n");
992                 return;
993         }
994 }
995
996 static __inline bool
997 e6000sw_is_cpuport(e6000sw_softc_t *sc, int port)
998 {
999
1000         return ((sc->cpuports_mask & (1 << port)) ? true : false);
1001 }
1002
1003 static __inline bool
1004 e6000sw_is_fixedport(e6000sw_softc_t *sc, int port)
1005 {
1006
1007         return ((sc->fixed_mask & (1 << port)) ? true : false);
1008 }
1009
1010 static __inline bool
1011 e6000sw_is_fixed25port(e6000sw_softc_t *sc, int port)
1012 {
1013
1014         return ((sc->fixed25_mask & (1 << port)) ? true : false);
1015 }
1016
1017 static __inline bool
1018 e6000sw_is_phyport(e6000sw_softc_t *sc, int port)
1019 {
1020         uint32_t phy_mask;
1021         phy_mask = ~(sc->fixed_mask | sc->cpuports_mask);
1022
1023         return ((phy_mask & (1 << port)) ? true : false);
1024 }
1025
1026 static __inline bool
1027 e6000sw_is_portenabled(e6000sw_softc_t *sc, int port)
1028 {
1029
1030         return ((sc->ports_mask & (1 << port)) ? true : false);
1031 }
1032
1033 static __inline int
1034 e6000sw_set_pvid(e6000sw_softc_t *sc, int port, int pvid)
1035 {
1036
1037         e6000sw_writereg(sc, REG_PORT(port), PORT_VID, pvid &
1038             PORT_VID_DEF_VID_MASK);
1039
1040         return (0);
1041 }
1042
1043 static __inline int
1044 e6000sw_get_pvid(e6000sw_softc_t *sc, int port, int *pvid)
1045 {
1046
1047         if (pvid == NULL)
1048                 return (ENXIO);
1049
1050         *pvid = e6000sw_readreg(sc, REG_PORT(port), PORT_VID) &
1051             PORT_VID_DEF_VID_MASK;
1052
1053         return (0);
1054 }
1055
1056 /*
1057  * Convert port status to ifmedia.
1058  */
1059 static void
1060 e6000sw_update_ifmedia(uint16_t portstatus, u_int *media_status, u_int *media_active)
1061 {
1062         *media_active = IFM_ETHER;
1063         *media_status = IFM_AVALID;
1064
1065         if ((portstatus & PORT_STATUS_LINK_MASK) != 0)
1066                 *media_status |= IFM_ACTIVE;
1067         else {
1068                 *media_active |= IFM_NONE;
1069                 return;
1070         }
1071
1072         switch (portstatus & PORT_STATUS_SPEED_MASK) {
1073         case PORT_STATUS_SPEED_10:
1074                 *media_active |= IFM_10_T;
1075                 break;
1076         case PORT_STATUS_SPEED_100:
1077                 *media_active |= IFM_100_TX;
1078                 break;
1079         case PORT_STATUS_SPEED_1000:
1080                 *media_active |= IFM_1000_T;
1081                 break;
1082         }
1083
1084         if ((portstatus & PORT_STATUS_DUPLEX_MASK) == 0)
1085                 *media_active |= IFM_FDX;
1086         else
1087                 *media_active |= IFM_HDX;
1088 }
1089
1090 static void
1091 e6000sw_tick (void *arg)
1092 {
1093         e6000sw_softc_t *sc;
1094         struct mii_data *mii;
1095         struct mii_softc *miisc;
1096         uint16_t portstatus;
1097         int port;
1098
1099         sc = arg;
1100
1101         E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
1102
1103         for (;;) {
1104                 E6000SW_LOCK(sc);
1105                 for (port = 0; port < sc->num_ports; port++) {
1106                         /* Tick only on PHY ports */
1107                         if (!e6000sw_is_portenabled(sc, port) ||
1108                             !e6000sw_is_phyport(sc, port))
1109                                 continue;
1110
1111                         mii = e6000sw_miiforphy(sc, port);
1112                         if (mii == NULL)
1113                                 continue;
1114
1115                         portstatus = e6000sw_readreg(sc, REG_PORT(port),
1116                             PORT_STATUS);
1117
1118                         e6000sw_update_ifmedia(portstatus,
1119                             &mii->mii_media_status, &mii->mii_media_active);
1120
1121                         LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1122                                 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media)
1123                                     != miisc->mii_inst)
1124                                         continue;
1125                                 mii_phy_update(miisc, MII_POLLSTAT);
1126                         }
1127                 }
1128                 E6000SW_UNLOCK(sc);
1129                 pause("e6000sw tick", 1000);
1130         }
1131 }
1132
1133 static void
1134 e6000sw_setup(device_t dev, e6000sw_softc_t *sc)
1135 {
1136         uint16_t atu_ctrl, atu_age;
1137
1138         /* Set aging time */
1139         e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL,
1140             (E6000SW_DEFAULT_AGETIME << ATU_CONTROL_AGETIME) |
1141             (1 << ATU_CONTROL_LEARN2ALL));
1142
1143         /* Send all with specific mac address to cpu port */
1144         e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_2x, MGMT_EN_ALL);
1145         e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_0x, MGMT_EN_ALL);
1146
1147         /* Disable Remote Management */
1148         e6000sw_writereg(sc, REG_GLOBAL, SWITCH_GLOBAL_CONTROL2, 0);
1149
1150         /* Disable loopback filter and flow control messages */
1151         e6000sw_writereg(sc, REG_GLOBAL2, SWITCH_MGMT,
1152             SWITCH_MGMT_PRI_MASK |
1153             (1 << SWITCH_MGMT_RSVD2CPU) |
1154             SWITCH_MGMT_FC_PRI_MASK |
1155             (1 << SWITCH_MGMT_FORCEFLOW));
1156
1157         e6000sw_atu_flush(dev, sc, NO_OPERATION);
1158         e6000sw_atu_mac_table(dev, sc, NULL, NO_OPERATION);
1159         e6000sw_set_atustat(dev, sc, 0, COUNT_ALL);
1160
1161         /* Set ATU AgeTime to 15 seconds */
1162         atu_age = 1;
1163
1164         atu_ctrl = e6000sw_readreg(sc, REG_GLOBAL, ATU_CONTROL);
1165
1166         /* Set new AgeTime field */
1167         atu_ctrl &= ~ATU_CONTROL_AGETIME_MASK;
1168         e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL, atu_ctrl |
1169             (atu_age << ATU_CONTROL_AGETIME));
1170 }
1171
1172 static void
1173 e6000sw_port_vlan_conf(e6000sw_softc_t *sc)
1174 {
1175         int port, ret;
1176         device_t dev;
1177
1178         dev = sc->dev;
1179         /* Disable all ports */
1180         for (port = 0; port < sc->num_ports; port++) {
1181                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1182                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
1183                     (ret & ~PORT_CONTROL_ENABLE));
1184         }
1185
1186         /* Set port priority */
1187         for (port = 0; port < sc->num_ports; port++) {
1188                 if (!e6000sw_is_portenabled(sc, port))
1189                         continue;
1190                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1191                 ret &= ~PORT_VID_PRIORITY_MASK;
1192                 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1193         }
1194
1195         /* Set VID map */
1196         for (port = 0; port < sc->num_ports; port++) {
1197                 if (!e6000sw_is_portenabled(sc, port))
1198                         continue;
1199                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1200                 ret &= ~PORT_VID_DEF_VID_MASK;
1201                 ret |= (port + 1);
1202                 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1203         }
1204
1205         /* Enable all ports */
1206         for (port = 0; port < sc->num_ports; port++) {
1207                 if (!e6000sw_is_portenabled(sc, port))
1208                         continue;
1209                 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1210                 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL, (ret |
1211                     PORT_CONTROL_ENABLE));
1212         }
1213 }
1214
1215 static void
1216 e6000sw_set_atustat(device_t dev, e6000sw_softc_t *sc, int bin, int flag)
1217 {
1218         uint16_t ret;
1219
1220         ret = e6000sw_readreg(sc, REG_GLOBAL2, ATU_STATS);
1221         e6000sw_writereg(sc, REG_GLOBAL2, ATU_STATS, (bin << ATU_STATS_BIN ) |
1222             (flag << ATU_STATS_FLAG));
1223 }
1224
1225 static int
1226 e6000sw_atu_mac_table(device_t dev, e6000sw_softc_t *sc, struct atu_opt *atu,
1227     int flag)
1228 {
1229         uint16_t ret_opt;
1230         uint16_t ret_data;
1231         int retries;
1232
1233         if (flag == NO_OPERATION)
1234                 return (0);
1235         else if ((flag & (LOAD_FROM_FIB | PURGE_FROM_FIB | GET_NEXT_IN_FIB |
1236             GET_VIOLATION_DATA | CLEAR_VIOLATION_DATA)) == 0) {
1237                 device_printf(dev, "Wrong Opcode for ATU operation\n");
1238                 return (EINVAL);
1239         }
1240
1241         ret_opt = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1242
1243         if (ret_opt & ATU_UNIT_BUSY) {
1244                 device_printf(dev, "ATU unit is busy, cannot access"
1245                     "register\n");
1246                 return (EBUSY);
1247         } else {
1248                 if(flag & LOAD_FROM_FIB) {
1249                         ret_data = e6000sw_readreg(sc, REG_GLOBAL, ATU_DATA);
1250                         e6000sw_writereg(sc, REG_GLOBAL2, ATU_DATA, (ret_data &
1251                             ~ENTRY_STATE));
1252                 }
1253                 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR01, atu->mac_01);
1254                 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR23, atu->mac_23);
1255                 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR45, atu->mac_45);
1256                 e6000sw_writereg(sc, REG_GLOBAL, ATU_FID, atu->fid);
1257
1258                 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret_opt |
1259                     ATU_UNIT_BUSY | flag));
1260
1261                 retries = E6000SW_RETRIES;
1262                 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1263                     ATU_OPERATION) & ATU_UNIT_BUSY))
1264                         DELAY(1);
1265
1266                 if (retries == 0)
1267                         device_printf(dev, "Timeout while flushing\n");
1268                 else if (flag & GET_NEXT_IN_FIB) {
1269                         atu->mac_01 = e6000sw_readreg(sc, REG_GLOBAL,
1270                             ATU_MAC_ADDR01);
1271                         atu->mac_23 = e6000sw_readreg(sc, REG_GLOBAL,
1272                             ATU_MAC_ADDR23);
1273                         atu->mac_45 = e6000sw_readreg(sc, REG_GLOBAL,
1274                             ATU_MAC_ADDR45);
1275                 }
1276         }
1277
1278         return (0);
1279 }
1280
1281 static int
1282 e6000sw_atu_flush(device_t dev, e6000sw_softc_t *sc, int flag)
1283 {
1284         uint16_t ret;
1285         int retries;
1286
1287         if (flag == NO_OPERATION)
1288                 return (0);
1289
1290         ret = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1291         if (ret & ATU_UNIT_BUSY) {
1292                 device_printf(dev, "Atu unit is busy, cannot flush\n");
1293                 return (EBUSY);
1294         } else {
1295                 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret |
1296                     ATU_UNIT_BUSY | flag));
1297                 retries = E6000SW_RETRIES;
1298                 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1299                     ATU_OPERATION) & ATU_UNIT_BUSY))
1300                         DELAY(1);
1301
1302                 if (retries == 0)
1303                         device_printf(dev, "Timeout while flushing\n");
1304         }
1305
1306         return (0);
1307 }