Linux-libre 4.19.116-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / netronome / nfp / nfpcore / nfp_hwinfo.c
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
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.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  */
33
34 /* Parse the hwinfo table that the ARM firmware builds in the ARM scratch SRAM
35  * after chip reset.
36  *
37  * Examples of the fields:
38  *   me.count = 40
39  *   me.mask = 0x7f_ffff_ffff
40  *
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.
43  *
44  *   (ie, in this example, ME 39 has been reserved by boardconfig.)
45  */
46
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>
54
55 #define NFP_SUBSYS "nfp_hwinfo"
56
57 #include "crc32.h"
58 #include "nfp.h"
59 #include "nfp_cpp.h"
60 #include "nfp6000/nfp6000.h"
61
62 #define HWINFO_SIZE_MIN 0x100
63 #define HWINFO_WAIT     20      /* seconds */
64
65 /* The Hardware Info Table defines the properties of the system.
66  *
67  * HWInfo v1 Table (fixed size)
68  *
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
77  *
78  * HWInfo v2 Table (variable size)
79  *
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
87  *
88  * If the HWInfo table is in the process of being updated, the low bit
89  * of version will be set.
90  *
91  * HWInfo v1 Key/Value Table
92  * -------------------------
93  *
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).
96  *
97  *  All keys are guaranteed to be unique.
98  *
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
103  * ...
104  *
105  * HWInfo v2 Key/Value Table
106  * -------------------------
107  *
108  * Packed UTF8Z strings, ie 'key1\000value1\000key2\000value2\000'
109  *
110  * Unsorted.
111  */
112
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)
116
117 struct nfp_hwinfo {
118         u8 start[0];
119
120         __le32 version;
121         __le32 size;
122
123         /* v2 specific fields */
124         __le32 limit;
125         __le32 resv;
126
127         char data[];
128 };
129
130 static bool nfp_hwinfo_is_updating(struct nfp_hwinfo *hwinfo)
131 {
132         return le32_to_cpu(hwinfo->version) & NFP_HWINFO_VERSION_UPDATING;
133 }
134
135 static int
136 hwinfo_db_walk(struct nfp_cpp *cpp, struct nfp_hwinfo *hwinfo, u32 size)
137 {
138         const char *key, *val, *end = hwinfo->data + size;
139
140         for (key = hwinfo->data; *key && key < end;
141              key = val + strlen(val) + 1) {
142
143                 val = key + strlen(key) + 1;
144                 if (val >= end) {
145                         nfp_warn(cpp, "Bad HWINFO - overflowing key\n");
146                         return -EINVAL;
147                 }
148
149                 if (val + strlen(val) + 1 > end) {
150                         nfp_warn(cpp, "Bad HWINFO - overflowing value\n");
151                         return -EINVAL;
152                 }
153         }
154
155         return 0;
156 }
157
158 static int
159 hwinfo_db_validate(struct nfp_cpp *cpp, struct nfp_hwinfo *db, u32 len)
160 {
161         u32 size, crc;
162
163         size = le32_to_cpu(db->size);
164         if (size > len) {
165                 nfp_err(cpp, "Unsupported hwinfo size %u > %u\n", size, len);
166                 return -EINVAL;
167         }
168
169         size -= sizeof(u32);
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));
174
175                 return -EINVAL;
176         }
177
178         return hwinfo_db_walk(cpp, db, size);
179 }
180
181 static struct nfp_hwinfo *
182 hwinfo_try_fetch(struct nfp_cpp *cpp, size_t *cpp_size)
183 {
184         struct nfp_hwinfo *header;
185         struct nfp_resource *res;
186         u64 cpp_addr;
187         u32 cpp_id;
188         int err;
189         u8 *db;
190
191         res = nfp_resource_acquire(cpp, NFP_RESOURCE_NFP_HWINFO);
192         if (!IS_ERR(res)) {
193                 cpp_id = nfp_resource_cpp_id(res);
194                 cpp_addr = nfp_resource_address(res);
195                 *cpp_size = nfp_resource_size(res);
196
197                 nfp_resource_release(res);
198
199                 if (*cpp_size < HWINFO_SIZE_MIN)
200                         return NULL;
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);
205                 cpp_addr = 0x30000;
206                 *cpp_size = 0x0e000;
207         } else {
208                 return NULL;
209         }
210
211         db = kmalloc(*cpp_size + 1, GFP_KERNEL);
212         if (!db)
213                 return NULL;
214
215         err = nfp_cpp_read(cpp, cpp_id, cpp_addr, db, *cpp_size);
216         if (err != *cpp_size)
217                 goto exit_free;
218
219         header = (void *)db;
220         if (nfp_hwinfo_is_updating(header))
221                 goto exit_free;
222
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));
226                 goto exit_free;
227         }
228
229         /* NULL-terminate for safety */
230         db[*cpp_size] = '\0';
231
232         return (void *)db;
233 exit_free:
234         kfree(db);
235         return NULL;
236 }
237
238 static struct nfp_hwinfo *hwinfo_fetch(struct nfp_cpp *cpp, size_t *hwdb_size)
239 {
240         const unsigned long wait_until = jiffies + HWINFO_WAIT * HZ;
241         struct nfp_hwinfo *db;
242         int err;
243
244         for (;;) {
245                 const unsigned long start_time = jiffies;
246
247                 db = hwinfo_try_fetch(cpp, hwdb_size);
248                 if (db)
249                         return db;
250
251                 err = msleep_interruptible(100);
252                 if (err || time_after(start_time, wait_until)) {
253                         nfp_err(cpp, "NFP access error\n");
254                         return NULL;
255                 }
256         }
257 }
258
259 struct nfp_hwinfo *nfp_hwinfo_read(struct nfp_cpp *cpp)
260 {
261         struct nfp_hwinfo *db;
262         size_t hwdb_size = 0;
263         int err;
264
265         db = hwinfo_fetch(cpp, &hwdb_size);
266         if (!db)
267                 return NULL;
268
269         err = hwinfo_db_validate(cpp, db, hwdb_size);
270         if (err) {
271                 kfree(db);
272                 return NULL;
273         }
274
275         return db;
276 }
277
278 /**
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
282  *
283  * Return: Value of the HWInfo name, or NULL
284  */
285 const char *nfp_hwinfo_lookup(struct nfp_hwinfo *hwinfo, const char *lookup)
286 {
287         const char *key, *val, *end;
288
289         if (!hwinfo || !lookup)
290                 return NULL;
291
292         end = hwinfo->data + le32_to_cpu(hwinfo->size) - sizeof(u32);
293
294         for (key = hwinfo->data; *key && key < end;
295              key = val + strlen(val) + 1) {
296
297                 val = key + strlen(key) + 1;
298
299                 if (strcmp(key, lookup) == 0)
300                         return val;
301         }
302
303         return NULL;
304 }
305
306 char *nfp_hwinfo_get_packed_strings(struct nfp_hwinfo *hwinfo)
307 {
308         return hwinfo->data;
309 }
310
311 u32 nfp_hwinfo_get_packed_str_size(struct nfp_hwinfo *hwinfo)
312 {
313         return le32_to_cpu(hwinfo->size) - sizeof(u32);
314 }