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