1 // SPDX-License-Identifier: GPL-2.0
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>
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
{
25 struct tb_property_rootdir_entry
{
28 struct tb_property_entry entries
[];
31 struct tb_property_dir_entry
{
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
,
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
,
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
)
61 if (entry
->value
+ entry
->length
> block_len
)
65 case TB_PROPERTY_TYPE_VALUE
:
66 if (entry
->length
!= 1)
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
);
88 strcpy(property
->key
, key
);
89 property
->type
= type
;
90 INIT_LIST_HEAD(&property
->list
);
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
))
105 parse_dwdata(key
, entry
, 2);
106 key
[TB_PROPERTY_KEY_SIZE
] = '\0';
108 property
= tb_property_alloc(key
, entry
->type
);
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);
122 property
->value
.dir
= dir
;
125 case TB_PROPERTY_TYPE_DATA
:
126 property
->value
.data
= kcalloc(property
->length
, sizeof(u32
),
128 if (!property
->value
.data
) {
132 parse_dwdata(property
->value
.data
, block
+ entry
->value
,
136 case TB_PROPERTY_TYPE_TEXT
:
137 property
->value
.text
= kcalloc(property
->length
, sizeof(u32
),
139 if (!property
->value
.text
) {
143 parse_dwdata(property
->value
.text
, block
+ entry
->value
,
145 /* Force null termination */
146 property
->value
.text
[property
->length
* 4 - 1] = '\0';
149 case TB_PROPERTY_TYPE_VALUE
:
150 property
->value
.immediate
= entry
->value
;
154 property
->type
= TB_PROPERTY_TYPE_UNKNOWN
;
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
);
174 content_offset
= dir_offset
+ 2;
175 content_len
= dir_len
;
177 dir
->uuid
= kmemdup(&block
[dir_offset
], sizeof(*dir
->uuid
),
180 tb_property_free_dir(dir
);
183 content_offset
= dir_offset
+ 4;
184 content_len
= dir_len
- 4; /* Length includes UUID */
187 entries
= (const struct tb_property_entry
*)&block
[content_offset
];
188 nentries
= content_len
/ (sizeof(*entries
) / 4);
190 INIT_LIST_HEAD(&dir
->properties
);
192 for (i
= 0; i
< nentries
; i
++) {
193 struct tb_property
*property
;
195 property
= tb_property_parse(block
, block_len
, &entries
[i
]);
197 tb_property_free_dir(dir
);
201 list_add_tail(&property
->list
, &dir
->properties
);
208 * tb_property_parse_dir() - Parses properties from given property block
209 * @block: Property block to parse
210 * @block_len: Number of dword elements in the property block
212 * This function parses the XDomain properties data block into format that
213 * can be traversed using the helper functions provided by this module.
214 * Upon success returns the parsed directory. In case of error returns
215 * %NULL. The resulting &struct tb_property_dir needs to be released by
216 * calling tb_property_free_dir() when not needed anymore.
218 * The @block is expected to be root directory.
220 struct tb_property_dir
*tb_property_parse_dir(const u32
*block
,
223 const struct tb_property_rootdir_entry
*rootdir
=
224 (const struct tb_property_rootdir_entry
*)block
;
226 if (rootdir
->magic
!= TB_PROPERTY_ROOTDIR_MAGIC
)
228 if (rootdir
->length
> block_len
)
231 return __tb_property_parse_dir(block
, block_len
, 0, rootdir
->length
,
236 * tb_property_create_dir() - Creates new property directory
237 * @uuid: UUID used to identify the particular directory
239 * Creates new, empty property directory. If @uuid is %NULL then the
240 * directory is assumed to be root directory.
242 struct tb_property_dir
*tb_property_create_dir(const uuid_t
*uuid
)
244 struct tb_property_dir
*dir
;
246 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
250 INIT_LIST_HEAD(&dir
->properties
);
252 dir
->uuid
= kmemdup(uuid
, sizeof(*dir
->uuid
), GFP_KERNEL
);
261 EXPORT_SYMBOL_GPL(tb_property_create_dir
);
263 static void tb_property_free(struct tb_property
*property
)
265 switch (property
->type
) {
266 case TB_PROPERTY_TYPE_DIRECTORY
:
267 tb_property_free_dir(property
->value
.dir
);
270 case TB_PROPERTY_TYPE_DATA
:
271 kfree(property
->value
.data
);
274 case TB_PROPERTY_TYPE_TEXT
:
275 kfree(property
->value
.text
);
286 * tb_property_free_dir() - Release memory allocated for property directory
287 * @dir: Directory to release
289 * This will release all the memory the directory occupies including all
290 * descendants. It is OK to pass %NULL @dir, then the function does
293 void tb_property_free_dir(struct tb_property_dir
*dir
)
295 struct tb_property
*property
, *tmp
;
300 list_for_each_entry_safe(property
, tmp
, &dir
->properties
, list
) {
301 list_del(&property
->list
);
302 tb_property_free(property
);
307 EXPORT_SYMBOL_GPL(tb_property_free_dir
);
309 static size_t tb_property_dir_length(const struct tb_property_dir
*dir
,
310 bool recurse
, size_t *data_len
)
312 const struct tb_property
*property
;
316 len
+= sizeof(*dir
->uuid
) / 4;
318 len
+= sizeof(struct tb_property_rootdir_entry
) / 4;
320 list_for_each_entry(property
, &dir
->properties
, list
) {
321 len
+= sizeof(struct tb_property_entry
) / 4;
323 switch (property
->type
) {
324 case TB_PROPERTY_TYPE_DIRECTORY
:
326 len
+= tb_property_dir_length(
327 property
->value
.dir
, recurse
, data_len
);
329 /* Reserve dword padding after each directory */
334 case TB_PROPERTY_TYPE_DATA
:
335 case TB_PROPERTY_TYPE_TEXT
:
337 *data_len
+= property
->length
;
348 static ssize_t
__tb_property_format_dir(const struct tb_property_dir
*dir
,
349 u32
*block
, unsigned int start_offset
, size_t block_len
)
351 unsigned int data_offset
, dir_end
;
352 const struct tb_property
*property
;
353 struct tb_property_entry
*entry
;
354 size_t dir_len
, data_len
= 0;
358 * The structure of property block looks like following. Leaf
359 * data/text is included right after the directory and each
360 * directory follows each other (even nested ones).
362 * +----------+ <-- start_offset
363 * | header | <-- root directory header
365 * | entry 0 | -^--------------------.
367 * | entry 1 | -|--------------------|--.
369 * | entry 2 | -|-----------------. | |
370 * +----------+ | | | |
371 * : : | dir_len | | |
374 * +----------+ | | | |
375 * | entry n | v | | |
376 * +----------+ <-- data_offset | | |
377 * | data 0 | <------------------|--' |
379 * | data 1 | <------------------|-----'
381 * | 00000000 | padding |
382 * +----------+ <-- dir_end <------'
383 * | UUID | <-- directory UUID (child directory)
398 * We use dir_end to hold pointer to the end of the directory. It
399 * will increase as we add directories and each directory should be
400 * added starting from previous dir_end.
402 dir_len
= tb_property_dir_length(dir
, false, &data_len
);
403 data_offset
= start_offset
+ dir_len
;
404 dir_end
= start_offset
+ data_len
+ dir_len
;
406 if (data_offset
> dir_end
)
408 if (dir_end
> block_len
)
411 /* Write headers first */
413 struct tb_property_dir_entry
*pe
;
415 pe
= (struct tb_property_dir_entry
*)&block
[start_offset
];
416 memcpy(pe
->uuid
, dir
->uuid
, sizeof(pe
->uuid
));
419 struct tb_property_rootdir_entry
*re
;
421 re
= (struct tb_property_rootdir_entry
*)&block
[start_offset
];
422 re
->magic
= TB_PROPERTY_ROOTDIR_MAGIC
;
423 re
->length
= dir_len
- sizeof(*re
) / 4;
427 list_for_each_entry(property
, &dir
->properties
, list
) {
428 const struct tb_property_dir
*child
;
430 format_dwdata(entry
, property
->key
, 2);
431 entry
->type
= property
->type
;
433 switch (property
->type
) {
434 case TB_PROPERTY_TYPE_DIRECTORY
:
435 child
= property
->value
.dir
;
436 ret
= __tb_property_format_dir(child
, block
, dir_end
,
440 entry
->length
= tb_property_dir_length(child
, false,
442 entry
->value
= dir_end
;
446 case TB_PROPERTY_TYPE_DATA
:
447 format_dwdata(&block
[data_offset
], property
->value
.data
,
449 entry
->length
= property
->length
;
450 entry
->value
= data_offset
;
451 data_offset
+= entry
->length
;
454 case TB_PROPERTY_TYPE_TEXT
:
455 format_dwdata(&block
[data_offset
], property
->value
.text
,
457 entry
->length
= property
->length
;
458 entry
->value
= data_offset
;
459 data_offset
+= entry
->length
;
462 case TB_PROPERTY_TYPE_VALUE
:
463 entry
->length
= property
->length
;
464 entry
->value
= property
->value
.immediate
;
478 * tb_property_format_dir() - Formats directory to the packed XDomain format
479 * @dir: Directory to format
480 * @block: Property block where the packed data is placed
481 * @block_len: Length of the property block
483 * This function formats the directory to the packed format that can be
484 * then send over the thunderbolt fabric to receiving host. Returns %0 in
485 * case of success and negative errno on faulure. Passing %NULL in @block
486 * returns number of entries the block takes.
488 ssize_t
tb_property_format_dir(const struct tb_property_dir
*dir
, u32
*block
,
494 size_t dir_len
, data_len
= 0;
496 dir_len
= tb_property_dir_length(dir
, true, &data_len
);
497 return dir_len
+ data_len
;
500 ret
= __tb_property_format_dir(dir
, block
, 0, block_len
);
501 return ret
< 0 ? ret
: 0;
505 * tb_property_add_immediate() - Add immediate property to directory
506 * @parent: Directory to add the property
507 * @key: Key for the property
508 * @value: Immediate value to store with the property
510 int tb_property_add_immediate(struct tb_property_dir
*parent
, const char *key
,
513 struct tb_property
*property
;
515 if (!tb_property_key_valid(key
))
518 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_VALUE
);
522 property
->length
= 1;
523 property
->value
.immediate
= value
;
525 list_add_tail(&property
->list
, &parent
->properties
);
528 EXPORT_SYMBOL_GPL(tb_property_add_immediate
);
531 * tb_property_add_data() - Adds arbitrary data property to directory
532 * @parent: Directory to add the property
533 * @key: Key for the property
534 * @buf: Data buffer to add
535 * @buflen: Number of bytes in the data buffer
537 * Function takes a copy of @buf and adds it to the directory.
539 int tb_property_add_data(struct tb_property_dir
*parent
, const char *key
,
540 const void *buf
, size_t buflen
)
542 /* Need to pad to dword boundary */
543 size_t size
= round_up(buflen
, 4);
544 struct tb_property
*property
;
546 if (!tb_property_key_valid(key
))
549 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DATA
);
553 property
->length
= size
/ 4;
554 property
->value
.data
= kzalloc(size
, GFP_KERNEL
);
555 if (!property
->value
.data
) {
560 memcpy(property
->value
.data
, buf
, buflen
);
562 list_add_tail(&property
->list
, &parent
->properties
);
565 EXPORT_SYMBOL_GPL(tb_property_add_data
);
568 * tb_property_add_text() - Adds string property to directory
569 * @parent: Directory to add the property
570 * @key: Key for the property
571 * @text: String to add
573 * Function takes a copy of @text and adds it to the directory.
575 int tb_property_add_text(struct tb_property_dir
*parent
, const char *key
,
578 /* Need to pad to dword boundary */
579 size_t size
= round_up(strlen(text
) + 1, 4);
580 struct tb_property
*property
;
582 if (!tb_property_key_valid(key
))
585 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_TEXT
);
589 property
->length
= size
/ 4;
590 property
->value
.text
= kzalloc(size
, GFP_KERNEL
);
591 if (!property
->value
.text
) {
596 strcpy(property
->value
.text
, text
);
598 list_add_tail(&property
->list
, &parent
->properties
);
601 EXPORT_SYMBOL_GPL(tb_property_add_text
);
604 * tb_property_add_dir() - Adds a directory to the parent directory
605 * @parent: Directory to add the property
606 * @key: Key for the property
607 * @dir: Directory to add
609 int tb_property_add_dir(struct tb_property_dir
*parent
, const char *key
,
610 struct tb_property_dir
*dir
)
612 struct tb_property
*property
;
614 if (!tb_property_key_valid(key
))
617 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DIRECTORY
);
621 property
->value
.dir
= dir
;
623 list_add_tail(&property
->list
, &parent
->properties
);
626 EXPORT_SYMBOL_GPL(tb_property_add_dir
);
629 * tb_property_remove() - Removes property from a parent directory
630 * @property: Property to remove
632 * Note memory for @property is released as well so it is not allowed to
633 * touch the object after call to this function.
635 void tb_property_remove(struct tb_property
*property
)
637 list_del(&property
->list
);
640 EXPORT_SYMBOL_GPL(tb_property_remove
);
643 * tb_property_find() - Find a property from a directory
644 * @dir: Directory where the property is searched
645 * @key: Key to look for
646 * @type: Type of the property
648 * Finds and returns property from the given directory. Does not recurse
649 * into sub-directories. Returns %NULL if the property was not found.
651 struct tb_property
*tb_property_find(struct tb_property_dir
*dir
,
652 const char *key
, enum tb_property_type type
)
654 struct tb_property
*property
;
656 list_for_each_entry(property
, &dir
->properties
, list
) {
657 if (property
->type
== type
&& !strcmp(property
->key
, key
))
663 EXPORT_SYMBOL_GPL(tb_property_find
);
666 * tb_property_get_next() - Get next property from directory
667 * @dir: Directory holding properties
668 * @prev: Previous property in the directory (%NULL returns the first)
670 struct tb_property
*tb_property_get_next(struct tb_property_dir
*dir
,
671 struct tb_property
*prev
)
674 if (list_is_last(&prev
->list
, &dir
->properties
))
676 return list_next_entry(prev
, list
);
678 return list_first_entry_or_null(&dir
->properties
, struct tb_property
,
681 EXPORT_SYMBOL_GPL(tb_property_get_next
);