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
),
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
]);
193 tb_property_free_dir(dir
);
197 list_add_tail(&property
->list
, &dir
->properties
);
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
,
219 const struct tb_property_rootdir_entry
*rootdir
=
220 (const struct tb_property_rootdir_entry
*)block
;
222 if (rootdir
->magic
!= TB_PROPERTY_ROOTDIR_MAGIC
)
224 if (rootdir
->length
> block_len
)
227 return __tb_property_parse_dir(block
, block_len
, 0, rootdir
->length
,
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
);
246 INIT_LIST_HEAD(&dir
->properties
);
248 dir
->uuid
= kmemdup(uuid
, sizeof(*dir
->uuid
), GFP_KERNEL
);
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
);
266 case TB_PROPERTY_TYPE_DATA
:
267 kfree(property
->value
.data
);
270 case TB_PROPERTY_TYPE_TEXT
:
271 kfree(property
->value
.text
);
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
289 void tb_property_free_dir(struct tb_property_dir
*dir
)
291 struct tb_property
*property
, *tmp
;
296 list_for_each_entry_safe(property
, tmp
, &dir
->properties
, list
) {
297 list_del(&property
->list
);
298 tb_property_free(property
);
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
;
312 len
+= sizeof(*dir
->uuid
) / 4;
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
:
322 len
+= tb_property_dir_length(
323 property
->value
.dir
, recurse
, data_len
);
325 /* Reserve dword padding after each directory */
330 case TB_PROPERTY_TYPE_DATA
:
331 case TB_PROPERTY_TYPE_TEXT
:
333 *data_len
+= property
->length
;
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;
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
361 * | entry 0 | -^--------------------.
363 * | entry 1 | -|--------------------|--.
365 * | entry 2 | -|-----------------. | |
366 * +----------+ | | | |
367 * : : | dir_len | | |
370 * +----------+ | | | |
371 * | entry n | v | | |
372 * +----------+ <-- data_offset | | |
373 * | data 0 | <------------------|--' |
375 * | data 1 | <------------------|-----'
377 * | 00000000 | padding |
378 * +----------+ <-- dir_end <------'
379 * | UUID | <-- directory UUID (child directory)
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
)
404 if (dir_end
> block_len
)
407 /* Write headers first */
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
));
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;
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
,
436 entry
->length
= tb_property_dir_length(child
, false,
438 entry
->value
= dir_end
;
442 case TB_PROPERTY_TYPE_DATA
:
443 format_dwdata(&block
[data_offset
], property
->value
.data
,
445 entry
->length
= property
->length
;
446 entry
->value
= data_offset
;
447 data_offset
+= entry
->length
;
450 case TB_PROPERTY_TYPE_TEXT
:
451 format_dwdata(&block
[data_offset
], property
->value
.text
,
453 entry
->length
= property
->length
;
454 entry
->value
= data_offset
;
455 data_offset
+= entry
->length
;
458 case TB_PROPERTY_TYPE_VALUE
:
459 entry
->length
= property
->length
;
460 entry
->value
= property
->value
.immediate
;
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
,
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
,
509 struct tb_property
*property
;
511 if (!tb_property_key_valid(key
))
514 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_VALUE
);
518 property
->length
= 1;
519 property
->value
.immediate
= value
;
521 list_add_tail(&property
->list
, &parent
->properties
);
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
))
545 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DATA
);
549 property
->length
= size
/ 4;
550 property
->value
.data
= kzalloc(size
, GFP_KERNEL
);
551 if (!property
->value
.data
) {
556 memcpy(property
->value
.data
, buf
, buflen
);
558 list_add_tail(&property
->list
, &parent
->properties
);
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
,
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
))
581 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_TEXT
);
585 property
->length
= size
/ 4;
586 property
->value
.text
= kzalloc(size
, GFP_KERNEL
);
587 if (!property
->value
.text
) {
592 strcpy(property
->value
.text
, text
);
594 list_add_tail(&property
->list
, &parent
->properties
);
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
))
613 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DIRECTORY
);
617 property
->value
.dir
= dir
;
619 list_add_tail(&property
->list
, &parent
->properties
);
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
);
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
))
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
)
670 if (list_is_last(&prev
->list
, &dir
->properties
))
672 return list_next_entry(prev
, list
);
674 return list_first_entry_or_null(&dir
->properties
, struct tb_property
,
677 EXPORT_SYMBOL_GPL(tb_property_get_next
);