/*
* net/dsa/dsa2.c - Hardware switch handling, binding version 2
* Copyright (c) 2008-2009 Marvell Semiconductor
* Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
* Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
#include <linux/rtnetlink.h>
#include <linux/of.h>
#include <linux/of_net.h>
#include "dsa_priv.h"
static LIST_HEAD(dsa_switch_trees);
static DEFINE_MUTEX(dsa2_mutex);
static const struct devlink_ops dsa_devlink_ops = {
};
static struct dsa_switch_tree *dsa_get_dst(u32 tree)
{
struct dsa_switch_tree *dst;
list_for_each_entry(dst, &dsa_switch_trees, list)
if (dst->tree == tree) {
kref_get(&dst->refcount);
return dst;
}
return NULL;
}
static void dsa_free_dst(struct kref *ref)
{
struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
refcount);
list_del(&dst->list);
kfree(dst);
}
static void dsa_put_dst(struct dsa_switch_tree *dst)
{
kref_put(&dst->refcount, dsa_free_dst);
}
static struct dsa_switch_tree *dsa_add_dst(u32 tree)
{
struct dsa_switch_tree *dst;
dst = kzalloc(sizeof(*dst), GFP_KERNEL);
if (!dst)
return NULL;
dst->tree = tree;
INIT_LIST_HEAD(&dst->list);
list_add_tail(&dsa_switch_trees, &dst->list);
kref_init(&dst->refcount);
return dst;
}
static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
struct dsa_switch *ds, u32 index)
{
kref_get(&dst->refcount);
dst->ds[index] = ds;
}
static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
struct dsa_switch *ds, u32 index)
{
dst->ds[index] = NULL;
kref_put(&dst->refcount, dsa_free_dst);
}
/* For platform data configurations, we need to have a valid name argument to
* differentiate a disabled port from an enabled one
*/
static bool dsa_port_is_valid(struct dsa_port *port)
{
return !!(port->dn || port->name);
}
static bool dsa_port_is_dsa(struct dsa_port *port)
{
if (port->name && !strcmp(port->name, "dsa"))
return true;
else
return !!of_parse_phandle(port->dn, "link", 0);
}
static bool dsa_port_is_cpu(struct dsa_port *port)
{
if (port->name && !strcmp(port->name, "cpu"))
return true;
else
return !!of_parse_phandle(port->dn, "ethernet", 0);
}
static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
struct device_node *port)
{
u32 index;
for (index = 0; index < ds->num_ports; index++)
if (ds->ports[index].dn == port)
return true;
return false;
}
static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
struct device_node *port)
{
struct dsa_switch *ds;
u32 index;
for (index = 0; index < DSA_MAX_SWITCHES; index++) {
ds = dst->ds[index];
if (!ds)
continue;
if (dsa_ds_find_port_dn(ds, port))
return ds;
}
return NULL;
}
static int dsa_port_complete(struct dsa_switch_tree *dst,
struct dsa_switch *src_ds,
struct dsa_port *port,
u32 src_port)
{
struct device_node *link;
int index;
struct dsa_switch *dst_ds;
for (index = 0;; index++) {
link = of_parse_phandle(port->dn, "link", index);
if (!link)
break;
dst_ds = dsa_dst_find_port_dn(dst, link);
of_node_put(link);
if (!dst_ds)
return 1;
src_ds->rtable[dst_ds->index] = src_port;
}
return 0;
}
/* A switch is complete if all the DSA ports phandles point to ports
* known in the tree. A return value of 1 means the tree is not
* complete. This is not an error condition. A value of 0 is
* success.
*/
static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
{
struct dsa_port *port;
u32 index;
int err;
for (index = 0; index < ds->num_ports; index++) {
port = &ds->ports[index];
if (!dsa_port_is_valid(port))
continue;
if (!dsa_port_is_dsa(port))
continue;
err = dsa_port_complete(dst, ds, port, index);
if (err != 0)
return err;
ds->dsa_port_mask |= BIT(index);
}
return 0;
}
/* A tr