Linux 5.1.15
[linux/fpc-iii.git] / drivers / thunderbolt / property.c
blob8c077c4f3b5b2be34d8389b0884eb5b46206c82a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Thunderbolt XDomain property support
5 * Copyright (C) 2017, Intel Corporation
6 * Authors: Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 */
10 #include <linux/err.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/uuid.h>
14 #include <linux/thunderbolt.h>
16 struct tb_property_entry {
17 u32 key_hi;
18 u32 key_lo;
19 u16 length;
20 u8 reserved;
21 u8 type;
22 u32 value;
25 struct tb_property_rootdir_entry {
26 u32 magic;
27 u32 length;
28 struct tb_property_entry entries[];
31 struct tb_property_dir_entry {
32 u32 uuid[4];
33 struct tb_property_entry entries[];
36 #define TB_PROPERTY_ROOTDIR_MAGIC 0x55584401
38 static struct tb_property_dir *__tb_property_parse_dir(const u32 *block,
39 size_t block_len, unsigned int dir_offset, size_t dir_len,
40 bool is_root);
42 static inline void parse_dwdata(void *dst, const void *src, size_t dwords)
44 be32_to_cpu_array(dst, src, dwords);
47 static inline void format_dwdata(void *dst, const void *src, size_t dwords)
49 cpu_to_be32_array(dst, src, dwords);
52 static bool tb_property_entry_valid(const struct tb_property_entry *entry,
53 size_t block_len)
55 switch (entry->type) {
56 case TB_PROPERTY_TYPE_DIRECTORY:
57 case TB_PROPERTY_TYPE_DATA:
58 case TB_PROPERTY_TYPE_TEXT:
59 if (entry->length > block_len)
60 return false;
61 if (entry->value + entry->length > block_len)
62 return false;
63 break;
65 case TB_PROPERTY_TYPE_VALUE:
66 if (entry->length != 1)
67 return false;
68 break;
71 return true;
74 static bool tb_property_key_valid(const char *key)
76 return key && strlen(key) <= TB_PROPERTY_KEY_SIZE;
79 static struct tb_property *
80 tb_property_alloc(const char *key, enum tb_property_type type)
82 struct tb_property *property;
84 property = kzalloc(sizeof(*property), GFP_KERNEL);
85 if (!property)
86 return NULL;
88 strcpy(property->key, key);
89 property->type = type;
90 INIT_LIST_HEAD(&property->list);
92 return property;
95 static struct tb_property *tb_property_parse(const u32 *block, size_t block_len,
96 const struct tb_property_entry *entry)
98 char key[TB_PROPERTY_KEY_SIZE + 1];
99 struct tb_property *property;
100 struct tb_property_dir *dir;
102 if (!tb_property_entry_valid(entry, block_len))
103 return NULL;
105 parse_dwdata(key, entry, 2);
106 key[TB_PROPERTY_KEY_SIZE] = '\0';
108 property = tb_property_alloc(key, entry->type);
109 if (!property)
110 return NULL;
112 property->length = entry->length;
114 switch (property->type) {
115 case TB_PROPERTY_TYPE_DIRECTORY:
116 dir = __tb_property_parse_dir(block, block_len, entry->value,
117 entry->length, false);
118 if (!dir) {
119 kfree(property);
120 return NULL;
122 property->value.dir = dir;
123 break;
125 case TB_PROPERTY_TYPE_DATA:
126 property->value.data = kcalloc(property->length, sizeof(u32),
127 GFP_KERNEL);
128 if (!property->value.data) {
129 kfree(property);
130 return NULL;
132 parse_dwdata(property->value.data, block + entry->value,
133 entry->length);
134 break;
136 case TB_PROPERTY_TYPE_TEXT:
137 property->value.text = kcalloc(property->length, sizeof(u32),
138 GFP_KERNEL);
139 if (!property->value.text) {
140 kfree(property);
141 return NULL;
143 parse_dwdata(property->value.text, block + entry->value,
144 entry->length);
145 /* Force null termination */
146 property->value.text[property->length * 4 - 1] = '\0';
147 break;
149 case TB_PROPERTY_TYPE_VALUE:
150 property->value.immediate = entry->value;
151 break;
153 default:
154 property->type = TB_PROPERTY_TYPE_UNKNOWN;
155 break;
158 return property;
161 static struct tb_property_dir *__tb_property_parse_dir(const u32 *block,
162 size_t block_len, unsigned int dir_offset, size_t dir_len, bool is_root)
164 const struct tb_property_entry *entries;
165 size_t i, content_len, nentries;
166 unsigned int content_offset;
167 struct tb_property_dir *dir;
169 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
170 if (!dir)
171 return NULL;
173 if (is_root) {
174 content_offset = dir_offset + 2;
175 content_len = dir_len;
176 } else {
177 dir->uuid = kmemdup(&block[dir_offset], sizeof(*dir->uuid),
178 GFP_KERNEL);
179 content_offset = dir_offset + 4;
180 content_len = dir_len - 4; /* Length includes UUID */
183 entries = (const struct tb_property_entry *)&block[content_offset];
184 nentries = content_len / (sizeof(*entries) / 4);
186 INIT_LIST_HEAD(&dir->properties);
188 for (i = 0; i < nentries; i++) {
189 struct tb_property *property;
191 property = tb_property_parse(block, block_len, &entries[i]);
192 if (!property) {
193 tb_property_free_dir(dir);
194 return NULL;
197 list_add_tail(&property->list, &dir->properties);
200 return dir;
204 * tb_property_parse_dir() - Parses properties from given property block
205 * @block: Property block to parse
206 * @block_len: Number of dword elements in the property block
208 * This function parses the XDomain properties data block into format that
209 * can be traversed using the helper functions provided by this module.
210 * Upon success returns the parsed directory. In case of error returns
211 * %NULL. The resulting &struct tb_property_dir needs to be released by
212 * calling tb_property_free_dir() when not needed anymore.
214 * The @block is expected to be root directory.
216 struct tb_property_dir *tb_property_parse_dir(const u32 *block,
217 size_t block_len)
219 const struct tb_property_rootdir_entry *rootdir =
220 (const struct tb_property_rootdir_entry *)block;
222 if (rootdir->magic != TB_PROPERTY_ROOTDIR_MAGIC)
223 return NULL;
224 if (rootdir->length > block_len)
225 return NULL;
227 return __tb_property_parse_dir(block, block_len, 0, rootdir->length,
228 true);
232 * tb_property_create_dir() - Creates new property directory
233 * @uuid: UUID used to identify the particular directory
235 * Creates new, empty property directory. If @uuid is %NULL then the
236 * directory is assumed to be root directory.
238 struct tb_property_dir *tb_property_create_dir(const uuid_t *uuid)
240 struct tb_property_dir *dir;
242 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
243 if (!dir)
244 return NULL;
246 INIT_LIST_HEAD(&dir->properties);
247 if (uuid) {
248 dir->uuid = kmemdup(uuid, sizeof(*dir->uuid), GFP_KERNEL);
249 if (!dir->uuid) {
250 kfree(dir);
251 return NULL;
255 return dir;
257 EXPORT_SYMBOL_GPL(tb_property_create_dir);
259 static void tb_property_free(struct tb_property *property)
261 switch (property->type) {
262 case TB_PROPERTY_TYPE_DIRECTORY:
263 tb_property_free_dir(property->value.dir);
264 break;
266 case TB_PROPERTY_TYPE_DATA:
267 kfree(property->value.data);
268 break;
270 case TB_PROPERTY_TYPE_TEXT:
271 kfree(property->value.text);
272 break;
274 default:
275 break;
278 kfree(property);
282 * tb_property_free_dir() - Release memory allocated for property directory
283 * @dir: Directory to release
285 * This will release all the memory the directory occupies including all
286 * descendants. It is OK to pass %NULL @dir, then the function does
287 * nothing.
289 void tb_property_free_dir(struct tb_property_dir *dir)
291 struct tb_property *property, *tmp;
293 if (!dir)
294 return;
296 list_for_each_entry_safe(property, tmp, &dir->properties, list) {
297 list_del(&property->list);
298 tb_property_free(property);
300 kfree(dir->uuid);
301 kfree(dir);
303 EXPORT_SYMBOL_GPL(tb_property_free_dir);
305 static size_t tb_property_dir_length(const struct tb_property_dir *dir,
306 bool recurse, size_t *data_len)
308 const struct tb_property *property;
309 size_t len = 0;
311 if (dir->uuid)
312 len += sizeof(*dir->uuid) / 4;
313 else
314 len += sizeof(struct tb_property_rootdir_entry) / 4;
316 list_for_each_entry(property, &dir->properties, list) {
317 len += sizeof(struct tb_property_entry) / 4;
319 switch (property->type) {
320 case TB_PROPERTY_TYPE_DIRECTORY:
321 if (recurse) {
322 len += tb_property_dir_length(
323 property->value.dir, recurse, data_len);
325 /* Reserve dword padding after each directory */
326 if (data_len)
327 *data_len += 1;
328 break;
330 case TB_PROPERTY_TYPE_DATA:
331 case TB_PROPERTY_TYPE_TEXT:
332 if (data_len)
333 *data_len += property->length;
334 break;
336 default:
337 break;
341 return len;
344 static ssize_t __tb_property_format_dir(const struct tb_property_dir *dir,
345 u32 *block, unsigned int start_offset, size_t block_len)
347 unsigned int data_offset, dir_end;
348 const struct tb_property *property;
349 struct tb_property_entry *entry;
350 size_t dir_len, data_len = 0;
351 int ret;
354 * The structure of property block looks like following. Leaf
355 * data/text is included right after the directory and each
356 * directory follows each other (even nested ones).
358 * +----------+ <-- start_offset
359 * | header | <-- root directory header
360 * +----------+ ---
361 * | entry 0 | -^--------------------.
362 * +----------+ | |
363 * | entry 1 | -|--------------------|--.
364 * +----------+ | | |
365 * | entry 2 | -|-----------------. | |
366 * +----------+ | | | |
367 * : : | dir_len | | |
368 * . . | | | |
369 * : : | | | |
370 * +----------+ | | | |
371 * | entry n | v | | |
372 * +----------+ <-- data_offset | | |
373 * | data 0 | <------------------|--' |
374 * +----------+ | |
375 * | data 1 | <------------------|-----'
376 * +----------+ |
377 * | 00000000 | padding |
378 * +----------+ <-- dir_end <------'
379 * | UUID | <-- directory UUID (child directory)
380 * +----------+
381 * | entry 0 |
382 * +----------+
383 * | entry 1 |
384 * +----------+
385 * : :
386 * . .
387 * : :
388 * +----------+
389 * | entry n |
390 * +----------+
391 * | data 0 |
392 * +----------+
394 * We use dir_end to hold pointer to the end of the directory. It
395 * will increase as we add directories and each directory should be
396 * added starting from previous dir_end.
398 dir_len = tb_property_dir_length(dir, false, &data_len);
399 data_offset = start_offset + dir_len;
400 dir_end = start_offset + data_len + dir_len;
402 if (data_offset > dir_end)
403 return -EINVAL;
404 if (dir_end > block_len)
405 return -EINVAL;
407 /* Write headers first */
408 if (dir->uuid) {
409 struct tb_property_dir_entry *pe;
411 pe = (struct tb_property_dir_entry *)&block[start_offset];
412 memcpy(pe->uuid, dir->uuid, sizeof(pe->uuid));
413 entry = pe->entries;
414 } else {
415 struct tb_property_rootdir_entry *re;
417 re = (struct tb_property_rootdir_entry *)&block[start_offset];
418 re->magic = TB_PROPERTY_ROOTDIR_MAGIC;
419 re->length = dir_len - sizeof(*re) / 4;
420 entry = re->entries;
423 list_for_each_entry(property, &dir->properties, list) {
424 const struct tb_property_dir *child;
426 format_dwdata(entry, property->key, 2);
427 entry->type = property->type;
429 switch (property->type) {
430 case TB_PROPERTY_TYPE_DIRECTORY:
431 child = property->value.dir;
432 ret = __tb_property_format_dir(child, block, dir_end,
433 block_len);
434 if (ret < 0)
435 return ret;
436 entry->length = tb_property_dir_length(child, false,
437 NULL);
438 entry->value = dir_end;
439 dir_end = ret;
440 break;
442 case TB_PROPERTY_TYPE_DATA:
443 format_dwdata(&block[data_offset], property->value.data,
444 property->length);
445 entry->length = property->length;
446 entry->value = data_offset;
447 data_offset += entry->length;
448 break;
450 case TB_PROPERTY_TYPE_TEXT:
451 format_dwdata(&block[data_offset], property->value.text,
452 property->length);
453 entry->length = property->length;
454 entry->value = data_offset;
455 data_offset += entry->length;
456 break;
458 case TB_PROPERTY_TYPE_VALUE:
459 entry->length = property->length;
460 entry->value = property->value.immediate;
461 break;
463 default:
464 break;
467 entry++;
470 return dir_end;
474 * tb_property_format_dir() - Formats directory to the packed XDomain format
475 * @dir: Directory to format
476 * @block: Property block where the packed data is placed
477 * @block_len: Length of the property block
479 * This function formats the directory to the packed format that can be
480 * then send over the thunderbolt fabric to receiving host. Returns %0 in
481 * case of success and negative errno on faulure. Passing %NULL in @block
482 * returns number of entries the block takes.
484 ssize_t tb_property_format_dir(const struct tb_property_dir *dir, u32 *block,
485 size_t block_len)
487 ssize_t ret;
489 if (!block) {
490 size_t dir_len, data_len = 0;
492 dir_len = tb_property_dir_length(dir, true, &data_len);
493 return dir_len + data_len;
496 ret = __tb_property_format_dir(dir, block, 0, block_len);
497 return ret < 0 ? ret : 0;
501 * tb_property_add_immediate() - Add immediate property to directory
502 * @parent: Directory to add the property
503 * @key: Key for the property
504 * @value: Immediate value to store with the property
506 int tb_property_add_immediate(struct tb_property_dir *parent, const char *key,
507 u32 value)
509 struct tb_property *property;
511 if (!tb_property_key_valid(key))
512 return -EINVAL;
514 property = tb_property_alloc(key, TB_PROPERTY_TYPE_VALUE);
515 if (!property)
516 return -ENOMEM;
518 property->length = 1;
519 property->value.immediate = value;
521 list_add_tail(&property->list, &parent->properties);
522 return 0;
524 EXPORT_SYMBOL_GPL(tb_property_add_immediate);
527 * tb_property_add_data() - Adds arbitrary data property to directory
528 * @parent: Directory to add the property
529 * @key: Key for the property
530 * @buf: Data buffer to add
531 * @buflen: Number of bytes in the data buffer
533 * Function takes a copy of @buf and adds it to the directory.
535 int tb_property_add_data(struct tb_property_dir *parent, const char *key,
536 const void *buf, size_t buflen)
538 /* Need to pad to dword boundary */
539 size_t size = round_up(buflen, 4);
540 struct tb_property *property;
542 if (!tb_property_key_valid(key))
543 return -EINVAL;
545 property = tb_property_alloc(key, TB_PROPERTY_TYPE_DATA);
546 if (!property)
547 return -ENOMEM;
549 property->length = size / 4;
550 property->value.data = kzalloc(size, GFP_KERNEL);
551 if (!property->value.data) {
552 kfree(property);
553 return -ENOMEM;
556 memcpy(property->value.data, buf, buflen);
558 list_add_tail(&property->list, &parent->properties);
559 return 0;
561 EXPORT_SYMBOL_GPL(tb_property_add_data);
564 * tb_property_add_text() - Adds string property to directory
565 * @parent: Directory to add the property
566 * @key: Key for the property
567 * @text: String to add
569 * Function takes a copy of @text and adds it to the directory.
571 int tb_property_add_text(struct tb_property_dir *parent, const char *key,
572 const char *text)
574 /* Need to pad to dword boundary */
575 size_t size = round_up(strlen(text) + 1, 4);
576 struct tb_property *property;
578 if (!tb_property_key_valid(key))
579 return -EINVAL;
581 property = tb_property_alloc(key, TB_PROPERTY_TYPE_TEXT);
582 if (!property)
583 return -ENOMEM;
585 property->length = size / 4;
586 property->value.text = kzalloc(size, GFP_KERNEL);
587 if (!property->value.text) {
588 kfree(property);
589 return -ENOMEM;
592 strcpy(property->value.text, text);
594 list_add_tail(&property->list, &parent->properties);
595 return 0;
597 EXPORT_SYMBOL_GPL(tb_property_add_text);
600 * tb_property_add_dir() - Adds a directory to the parent directory
601 * @parent: Directory to add the property
602 * @key: Key for the property
603 * @dir: Directory to add
605 int tb_property_add_dir(struct tb_property_dir *parent, const char *key,
606 struct tb_property_dir *dir)
608 struct tb_property *property;
610 if (!tb_property_key_valid(key))
611 return -EINVAL;
613 property = tb_property_alloc(key, TB_PROPERTY_TYPE_DIRECTORY);
614 if (!property)
615 return -ENOMEM;
617 property->value.dir = dir;
619 list_add_tail(&property->list, &parent->properties);
620 return 0;
622 EXPORT_SYMBOL_GPL(tb_property_add_dir);
625 * tb_property_remove() - Removes property from a parent directory
626 * @property: Property to remove
628 * Note memory for @property is released as well so it is not allowed to
629 * touch the object after call to this function.
631 void tb_property_remove(struct tb_property *property)
633 list_del(&property->list);
634 kfree(property);
636 EXPORT_SYMBOL_GPL(tb_property_remove);
639 * tb_property_find() - Find a property from a directory
640 * @dir: Directory where the property is searched
641 * @key: Key to look for
642 * @type: Type of the property
644 * Finds and returns property from the given directory. Does not recurse
645 * into sub-directories. Returns %NULL if the property was not found.
647 struct tb_property *tb_property_find(struct tb_property_dir *dir,
648 const char *key, enum tb_property_type type)
650 struct tb_property *property;
652 list_for_each_entry(property, &dir->properties, list) {
653 if (property->type == type && !strcmp(property->key, key))
654 return property;
657 return NULL;
659 EXPORT_SYMBOL_GPL(tb_property_find);
662 * tb_property_get_next() - Get next property from directory
663 * @dir: Directory holding properties
664 * @prev: Previous property in the directory (%NULL returns the first)
666 struct tb_property *tb_property_get_next(struct tb_property_dir *dir,
667 struct tb_property *prev)
669 if (prev) {
670 if (list_is_last(&prev->list, &dir->properties))
671 return NULL;
672 return list_next_entry(prev, list);
674 return list_first_entry_or_null(&dir->properties, struct tb_property,
675 list);
677 EXPORT_SYMBOL_GPL(tb_property_get_next);