From: Wei Fang <wei…@nxp.com> Some NTMP tables have their entry IDs allocated by software, and adding a new entry to them follows the same logic: look up the table ops, allocate the request buffer, fill the common request data and header, then issue the add command over a CBDR. Adding such a table today means duplicating this boilerplate. Introduce ntmp_add_entry_by_id() to factor out this common logic, along with ntmp_table_ops() to select the table ops by table ID. The update actions are passed in so callers can request the needed element updates. Convert the MAFT and ETT add paths to use the new helper. More tables will reuse it later. Signed-off-by: Wei Fang <wei…@nxp.com> --- drivers/net/ethernet/freescale/enetc/ntmp.c | 182 +++++++++++--------- 1 file changed, 96 insertions(+), 86 deletions(-) diff --git a/drivers/net/ethernet/freescale/enetc/ntmp.c b/drivers/net/ethernet/freescale/enetc/ntmp.c index 07a9117273220..fec6c3fcfd2e4 100644 --- a/drivers/net/ethernet/freescale/enetc/ntmp.c +++ b/drivers/net/ethernet/freescale/enetc/ntmp.c @@ -640,6 +640,76 @@ static const struct ntmp_tbl_ops *ntmp_get_bpt_ops(u8 version) } } +static const struct ntmp_tbl_ops *ntmp_table_ops(int tbl_id, u8 version) +{ + switch (tbl_id) { + case NTMP_MAFT_ID: + return ntmp_get_maft_ops(version); + case NTMP_IPFT_ID: + return ntmp_get_ipft_ops(version); + case NTMP_FDBT_ID: + return ntmp_get_fdbt_ops(version); + case NTMP_VFT_ID: + return ntmp_get_vft_ops(version); + case NTMP_ETT_ID: + return ntmp_get_ett_ops(version); + case NTMP_BPT_ID: + return ntmp_get_bpt_ops(version); + default: + return NULL; + } +} + +/** + * ntmp_add_entry_by_id - add a new entry to a table whose entry ID is + * allocated by software + * @user: target ntmp_user struct + * @tbl_id: ID of the target table + * @entry_id: the software-allocated entry ID + * @entry: the logical entry data to be packed into the request buffer + * @ua: update actions applied to the entry, 0 if none + * + * A generic helper for tables that add a new entry with a software-allocated + * entry ID. It looks up the table ops for the current version, allocates the + * request buffer, packs the entry data and issues the add command over a CBDR. + * + * Return: 0 on success, otherwise a negative error code + */ +static int ntmp_add_entry_by_id(struct ntmp_user *user, int tbl_id, + u32 entry_id, void *entry, u16 ua) +{ + u8 version = user->tbl_ver[tbl_id]; + const struct ntmp_tbl_ops *ops; + struct netc_swcbd swcbd; + struct netc_cbdr *cbdr; + union netc_cbd cbd; + void *buf; + int err; + + ops = ntmp_table_ops(tbl_id, version); + if (!ops) + return -EOPNOTSUPP; + + swcbd.size = ops->req_ua_len; + err = ntmp_alloc_data_mem(user->dev, &swcbd, &buf); + if (err) + return err; + + ntmp_fill_crd_eid(buf, version, 0, ua, entry_id); + ops->pack(entry, buf, NTMP_AM_ENTRY_ID); + ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(ops->req_ua_len, 0), + tbl_id, NTMP_CMD_ADD, NTMP_AM_ENTRY_ID); + + ntmp_select_and_lock_cbdr(user, &cbdr); + err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd); + if (err) + dev_err(user->dev, "Failed to add %s entry 0x%x, err: %pe\n", + ntmp_table_name(tbl_id), entry_id, ERR_PTR(err)); + ntmp_unlock_cbdr(cbdr); + + return err; +} + static int ntmp_delete_entry_by_id(struct ntmp_user *user, int tbl_id, u32 entry_id, u32 req_len, u32 resp_len) { @@ -716,39 +786,8 @@ static int ntmp_query_entry_by_id(struct netc_cbdr *cbdr, int tbl_id, int ntmp_maft_add_entry(struct ntmp_user *user, struct maft_entry_data *entry) { - u8 version = user->tbl_ver[NTMP_MAFT_ID]; - const struct ntmp_tbl_ops *ops; - struct netc_swcbd swcbd; - struct netc_cbdr *cbdr; - union netc_cbd cbd; - void *buf; - int err; - - ops = ntmp_get_maft_ops(version); - if (!ops) - return -EOPNOTSUPP; - - swcbd.size = ops->req_ua_len; - err = ntmp_alloc_data_mem(user->dev, &swcbd, &buf); - if (err) - return err; - - /* Set mac address filter table request data buffer */ - ntmp_fill_crd_eid(buf, version, 0, 0, entry->entry_id); - ops->pack(entry, buf, 0); - - ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0), - NTMP_MAFT_ID, NTMP_CMD_ADD, NTMP_AM_ENTRY_ID); - - ntmp_select_and_lock_cbdr(user, &cbdr); - err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd); - if (err) - dev_err(user->dev, "Failed to add %s entry 0x%x, err: %pe\n", - ntmp_table_name(NTMP_MAFT_ID), entry->entry_id, - ERR_PTR(err)); - ntmp_unlock_cbdr(cbdr); - - return err; + return ntmp_add_entry_by_id(user, NTMP_MAFT_ID, entry->entry_id, + (void *)entry, 0); } EXPORT_SYMBOL_GPL(ntmp_maft_add_entry); @@ -1460,17 +1499,32 @@ int ntmp_vft_delete_entry(struct ntmp_user *user, u16 vid) EXPORT_SYMBOL_GPL(ntmp_vft_delete_entry); /** - * ntmp_ett_set_entry - add a new entry to the egress treatment table or - * update the configuration element data of the specified entry + * ntmp_ett_add_entry - add a new entry to the egress treatment table * @user: target ntmp_user struct - * @cmd: command type, NTMP_CMD_ADD or NTMP_CMD_UPDATE - * @entry: the entry data, entry->entry_id selects the entry and the + * @entry: the entry data, entry->entry_id is the entry ID and the * configuration element data provides the values * * Return: 0 on success, otherwise a negative error code */ -static int ntmp_ett_set_entry(struct ntmp_user *user, int cmd, - const struct ett_entry_data *entry) +int ntmp_ett_add_entry(struct ntmp_user *user, + const struct ett_entry_data *entry) +{ + return ntmp_add_entry_by_id(user, NTMP_ETT_ID, entry->entry_id, + (void *)entry, NTMP_GEN_UA_CFGEU); +} +EXPORT_SYMBOL_GPL(ntmp_ett_add_entry); + +/** + * ntmp_ett_update_entry - update the configuration element data of the + * specified entry + * @user: target ntmp_user struct + * @entry: the entry data, entry->entry_id selects the entry and the + * configuration element data provides the new values + * + * Return: 0 on success, otherwise a negative error code + */ +int ntmp_ett_update_entry(struct ntmp_user *user, + const struct ett_entry_data *entry) { u8 version = user->tbl_ver[NTMP_ETT_ID]; const struct ntmp_tbl_ops *ops; @@ -1480,9 +1534,6 @@ static int ntmp_ett_set_entry(struct ntmp_user *user, int cmd, void *buf; int err; - if (cmd != NTMP_CMD_ADD && cmd != NTMP_CMD_UPDATE) - return -EINVAL; - ops = ntmp_get_ett_ops(version); if (!ops) return -EOPNOTSUPP; @@ -1499,59 +1550,18 @@ static int ntmp_ett_set_entry(struct ntmp_user *user, int cmd, /* Request header */ ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0), - NTMP_ETT_ID, cmd, NTMP_AM_ENTRY_ID); + NTMP_ETT_ID, NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID); ntmp_select_and_lock_cbdr(user, &cbdr); err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd); - ntmp_unlock_cbdr(cbdr); - - return err; -} - -/** - * ntmp_ett_add_entry - add a new entry to the egress treatment table - * @user: target ntmp_user struct - * @entry: the entry data, entry->entry_id is the entry ID and the - * configuration element data provides the values - * - * Return: 0 on success, otherwise a negative error code - */ -int ntmp_ett_add_entry(struct ntmp_user *user, - const struct ett_entry_data *entry) -{ - int err; - - err = ntmp_ett_set_entry(user, NTMP_CMD_ADD, entry); - if (err) - dev_err(user->dev, "Failed to add %s entry 0x%x, err: %pe\n", - ntmp_table_name(NTMP_ETT_ID), entry->entry_id, - ERR_PTR(err)); - - return err; -} -EXPORT_SYMBOL_GPL(ntmp_ett_add_entry); - -/** - * ntmp_ett_update_entry - update the configuration element data of the - * specified entry - * @user: target ntmp_user struct - * @entry: the entry data, entry->entry_id selects the entry and the - * configuration element data provides the new values - * - * Return: 0 on success, otherwise a negative error code - */ -int ntmp_ett_update_entry(struct ntmp_user *user, - const struct ett_entry_data *entry) -{ - int err; - - err = ntmp_ett_set_entry(user, NTMP_CMD_UPDATE, entry); if (err) dev_err(user->dev, "Failed to update %s entry 0x%x, err: %pe\n", ntmp_table_name(NTMP_ETT_ID), entry->entry_id, ERR_PTR(err)); + ntmp_unlock_cbdr(cbdr); + return err; } EXPORT_SYMBOL_GPL(ntmp_ett_update_entry);
-- 2.34.1