From: Wei Fang <wei…@nxp.com> Current SoCs only implement FDBT version 0, but future versions may extend the table with a different buffer layout and size. Today struct fdbt_entry_data embeds the version 0 hardware KEYE/CFGE structures directly, so callers must know the wire format and open-code endianness conversions. This interface is hard to extend: once a new version changes the buffer layout or size, updating the callers becomes cumbersome. Turn struct fdbt_entry_data into a version-agnostic config using native CPU types, and move the wire structures into ntmp_private.h to keep them internal to the NTMP layer. Add a struct ntmp_tbl_ops instance holding the buffer sizes and pack()/unpack() callbacks, and select it via ntmp_get_fdbt_ops(), following the MAFT and IPFT tables. Reshape the FDBT interface around the logical config. ntmp_fdbt_add_entry() and ntmp_fdbt_update_entry() now take a struct fdbt_entry_data pointer instead of separate keye/cfge pointers and an entry ID. Add a status field to the logical config to carry the RESUME_ENTRY_ID of a search, so ntmp_fdbt_search_port_entry() no longer needs a separate resume_entry_id argument and its caller resumes from entry->status. A future version can then be added within the NTMP layer by providing its wire structure, pack/unpack helpers and ops instance. Callers may still need small adjustments, but they no longer deal with the wire format or endianness. Signed-off-by: Wei Fang <wei…@nxp.com> --- drivers/net/dsa/netc/netc_main.c | 71 ++++---- drivers/net/dsa/netc/netc_switch.h | 4 +- drivers/net/ethernet/freescale/enetc/ntmp.c | 155 ++++++++++++------ .../ethernet/freescale/enetc/ntmp_private.h | 15 ++ include/linux/fsl/ntmp.h | 35 ++-- 5 files changed, 167 insertions(+), 113 deletions(-) diff --git a/drivers/net/dsa/netc/netc_main.c b/drivers/net/dsa/netc/netc_main.c index 9529eb66571c2..9573672d65a3f 100644 --- a/drivers/net/dsa/netc/netc_main.c +++ b/drivers/net/dsa/netc/netc_main.c @@ -21,8 +21,8 @@ netc_lookup_fdb_entry(struct netc_switch *priv, struct netc_fdb_entry *entry; hlist_for_each_entry(entry, &priv->fdb_list, node) - if (ether_addr_equal(entry->keye.mac_addr, addr) && - le16_to_cpu(entry->keye.fid) == vid) + if (ether_addr_equal(entry->data.mac_addr, addr) && + entry->data.fid == vid) return entry; return NULL; @@ -739,26 +739,19 @@ static int netc_port_add_fdb_entry(struct netc_port *np, { struct netc_switch *priv = np->switch_priv; struct netc_fdb_entry *entry; - struct fdbt_keye_data *keye; - struct fdbt_cfge_data *cfge; int port = np->dp->index; - u32 cfg = 0; int err; entry = kzalloc_obj(*entry); if (!entry) return -ENOMEM; - keye = &entry->keye; - cfge = &entry->cfge; - ether_addr_copy(keye->mac_addr, addr); - keye->fid = cpu_to_le16(vid); - - cfge->port_bitmap = cpu_to_le32(BIT(port)); - cfge->cfg = cpu_to_le32(cfg); - cfge->et_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID); + ether_addr_copy(entry->data.mac_addr, addr); + entry->data.fid = vid; + entry->data.port_bitmap = BIT(port); + entry->data.et_eid = NTMP_NULL_ENTRY_ID; - err = ntmp_fdbt_add_entry(&priv->ntmp, &entry->entry_id, keye, cfge); + err = ntmp_fdbt_add_entry(&priv->ntmp, &entry->data); if (err) { kfree(entry); @@ -775,9 +768,8 @@ static int netc_port_set_fdb_entry(struct netc_port *np, { struct netc_switch *priv = np->switch_priv; struct netc_fdb_entry *entry; - struct fdbt_cfge_data *cfge; int port = np->dp->index; - __le32 old_port_bitmap; + u32 old_port_bitmap; int err = 0; mutex_lock(&priv->fdbt_lock); @@ -793,24 +785,23 @@ static int netc_port_set_fdb_entry(struct netc_port *np, goto unlock_fdbt; } - cfge = &entry->cfge; /* If the entry already exists on the port, return 0 directly */ - if (unlikely(cfge->port_bitmap & cpu_to_le32(BIT(port)))) + if (unlikely(entry->data.port_bitmap & BIT(port))) goto unlock_fdbt; /* If the entry already exists, but not on this port, we need to * update the port bitmap. In general, it should only be valid * for multicast or broadcast address. */ - old_port_bitmap = cfge->port_bitmap; + old_port_bitmap = entry->data.port_bitmap; if (is_multicast_ether_addr(addr)) - cfge->port_bitmap |= cpu_to_le32(BIT(port)); + entry->data.port_bitmap |= BIT(port); else - cfge->port_bitmap = cpu_to_le32(BIT(port)); + entry->data.port_bitmap = BIT(port); - err = ntmp_fdbt_update_entry(&priv->ntmp, entry->entry_id, cfge); + err = ntmp_fdbt_update_entry(&priv->ntmp, &entry->data); if (err) { - cfge->port_bitmap = old_port_bitmap; + entry->data.port_bitmap = old_port_bitmap; dev_err(priv->dev, "Failed to set FDB entry on port %d\n", port); } @@ -827,7 +818,6 @@ static int netc_port_del_fdb_entry(struct netc_port *np, struct netc_switch *priv = np->switch_priv; struct ntmp_user *ntmp = &priv->ntmp; struct netc_fdb_entry *entry; - struct fdbt_cfge_data *cfge; int port = np->dp->index; int err = 0; @@ -848,25 +838,24 @@ static int netc_port_del_fdb_entry(struct netc_port *np, */ goto unlock_fdbt; - cfge = &entry->cfge; - if (unlikely(!(cfge->port_bitmap & cpu_to_le32(BIT(port))))) + if (unlikely(!(entry->data.port_bitmap & BIT(port)))) goto unlock_fdbt; - if (cfge->port_bitmap != cpu_to_le32(BIT(port))) { + if (entry->data.port_bitmap != BIT(port)) { /* If the entry also exists on other ports, we need to * update the entry in the FDB table. */ - cfge->port_bitmap &= cpu_to_le32(~BIT(port)); - err = ntmp_fdbt_update_entry(ntmp, entry->entry_id, cfge); + entry->data.port_bitmap &= ~BIT(port); + err = ntmp_fdbt_update_entry(ntmp, &entry->data); if (err) { - cfge->port_bitmap |= cpu_to_le32(BIT(port)); + entry->data.port_bitmap |= BIT(port); goto unlock_fdbt; } } else { /* If the entry only exists on this port, just delete * it from the FDB table. */ - err = ntmp_fdbt_delete_entry(ntmp, entry->entry_id); + err = ntmp_fdbt_delete_entry(ntmp, entry->data.entry_id); if (err) goto unlock_fdbt; @@ -1818,10 +1807,8 @@ static int netc_port_fdb_dump(struct dsa_switch *ds, int port, struct netc_switch *priv = ds->priv; u32 resume_eid = NTMP_NULL_ENTRY_ID; struct fdbt_entry_data *entry; - struct fdbt_keye_data *keye; - struct fdbt_cfge_data *cfge; - u32 cfg, cnt = 0; bool is_static; + u32 cnt = 0; int err; u16 vid; @@ -1829,24 +1816,22 @@ static int netc_port_fdb_dump(struct dsa_switch *ds, int port, if (!entry) return -ENOMEM; - keye = &entry->keye; - cfge = &entry->cfge; mutex_lock(&priv->fdbt_lock); do { memset(entry, 0, sizeof(*entry)); - err = ntmp_fdbt_search_port_entry(&priv->ntmp, port, - &resume_eid, entry); + entry->status = resume_eid; + + err = ntmp_fdbt_search_port_entry(&priv->ntmp, port, entry); if (err || entry->entry_id == NTMP_NULL_ENTRY_ID) break; - cfg = le32_to_cpu(cfge->cfg); - is_static = (cfg & FDBT_DYNAMIC) ? false : true; - vid = le16_to_cpu(keye->fid); + is_static = (entry->cfg & FDBT_DYNAMIC) ? false : true; + vid = entry->fid; if (vid >= NETC_VLAN_UNAWARE_PVID(ds->max_num_bridges)) vid = 0; - err = cb(keye->mac_addr, vid, is_static, data); + err = cb(entry->mac_addr, vid, is_static, data); if (err) break; @@ -1855,6 +1840,8 @@ static int netc_port_fdb_dump(struct dsa_switch *ds, int port, */ if (++cnt >= priv->htmcapr_num_words) break; + + resume_eid = entry->status; } while (resume_eid != NTMP_NULL_ENTRY_ID); mutex_unlock(&priv->fdbt_lock); diff --git a/drivers/net/dsa/netc/netc_switch.h b/drivers/net/dsa/netc/netc_switch.h index ae5e24c91f1cb..3c8dd539ed830 100644 --- a/drivers/net/dsa/netc/netc_switch.h +++ b/drivers/net/dsa/netc/netc_switch.h @@ -136,9 +136,7 @@ struct netc_switch_regs { }; struct netc_fdb_entry { - u32 entry_id; - struct fdbt_cfge_data cfge; - struct fdbt_keye_data keye; + struct fdbt_entry_data data; struct hlist_node node; }; diff --git a/drivers/net/ethernet/freescale/enetc/ntmp.c b/drivers/net/ethernet/freescale/enetc/ntmp.c index 6143e8b5c6b1f..801e4e7bb8314 100644 --- a/drivers/net/ethernet/freescale/enetc/ntmp.c +++ b/drivers/net/ethernet/freescale/enetc/ntmp.c @@ -468,6 +468,62 @@ static const struct ntmp_tbl_ops *ntmp_get_ipft_ops(u8 version) } } +static void ntmp_fdbt_pack_v0(const void *entry, void *buf, int access_method) +{ + const struct fdbt_entry_data *e = entry; + struct fdbt_req_ua *req = buf; + struct fdbt_cfge_data *cfge; + + if (access_method == NTMP_AM_EXACT_KEY) { + struct fdbt_keye_data *keye = &req->ak.exact.keye; + + memcpy(keye->mac_addr, e->mac_addr, ETH_ALEN); + keye->fid = cpu_to_le16(e->fid); + } else { /* NTMP_AM_ENTRY_ID */ + req->ak.eid.entry_id = cpu_to_le32(e->entry_id); + } + + cfge = &req->cfge; + cfge->port_bitmap = cpu_to_le32(e->port_bitmap); + cfge->cfg = cpu_to_le32(e->cfg); + cfge->et_eid = cpu_to_le32(e->et_eid); +} + +static void ntmp_fdbt_unpack_v0(const void *buf, void *entry, bool only_eid) +{ + const struct fdbt_resp_query *resp = buf; + struct fdbt_entry_data *e = entry; + + e->entry_id = le32_to_cpu(resp->entry_id); + if (only_eid) + return; + + e->status = le32_to_cpu(resp->status); + memcpy(e->mac_addr, resp->keye.mac_addr, ETH_ALEN); + e->fid = le16_to_cpu(resp->keye.fid); + e->port_bitmap = le32_to_cpu(resp->cfge.port_bitmap); + e->cfg = le32_to_cpu(resp->cfge.cfg); + e->et_eid = le32_to_cpu(resp->cfge.et_eid); + e->acte = resp->acte; +} + +static const struct ntmp_tbl_ops fdbt_ops_v0 = { + .req_ua_len = sizeof(struct fdbt_req_ua), + .resp_query_len = sizeof(struct fdbt_resp_query), + .pack = ntmp_fdbt_pack_v0, + .unpack = ntmp_fdbt_unpack_v0, +}; + +static const struct ntmp_tbl_ops *ntmp_get_fdbt_ops(u8 version) +{ + switch (version) { + case NTMP_TBL_V(0): + return &fdbt_ops_v0; + default: + return NULL; + } +} + static int ntmp_delete_entry_by_id(struct ntmp_user *user, int tbl_id, u32 entry_id, u32 req_len, u32 resp_len) { @@ -785,36 +841,38 @@ EXPORT_SYMBOL_GPL(ntmp_ipft_delete_entry); /** * ntmp_fdbt_add_entry - add an entry into the FDB table * @user: target ntmp_user struct - * @entry_id: returned value, the entry ID of the new added entry - * @keye: key element data - * @cfge: configuration element data + * @entry: the entry data, the key element data and configuration element + * data are used as input. Since the entry ID is assigned by the hardware, + * entry->entry_id is a returned value for the driver to use. * * Return: 0 on success, otherwise a negative error code */ -int ntmp_fdbt_add_entry(struct ntmp_user *user, u32 *entry_id, - const struct fdbt_keye_data *keye, - const struct fdbt_cfge_data *cfge) +int ntmp_fdbt_add_entry(struct ntmp_user *user, + struct fdbt_entry_data *entry) { - struct fdbt_resp_query *resp; - struct fdbt_req_ua *req; + 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; - swcbd.size = sizeof(*req); - err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req); + 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(&req->crd, user->tbl_ver[NTMP_FDBT_ID], NTMP_QA_ENTRY_ID, - NTMP_GEN_UA_CFGEU); - req->ak.exact.keye = *keye; - req->cfge = *cfge; + ntmp_fill_crd(buf, version, NTMP_QA_ENTRY_ID, NTMP_GEN_UA_CFGEU); + ops->pack(entry, buf, NTMP_AM_EXACT_KEY); - len = NTMP_LEN(swcbd.size, sizeof(*resp)); + 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. @@ -830,10 +888,7 @@ int ntmp_fdbt_add_entry(struct ntmp_user *user, u32 *entry_id, goto unlock_cbdr; } - if (entry_id) { - resp = (struct fdbt_resp_query *)req; - *entry_id = le32_to_cpu(resp->entry_id); - } + ops->unpack(buf, entry, true); unlock_cbdr: ntmp_unlock_cbdr(cbdr); @@ -846,33 +901,38 @@ EXPORT_SYMBOL_GPL(ntmp_fdbt_add_entry); * ntmp_fdbt_update_entry - update the configuration element data of the * specified FDB entry * @user: target ntmp_user struct - * @entry_id: the specified entry ID of the FDB table - * @cfge: configuration element data + * @entry: the entry data, entry->entry_id selects the FDB entry to update + * and the configuration element data provides the new values * * Return: 0 on success, otherwise a negative error code */ -int ntmp_fdbt_update_entry(struct ntmp_user *user, u32 entry_id, - const struct fdbt_cfge_data *cfge) +int ntmp_fdbt_update_entry(struct ntmp_user *user, + const struct fdbt_entry_data *entry) { - struct fdbt_req_ua *req; + 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; - swcbd.size = sizeof(*req); - err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req); + ops = ntmp_get_fdbt_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; /* Request data */ - ntmp_fill_crd(&req->crd, user->tbl_ver[NTMP_FDBT_ID], 0, NTMP_GEN_UA_CFGEU); - req->ak.eid.entry_id = cpu_to_le32(entry_id); - req->cfge = *cfge; + ntmp_fill_crd(buf, version, 0, NTMP_GEN_UA_CFGEU); + ops->pack(entry, buf, NTMP_AM_ENTRY_ID); /* Request header */ - len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN); + len = NTMP_LEN(ops->req_ua_len, NTMP_STATUS_RESP_LEN); ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID, NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID); @@ -909,19 +969,19 @@ EXPORT_SYMBOL_GPL(ntmp_fdbt_delete_entry); * port based on RESUME_ENTRY_ID * @user: target ntmp_user struct * @port: the specified switch port ID - * @resume_entry_id: it is both an input and an output. As an input, it - * represents the FDB entry ID to be searched. If it is a NULL entry ID, - * it indicates that the first FDB entry for that port is being searched. - * As an output, it represents the next FDB entry ID to be searched. - * @entry: returned value, the response data of the searched FDB entry + * @entry: both an input and an output. On input, entry->status is the + * FDB entry ID to resume the search from; a NULL entry ID means searching + * from the first FDB entry of that port. On output, entry holds the + * response data of the searched FDB entry, and entry->status is the next + * FDB entry ID to resume from. * * Return: 0 on success, otherwise a negative error code */ int ntmp_fdbt_search_port_entry(struct ntmp_user *user, int port, - u32 *resume_entry_id, struct fdbt_entry_data *entry) { - struct fdbt_resp_query *resp; + u8 version = user->tbl_ver[NTMP_FDBT_ID]; + const struct ntmp_tbl_ops *ops; struct fdbt_req_qd *req; struct netc_swcbd swcbd; struct netc_cbdr *cbdr; @@ -929,20 +989,24 @@ int ntmp_fdbt_search_port_entry(struct ntmp_user *user, int port, u32 len; int err; - swcbd.size = sizeof(*req); + ops = ntmp_get_fdbt_ops(version); + if (!ops) + return -EOPNOTSUPP; + + swcbd.size = max_t(u32, sizeof(*req), ops->resp_query_len); err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req); if (err) return err; /* Request data */ - ntmp_fill_crd(&req->crd, user->tbl_ver[NTMP_FDBT_ID], 0, 0); - req->ak.search.resume_eid = cpu_to_le32(*resume_entry_id); + ntmp_fill_crd(&req->crd, version, 0, 0); + req->ak.search.resume_eid = cpu_to_le32(entry->status); req->ak.search.cfge.port_bitmap = cpu_to_le32(BIT(port)); /* Match CFGE_DATA[PORT_BITMAP] field */ req->ak.search.cfge_mc = FDBT_CFGE_MC_PORT_BITMAP; /* Request header */ - len = NTMP_LEN(swcbd.size, sizeof(*resp)); + len = NTMP_LEN(swcbd.size, ops->resp_query_len); ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID, NTMP_CMD_QUERY, NTMP_AM_SEARCH); @@ -957,16 +1021,11 @@ int ntmp_fdbt_search_port_entry(struct ntmp_user *user, int port, if (!cbd.resp_hdr.num_matched) { entry->entry_id = NTMP_NULL_ENTRY_ID; - *resume_entry_id = NTMP_NULL_ENTRY_ID; + entry->status = NTMP_NULL_ENTRY_ID; goto unlock_cbdr; } - resp = (struct fdbt_resp_query *)req; - *resume_entry_id = le32_to_cpu(resp->status); - entry->entry_id = le32_to_cpu(resp->entry_id); - entry->keye = resp->keye; - entry->cfge = resp->cfge; - entry->acte = resp->acte; + ops->unpack(req, entry, false); unlock_cbdr: ntmp_unlock_cbdr(cbdr); diff --git a/drivers/net/ethernet/freescale/enetc/ntmp_private.h b/drivers/net/ethernet/freescale/enetc/ntmp_private.h index 5dcfc98b51066..f75129af40606 100644 --- a/drivers/net/ethernet/freescale/enetc/ntmp_private.h +++ b/drivers/net/ethernet/freescale/enetc/ntmp_private.h @@ -219,6 +219,21 @@ struct ipft_req_qd { __le32 resv[52]; }; +/* FDB Table Key Element Data */ +struct fdbt_keye_data { + u8 mac_addr[ETH_ALEN]; /* big-endian */ + __le16 resv0; + __le16 fid; + __le16 resv1; +}; + +/* FDB Table Configuration Element Data */ +struct fdbt_cfge_data { + __le32 port_bitmap; + __le32 cfg; + __le32 et_eid; +}; + /* Access Key Format of FDB Table */ struct fdbt_ak_eid { __le32 entry_id; diff --git a/include/linux/fsl/ntmp.h b/include/linux/fsl/ntmp.h index 226c672c845af..411a3f67c0268 100644 --- a/include/linux/fsl/ntmp.h +++ b/include/linux/fsl/ntmp.h @@ -169,31 +169,28 @@ struct ipft_entry_data { u32 flta_tgt; }; -struct fdbt_keye_data { +struct fdbt_entry_data { + u32 entry_id; + u32 status; /* RESUME_ENTRY_ID returned by a search operation */ + + /* KEYE_DATA */ u8 mac_addr[ETH_ALEN]; /* big-endian */ - __le16 resv0; - __le16 fid; + u16 fid; #define FDBT_FID GENMASK(11, 0) - __le16 resv1; -}; -struct fdbt_cfge_data { - __le32 port_bitmap; + /* CFGE_DATA */ + u32 port_bitmap; #define FDBT_PORT_BITMAP GENMASK(23, 0) - __le32 cfg; + u32 cfg; #define FDBT_OETEID GENMASK(1, 0) #define FDBT_EPORT GENMASK(6, 2) #define FDBT_IMIRE BIT(7) #define FDBT_CTD GENMASK(10, 9) #define FDBT_DYNAMIC BIT(11) #define FDBT_TIMECAPE BIT(12) - __le32 et_eid; -}; + u32 et_eid; -struct fdbt_entry_data { - u32 entry_id; - struct fdbt_keye_data keye; - struct fdbt_cfge_data cfge; + /* ACTE_DATA */ u8 acte; #define FDBT_ACT_CNT GENMASK(6, 0) #define FDBT_ACT_FLAG BIT(7) @@ -293,14 +290,12 @@ int ntmp_rsst_query_entry(struct ntmp_user *user, int ntmp_ipft_add_entry(struct ntmp_user *user, struct ipft_entry_data *entry); int ntmp_ipft_delete_entry(struct ntmp_user *user, u32 entry_id); -int ntmp_fdbt_add_entry(struct ntmp_user *user, u32 *entry_id, - const struct fdbt_keye_data *keye, - const struct fdbt_cfge_data *cfge); -int ntmp_fdbt_update_entry(struct ntmp_user *user, u32 entry_id, - const struct fdbt_cfge_data *cfge); +int ntmp_fdbt_add_entry(struct ntmp_user *user, + struct fdbt_entry_data *entry); +int ntmp_fdbt_update_entry(struct ntmp_user *user, + const struct fdbt_entry_data *entry); int ntmp_fdbt_delete_entry(struct ntmp_user *user, u32 entry_id); int ntmp_fdbt_search_port_entry(struct ntmp_user *user, int port, - u32 *resume_entry_id, struct fdbt_entry_data *entry); int ntmp_fdbt_update_activity_element(struct ntmp_user *user); int ntmp_fdbt_delete_ageing_entries(struct ntmp_user *user, u8 act_cnt);
-- 2.34.1