2 * Copyright (C) 2015-2017 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 /* Parse the hwinfo table that the ARM firmware builds in the ARM scratch SRAM
37 * Examples of the fields:
39 * me.mask = 0x7f_ffff_ffff
41 * me.count is the total number of MEs on the system.
42 * me.mask is the bitmask of MEs that are available for application usage.
44 * (ie, in this example, ME 39 has been reserved by boardconfig.)
47 #include <asm/byteorder.h>
48 #include <asm/unaligned.h>
49 #include <linux/delay.h>
50 #include <linux/log2.h>
51 #include <linux/kernel.h>
52 #include <linux/module.h>
53 #include <linux/slab.h>
55 #define NFP_SUBSYS "nfp_hwinfo"
60 #include "nfp6000/nfp6000.h"
62 #define HWINFO_SIZE_MIN 0x100
63 #define HWINFO_WAIT 20 /* seconds */
65 /* The Hardware Info Table defines the properties of the system.
67 * HWInfo v1 Table (fixed size)
69 * 0x0000: u32 version Hardware Info Table version (1.0)
70 * 0x0004: u32 size Total size of the table, including
71 * the CRC32 (IEEE 802.3)
72 * 0x0008: u32 jumptab Offset of key/value table
73 * 0x000c: u32 keys Total number of keys in the key/value table
74 * NNNNNN: Key/value jump table and string data
75 * (size - 4): u32 crc32 CRC32 (same as IEEE 802.3, POSIX csum, etc)
76 * CRC32("",0) = ~0, CRC32("a",1) = 0x48C279FE
78 * HWInfo v2 Table (variable size)
80 * 0x0000: u32 version Hardware Info Table version (2.0)
81 * 0x0004: u32 size Current size of the data area, excluding CRC32
82 * 0x0008: u32 limit Maximum size of the table
83 * 0x000c: u32 reserved Unused, set to zero
84 * NNNNNN: Key/value data
85 * (size - 4): u32 crc32 CRC32 (same as IEEE 802.3, POSIX csum, etc)
86 * CRC32("",0) = ~0, CRC32("a",1) = 0x48C279FE
88 * If the HWInfo table is in the process of being updated, the low bit
89 * of version will be set.
91 * HWInfo v1 Key/Value Table
92 * -------------------------
94 * The key/value table is a set of offsets to ASCIIZ strings which have
95 * been strcmp(3) sorted (yes, please use bsearch(3) on the table).
97 * All keys are guaranteed to be unique.
99 * N+0: u32 key_1 Offset to the first key
100 * N+4: u32 val_1 Offset to the first value
101 * N+8: u32 key_2 Offset to the second key
102 * N+c: u32 val_2 Offset to the second value
105 * HWInfo v2 Key/Value Table
106 * -------------------------
108 * Packed UTF8Z strings, ie 'key1\000value1\000key2\000value2\000'
113 #define NFP_HWINFO_VERSION_1 ('H' << 24 | 'I' << 16 | 1 << 8 | 0 << 1 | 0)
114 #define NFP_HWINFO_VERSION_2 ('H' << 24 | 'I' << 16 | 2 << 8 | 0 << 1 | 0)
115 #define NFP_HWINFO_VERSION_UPDATING BIT(0)
123 /* v2 specific fields */
130 static bool nfp_hwinfo_is_updating(struct nfp_hwinfo *hwinfo)
132 return le32_to_cpu(hwinfo->version) & NFP_HWINFO_VERSION_UPDATING;
136 hwinfo_db_walk(struct nfp_cpp *cpp, struct nfp_hwinfo *hwinfo, u32 size)
138 const char *key, *val, *end = hwinfo->data + size;
140 for (key = hwinfo->data; *key && key < end;
141 key = val + strlen(val) + 1) {
143 val = key + strlen(key) + 1;
145 nfp_warn(cpp, "Bad HWINFO - overflowing key\n");
149 if (val + strlen(val) + 1 > end) {
150 nfp_warn(cpp, "Bad HWINFO - overflowing value\n");
159 hwinfo_db_validate(struct nfp_cpp *cpp, struct nfp_hwinfo *db, u32 len)
163 size = le32_to_cpu(db->size);
165 nfp_err(cpp, "Unsupported hwinfo size %u > %u\n", size, len);
170 crc = crc32_posix(db, size);
171 if (crc != get_unaligned_le32(db->start + size)) {
172 nfp_err(cpp, "Corrupt hwinfo table (CRC mismatch), calculated 0x%x, expected 0x%x\n",
173 crc, get_unaligned_le32(db->start + size));
178 return hwinfo_db_walk(cpp, db, size);
181 static struct nfp_hwinfo *
182 hwinfo_try_fetch(struct nfp_cpp *cpp, size_t *cpp_size)
184 struct nfp_hwinfo *header;
185 struct nfp_resource *res;
191 res = nfp_resource_acquire(cpp, NFP_RESOURCE_NFP_HWINFO);
193 cpp_id = nfp_resource_cpp_id(res);
194 cpp_addr = nfp_resource_address(res);
195 *cpp_size = nfp_resource_size(res);
197 nfp_resource_release(res);
199 if (*cpp_size < HWINFO_SIZE_MIN)
201 } else if (PTR_ERR(res) == -ENOENT) {
202 /* Try getting the HWInfo table from the 'classic' location */
203 cpp_id = NFP_CPP_ISLAND_ID(NFP_CPP_TARGET_MU,
204 NFP_CPP_ACTION_RW, 0, 1);
211 db = kmalloc(*cpp_size + 1, GFP_KERNEL);
215 err = nfp_cpp_read(cpp, cpp_id, cpp_addr, db, *cpp_size);
216 if (err != *cpp_size)
220 if (nfp_hwinfo_is_updating(header))
223 if (le32_to_cpu(header->version) != NFP_HWINFO_VERSION_2) {
224 nfp_err(cpp, "Unknown HWInfo version: 0x%08x\n",
225 le32_to_cpu(header->version));
229 /* NULL-terminate for safety */
230 db[*cpp_size] = '\0';
238 static struct nfp_hwinfo *hwinfo_fetch(struct nfp_cpp *cpp, size_t *hwdb_size)
240 const unsigned long wait_until = jiffies + HWINFO_WAIT * HZ;
241 struct nfp_hwinfo *db;
245 const unsigned long start_time = jiffies;
247 db = hwinfo_try_fetch(cpp, hwdb_size);
251 err = msleep_interruptible(100);
252 if (err || time_after(start_time, wait_until)) {
253 nfp_err(cpp, "NFP access error\n");
259 struct nfp_hwinfo *nfp_hwinfo_read(struct nfp_cpp *cpp)
261 struct nfp_hwinfo *db;
262 size_t hwdb_size = 0;
265 db = hwinfo_fetch(cpp, &hwdb_size);
269 err = hwinfo_db_validate(cpp, db, hwdb_size);
279 * nfp_hwinfo_lookup() - Find a value in the HWInfo table by name
280 * @hwinfo: NFP HWinfo table
281 * @lookup: HWInfo name to search for
283 * Return: Value of the HWInfo name, or NULL
285 const char *nfp_hwinfo_lookup(struct nfp_hwinfo *hwinfo, const char *lookup)
287 const char *key, *val, *end;
289 if (!hwinfo || !lookup)
292 end = hwinfo->data + le32_to_cpu(hwinfo->size) - sizeof(u32);
294 for (key = hwinfo->data; *key && key < end;
295 key = val + strlen(val) + 1) {
297 val = key + strlen(key) + 1;
299 if (strcmp(key, lookup) == 0)
306 char *nfp_hwinfo_get_packed_strings(struct nfp_hwinfo *hwinfo)
311 u32 nfp_hwinfo_get_packed_str_size(struct nfp_hwinfo *hwinfo)
313 return le32_to_cpu(hwinfo->size) - sizeof(u32);