Skip to content
Snippets Groups Projects
bcm.c 7.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * bcm.c - implements for Broadcom eth switch
     *
     * Copyright (C) 2018 iopsys Software Solutions AB. All rights reserved.
     *
     * Author: anjan.chanda@iopsys.eu
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     * This program is distributed in the hope that it will be useful, but
     * WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
     * 02110-1301 USA
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <string.h>
    #include <ctype.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <net/if.h>
    #include <stdbool.h>
    #include <linux/mii.h>
    
    #include "easy.h"
    #include "ethernet.h"
    #include "bcmswapitypes.h"
    #include "bcmnet.h"
    
    #define PHYID_2_MII_IOCTL(phy_id, mii_ioctl_data) \
                        {mii_ioctl_data->val_out = (phy_id >> 24 ) & 0xff; \
                        mii_ioctl_data->phy_id = phy_id & 0x1f;}
    
    static int bcm_eth_get_unit_port(const char *ifname, int *unit, int *port)
    {
    	struct ethswctl_data data;
    	int ret;
    	int i;
    
    	memset(&data, 0, sizeof(struct ethswctl_data));
    
    	data.op = ETHSWUNITPORT;
    	data.type = TYPE_GET;
    	strncpy(data.ifname, ifname, 16);
    
    	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
    						sizeof(struct ethswctl_data));
    	if (ret != 0) {
    		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
    		return -1;
    	}
    
    	*unit = data.unit;
    	*port = data.port;
    
    	if (!data.port && data.port_map) {
    		unsigned int portmap = data.port_map;
    
    		for (i = 0; i < sizeof(portmap); i++) {
    			if (!!(portmap & (1 << i))) {
    				*port = i;
    				break;
    			}
    		}
    	}
    
    	/* fprintf(stderr, "[%s] unit = %d port = %d portmap = 0x%x "
    			"phyportmap = 0x%x\n",
    			ifname, *unit, *port, data.port_map,
    			data.phy_portmap); */
    
    	return 0;
    }
    
    int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
    {
    	struct ethswctl_data data;
    	int unit = -1;
    	int port = -1;
    	int ret;
    
    	memset(&data, 0, sizeof(struct ethswctl_data));
    	ret = bcm_eth_get_unit_port(ifname, &unit, &port);
    	if (ret)
    		return -1;
    
    	data.op = ETHSWPHYMODE;
    	data.port = port;
    	data.unit = unit;
    	data.type = TYPE_GET;
    
    	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
    						sizeof(struct ethswctl_data));
    	if (ret != 0) {
    		fprintf(stderr, "ioctl failed!\n");
    		return -1;
    	}
    
    	link->portid = port;
    	link->speed = data.speed;
    	link->fullduplex = data.duplex == 1 ? false : true;
    
    	/* fprintf(stderr, "port: %d  speed = %d  fullduplex = %d\n",
    			link->portid, link->speed, link->fullduplex); */
    
    	if (!!(data.phycfg & PHY_CFG_1000FD))
    		link->capability |= ETH_1000_Full;
    
    	if (!!(data.phycfg & PHY_CFG_1000HD))
    		link->capability |= ETH_1000_Half;
    
    	if (!!(data.phycfg & PHY_CFG_100FD))
    		link->capability |= ETH_100_Full;
    
    	if (!!(data.phycfg & PHY_CFG_100HD))
    		link->capability |= ETH_100_Half;
    
    	if (!!(data.phycfg & PHY_CFG_10FD))
    		link->capability |= ETH_10_Full;
    
    	if (!!(data.phycfg & PHY_CFG_10HD))
    		link->capability |= ETH_10_Half;
    
    	if (!!(data.phycfg & PHY_CFG_5000FD))
    		link->capability |= ETH_5000_Full;
    
    	if (!!(data.phycfg & PHY_CFG_10000FD))
    		link->capability |= ETH_10000_Full;
    
    
    	memset(&data, 0, sizeof(struct ethswctl_data));
    	data.op = ETHSWPHYAUTONEG;
    	data.port = port;
    	data.unit = unit;
    	data.type = TYPE_GET;
    
    	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
    						sizeof(struct ethswctl_data));
    	if (ret != 0) {
    		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
    		return -1;
    	}
    
    	link->autoneg = data.autoneg_info == 0 ? false : true;
    	/* fprintf(stderr, "autoneg = %d\n", link->autoneg); */
    
    	memset(&data, 0, sizeof(struct ethswctl_data));
    	data.op = ETHSWLINKSTATUS;
    	data.port = port;
    	data.unit = unit;
    	data.type = TYPE_GET;
    
    	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
    						sizeof(struct ethswctl_data));
    	if (ret != 0) {
    		fprintf(stderr, "ioctl failed!\n");
    		return -1;
    	}
    
    	link->down = data.status == 0 ? true : false;
    
    	return 0;
    }
    
    int bcm_eth_set_link_settings(const char *name, struct eth_link link)
    {
    	fprintf(stderr, "%s(): TODO\n", __func__);
    	return 0;
    }
    
    int bcm_eth_poweron_phy(const char *name, struct eth_phy p)
    {
    	struct ethctl_data data;
    
    	memset(&data, 0, sizeof(struct ethctl_data));
    	data.op = ETHSETSPOWERUP;
    
    	if (eth_ioctl(name, SIOCETHSWCTLOPS, &data,
    				sizeof(struct ethswctl_data)) < 0)
    		return -1;
    
    	return 0;
    }
    
    int bcm_eth_poweroff_phy(const char *name, struct eth_phy p)
    {
    	struct ethctl_data data;
    
    	memset(&data, 0, sizeof(struct ethctl_data));
    	data.op = ETHSETSPOWERDOWN;
    
    	if (eth_ioctl(name, SIOCETHSWCTLOPS, &data,
    				sizeof(struct ethswctl_data)) < 0)
    		return -1;
    
    	return 0;
    }
    
    int bcm_eth_reset_phy(const char *name, int phy_id)
    {
    	return eth_mii_reset_phy(name, phy_id & 0x1f);
    }
    
    
    int bcm_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *tx,
    						struct eth_rmon_stats *rx)
    {
    	struct ethswctl_data data;
    	int ret;
    	int unit = -1;
    	int port = -1;
    
    	memset(&data, 0, sizeof(struct ethswctl_data));
    	ret = bcm_eth_get_unit_port(ifname, &unit, &port);
    	if (ret)
    		return -1;
    
    	//data.op = ETHSWDUMPMIB;
    	data.op = ETHSWEMACGET;
    	data.port = port;
    	data.unit = unit;
    	data.queue = -1;	/* prio_q = -1 */
    
    	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
    						sizeof(struct ethswctl_data));
    	if (ret != 0) {
    		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
    		return -1;
    	}
    
    	tx->all_q = true;
    	tx->bytes = data.emac_stats_s.tx_byte;
    	tx->packets = data.emac_stats_s.tx_packet;
    	tx->bcast_packets = data.emac_stats_s.tx_broadcast_packet;
    	tx->mcast_packets = data.emac_stats_s.tx_multicast_packet;
    	tx->crc_err_packets = data.emac_stats_s.tx_fcs_error;
    	tx->under_sz_packets = data.emac_stats_s.tx_undersize_frame;
    	tx->over_sz_packets = data.emac_stats_s.tx_oversize_frame;
    	tx->packets_64bytes = data.emac_stats_s.tx_frame_64;
    	tx->packets_65to127bytes = data.emac_stats_s.tx_frame_65_127;
    	tx->packets_128to255bytes = data.emac_stats_s.tx_frame_128_255;
    	tx->packets_256to511bytes = data.emac_stats_s.tx_frame_256_511;
    	tx->packets_512to1023bytes = data.emac_stats_s.tx_frame_512_1023;
    	tx->packets_1024to1518bytes = data.emac_stats_s.tx_frame_1024_1518;
    
    
    	rx->all_q = true;
    	rx->bytes = data.emac_stats_s.rx_byte;
    	rx->packets = data.emac_stats_s.rx_packet;
    	rx->bcast_packets = data.emac_stats_s.rx_broadcast_packet;
    	rx->mcast_packets = data.emac_stats_s.rx_multicast_packet;
    	rx->crc_err_packets = data.emac_stats_s.rx_fcs_error;
    	rx->under_sz_packets = data.emac_stats_s.rx_undersize_packet;
    	rx->over_sz_packets = data.emac_stats_s.rx_oversize_packet;
    	rx->packets_64bytes = data.emac_stats_s.rx_frame_64;
    	rx->packets_65to127bytes = data.emac_stats_s.rx_frame_65_127;
    	rx->packets_128to255bytes = data.emac_stats_s.rx_frame_128_255;
    	rx->packets_256to511bytes = data.emac_stats_s.rx_frame_256_511;
    	rx->packets_512to1023bytes = data.emac_stats_s.rx_frame_512_1023;
    	rx->packets_1024to1518bytes = data.emac_stats_s.rx_frame_1024_1518;
    
    	return 0;
    }
    
    const struct eth_ops bcm_eth_ops = {
    	.ifname = "eth",
    	//.up = bcm_eth_up,
    	//.down = bcm_eth_down,
    	.set_link_settings = bcm_eth_set_link_settings,
    	.get_link_settings = bcm_eth_get_link_settings,
    	//.get_stats = bcm_eth_get_stats,
    	.get_rmon_stats = bcm_eth_get_rmon_stats,
    	.poweron_phy = bcm_eth_poweron_phy,
    	.poweroff_phy = bcm_eth_poweroff_phy,
    	.reset_phy = bcm_eth_reset_phy,
    };