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_copy_dir() - Take a deep copy of directory
506 * @dir: Directory to copy
508 * This function takes a deep copy of @dir and returns back the copy. In
509 * case of error returns %NULL. The resulting directory needs to be
510 * released by calling tb_property_free_dir().
512 struct tb_property_dir
*tb_property_copy_dir(const struct tb_property_dir
*dir
)
514 struct tb_property
*property
, *p
= NULL
;
515 struct tb_property_dir
*d
;
520 d
= tb_property_create_dir(dir
->uuid
);
524 list_for_each_entry(property
, &dir
->properties
, list
) {
525 struct tb_property
*p
;
527 p
= tb_property_alloc(property
->key
, property
->type
);
531 p
->length
= property
->length
;
533 switch (property
->type
) {
534 case TB_PROPERTY_TYPE_DIRECTORY
:
535 p
->value
.dir
= tb_property_copy_dir(property
->value
.dir
);
540 case TB_PROPERTY_TYPE_DATA
:
541 p
->value
.data
= kmemdup(property
->value
.data
,
542 property
->length
* 4,
548 case TB_PROPERTY_TYPE_TEXT
:
549 p
->value
.text
= kzalloc(p
->length
* 4, GFP_KERNEL
);
552 strcpy(p
->value
.text
, property
->value
.text
);
555 case TB_PROPERTY_TYPE_VALUE
:
556 p
->value
.immediate
= property
->value
.immediate
;
563 list_add_tail(&p
->list
, &d
->properties
);
570 tb_property_free_dir(d
);
576 * tb_property_add_immediate() - Add immediate property to directory
577 * @parent: Directory to add the property
578 * @key: Key for the property
579 * @value: Immediate value to store with the property
581 int tb_property_add_immediate(struct tb_property_dir
*parent
, const char *key
,
584 struct tb_property
*property
;
586 if (!tb_property_key_valid(key
))
589 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_VALUE
);
593 property
->length
= 1;
594 property
->value
.immediate
= value
;
596 list_add_tail(&property
->list
, &parent
->properties
);
599 EXPORT_SYMBOL_GPL(tb_property_add_immediate
);
602 * tb_property_add_data() - Adds arbitrary data property to directory
603 * @parent: Directory to add the property
604 * @key: Key for the property
605 * @buf: Data buffer to add
606 * @buflen: Number of bytes in the data buffer
608 * Function takes a copy of @buf and adds it to the directory.
610 int tb_property_add_data(struct tb_property_dir
*parent
, const char *key
,
611 const void *buf
, size_t buflen
)
613 /* Need to pad to dword boundary */
614 size_t size
= round_up(buflen
, 4);
615 struct tb_property
*property
;
617 if (!tb_property_key_valid(key
))
620 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DATA
);
624 property
->length
= size
/ 4;
625 property
->value
.data
= kzalloc(size
, GFP_KERNEL
);
626 if (!property
->value
.data
) {
631 memcpy(property
->value
.data
, buf
, buflen
);
633 list_add_tail(&property
->list
, &parent
->properties
);
636 EXPORT_SYMBOL_GPL(tb_property_add_data
);
639 * tb_property_add_text() - Adds string property to directory
640 * @parent: Directory to add the property
641 * @key: Key for the property
642 * @text: String to add
644 * Function takes a copy of @text and adds it to the directory.
646 int tb_property_add_text(struct tb_property_dir
*parent
, const char *key
,
649 /* Need to pad to dword boundary */
650 size_t size
= round_up(strlen(text
) + 1, 4);
651 struct tb_property
*property
;
653 if (!tb_property_key_valid(key
))
656 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_TEXT
);
660 property
->length
= size
/ 4;
661 property
->value
.text
= kzalloc(size
, GFP_KERNEL
);
662 if (!property
->value
.text
) {
667 strcpy(property
->value
.text
, text
);
669 list_add_tail(&property
->list
, &parent
->properties
);
672 EXPORT_SYMBOL_GPL(tb_property_add_text
);
675 * tb_property_add_dir() - Adds a directory to the parent directory
676 * @parent: Directory to add the property
677 * @key: Key for the property
678 * @dir: Directory to add
680 int tb_property_add_dir(struct tb_property_dir
*parent
, const char *key
,
681 struct tb_property_dir
*dir
)
683 struct tb_property
*property
;
685 if (!tb_property_key_valid(key
))
688 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DIRECTORY
);
692 property
->value
.dir
= dir
;
694 list_add_tail(&property
->list
, &parent
->properties
);
697 EXPORT_SYMBOL_GPL(tb_property_add_dir
);
700 * tb_property_remove() - Removes property from a parent directory
701 * @property: Property to remove
703 * Note memory for @property is released as well so it is not allowed to
704 * touch the object after call to this function.
706 void tb_property_remove(struct tb_property
*property
)
708 list_del(&property
->list
);
711 EXPORT_SYMBOL_GPL(tb_property_remove
);
714 * tb_property_find() - Find a property from a directory
715 * @dir: Directory where the property is searched
716 * @key: Key to look for
717 * @type: Type of the property
719 * Finds and returns property from the given directory. Does not recurse
720 * into sub-directories. Returns %NULL if the property was not found.
722 struct tb_property
*tb_property_find(struct tb_property_dir
*dir
,
723 const char *key
, enum tb_property_type type
)
725 struct tb_property
*property
;
727 list_for_each_entry(property
, &dir
->properties
, list
) {
728 if (property
->type
== type
&& !strcmp(property
->key
, key
))
734 EXPORT_SYMBOL_GPL(tb_property_find
);
737 * tb_property_get_next() - Get next property from directory
738 * @dir: Directory holding properties
739 * @prev: Previous property in the directory (%NULL returns the first)
741 struct tb_property
*tb_property_get_next(struct tb_property_dir
*dir
,
742 struct tb_property
*prev
)
745 if (list_is_last(&prev
->list
, &dir
->properties
))
747 return list_next_entry(prev
, list
);
749 return list_first_entry_or_null(&dir
->properties
, struct tb_property
,
752 EXPORT_SYMBOL_GPL(tb_property_get_next
);