dsa: updated api to use net_if

Change DSA API to use `net_if` directly to make API calls instead of
indirectly via `dsa_context` and `switch_id`.
Remove unused `switch_id`, `switch_enable_port`, and `dsa_get_context`.

Signed-off-by: Arvin Farahmand <arvinf@ip-logix.com>
This commit is contained in:
Arvin Farahmand 2021-05-05 18:14:09 -04:00 committed by Christopher Friedt
parent 9cb353a97e
commit c95e0825d2
3 changed files with 221 additions and 76 deletions

View file

@ -44,11 +44,6 @@ struct ksz8xxx_data {
#endif
};
static struct ksz8xxx_data private_data = {
.iface_init_count = 0,
.is_init = false,
};
#define DEV_DATA(dev) ((struct dsa_context *const)(dev)->data)
#define PRV_DATA(ctx) ((struct ksz8xxx_data *const)(ctx)->prv_data)
@ -796,25 +791,29 @@ int dsa_port_init(const struct device *dev)
}
/* Generic implementation of writing value to DSA register */
static int dsa_ksz8xxx_sw_write_reg(int switch_id, uint16_t reg_addr,
static int dsa_ksz8xxx_sw_write_reg(const struct device *dev, uint16_t reg_addr,
uint8_t value)
{
dsa_ksz8xxx_write_reg(&private_data, reg_addr, value);
struct ksz8xxx_data *pdev = PRV_DATA(DEV_DATA(dev));
dsa_ksz8xxx_write_reg(pdev, reg_addr, value);
return 0;
}
/* Generic implementation of reading value from DSA register */
static int dsa_ksz8xxx_sw_read_reg(int switch_id, uint16_t reg_addr,
static int dsa_ksz8xxx_sw_read_reg(const struct device *dev, uint16_t reg_addr,
uint8_t *value)
{
dsa_ksz8xxx_read_reg(&private_data, reg_addr, value);
struct ksz8xxx_data *pdev = PRV_DATA(DEV_DATA(dev));
dsa_ksz8xxx_read_reg(pdev, reg_addr, value);
return 0;
}
/**
* @brief Set entry to DSA MAC address table
*
* @param switch_id The id number (equal to reg=<X>) of switch chip
* @param dev DSA device
* @param mac The MAC address to be set in the table
* @param fw_port Port number to forward packets
* @param tbl_entry_idx The index of entry in the table
@ -822,16 +821,19 @@ static int dsa_ksz8xxx_sw_read_reg(int switch_id, uint16_t reg_addr,
*
* @return 0 if ok, < 0 if error
*/
static int dsa_ksz8xxx_set_mac_table_entry(int switch_id, const uint8_t *mac,
uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags)
static int dsa_ksz8xxx_set_mac_table_entry(const struct device *dev,
const uint8_t *mac,
uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags)
{
struct ksz8xxx_data *pdev = PRV_DATA(DEV_DATA(dev));
if (flags != 0) {
return -EINVAL;
}
dsa_ksz8xxx_set_static_mac_table(&private_data, mac, fw_port,
dsa_ksz8xxx_set_static_mac_table(pdev, mac, fw_port,
tbl_entry_idx);
return 0;
@ -840,16 +842,19 @@ static int dsa_ksz8xxx_set_mac_table_entry(int switch_id, const uint8_t *mac,
/**
* @brief Get DSA MAC address table entry
*
* @param switch_id The id number (equal to reg=<X>) of switch chip
* @param dev DSA device
* @param buf The buffer for data read from the table
* @param tbl_entry_idx The index of entry in the table
*
* @return 0 if ok, < 0 if error
*/
static int dsa_ksz8xxx_get_mac_table_entry(int switch_id, uint8_t *buf,
uint16_t tbl_entry_idx)
static int dsa_ksz8xxx_get_mac_table_entry(const struct device *dev,
uint8_t *buf,
uint16_t tbl_entry_idx)
{
dsa_ksz8xxx_read_static_mac_table(&private_data, tbl_entry_idx, buf);
struct ksz8xxx_data *pdev = PRV_DATA(DEV_DATA(dev));
dsa_ksz8xxx_read_static_mac_table(pdev, tbl_entry_idx, buf);
return 0;
}
@ -1072,13 +1077,6 @@ static struct dsa_api dsa_api_f = {
#endif
};
static struct dsa_context dsa_context = {
.num_slave_ports = DT_INST_PROP(0, dsa_slave_ports),
.switch_id = 0,
.dapi = &dsa_api_f,
.prv_data = (void *)&private_data,
};
/*
* The order of NET_DEVICE_INIT_INSTANCE() placement IS important.
*
@ -1103,16 +1101,16 @@ static struct dsa_context dsa_context = {
* For simple cases it is just good enough.
*/
#define NET_SLAVE_DEVICE_INIT_INSTANCE(slave) \
#define NET_SLAVE_DEVICE_INIT_INSTANCE(slave, n) \
const struct dsa_slave_config dsa_0_slave_##slave##_config = { \
.mac_addr = DT_PROP_OR(slave, local_mac_address, {0}) \
}; \
NET_DEVICE_INIT_INSTANCE(dsa_slave_port_##slave, \
DT_LABEL(slave), \
0, \
n, \
dsa_port_init, \
NULL, \
&dsa_context, \
&dsa_context_##n, \
&dsa_0_slave_##slave##_config, \
CONFIG_ETH_INIT_PRIORITY, \
&dsa_eth_api_funcs, \
@ -1120,4 +1118,28 @@ static struct dsa_context dsa_context = {
NET_L2_GET_CTX_TYPE(ETHERNET_L2), \
NET_ETH_MTU);
DT_INST_FOREACH_CHILD(0, NET_SLAVE_DEVICE_INIT_INSTANCE);
#define NET_SLAVE_DEVICE_0_INIT_INSTANCE(slave) \
NET_SLAVE_DEVICE_INIT_INSTANCE(slave, 0)
#define NET_SLAVE_DEVICE_1_INIT_INSTANCE(slave) \
NET_SLAVE_DEVICE_INIT_INSTANCE(slave, 1)
#define NET_SLAVE_DEVICE_2_INIT_INSTANCE(slave) \
NET_SLAVE_DEVICE_INIT_INSTANCE(slave, 2)
#define NET_SLAVE_DEVICE_3_INIT_INSTANCE(slave) \
NET_SLAVE_DEVICE_INIT_INSTANCE(slave, 3)
#define NET_SLAVE_DEVICE_4_INIT_INSTANCE(slave) \
NET_SLAVE_DEVICE_INIT_INSTANCE(slave, 4)
#define DSA_DEVICE(n) \
static struct ksz8xxx_data dsa_device_prv_data_##n = { \
.iface_init_count = 0, \
.is_init = false, \
}; \
static struct dsa_context dsa_context_##n = { \
.num_slave_ports = DT_INST_PROP(0, dsa_slave_ports), \
.dapi = &dsa_api_f, \
.prv_data = (void *)&dsa_device_prv_data_##n, \
}; \
DT_INST_FOREACH_CHILD_VARGS(n, NET_SLAVE_DEVICE_INIT_INSTANCE, n);
DT_INST_FOREACH_STATUS_OKAY(DSA_DEVICE);

View file

@ -88,20 +88,6 @@ int dsa_register_recv_callback(struct net_if *iface, dsa_net_recv_cb_t cb);
*/
struct net_if *dsa_net_recv(struct net_if *iface, struct net_pkt **pkt);
/**
* @brief DSA helper function to get context
*
* This is the helper function to provide DSA context pointer.
* (master and LAN interfaces shall have pointer to struct dsa_context
* available)
*
* @param iface Network interface (master)
*
* Returns:
* - 0 if ok (packet sent via master iface), < 0 if error
*/
struct dsa_context *dsa_get_context(struct net_if *iface);
/**
* @brief Pointer to master interface send function
*/
@ -128,6 +114,13 @@ int dsa_register_master_tx(struct net_if *iface, dsa_send_t fn);
*/
bool dsa_is_port_master(struct net_if *iface);
/**
* @cond INTERNAL_HIDDEN
*
* These are for internal use only, so skip these in
* public documentation.
*/
/** DSA context data */
struct dsa_context {
/** Pointers to all DSA slave network interfaces */
@ -142,11 +135,6 @@ struct dsa_context {
/** DSA related work (e.g. monitor if network interface is up) */
struct k_work_delayable dsa_work;
/** The switch_id, which equals to the reg property number from
* DTS is used to distinct between many connected switches.
*/
uint8_t switch_id;
/** Number of slave ports in the DSA switch */
uint8_t num_slave_ports;
@ -172,22 +160,23 @@ struct dsa_api {
* dsa_context.
*/
/** Read value from DSA register */
int (*switch_read)(int switch_id, uint16_t reg_addr, uint8_t *value);
int (*switch_read)(const struct device *dev, uint16_t reg_addr,
uint8_t *value);
/** Write value to DSA register */
int (*switch_write)(int switch_id, uint16_t reg_addr, uint8_t value);
/** Enable single port in the DSA switch */
int (*switch_enable_port)(int switch_id, uint8_t port);
int (*switch_write)(const struct device *dev, uint16_t reg_addr,
uint8_t value);
/** Program (set) mac table entry in the DSA switch */
int (*switch_set_mac_table_entry)(int switch_id,
const uint8_t *mac, uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags);
int (*switch_set_mac_table_entry)(const struct device *dev,
const uint8_t *mac,
uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags);
/** Read mac table entry from the DSA switch */
int (*switch_get_mac_table_entry)(int switch_id, uint8_t *buf,
uint16_t tbl_entry_idx);
int (*switch_get_mac_table_entry)(const struct device *dev,
uint8_t *buf,
uint16_t tbl_entry_idx);
/*
* DSA helper callbacks
@ -196,6 +185,73 @@ struct dsa_api {
struct net_pkt *pkt);
};
/**
* @endcond
*/
/**
* @brief Get network interface of a slave port
*
* @param iface Master port
* @param[in] slave_num Slave port number
*
* @return network interface of the slave if successful
* @return NULL if slave port does not exist
*/
struct net_if *dsa_get_slave_port(struct net_if *iface, int slave_num);
/**
* @brief Read from DSA switch register
*
* @param iface The interface
* @param[in] reg_addr The register address
* @param value The value
*
* @return 0 if successful, negative if error
*/
int dsa_switch_read(struct net_if *iface, uint16_t reg_addr, uint8_t *value);
/**
* @brief Write to DSA switch
*
* @param iface The interface
* @param[in] reg_addr The register address
* @param[in] value The value
*
* @return { description_of_the_return_value }
*/
int dsa_switch_write(struct net_if *iface, uint16_t reg_addr, uint8_t value);
/**
* @brief Write static MAC table entry
*
* @param iface Master DSA interface
* @param[in] mac MAC address
* @param[in] fw_port The firmware port
* @param[in] tbl_entry_idx Table entry index
* @param[in] flags Flags
*
* @return 0 if successful, negative if error
*/
int dsa_switch_set_mac_table_entry(struct net_if *iface,
const uint8_t *mac,
uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags);
/**
* @brief Read static MAC table entry
*
* @param iface Master DSA interface
* @param buf Buffer to receive MAC address
* @param[in] tbl_entry_idx Table entry index
*
* @return 0 if successful, negative if error
*/
int dsa_switch_get_mac_table_entry(struct net_if *iface,
uint8_t *buf,
uint16_t tbl_entry_idx);
/**
* @brief Structure to provide mac address for each LAN interface
*/

View file

@ -19,22 +19,6 @@ LOG_MODULE_REGISTER(net_dsa, CONFIG_NET_DSA_LOG_LEVEL);
#include <net/net_mgmt.h>
#include <net/dsa.h>
/*
* Helper functions
*/
struct dsa_context *dsa_get_context(struct net_if *iface)
{
struct ethernet_context *eth_ctx;
eth_ctx = net_if_l2_data(iface);
if (eth_ctx == NULL) {
LOG_ERR("Iface %p context not available!", iface);
return NULL;
}
return eth_ctx->dsa_ctx;
}
/*
* Store, in the ethernet_context for master interface, the original
* eth_tx() function, which will send packet with tag appended.
@ -198,3 +182,86 @@ int dsa_tx(const struct device *dev, struct net_pkt *pkt)
return ctx->dsa_send(net_if_get_device(iface_master),
context->dapi->dsa_xmit_pkt(iface, pkt));
}
struct net_if *dsa_get_slave_port(struct net_if *iface, int slave_num)
{
struct ethernet_context *eth_ctx;
struct dsa_context *dsa_ctx;
eth_ctx = net_if_l2_data(iface);
if (eth_ctx == NULL) {
LOG_ERR("Iface %p context not available!", iface);
return NULL;
}
dsa_ctx = eth_ctx->dsa_ctx;
if (slave_num < 0 || slave_num >= dsa_ctx->num_slave_ports) {
return NULL;
}
return dsa_ctx->iface_slave[slave_num];
}
int dsa_switch_read(struct net_if *iface, uint16_t reg_addr, uint8_t *value)
{
const struct device *dev = iface->if_dev->dev;
const struct dsa_api *api =
(const struct dsa_api *)dev->api;
return api->switch_read(dev, reg_addr, value);
}
int dsa_switch_write(struct net_if *iface, uint16_t reg_addr, uint8_t value)
{
const struct device *dev = iface->if_dev->dev;
const struct dsa_api *api =
(const struct dsa_api *)dev->api;
return api->switch_write(dev, reg_addr, value);
}
/**
* @brief Write static MAC table entry
*
* @param iface Master DSA interface
* @param[in] mac MAC address
* @param[in] fw_port The firmware port
* @param[in] tbl_entry_idx Table entry index
* @param[in] flags Flags
*
* @return 0 if successful, negative if error
*/
int dsa_switch_set_mac_table_entry(struct net_if *iface,
const uint8_t *mac,
uint8_t fw_port,
uint16_t tbl_entry_idx,
uint16_t flags)
{
const struct device *dev = iface->if_dev->dev;
const struct dsa_api *api =
(const struct dsa_api *)dev->api;
return api->switch_set_mac_table_entry(dev, mac, fw_port,
tbl_entry_idx, flags);
}
/**
* @brief Read static MAC table entry
*
* @param iface Master DSA interface
* @param buf Buffer to receive MAC address
* @param[in] tbl_entry_idx Table entry index
*
* @return 0 if successful, negative if error
*/
int dsa_switch_get_mac_table_entry(struct net_if *iface,
uint8_t *buf,
uint16_t tbl_entry_idx)
{
const struct device *dev = iface->if_dev->dev;
const struct dsa_api *api =
(const struct dsa_api *)dev->api;
return api->switch_get_mac_table_entry(dev, buf, tbl_entry_idx);
}