[Zrouter-src-freebsd] ZRouter.org: push to FreeBSD HEAD tree

zrouter-src-freebsd at zrouter.org zrouter-src-freebsd at zrouter.org
Mon Jan 16 12:37:18 UTC 2012


details:   http://zrouter.org/hg/FreeBSD/head//rev/cf0c91eff5d7
changeset: 281:cf0c91eff5d7
user:      ray at terran.dlink.ua
date:      Sun Jan 15 00:22:26 2012 +0200
description:
Real working driver better example than stale template.

diffstat:

 head/sys/dev/switch/template_switch.c |  572 ----------------------------------
 1 files changed, 0 insertions(+), 572 deletions(-)

diffs (576 lines):

diff -r 04aa9374c39b -r cf0c91eff5d7 head/sys/dev/switch/template_switch.c
--- a/head/sys/dev/switch/template_switch.c	Sun Jan 15 00:21:43 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,572 +0,0 @@
-/*-
- * Copyright (c) 2010-2011 Aleksandr Rybalko.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/kernel.h>
-#include <sys/socket.h>
-#include <sys/errno.h>
-#include <sys/module.h>
-#include <sys/bus.h>
-
-#include <machine/bus.h>
-
-#include <net/if.h>
-#include <net/if_media.h>
-
-#include <dev/switch/switchvar.h>
-#include <dev/switch/switch_ioctl.h>
-
-#include "switch_if.h"
-#include "switchb_if.h"
-
-/* TODO */
-static int find_mac_addr(device_t dev, uint64_t mac);
-/* TODO */
-static int mac_table_write(device_t dev, uint64_t mac, int idx, 
-    uint32_t port_map, uint8_t age, int *hash_idx );
-
-static int	get_reg(device_t dev, uint32_t reg, uint32_t *value);
-static int 	set_reg(device_t dev, uint32_t reg, uint32_t *value);
-static int	set_port_vid(device_t dev, int port, uint16_t pvid);
-static int	get_port_vid(device_t dev, int port, uint16_t *pvid);
-static int	set_vid(device_t dev, int idx, uint16_t vid);
-static int	get_vid(device_t dev, int idx, uint16_t *vid);
-static int	set_vlan_ports(device_t dev, int idx, uint32_t memb);
-static int	get_vlan_ports(device_t dev, int idx, uint32_t *memb);
-
-static int	get_port_link(device_t dev, int port);
-static int	get_port_speed(device_t dev, int port);
-static int	force_port_mode(device_t dev, int port, uint32_t mode);
-
-#ifdef mustbe_rewriten
-static uint32_t stat_good_in_packet_count(device_t dev, int port);
-static uint32_t stat_good_out_packet_count(device_t dev, int port);
-static uint32_t stat_bad_in_packet_count(device_t dev, int port);
-static uint32_t stat_bad_out_packet_count(device_t dev, int port);
-#endif
-
-static int TEMPLATE_switch_isr(void *arg);
-
-static int
-TEMPLATE_switch_probe(device_t dev)
-{
-	struct child_res_avl *res;
-
-	res = device_get_ivars(dev);
-
-	/* If we use memory window, check size */
-	if (res->memres_size < (RT_SW_P5PC - RT_SW_ISR))
-		return (ENXIO);
-
-	/* If we use IRQ, get count */
-	if (res->irqs < 1)
-		return (ENXIO);
-
-#define MY_PHY_MASK 0x01010101	/* PHY0, PHY8, PHY16, PHY24 */
-	/* If we use PHYs, check phys bimap */
-	if ((res->phys & MY_PHY_MASK) == MY_PHY_MASK)
-		return (ENXIO);
-
-	device_set_desc(dev, "TEMPLATE ethernet switch");
-	return (BUS_PROBE_DEFAULT);
-}
-
-static int
-TEMPLATE_switch_attach(device_t dev)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	sc->parent = device_get_parent(dev);
-
-	/* Some test, if fail return (ENXIO); */
-
-	sc->caps = malloc(sizeof(struct switch_capability), M_DEVBUF,
-	    M_WAITOK | M_ZERO);
-
-	if (!sc->caps)
-		return (ENXIO);
-
-	sc->caps->ports = sc->ports = 7; 	/* Ports switch have */
-	sc->vlans = 16;				/* VLANs count */
-	sc->sc_dev = dev;
-
-	/* If need IRQ, register to it */
-	SWITCHB_REGISTER_ISR(sc->parent, TEMPLATE_switch_isr, dev);
-
-#define S_C(x) SWITCH_CAPS_ ## x
-	/* List of caps, see struct switch_caps in switch_ioctl.h*/
-	sc->caps->main = S_C(MAIN_PORT_POWER);
-	sc->caps->vlan = S_C(VLAN_DOT1Q) |
-	    ((sc->vlans << S_C(VLAN_MAX_SHIFT_SHIFT)) & S_C(VLAN_MAX_SHIFT_MASK));
-	sc->caps->qos = (2 << S_C(QOS_QUEUES_SHIFT)) & S_C(QOS_QUEUES_MASK);
-	sc->caps->lacp = 0; 	/* LACP caps */
-	sc->caps->stp = 0; 	/* STP caps */
-	sc->caps->acl = 0; 	/* ACL caps */
-	sc->caps->stat = 0;	/* Statistics caps */
-#undef S_C
-
-	/* Initialization */
-
-#ifdef TEMPLATE_SWITCH_DEBUG
-	int i;
-	for (i = 0; i < 256/* ,ax reg */; i += 4) {
-		printf("%08x%c", READ4(sc, i), ((i+4)%16)?' ':'\n');
-	}
-#endif
-
-	return (0);
-}
-
-static int
-TEMPLATE_switch_detach(device_t dev)
-{
-	struct TEMPLATE_switch_softc *sc;
-
-	sc = device_get_softc(dev);
-
-	if (sc->parent)
-		SWITCHB_UNREGISTER_ISR(sc->parent, dev);
-
-	if (sc->caps)
-		free(sc->caps, M_DEVBUF);
-
-	return (0);
-}
-
-static int
-TEMPLATE_switch_isr(void *arg)
-{
-	/* Interrup handling */
-	return (FILTER_HANDLED);
-}
-
-
-/*
- * Switch interface methods
- */
-static struct switch_capability *
-get_caps(device_t dev)
-{
-	struct TEMPLATE_switch_softc *sc;
-
-	/* Just return capability struct */
-	sc = device_get_softc(dev);
-
-	return (sc->caps);
-}
-
-/* TODO */
-static int
-find_mac_addr(device_t dev, uint64_t mac)
-{
-	return (0);
-}
-
-/* TODO */
-static int
-mac_table_write(device_t dev, uint64_t mac, int idx, uint32_t port_map,
-    uint8_t age, int *hash_idx )
-{
-	return (0);
-}
-
-static int
-get_reg(device_t dev, uint32_t reg, uint32_t *value)
-{
-
-	/* if errors, return (EINVAL) or other errno(2) value */
-	/* Upper bit maybe used to decide ask raw register or indirect one */
-	if (reg & 0x80000000) {
-		/* Indirect */
-		*value = 0;
-	else
-		/* Raw */
-		*value = 0;
-	return (0);
-}
-
-static int
-set_reg(device_t dev, uint32_t reg, uint32_t *value)
-{
-	uint32_t old;
-
-	/* if errors, return (EINVAL) or other errno(2) value */
-	/* Upper bit maybe used to decide ask raw register or indirect one */
-	if (reg & 0x80000000) {
-		/* Indirect */
-		old = 0; /* save old value */
-		/* set new WRITE4(sc, reg, value); */
-	} else {
-		/* Raw */
-		old = 0; /* save old value */
-		/* set new MII_SW_WRITE4(sc, reg, value); */
-	}
-
-	*value = old;
-	return (0);
-}
-static int
-set_port_vid(device_t dev, int port, uint16_t pvid)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	if (port > (sc->ports - 1))
-		return (EINVAL);
-
-	/* Set port VLAN id */
-
-	return (0);
-}
-
-static int
-get_port_vid(device_t dev, int port, uint16_t *pvid)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (EINVAL);
-
-	*pvid = 0;
-	/* Get port VLAN id */
-	return (0);
-}
-
-static int
-get_port_flags(device_t dev, int port, uint32_t *flags)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg = 0;
-
-	flags = 0;
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (EINVAL);
-
-	/*
-	 * If Q-in-Q enabled
-	 * flags |= DOT1Q_VLAN_PORT_FLAG_DOUBLE_TAG;
-	 * If LAN/WAN mapping enabled
-	 * flags |= DOT1Q_VLAN_PORT_FLAG_LAN or DOT1Q_VLAN_PORT_FLAG_WAN;
-	 * If global tagging
-	 * flags |= DOT1Q_VLAN_PORT_FLAG_UNTAGGED or DOT1Q_VLAN_PORT_FLAG_UNTAGGED;
-	*/
-
-	return (0);
-}
-
-static int
-set_port_flags(device_t dev, int port, uint32_t flags)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (1);
-
-	if (flags & DOT1Q_VLAN_PORT_FLAG_TAGGED) {
-		/*
-		 * If support global tagging, mark as tagged 
-		 * some switch use single tag/untag flag for all VLANS
-		 */
-	} else if (flags & DOT1Q_VLAN_PORT_FLAG_UNTAGGED) {
-		/* untagged */
-	}
-
-	if (flags & DOT1Q_VLAN_PORT_FLAG_DOUBLE_TAG) {
-		/* Enable Q-in-Q on port */
-	} else {
-		/* Disable Q-in-Q on port */
-	}
-
-	/* LAN/WAN */
-	if (flags & DOT1Q_VLAN_PORT_FLAG_LAN) {
-		/* If switch support LAN/WAN port mapping, mark as LAN */
-	} else if (flags & DOT1Q_VLAN_PORT_FLAG_WAN) {
-		/* mark as WAN */
-	}
-
-	return (0);
-}
-
-static int
-set_vid(device_t dev, int idx, uint16_t vid)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (idx > (sc->vlans - 1))
-		return (1);
-
-	/* Map VID to index `idx` */
-
-	return (0);
-}
-
-static int
-get_vid(device_t dev, int idx, uint16_t *vid)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	if (idx > (sc->vlans - 1))
-		return (1);
-
-	/* Get VID of index `idx` */
-
-	return (0);
-}
-
-/*
- * NOTE: set_vlan_ports must return tagged + untagged ports.
- * Not only tagged.
- */
-
-static int
-set_vlan_ports(device_t dev, int idx, uint32_t memb)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	printf("%s: idx=%d, memb=%08x\n", __func__, idx, memb);
-
-	if (idx > (sc->vlans - 1))
-		return (EINVAL);
-	if (memb & ~((1 << sc->ports) - 1))
-		return (EINVAL);
-
-	/* Set VLAN members */
-
-	return (0);
-}
-
-static int
-get_vlan_ports(device_t dev, int idx, uint32_t *memb)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	if (idx > (sc->vlans - 1))
-		return (EINVAL);
-
-	/* Get VLAN members */
-
-	return (0);
-}
-
-/* If switch support per VLAN tagging option */
-static int
-set_vlan_untagged_ports(device_t dev, int idx, uint32_t memb)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	printf("%s: idx=%d, memb=%08x\n", __func__, idx, memb);
-
-	if (idx > (sc->vlans - 1))
-		return (EINVAL);
-	if (memb & ~((1 << sc->ports) - 1))
-		return (EINVAL);
-
-	/* Set untagged members */
-
-	return (0);
-}
-
-static int
-get_vlan_untagged_ports(device_t dev, int idx, uint32_t *memb)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t reg;
-
-	if (idx > (sc->vlans - 1))
-		return (1);
-
-	/* Get untagged members */
-
-	return (0);
-}
-
-static int
-get_port_link(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	int link;
-
-	if (port > (sc->ports - 1))
-		return (-1);
-
-	/* Get port link status */
-
-	return (link);
-}
-
-static int
-get_port_speed(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-	uint32_t link, fdx;
-
-	/* Get port link info */
-
-	return (IFM_10_T | IFM_HDX);
-}
-
-static int
-force_port_mode(device_t dev, int port, uint32_t mode)
-{
-	struct TEMPLATE_switch_softc *sc = device_get_softc(dev);
-
-	if (port > (sc->ports - 1))
-		return (1);
-
-	/* Force link info */
-
-	return (0);
-}
-
-#ifdef mustbe_rewriten
-static uint32_t
-stat_good_in_packet_count(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (~0);
-
-	/* Good packets on input for port */
-
-	return (0);
-}
-
-static uint32_t
-stat_good_out_packet_count(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (~0);
-
-	/* Good packets on output for port */
-
-	return (~0);
-}
-
-static uint32_t
-stat_bad_in_packet_count(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (~0);
-
-	/* Bad packets on input for port */
-
-	return (~0);
-}
-
-static uint32_t
-stat_bad_out_packet_count(device_t dev, int port)
-{
-	struct TEMPLATE_switch_softc *sc;
-	uint32_t reg;
-
-	sc = device_get_softc(dev);
-	if (port > (sc->ports - 1))
-		return (~0);
-
-	/* Bad packets on output for port */
-
-	return (~0);
-}
-#endif
-
-static device_method_t TEMPLATE_switch_methods[] = {
-	DEVMETHOD(device_probe,		TEMPLATE_switch_probe),
-	DEVMETHOD(device_attach,	TEMPLATE_switch_attach),
-	DEVMETHOD(device_detach,	TEMPLATE_switch_detach),
-
-	/* Capability */
-	DEVMETHOD(switch_get_caps,	get_caps),
-
-	/* MAC address table */
-	DEVMETHOD(switch_find_mac,	find_mac_addr),
-	DEVMETHOD(switch_mac_write,	mac_table_write),
-
-	/* 802.1q */
-	DEVMETHOD(switch_set_pvid,	set_port_vid),
-	DEVMETHOD(switch_get_pvid,	get_port_vid),
-	DEVMETHOD(switch_set_pflags,	set_port_flags),
-	DEVMETHOD(switch_get_pflags,	get_port_flags),
-	DEVMETHOD(switch_set_vid,	set_vid),
-	DEVMETHOD(switch_get_vid,	get_vid),
-	DEVMETHOD(switch_set_vlanports,	set_vlan_ports),
-	DEVMETHOD(switch_get_vlanports,	get_vlan_ports),
-	DEVMETHOD(switch_set_vlanutports,	set_vlan_untagged_ports),
-	DEVMETHOD(switch_get_vlanutports,	get_vlan_untagged_ports),
-
-	/* Port state */
-	DEVMETHOD(switch_get_portlink,	get_port_link),
-	DEVMETHOD(switch_get_portspeed,	get_port_speed),
-	DEVMETHOD(switch_force_mode,	force_port_mode),
-
-#ifdef mustbe_rewriten
-	/* Statistics */
-	DEVMETHOD(switch_good_in_cnt,	stat_good_in_packet_count),
-	DEVMETHOD(switch_good_out_cnt,	stat_good_out_packet_count),
-	DEVMETHOD(switch_bad_in_cnt,	stat_bad_in_packet_count),
-	DEVMETHOD(switch_bad_out_cnt,	stat_bad_out_packet_count),
-#endif
-
-	{0, 0},
-};
-
-static driver_t TEMPLATE_switch_driver = {
-	"TEMPLATE_switch",
-	TEMPLATE_switch_methods,
-	sizeof(struct TEMPLATE_switch_softc),
-};
-static devclass_t TEMPLATE_switch_devclass;
-
-DRIVER_MODULE(TEMPLATE_switch, switch, TEMPLATE_switch_driver, TEMPLATE_switch_devclass, 0, 0);
-MODULE_VERSION(TEMPLATE_switch, 1);
-MODULE_DEPEND(TEMPLATE_switch, switch, 1, 1, 1);
-


More information about the Zrouter-src-freebsd mailing list