From: Wei Fang <wei…@nxp.com> The Add operations for the IPFT, FDBT and VLAN filter tables share the same logic: pack the request, issue an "Add then Query" command so the hardware-allotted entry ID can be read back, and unpack the response. Three separate functions open-coded this identical flow. Add a generic ntmp_add_entry_by_key() helper that takes the table ID, the update-actions mask and the access method, and convert ntmp_ipft_add_entry(), ntmp_fdbt_add_entry() and ntmp_vft_add_entry() into thin wrappers around it. With the Add path moved out, ntmp_vft_set_entry() no longer needs to handle both commands. Turn it into a dedicated ntmp_vft_update_entry() that always issues NTMP_CMD_UPDATE, and drop the now-redundant command argument and its validation. Signed-off-by: Wei Fang <wei…@nxp.com> --- drivers/net/ethernet/freescale/enetc/ntmp.c | 242 ++++++++------------ 1 file changed, 95 insertions(+), 147 deletions(-) diff --git a/drivers/net/ethernet/freescale/enetc/ntmp.c b/drivers/net/ethernet/freescale/enetc/ntmp.c index 3f8a39df78b6f..0622e7e0b5756 100644 --- a/drivers/net/ethernet/freescale/enetc/ntmp.c +++ b/drivers/net/ethernet/freescale/enetc/ntmp.c @@ -910,6 +910,73 @@ static int ntmp_query_entry_by_id(struct ntmp_user *user, int tbl_id, return err; } +/** + * ntmp_add_entry_by_key - add a new entry to a table by its key element + * @user: target ntmp_user struct + * @tbl_id: ID of the target table + * @entry: the logical entry data to be packed into the request buffer, the + * hardware-allocated entry ID is read back into it on success + * @ua: update actions applied to the entry, 0 if none + * @access_method: how the key element is matched, either NTMP_AM_EXACT_KEY + * for tables matched by an exact key or NTMP_AM_TERNARY_KEY for tables + * matched by a masked (ternary) key + * + * A generic helper for tables that add a new entry by its key element. The + * entry ID of such tables is allocated by hardware, so it packs the entry + * data and issues an "Add and Query" command over a CBDR, so that the + * hardware-allocated entry ID can be read back and unpacked into @entry. + * + * Return: 0 on success, otherwise a negative error code + */ +static int ntmp_add_entry_by_key(struct ntmp_user *user, int tbl_id, + void *entry, u16 ua, int access_method) +{ + 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; + u32 len; + int err; + + ops = ntmp_table_ops(tbl_id, version); + if (!ops) + return -EOPNOTSUPP; + + swcbd.size = max(ops->req_ua_len, ops->resp_query_len); + err = ntmp_alloc_data_mem(user->dev, &swcbd, &buf); + if (err) + return err; + + /* Request data */ + ntmp_fill_crd(buf, version, NTMP_QA_ENTRY_ID, ua); + ops->pack(entry, buf, access_method); + + len = NTMP_LEN(ops->req_ua_len, ops->resp_query_len); + /* The entry ID is allocated by hardware, so an "Add, followed by a + * Query" operation is required to retrieve the entry ID from the + * hardware. + */ + ntmp_fill_request_hdr(&cbd, swcbd.dma, len, tbl_id, + NTMP_CMD_AQ, access_method); + + 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, err: %pe\n", + ntmp_table_name(tbl_id), ERR_PTR(err)); + goto unlock_cbdr; + } + + ops->unpack(buf, entry, true); + +unlock_cbdr: + ntmp_unlock_cbdr(cbdr); + + return err; +} + int ntmp_maft_add_entry(struct ntmp_user *user, struct maft_entry_data *entry) { return ntmp_add_entry_by_id(user, NTMP_MAFT_ID, entry->entry_id, @@ -1028,51 +1095,9 @@ EXPORT_SYMBOL_GPL(ntmp_rsst_query_entry); int ntmp_ipft_add_entry(struct ntmp_user *user, struct ipft_entry_data *entry) { - u8 version = user->tbl_ver[NTMP_IPFT_ID]; - const struct ntmp_tbl_ops *ops; - struct netc_swcbd swcbd; - struct netc_cbdr *cbdr; - union netc_cbd cbd; - void *buf; - u32 len; - int err; - - ops = ntmp_get_ipft_ops(version); - if (!ops) - return -EOPNOTSUPP; - - swcbd.size = max(ops->req_ua_len, ops->resp_query_len); - err = ntmp_alloc_data_mem(user->dev, &swcbd, &buf); - if (err) - return err; - - /* Note that NTMP_GEN_UA_STSEU is used to reset the statistics of - * the entry. The STSE_DATA is not present in the request data for - * 'Add' operation. - */ - ntmp_fill_crd(buf, version, NTMP_QA_ENTRY_ID, - NTMP_GEN_UA_CFGEU | NTMP_GEN_UA_STSEU); - ops->pack(entry, buf, NTMP_AM_TERNARY_KEY); - - len = NTMP_LEN(ops->req_ua_len, swcbd.size); - ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_IPFT_ID, - NTMP_CMD_AQ, NTMP_AM_TERNARY_KEY); - - 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, err: %pe\n", - ntmp_table_name(NTMP_IPFT_ID), ERR_PTR(err)); - - goto unlock_cbdr; - } - - ops->unpack(buf, entry, true); - -unlock_cbdr: - ntmp_unlock_cbdr(cbdr); - - return err; + return ntmp_add_entry_by_key(user, NTMP_IPFT_ID, (void *)entry, + NTMP_GEN_UA_CFGEU | NTMP_GEN_UA_STSEU, + NTMP_AM_TERNARY_KEY); } EXPORT_SYMBOL_GPL(ntmp_ipft_add_entry); @@ -1104,50 +1129,8 @@ EXPORT_SYMBOL_GPL(ntmp_ipft_delete_entry); int ntmp_fdbt_add_entry(struct ntmp_user *user, struct fdbt_entry_data *entry) { - u8 version = user->tbl_ver[NTMP_FDBT_ID]; - const struct ntmp_tbl_ops *ops; - struct netc_swcbd swcbd; - struct netc_cbdr *cbdr; - union netc_cbd cbd; - void *buf; - u32 len; - int err; - - ops = ntmp_get_fdbt_ops(version); - if (!ops) - return -EOPNOTSUPP; - - swcbd.size = max(ops->req_ua_len, ops->resp_query_len); - err = ntmp_alloc_data_mem(user->dev, &swcbd, &buf); - if (err) - return err; - - /* Request data */ - ntmp_fill_crd(buf, version, NTMP_QA_ENTRY_ID, NTMP_GEN_UA_CFGEU); - ops->pack(entry, buf, NTMP_AM_EXACT_KEY); - - len = NTMP_LEN(ops->req_ua_len, ops->resp_query_len); - /* The entry ID is allotted by hardware, so we need to perform - * a query action after the add action to get the entry ID from - * hardware. - */ - ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID, - NTMP_CMD_AQ, NTMP_AM_EXACT_KEY); - - 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, err: %pe\n", - ntmp_table_name(NTMP_FDBT_ID), ERR_PTR(err)); - goto unlock_cbdr; - } - - ops->unpack(buf, entry, true); - -unlock_cbdr: - ntmp_unlock_cbdr(cbdr); - - return err; + return ntmp_add_entry_by_key(user, NTMP_FDBT_ID, (void *)entry, + NTMP_GEN_UA_CFGEU, NTMP_AM_EXACT_KEY); } EXPORT_SYMBOL_GPL(ntmp_fdbt_add_entry); @@ -1418,17 +1401,31 @@ int ntmp_fdbt_delete_port_dynamic_entries(struct ntmp_user *user, int port) EXPORT_SYMBOL_GPL(ntmp_fdbt_delete_port_dynamic_entries); /** - * ntmp_vft_set_entry - add an entry into the VLAN filter table or update - * the configuration element data of the specified VLAN filter entry + * ntmp_vft_add_entry - add an entry into the VLAN filter table + * @user: target ntmp_user struct + * @entry: the entry data, entry->vid is the VLAN ID and the configuration + * element data provides the values + * Return: 0 on success, otherwise a negative error code + */ +int ntmp_vft_add_entry(struct ntmp_user *user, + struct vft_entry_data *entry) +{ + return ntmp_add_entry_by_key(user, NTMP_VFT_ID, (void *)entry, + NTMP_GEN_UA_CFGEU, NTMP_AM_EXACT_KEY); +} +EXPORT_SYMBOL_GPL(ntmp_vft_add_entry); + +/** + * ntmp_vft_update_entry - update the configuration element data of the + * specified VLAN filter entry * @user: target ntmp_user struct - * @cmd: command type, NTMP_CMD_AQ or NTMP_CMD_UPDATE * @entry: the entry data, entry->vid selects the VLAN filter entry and - * the configuration element data provides the values + * the configuration element data provides the new values * * Return: 0 on success, otherwise a negative error code */ -static int ntmp_vft_set_entry(struct ntmp_user *user, int cmd, - struct vft_entry_data *entry) +int ntmp_vft_update_entry(struct ntmp_user *user, + struct vft_entry_data *entry) { u8 version = user->tbl_ver[NTMP_VFT_ID]; const struct ntmp_tbl_ops *ops; @@ -1455,67 +1452,18 @@ static int ntmp_vft_set_entry(struct ntmp_user *user, int cmd, /* Request header */ len = NTMP_LEN(ops->req_ua_len, ops->resp_query_len); ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_VFT_ID, - cmd, NTMP_AM_EXACT_KEY); + NTMP_CMD_UPDATE, NTMP_AM_EXACT_KEY); ntmp_select_and_lock_cbdr(user, &cbdr); err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd); - if (err) - goto unlock_cbdr; - - if (cmd == NTMP_CMD_AQ) - ops->unpack(buf, (void *)entry, true); - -unlock_cbdr: - ntmp_unlock_cbdr(cbdr); - - return err; -} - -/** - * ntmp_vft_add_entry - add an entry into the VLAN filter table - * @user: target ntmp_user struct - * @entry: the entry data, entry->vid is the VLAN ID and the configuration - * element data provides the values - * - * Return: 0 on success, otherwise a negative error code - */ -int ntmp_vft_add_entry(struct ntmp_user *user, - struct vft_entry_data *entry) -{ - int err; - - err = ntmp_vft_set_entry(user, NTMP_CMD_AQ, entry); - if (err) - dev_err(user->dev, - "Failed to add %s entry, vid: %u, err: %pe\n", - ntmp_table_name(NTMP_VFT_ID), entry->vid, - ERR_PTR(err)); - - return err; -} -EXPORT_SYMBOL_GPL(ntmp_vft_add_entry); - -/** - * ntmp_vft_update_entry - update the configuration element data of the - * specified VLAN filter entry - * @user: target ntmp_user struct - * @entry: the entry data, entry->vid selects the VLAN filter entry and - * the configuration element data provides the new values - * - * Return: 0 on success, otherwise a negative error code - */ -int ntmp_vft_update_entry(struct ntmp_user *user, - struct vft_entry_data *entry) -{ - int err; - - err = ntmp_vft_set_entry(user, NTMP_CMD_UPDATE, entry); if (err) dev_err(user->dev, "Failed to update %s entry, vid: %u, err: %pe\n", ntmp_table_name(NTMP_VFT_ID), entry->vid, ERR_PTR(err)); + ntmp_unlock_cbdr(cbdr); + return err; } EXPORT_SYMBOL_GPL(ntmp_vft_update_entry);
-- 2.34.1