1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2017 Netronome Systems, Inc. */
4 /* Parse the hwinfo table that the ARM firmware builds in the ARM scratch SRAM
7 * Examples of the fields:
9 * me.mask = 0x7f_ffff_ffff
11 * me.count is the total number of MEs on the system.
12 * me.mask is the bitmask of MEs that are available for application usage.
14 * (ie, in this example, ME 39 has been reserved by boardconfig.)
17 #include <asm/byteorder.h>
18 #include <asm/unaligned.h>
19 #include <linux/delay.h>
20 #include <linux/log2.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
25 #define NFP_SUBSYS "nfp_hwinfo"
30 #include "nfp6000/nfp6000.h"
32 #define HWINFO_SIZE_MIN 0x100
33 #define HWINFO_WAIT 20 /* seconds */
35 /* The Hardware Info Table defines the properties of the system.
37 * HWInfo v1 Table (fixed size)
39 * 0x0000: u32 version Hardware Info Table version (1.0)
40 * 0x0004: u32 size Total size of the table, including
41 * the CRC32 (IEEE 802.3)
42 * 0x0008: u32 jumptab Offset of key/value table
43 * 0x000c: u32 keys Total number of keys in the key/value table
44 * NNNNNN: Key/value jump table and string data
45 * (size - 4): u32 crc32 CRC32 (same as IEEE 802.3, POSIX csum, etc)
46 * CRC32("",0) = ~0, CRC32("a",1) = 0x48C279FE
48 * HWInfo v2 Table (variable size)
50 * 0x0000: u32 version Hardware Info Table version (2.0)
51 * 0x0004: u32 size Current size of the data area, excluding CRC32
52 * 0x0008: u32 limit Maximum size of the table
53 * 0x000c: u32 reserved Unused, set to zero
54 * NNNNNN: Key/value data
55 * (size - 4): u32 crc32 CRC32 (same as IEEE 802.3, POSIX csum, etc)
56 * CRC32("",0) = ~0, CRC32("a",1) = 0x48C279FE
58 * If the HWInfo table is in the process of being updated, the low bit
59 * of version will be set.
61 * HWInfo v1 Key/Value Table
62 * -------------------------
64 * The key/value table is a set of offsets to ASCIIZ strings which have
65 * been strcmp(3) sorted (yes, please use bsearch(3) on the table).
67 * All keys are guaranteed to be unique.
69 * N+0: u32 key_1 Offset to the first key
70 * N+4: u32 val_1 Offset to the first value
71 * N+8: u32 key_2 Offset to the second key
72 * N+c: u32 val_2 Offset to the second value
75 * HWInfo v2 Key/Value Table
76 * -------------------------
78 * Packed UTF8Z strings, ie 'key1\000value1\000key2\000value2\000'
83 #define NFP_HWINFO_VERSION_1 ('H' << 24 | 'I' << 16 | 1 << 8 | 0 << 1 | 0)
84 #define NFP_HWINFO_VERSION_2 ('H' << 24 | 'I' << 16 | 2 << 8 | 0 << 1 | 0)
85 #define NFP_HWINFO_VERSION_UPDATING BIT(0)
93 /* v2 specific fields */
100 static bool nfp_hwinfo_is_updating(struct nfp_hwinfo *hwinfo)
102 return le32_to_cpu(hwinfo->version) & NFP_HWINFO_VERSION_UPDATING;
106 hwinfo_db_walk(struct nfp_cpp *cpp, struct nfp_hwinfo *hwinfo, u32 size)
108 const char *key, *val, *end = hwinfo->data + size;
110 for (key = hwinfo->data; *key && key < end;
111 key = val + strlen(val) + 1) {
113 val = key + strlen(key) + 1;
115 nfp_warn(cpp, "Bad HWINFO - overflowing key\n");
119 if (val + strlen(val) + 1 > end) {
120 nfp_warn(cpp, "Bad HWINFO - overflowing value\n");
129 hwinfo_db_validate(struct nfp_cpp *cpp, struct nfp_hwinfo *db, u32 len)
133 size = le32_to_cpu(db->size);
135 nfp_err(cpp, "Unsupported hwinfo size %u > %u\n", size, len);
140 crc = crc32_posix(db, size);
141 if (crc != get_unaligned_le32(db->start + size)) {
142 nfp_err(cpp, "Corrupt hwinfo table (CRC mismatch), calculated 0x%x, expected 0x%x\n",
143 crc, get_unaligned_le32(db->start + size));
148 return hwinfo_db_walk(cpp, db, size);
151 static struct nfp_hwinfo *
152 hwinfo_try_fetch(struct nfp_cpp *cpp, size_t *cpp_size)
154 struct nfp_hwinfo *header;
155 struct nfp_resource *res;
161 res = nfp_resource_acquire(cpp, NFP_RESOURCE_NFP_HWINFO);
163 cpp_id = nfp_resource_cpp_id(res);
164 cpp_addr = nfp_resource_address(res);
165 *cpp_size = nfp_resource_size(res);
167 nfp_resource_release(res);
169 if (*cpp_size < HWINFO_SIZE_MIN)
171 } else if (PTR_ERR(res) == -ENOENT) {
172 /* Try getting the HWInfo table from the 'classic' location */
173 cpp_id = NFP_CPP_ISLAND_ID(NFP_CPP_TARGET_MU,
174 NFP_CPP_ACTION_RW, 0, 1);
181 db = kmalloc(*cpp_size + 1, GFP_KERNEL);
185 err = nfp_cpp_read(cpp, cpp_id, cpp_addr, db, *cpp_size);
186 if (err != *cpp_size)
190 if (nfp_hwinfo_is_updating(header))
193 if (le32_to_cpu(header->version) != NFP_HWINFO_VERSION_2) {
194 nfp_err(cpp, "Unknown HWInfo version: 0x%08x\n",
195 le32_to_cpu(header->version));
199 /* NULL-terminate for safety */
200 db[*cpp_size] = '\0';
208 static struct nfp_hwinfo *hwinfo_fetch(struct nfp_cpp *cpp, size_t *hwdb_size)
210 const unsigned long wait_until = jiffies + HWINFO_WAIT * HZ;
211 struct nfp_hwinfo *db;
215 const unsigned long start_time = jiffies;
217 db = hwinfo_try_fetch(cpp, hwdb_size);
221 err = msleep_interruptible(100);
222 if (err || time_after(start_time, wait_until)) {
223 nfp_err(cpp, "NFP access error\n");
229 struct nfp_hwinfo *nfp_hwinfo_read(struct nfp_cpp *cpp)
231 struct nfp_hwinfo *db;
232 size_t hwdb_size = 0;
235 db = hwinfo_fetch(cpp, &hwdb_size);
239 err = hwinfo_db_validate(cpp, db, hwdb_size);
249 * nfp_hwinfo_lookup() - Find a value in the HWInfo table by name
250 * @hwinfo: NFP HWinfo table
251 * @lookup: HWInfo name to search for
253 * Return: Value of the HWInfo name, or NULL
255 const char *nfp_hwinfo_lookup(struct nfp_hwinfo *hwinfo, const char *lookup)
257 const char *key, *val, *end;
259 if (!hwinfo || !lookup)
262 end = hwinfo->data + le32_to_cpu(hwinfo->size) - sizeof(u32);
264 for (key = hwinfo->data; *key && key < end;
265 key = val + strlen(val) + 1) {
267 val = key + strlen(key) + 1;
269 if (strcmp(key, lookup) == 0)
276 char *nfp_hwinfo_get_packed_strings(struct nfp_hwinfo *hwinfo)
281 u32 nfp_hwinfo_get_packed_str_size(struct nfp_hwinfo *hwinfo)
283 return le32_to_cpu(hwinfo->size) - sizeof(u32);