1a043e8c7SAdrian Chadd /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4a043e8c7SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke. 5a043e8c7SAdrian Chadd * All rights reserved. 6a043e8c7SAdrian Chadd * 7a043e8c7SAdrian Chadd * Redistribution and use in source and binary forms, with or without 8a043e8c7SAdrian Chadd * modification, are permitted provided that the following conditions 9a043e8c7SAdrian Chadd * are met: 10a043e8c7SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 11a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer. 12a043e8c7SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 13a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer in the 14a043e8c7SAdrian Chadd * documentation and/or other materials provided with the distribution. 15a043e8c7SAdrian Chadd * 16a043e8c7SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17a043e8c7SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18a043e8c7SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19a043e8c7SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20a043e8c7SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21a043e8c7SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22a043e8c7SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23a043e8c7SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24a043e8c7SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25a043e8c7SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26a043e8c7SAdrian Chadd * SUCH DAMAGE. 27a043e8c7SAdrian Chadd */ 28a043e8c7SAdrian Chadd #ifndef __ARSWITCHVAR_H__ 29a043e8c7SAdrian Chadd #define __ARSWITCHVAR_H__ 30a043e8c7SAdrian Chadd 31a043e8c7SAdrian Chadd typedef enum { 32a043e8c7SAdrian Chadd AR8X16_SWITCH_NONE, 33a043e8c7SAdrian Chadd AR8X16_SWITCH_AR8216, 34a043e8c7SAdrian Chadd AR8X16_SWITCH_AR8226, 35a043e8c7SAdrian Chadd AR8X16_SWITCH_AR8316, 360e67bf94SAdrian Chadd AR8X16_SWITCH_AR8327, 370e67bf94SAdrian Chadd AR8X16_SWITCH_AR8337, 38a043e8c7SAdrian Chadd } ar8x16_switch_type; 39a043e8c7SAdrian Chadd 40a043e8c7SAdrian Chadd /* 41a043e8c7SAdrian Chadd * XXX TODO: start using this where required 42a043e8c7SAdrian Chadd */ 43a043e8c7SAdrian Chadd #define AR8X16_IS_SWITCH(_sc, _type) \ 44a043e8c7SAdrian Chadd (!!((_sc)->sc_switchtype == AR8X16_SWITCH_ ## _type)) 45a043e8c7SAdrian Chadd 46570c2125SAdrian Chadd #define ARSWITCH_NUM_PORTS MAX(AR8327_NUM_PORTS, AR8X16_NUM_PORTS) 47570c2125SAdrian Chadd #define ARSWITCH_NUM_PHYS MAX(AR8327_NUM_PHYS, AR8X16_NUM_PHYS) 48570c2125SAdrian Chadd 49c94dc808SAdrian Chadd #define ARSWITCH_NUM_LEDS 3 50c94dc808SAdrian Chadd 51c94dc808SAdrian Chadd struct arswitch_dev_led { 52c94dc808SAdrian Chadd struct arswitch_softc *sc; 53c94dc808SAdrian Chadd struct cdev *led; 54c94dc808SAdrian Chadd int phy; 55c94dc808SAdrian Chadd int lednum; 56c94dc808SAdrian Chadd }; 57c94dc808SAdrian Chadd 58a043e8c7SAdrian Chadd struct arswitch_softc { 59a043e8c7SAdrian Chadd struct mtx sc_mtx; /* serialize access to softc */ 60a043e8c7SAdrian Chadd device_t sc_dev; 61a043e8c7SAdrian Chadd int phy4cpu; /* PHY4 is connected to the CPU */ 62a043e8c7SAdrian Chadd int numphys; /* PHYs we manage */ 63a043e8c7SAdrian Chadd int is_rgmii; /* PHY mode is RGMII (XXX which PHY?) */ 64a043e8c7SAdrian Chadd int is_gmii; /* PHY mode is GMII (XXX which PHY?) */ 65657b8479SAdrian Chadd int is_mii; /* PHY mode is MII (XXX which PHY?) */ 66a043e8c7SAdrian Chadd int page; 67dd843f87SAdrian Chadd int chip_ver; 68dd843f87SAdrian Chadd int chip_rev; 6978549b94SAdrian Chadd int mii_lo_first; /* Send low data DWORD before high */ 70a043e8c7SAdrian Chadd ar8x16_switch_type sc_switchtype; 71570c2125SAdrian Chadd /* should be the max of both pre-AR8327 and AR8327 ports */ 72570c2125SAdrian Chadd char *ifname[ARSWITCH_NUM_PHYS]; 73570c2125SAdrian Chadd device_t miibus[ARSWITCH_NUM_PHYS]; 742e6a8c1aSJustin Hibbits if_t ifp[ARSWITCH_NUM_PHYS]; 75c94dc808SAdrian Chadd struct arswitch_dev_led dev_led[ARSWITCH_NUM_PHYS][ARSWITCH_NUM_LEDS]; 76a043e8c7SAdrian Chadd struct callout callout_tick; 77a043e8c7SAdrian Chadd etherswitch_info_t info; 78a043e8c7SAdrian Chadd 791b334c8bSAdrian Chadd uint32_t sc_debug; 801b334c8bSAdrian Chadd 81b9f07b86SLuiz Otavio O Souza /* VLANs support */ 82b9f07b86SLuiz Otavio O Souza int vid[AR8X16_MAX_VLANS]; 83b9f07b86SLuiz Otavio O Souza uint32_t vlan_mode; 84b9f07b86SLuiz Otavio O Souza 8562042c97SAdrian Chadd /* ATU (address table unit) support */ 8662042c97SAdrian Chadd struct { 8762042c97SAdrian Chadd int count; 8862042c97SAdrian Chadd int size; 8962042c97SAdrian Chadd etherswitch_atu_entry_t *entries; 9062042c97SAdrian Chadd } atu; 9162042c97SAdrian Chadd 92a043e8c7SAdrian Chadd struct { 93e3ba3a89SAdrian Chadd /* Global setup */ 94a043e8c7SAdrian Chadd int (* arswitch_hw_setup) (struct arswitch_softc *); 95a043e8c7SAdrian Chadd int (* arswitch_hw_global_setup) (struct arswitch_softc *); 96e3ba3a89SAdrian Chadd 972ba4bf8fSAdrian Chadd int (* arswitch_hw_get_switch_macaddr) (struct arswitch_softc *, 982ba4bf8fSAdrian Chadd struct ether_addr *sa); 992ba4bf8fSAdrian Chadd int (* arswitch_hw_set_switch_macaddr) (struct arswitch_softc *, 1002ba4bf8fSAdrian Chadd const struct ether_addr *sa); 1012ba4bf8fSAdrian Chadd 102e3ba3a89SAdrian Chadd /* Port functions */ 103e3ba3a89SAdrian Chadd void (* arswitch_port_init) (struct arswitch_softc *, int); 1042bddba6aSAdrian Chadd 1054ff2f60dSAdrian Chadd /* ATU functions */ 1064ff2f60dSAdrian Chadd int (* arswitch_atu_flush) (struct arswitch_softc *); 107676e92f2SAdrian Chadd int (* arswitch_atu_flush_port) (struct arswitch_softc *, int); 108676e92f2SAdrian Chadd int (* arswitch_atu_learn_default) (struct arswitch_softc *); 10962042c97SAdrian Chadd int (* arswitch_atu_fetch_table) (struct arswitch_softc *, 11062042c97SAdrian Chadd etherswitch_atu_entry_t *, int atu_fetch_op); 1114ff2f60dSAdrian Chadd 1122bddba6aSAdrian Chadd /* VLAN functions */ 1132bddba6aSAdrian Chadd int (* arswitch_port_vlan_setup) (struct arswitch_softc *, 1142bddba6aSAdrian Chadd etherswitch_port_t *); 1152bddba6aSAdrian Chadd int (* arswitch_port_vlan_get) (struct arswitch_softc *, 1162bddba6aSAdrian Chadd etherswitch_port_t *); 117570c2125SAdrian Chadd void (* arswitch_vlan_init_hw) (struct arswitch_softc *); 118570c2125SAdrian Chadd int (* arswitch_vlan_getvgroup) (struct arswitch_softc *, 119570c2125SAdrian Chadd etherswitch_vlangroup_t *); 120570c2125SAdrian Chadd int (* arswitch_vlan_setvgroup) (struct arswitch_softc *, 121570c2125SAdrian Chadd etherswitch_vlangroup_t *); 122570c2125SAdrian Chadd int (* arswitch_vlan_get_pvid) (struct arswitch_softc *, int, 123570c2125SAdrian Chadd int *); 124570c2125SAdrian Chadd int (* arswitch_vlan_set_pvid) (struct arswitch_softc *, int, 125570c2125SAdrian Chadd int); 12678549b94SAdrian Chadd 127f35f94f4SAdrian Chadd int (* arswitch_flush_dot1q_vlan) (struct arswitch_softc *sc); 128f35f94f4SAdrian Chadd int (* arswitch_purge_dot1q_vlan) (struct arswitch_softc *sc, 129f35f94f4SAdrian Chadd int vid); 130749cac13SAdrian Chadd int (* arswitch_get_dot1q_vlan) (struct arswitch_softc *, 131036e1c76SAdrian Chadd uint32_t *ports, uint32_t *untagged_ports, int vid); 132749cac13SAdrian Chadd int (* arswitch_set_dot1q_vlan) (struct arswitch_softc *sc, 133036e1c76SAdrian Chadd uint32_t ports, uint32_t untagged_ports, int vid); 134749cac13SAdrian Chadd int (* arswitch_get_port_vlan) (struct arswitch_softc *sc, 135749cac13SAdrian Chadd uint32_t *ports, int vid); 136749cac13SAdrian Chadd int (* arswitch_set_port_vlan) (struct arswitch_softc *sc, 137749cac13SAdrian Chadd uint32_t ports, int vid); 138749cac13SAdrian Chadd 13978549b94SAdrian Chadd /* PHY functions */ 14078549b94SAdrian Chadd int (* arswitch_phy_read) (device_t, int, int); 14178549b94SAdrian Chadd int (* arswitch_phy_write) (device_t, int, int, int); 142a043e8c7SAdrian Chadd } hal; 1439ab21e32SAdrian Chadd 1449ab21e32SAdrian Chadd struct { 1459ab21e32SAdrian Chadd uint32_t port0_status; 1469ab21e32SAdrian Chadd uint32_t port5_status; 1479ab21e32SAdrian Chadd uint32_t port6_status; 1489ab21e32SAdrian Chadd } ar8327; 149a043e8c7SAdrian Chadd }; 150a043e8c7SAdrian Chadd 151a043e8c7SAdrian Chadd #define ARSWITCH_LOCK(_sc) \ 152a043e8c7SAdrian Chadd mtx_lock(&(_sc)->sc_mtx) 153a043e8c7SAdrian Chadd #define ARSWITCH_UNLOCK(_sc) \ 154a043e8c7SAdrian Chadd mtx_unlock(&(_sc)->sc_mtx) 155a043e8c7SAdrian Chadd #define ARSWITCH_LOCK_ASSERT(_sc, _what) \ 1563d094897SAleksandr Rybalko mtx_assert(&(_sc)->sc_mtx, (_what)) 157a043e8c7SAdrian Chadd #define ARSWITCH_TRYLOCK(_sc) \ 158a043e8c7SAdrian Chadd mtx_trylock(&(_sc)->sc_mtx) 159a043e8c7SAdrian Chadd 1601b334c8bSAdrian Chadd #define ARSWITCH_DBG_RESET 0x00000001 1611b334c8bSAdrian Chadd #define ARSWITCH_DBG_REGIO 0x00000002 1621b334c8bSAdrian Chadd #define ARSWITCH_DBG_PHYIO 0x00000004 1631b334c8bSAdrian Chadd #define ARSWITCH_DBG_POLL 0x00000008 1641b334c8bSAdrian Chadd #define ARSWITCH_DBG_VLAN 0x00000010 165676e92f2SAdrian Chadd #define ARSWITCH_DBG_ATU 0x00000020 1661b334c8bSAdrian Chadd #define ARSWITCH_DBG_ANY 0xffffffff 1671b334c8bSAdrian Chadd 1681b334c8bSAdrian Chadd #if 1 1691b334c8bSAdrian Chadd #define DPRINTF(sc, dbg, args...) \ 1701b334c8bSAdrian Chadd do { \ 1711b334c8bSAdrian Chadd if (((sc)->sc_debug & (dbg)) || \ 1721b334c8bSAdrian Chadd ((sc)->sc_debug == ARSWITCH_DBG_ANY)) { \ 1731b334c8bSAdrian Chadd device_printf((sc)->sc_dev, args); \ 1741b334c8bSAdrian Chadd } \ 1751b334c8bSAdrian Chadd } while (0) 176a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) do { \ 177a043e8c7SAdrian Chadd if (err != 0) device_printf(dev, fmt, err, args); \ 178a043e8c7SAdrian Chadd } while (0) 179a043e8c7SAdrian Chadd #else 1801b334c8bSAdrian Chadd #define DPRINTF(dev, dbg, args...) 181a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) 182a043e8c7SAdrian Chadd #endif 183a043e8c7SAdrian Chadd 184a043e8c7SAdrian Chadd #endif /* __ARSWITCHVAR_H__ */ 185a043e8c7SAdrian Chadd 186