196de2506SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
296de2506SJakub Kicinski /* Copyright (C) 2018 Netronome Systems, Inc. */
373a0329bSJakub Kicinski
473a0329bSJakub Kicinski #include <linux/bitfield.h>
573a0329bSJakub Kicinski #include <linux/device.h>
673a0329bSJakub Kicinski #include <linux/kernel.h>
773a0329bSJakub Kicinski #include <linux/types.h>
873a0329bSJakub Kicinski
973a0329bSJakub Kicinski #include "nfp_net_ctrl.h"
1073a0329bSJakub Kicinski #include "nfp_net.h"
1173a0329bSJakub Kicinski
nfp_net_tlv_caps_reset(struct nfp_net_tlv_caps * caps)1273a0329bSJakub Kicinski static void nfp_net_tlv_caps_reset(struct nfp_net_tlv_caps *caps)
1373a0329bSJakub Kicinski {
1473a0329bSJakub Kicinski memset(caps, 0, sizeof(*caps));
15ce991ab6SJakub Kicinski caps->me_freq_mhz = 1200;
16527d7d1bSJakub Kicinski caps->mbox_off = NFP_NET_CFG_MBOX_BASE;
17527d7d1bSJakub Kicinski caps->mbox_len = NFP_NET_CFG_MBOX_VAL_MAX_SZ;
1873a0329bSJakub Kicinski }
1973a0329bSJakub Kicinski
20*6a35ddc5SJakub Kicinski static bool
nfp_net_tls_parse_crypto_ops(struct device * dev,struct nfp_net_tlv_caps * caps,u8 __iomem * ctrl_mem,u8 __iomem * data,unsigned int length,unsigned int offset,bool rx_stream_scan)21*6a35ddc5SJakub Kicinski nfp_net_tls_parse_crypto_ops(struct device *dev, struct nfp_net_tlv_caps *caps,
22*6a35ddc5SJakub Kicinski u8 __iomem *ctrl_mem, u8 __iomem *data,
23*6a35ddc5SJakub Kicinski unsigned int length, unsigned int offset,
24*6a35ddc5SJakub Kicinski bool rx_stream_scan)
25*6a35ddc5SJakub Kicinski {
26*6a35ddc5SJakub Kicinski /* Ignore the legacy TLV if new one was already parsed */
27*6a35ddc5SJakub Kicinski if (caps->tls_resync_ss && !rx_stream_scan)
28*6a35ddc5SJakub Kicinski return true;
29*6a35ddc5SJakub Kicinski
30*6a35ddc5SJakub Kicinski if (length < 32) {
31*6a35ddc5SJakub Kicinski dev_err(dev,
32*6a35ddc5SJakub Kicinski "CRYPTO OPS TLV should be at least 32B, is %dB offset:%u\n",
33*6a35ddc5SJakub Kicinski length, offset);
34*6a35ddc5SJakub Kicinski return false;
35*6a35ddc5SJakub Kicinski }
36*6a35ddc5SJakub Kicinski
37*6a35ddc5SJakub Kicinski caps->crypto_ops = readl(data);
38*6a35ddc5SJakub Kicinski caps->crypto_enable_off = data - ctrl_mem + 16;
39*6a35ddc5SJakub Kicinski caps->tls_resync_ss = rx_stream_scan;
40*6a35ddc5SJakub Kicinski
41*6a35ddc5SJakub Kicinski return true;
42*6a35ddc5SJakub Kicinski }
43*6a35ddc5SJakub Kicinski
nfp_net_tlv_caps_parse(struct device * dev,u8 __iomem * ctrl_mem,struct nfp_net_tlv_caps * caps)4473a0329bSJakub Kicinski int nfp_net_tlv_caps_parse(struct device *dev, u8 __iomem *ctrl_mem,
4573a0329bSJakub Kicinski struct nfp_net_tlv_caps *caps)
4673a0329bSJakub Kicinski {
4773a0329bSJakub Kicinski u8 __iomem *data = ctrl_mem + NFP_NET_CFG_TLV_BASE;
4873a0329bSJakub Kicinski u8 __iomem *end = ctrl_mem + NFP_NET_CFG_BAR_SZ;
4973a0329bSJakub Kicinski u32 hdr;
5073a0329bSJakub Kicinski
5173a0329bSJakub Kicinski nfp_net_tlv_caps_reset(caps);
5273a0329bSJakub Kicinski
5373a0329bSJakub Kicinski hdr = readl(data);
5473a0329bSJakub Kicinski if (!hdr)
5573a0329bSJakub Kicinski return 0;
5673a0329bSJakub Kicinski
5773a0329bSJakub Kicinski while (true) {
5873a0329bSJakub Kicinski unsigned int length, offset;
5973a0329bSJakub Kicinski u32 hdr = readl(data);
6073a0329bSJakub Kicinski
6173a0329bSJakub Kicinski length = FIELD_GET(NFP_NET_CFG_TLV_HEADER_LENGTH, hdr);
621d8ef0c0SEdwin Peer offset = data - ctrl_mem;
6373a0329bSJakub Kicinski
6473a0329bSJakub Kicinski /* Advance past the header */
6573a0329bSJakub Kicinski data += 4;
6673a0329bSJakub Kicinski
6773a0329bSJakub Kicinski if (length % NFP_NET_CFG_TLV_LENGTH_INC) {
689571d987SJakub Kicinski dev_err(dev, "TLV size not multiple of %u offset:%u len:%u\n",
699571d987SJakub Kicinski NFP_NET_CFG_TLV_LENGTH_INC, offset, length);
7073a0329bSJakub Kicinski return -EINVAL;
7173a0329bSJakub Kicinski }
7273a0329bSJakub Kicinski if (data + length > end) {
7373a0329bSJakub Kicinski dev_err(dev, "oversized TLV offset:%u len:%u\n",
7473a0329bSJakub Kicinski offset, length);
7573a0329bSJakub Kicinski return -EINVAL;
7673a0329bSJakub Kicinski }
7773a0329bSJakub Kicinski
7873a0329bSJakub Kicinski switch (FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE, hdr)) {
7973a0329bSJakub Kicinski case NFP_NET_CFG_TLV_TYPE_UNKNOWN:
8073a0329bSJakub Kicinski dev_err(dev, "NULL TLV at offset:%u\n", offset);
8173a0329bSJakub Kicinski return -EINVAL;
8273a0329bSJakub Kicinski case NFP_NET_CFG_TLV_TYPE_RESERVED:
8373a0329bSJakub Kicinski break;
8473a0329bSJakub Kicinski case NFP_NET_CFG_TLV_TYPE_END:
8573a0329bSJakub Kicinski if (!length)
8673a0329bSJakub Kicinski return 0;
8773a0329bSJakub Kicinski
889571d987SJakub Kicinski dev_err(dev, "END TLV should be empty, has offset:%u len:%d\n",
899571d987SJakub Kicinski offset, length);
9073a0329bSJakub Kicinski return -EINVAL;
91ce991ab6SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_ME_FREQ:
92ce991ab6SJakub Kicinski if (length != 4) {
93ce991ab6SJakub Kicinski dev_err(dev,
949571d987SJakub Kicinski "ME FREQ TLV should be 4B, is %dB offset:%u\n",
959571d987SJakub Kicinski length, offset);
96ce991ab6SJakub Kicinski return -EINVAL;
97ce991ab6SJakub Kicinski }
98ce991ab6SJakub Kicinski
99ce991ab6SJakub Kicinski caps->me_freq_mhz = readl(data);
100ce991ab6SJakub Kicinski break;
101527d7d1bSJakub Kicinski case NFP_NET_CFG_TLV_TYPE_MBOX:
102527d7d1bSJakub Kicinski if (!length) {
103527d7d1bSJakub Kicinski caps->mbox_off = 0;
104527d7d1bSJakub Kicinski caps->mbox_len = 0;
105527d7d1bSJakub Kicinski } else {
106527d7d1bSJakub Kicinski caps->mbox_off = data - ctrl_mem;
107527d7d1bSJakub Kicinski caps->mbox_len = length;
108527d7d1bSJakub Kicinski }
109527d7d1bSJakub Kicinski break;
11097ea8ac3SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_EXPERIMENTAL0:
11197ea8ac3SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_EXPERIMENTAL1:
11297ea8ac3SJakub Kicinski dev_warn(dev,
11397ea8ac3SJakub Kicinski "experimental TLV type:%u offset:%u len:%u\n",
11497ea8ac3SJakub Kicinski FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE, hdr),
11597ea8ac3SJakub Kicinski offset, length);
11697ea8ac3SJakub Kicinski break;
11751a6588eSJakub Kicinski case NFP_NET_CFG_TLV_TYPE_REPR_CAP:
11851a6588eSJakub Kicinski if (length < 4) {
1199571d987SJakub Kicinski dev_err(dev, "REPR CAP TLV short %dB < 4B offset:%u\n",
1209571d987SJakub Kicinski length, offset);
12151a6588eSJakub Kicinski return -EINVAL;
12251a6588eSJakub Kicinski }
12351a6588eSJakub Kicinski
12451a6588eSJakub Kicinski caps->repr_cap = readl(data);
12551a6588eSJakub Kicinski break;
126a6863489SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_MBOX_CMSG_TYPES:
127a6863489SJakub Kicinski if (length >= 4)
128a6863489SJakub Kicinski caps->mbox_cmsg_types = readl(data);
129a6863489SJakub Kicinski break;
130d9d2d4c5SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_CRYPTO_OPS:
131*6a35ddc5SJakub Kicinski if (!nfp_net_tls_parse_crypto_ops(dev, caps, ctrl_mem,
132*6a35ddc5SJakub Kicinski data, length, offset,
133*6a35ddc5SJakub Kicinski false))
134d9d2d4c5SJakub Kicinski return -EINVAL;
135d9d2d4c5SJakub Kicinski break;
136ca866ee8SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_VNIC_STATS:
137ca866ee8SJakub Kicinski if ((data - ctrl_mem) % 8) {
138ca866ee8SJakub Kicinski dev_warn(dev, "VNIC STATS TLV misaligned, ignoring offset:%u len:%u\n",
139ca866ee8SJakub Kicinski offset, length);
140ca866ee8SJakub Kicinski break;
141ca866ee8SJakub Kicinski }
142ca866ee8SJakub Kicinski caps->vnic_stats_off = data - ctrl_mem;
143ca866ee8SJakub Kicinski caps->vnic_stats_cnt = length / 10;
144ca866ee8SJakub Kicinski break;
145*6a35ddc5SJakub Kicinski case NFP_NET_CFG_TLV_TYPE_CRYPTO_OPS_RX_SCAN:
146*6a35ddc5SJakub Kicinski if (!nfp_net_tls_parse_crypto_ops(dev, caps, ctrl_mem,
147*6a35ddc5SJakub Kicinski data, length, offset,
148*6a35ddc5SJakub Kicinski true))
149*6a35ddc5SJakub Kicinski return -EINVAL;
150*6a35ddc5SJakub Kicinski break;
15173a0329bSJakub Kicinski default:
15273a0329bSJakub Kicinski if (!FIELD_GET(NFP_NET_CFG_TLV_HEADER_REQUIRED, hdr))
15373a0329bSJakub Kicinski break;
15473a0329bSJakub Kicinski
15573a0329bSJakub Kicinski dev_err(dev, "unknown TLV type:%u offset:%u len:%u\n",
15673a0329bSJakub Kicinski FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE, hdr),
15773a0329bSJakub Kicinski offset, length);
15873a0329bSJakub Kicinski return -EINVAL;
15973a0329bSJakub Kicinski }
16073a0329bSJakub Kicinski
16173a0329bSJakub Kicinski data += length;
16273a0329bSJakub Kicinski if (data + 4 > end) {
16373a0329bSJakub Kicinski dev_err(dev, "reached end of BAR without END TLV\n");
16473a0329bSJakub Kicinski return -EINVAL;
16573a0329bSJakub Kicinski }
16673a0329bSJakub Kicinski }
16773a0329bSJakub Kicinski
16873a0329bSJakub Kicinski /* Not reached */
16973a0329bSJakub Kicinski return -EINVAL;
17073a0329bSJakub Kicinski }
171