2 * Copyright (C) 2018 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/bitfield.h>
35 #include <linux/device.h>
36 #include <linux/kernel.h>
37 #include <linux/types.h>
39 #include "nfp_net_ctrl.h"
42 static void nfp_net_tlv_caps_reset(struct nfp_net_tlv_caps
*caps
)
44 memset(caps
, 0, sizeof(*caps
));
45 caps
->me_freq_mhz
= 1200;
46 caps
->mbox_off
= NFP_NET_CFG_MBOX_BASE
;
47 caps
->mbox_len
= NFP_NET_CFG_MBOX_VAL_MAX_SZ
;
50 int nfp_net_tlv_caps_parse(struct device
*dev
, u8 __iomem
*ctrl_mem
,
51 struct nfp_net_tlv_caps
*caps
)
53 u8 __iomem
*data
= ctrl_mem
+ NFP_NET_CFG_TLV_BASE
;
54 u8 __iomem
*end
= ctrl_mem
+ NFP_NET_CFG_BAR_SZ
;
57 nfp_net_tlv_caps_reset(caps
);
64 unsigned int length
, offset
;
65 u32 hdr
= readl(data
);
67 length
= FIELD_GET(NFP_NET_CFG_TLV_HEADER_LENGTH
, hdr
);
68 offset
= data
- ctrl_mem
;
70 /* Advance past the header */
73 if (length
% NFP_NET_CFG_TLV_LENGTH_INC
) {
74 dev_err(dev
, "TLV size not multiple of %u len:%u\n",
75 NFP_NET_CFG_TLV_LENGTH_INC
, length
);
78 if (data
+ length
> end
) {
79 dev_err(dev
, "oversized TLV offset:%u len:%u\n",
84 switch (FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE
, hdr
)) {
85 case NFP_NET_CFG_TLV_TYPE_UNKNOWN
:
86 dev_err(dev
, "NULL TLV at offset:%u\n", offset
);
88 case NFP_NET_CFG_TLV_TYPE_RESERVED
:
90 case NFP_NET_CFG_TLV_TYPE_END
:
94 dev_err(dev
, "END TLV should be empty, has len:%d\n",
97 case NFP_NET_CFG_TLV_TYPE_ME_FREQ
:
100 "ME FREQ TLV should be 4B, is %dB\n",
105 caps
->me_freq_mhz
= readl(data
);
107 case NFP_NET_CFG_TLV_TYPE_MBOX
:
112 caps
->mbox_off
= data
- ctrl_mem
;
113 caps
->mbox_len
= length
;
117 if (!FIELD_GET(NFP_NET_CFG_TLV_HEADER_REQUIRED
, hdr
))
120 dev_err(dev
, "unknown TLV type:%u offset:%u len:%u\n",
121 FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE
, hdr
),
127 if (data
+ 4 > end
) {
128 dev_err(dev
, "reached end of BAR without END TLV\n");