2 * Thunderbolt XDomain property support
4 * Copyright (C) 2017, Intel Corporation
5 * Authors: Michael Jamet <michael.jamet@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/uuid.h>
17 #include <linux/thunderbolt.h>
19 struct tb_property_entry
{
28 struct tb_property_rootdir_entry
{
31 struct tb_property_entry entries
[];
34 struct tb_property_dir_entry
{
36 struct tb_property_entry entries
[];
39 #define TB_PROPERTY_ROOTDIR_MAGIC 0x55584401
41 static struct tb_property_dir
*__tb_property_parse_dir(const u32
*block
,
42 size_t block_len
, unsigned int dir_offset
, size_t dir_len
,
45 static inline void parse_dwdata(void *dst
, const void *src
, size_t dwords
)
47 be32_to_cpu_array(dst
, src
, dwords
);
50 static inline void format_dwdata(void *dst
, const void *src
, size_t dwords
)
52 cpu_to_be32_array(dst
, src
, dwords
);
55 static bool tb_property_entry_valid(const struct tb_property_entry
*entry
,
58 switch (entry
->type
) {
59 case TB_PROPERTY_TYPE_DIRECTORY
:
60 case TB_PROPERTY_TYPE_DATA
:
61 case TB_PROPERTY_TYPE_TEXT
:
62 if (entry
->length
> block_len
)
64 if (entry
->value
+ entry
->length
> block_len
)
68 case TB_PROPERTY_TYPE_VALUE
:
69 if (entry
->length
!= 1)
77 static bool tb_property_key_valid(const char *key
)
79 return key
&& strlen(key
) <= TB_PROPERTY_KEY_SIZE
;
82 static struct tb_property
*
83 tb_property_alloc(const char *key
, enum tb_property_type type
)
85 struct tb_property
*property
;
87 property
= kzalloc(sizeof(*property
), GFP_KERNEL
);
91 strcpy(property
->key
, key
);
92 property
->type
= type
;
93 INIT_LIST_HEAD(&property
->list
);
98 static struct tb_property
*tb_property_parse(const u32
*block
, size_t block_len
,
99 const struct tb_property_entry
*entry
)
101 char key
[TB_PROPERTY_KEY_SIZE
+ 1];
102 struct tb_property
*property
;
103 struct tb_property_dir
*dir
;
105 if (!tb_property_entry_valid(entry
, block_len
))
108 parse_dwdata(key
, entry
, 2);
109 key
[TB_PROPERTY_KEY_SIZE
] = '\0';
111 property
= tb_property_alloc(key
, entry
->type
);
115 property
->length
= entry
->length
;
117 switch (property
->type
) {
118 case TB_PROPERTY_TYPE_DIRECTORY
:
119 dir
= __tb_property_parse_dir(block
, block_len
, entry
->value
,
120 entry
->length
, false);
125 property
->value
.dir
= dir
;
128 case TB_PROPERTY_TYPE_DATA
:
129 property
->value
.data
= kcalloc(property
->length
, sizeof(u32
),
131 if (!property
->value
.data
) {
135 parse_dwdata(property
->value
.data
, block
+ entry
->value
,
139 case TB_PROPERTY_TYPE_TEXT
:
140 property
->value
.text
= kcalloc(property
->length
, sizeof(u32
),
142 if (!property
->value
.text
) {
146 parse_dwdata(property
->value
.text
, block
+ entry
->value
,
148 /* Force null termination */
149 property
->value
.text
[property
->length
* 4 - 1] = '\0';
152 case TB_PROPERTY_TYPE_VALUE
:
153 property
->value
.immediate
= entry
->value
;
157 property
->type
= TB_PROPERTY_TYPE_UNKNOWN
;
164 static struct tb_property_dir
*__tb_property_parse_dir(const u32
*block
,
165 size_t block_len
, unsigned int dir_offset
, size_t dir_len
, bool is_root
)
167 const struct tb_property_entry
*entries
;
168 size_t i
, content_len
, nentries
;
169 unsigned int content_offset
;
170 struct tb_property_dir
*dir
;
172 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
177 content_offset
= dir_offset
+ 2;
178 content_len
= dir_len
;
180 dir
->uuid
= kmemdup(&block
[dir_offset
], sizeof(*dir
->uuid
),
182 content_offset
= dir_offset
+ 4;
183 content_len
= dir_len
- 4; /* Length includes UUID */
186 entries
= (const struct tb_property_entry
*)&block
[content_offset
];
187 nentries
= content_len
/ (sizeof(*entries
) / 4);
189 INIT_LIST_HEAD(&dir
->properties
);
191 for (i
= 0; i
< nentries
; i
++) {
192 struct tb_property
*property
;
194 property
= tb_property_parse(block
, block_len
, &entries
[i
]);
196 tb_property_free_dir(dir
);
200 list_add_tail(&property
->list
, &dir
->properties
);
207 * tb_property_parse_dir() - Parses properties from given property block
208 * @block: Property block to parse
209 * @block_len: Number of dword elements in the property block
211 * This function parses the XDomain properties data block into format that
212 * can be traversed using the helper functions provided by this module.
213 * Upon success returns the parsed directory. In case of error returns
214 * %NULL. The resulting &struct tb_property_dir needs to be released by
215 * calling tb_property_free_dir() when not needed anymore.
217 * The @block is expected to be root directory.
219 struct tb_property_dir
*tb_property_parse_dir(const u32
*block
,
222 const struct tb_property_rootdir_entry
*rootdir
=
223 (const struct tb_property_rootdir_entry
*)block
;
225 if (rootdir
->magic
!= TB_PROPERTY_ROOTDIR_MAGIC
)
227 if (rootdir
->length
> block_len
)
230 return __tb_property_parse_dir(block
, block_len
, 0, rootdir
->length
,
235 * tb_property_create_dir() - Creates new property directory
236 * @uuid: UUID used to identify the particular directory
238 * Creates new, empty property directory. If @uuid is %NULL then the
239 * directory is assumed to be root directory.
241 struct tb_property_dir
*tb_property_create_dir(const uuid_t
*uuid
)
243 struct tb_property_dir
*dir
;
245 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
249 INIT_LIST_HEAD(&dir
->properties
);
251 dir
->uuid
= kmemdup(uuid
, sizeof(*dir
->uuid
), GFP_KERNEL
);
260 EXPORT_SYMBOL_GPL(tb_property_create_dir
);
262 static void tb_property_free(struct tb_property
*property
)
264 switch (property
->type
) {
265 case TB_PROPERTY_TYPE_DIRECTORY
:
266 tb_property_free_dir(property
->value
.dir
);
269 case TB_PROPERTY_TYPE_DATA
:
270 kfree(property
->value
.data
);
273 case TB_PROPERTY_TYPE_TEXT
:
274 kfree(property
->value
.text
);
285 * tb_property_free_dir() - Release memory allocated for property directory
286 * @dir: Directory to release
288 * This will release all the memory the directory occupies including all
289 * descendants. It is OK to pass %NULL @dir, then the function does
292 void tb_property_free_dir(struct tb_property_dir
*dir
)
294 struct tb_property
*property
, *tmp
;
299 list_for_each_entry_safe(property
, tmp
, &dir
->properties
, list
) {
300 list_del(&property
->list
);
301 tb_property_free(property
);
306 EXPORT_SYMBOL_GPL(tb_property_free_dir
);
308 static size_t tb_property_dir_length(const struct tb_property_dir
*dir
,
309 bool recurse
, size_t *data_len
)
311 const struct tb_property
*property
;
315 len
+= sizeof(*dir
->uuid
) / 4;
317 len
+= sizeof(struct tb_property_rootdir_entry
) / 4;
319 list_for_each_entry(property
, &dir
->properties
, list
) {
320 len
+= sizeof(struct tb_property_entry
) / 4;
322 switch (property
->type
) {
323 case TB_PROPERTY_TYPE_DIRECTORY
:
325 len
+= tb_property_dir_length(
326 property
->value
.dir
, recurse
, data_len
);
328 /* Reserve dword padding after each directory */
333 case TB_PROPERTY_TYPE_DATA
:
334 case TB_PROPERTY_TYPE_TEXT
:
336 *data_len
+= property
->length
;
347 static ssize_t
__tb_property_format_dir(const struct tb_property_dir
*dir
,
348 u32
*block
, unsigned int start_offset
, size_t block_len
)
350 unsigned int data_offset
, dir_end
;
351 const struct tb_property
*property
;
352 struct tb_property_entry
*entry
;
353 size_t dir_len
, data_len
= 0;
357 * The structure of property block looks like following. Leaf
358 * data/text is included right after the directory and each
359 * directory follows each other (even nested ones).
361 * +----------+ <-- start_offset
362 * | header | <-- root directory header
364 * | entry 0 | -^--------------------.
366 * | entry 1 | -|--------------------|--.
368 * | entry 2 | -|-----------------. | |
369 * +----------+ | | | |
370 * : : | dir_len | | |
373 * +----------+ | | | |
374 * | entry n | v | | |
375 * +----------+ <-- data_offset | | |
376 * | data 0 | <------------------|--' |
378 * | data 1 | <------------------|-----'
380 * | 00000000 | padding |
381 * +----------+ <-- dir_end <------'
382 * | UUID | <-- directory UUID (child directory)
397 * We use dir_end to hold pointer to the end of the directory. It
398 * will increase as we add directories and each directory should be
399 * added starting from previous dir_end.
401 dir_len
= tb_property_dir_length(dir
, false, &data_len
);
402 data_offset
= start_offset
+ dir_len
;
403 dir_end
= start_offset
+ data_len
+ dir_len
;
405 if (data_offset
> dir_end
)
407 if (dir_end
> block_len
)
410 /* Write headers first */
412 struct tb_property_dir_entry
*pe
;
414 pe
= (struct tb_property_dir_entry
*)&block
[start_offset
];
415 memcpy(pe
->uuid
, dir
->uuid
, sizeof(pe
->uuid
));
418 struct tb_property_rootdir_entry
*re
;
420 re
= (struct tb_property_rootdir_entry
*)&block
[start_offset
];
421 re
->magic
= TB_PROPERTY_ROOTDIR_MAGIC
;
422 re
->length
= dir_len
- sizeof(*re
) / 4;
426 list_for_each_entry(property
, &dir
->properties
, list
) {
427 const struct tb_property_dir
*child
;
429 format_dwdata(entry
, property
->key
, 2);
430 entry
->type
= property
->type
;
432 switch (property
->type
) {
433 case TB_PROPERTY_TYPE_DIRECTORY
:
434 child
= property
->value
.dir
;
435 ret
= __tb_property_format_dir(child
, block
, dir_end
,
439 entry
->length
= tb_property_dir_length(child
, false,
441 entry
->value
= dir_end
;
445 case TB_PROPERTY_TYPE_DATA
:
446 format_dwdata(&block
[data_offset
], property
->value
.data
,
448 entry
->length
= property
->length
;
449 entry
->value
= data_offset
;
450 data_offset
+= entry
->length
;
453 case TB_PROPERTY_TYPE_TEXT
:
454 format_dwdata(&block
[data_offset
], property
->value
.text
,
456 entry
->length
= property
->length
;
457 entry
->value
= data_offset
;
458 data_offset
+= entry
->length
;
461 case TB_PROPERTY_TYPE_VALUE
:
462 entry
->length
= property
->length
;
463 entry
->value
= property
->value
.immediate
;
477 * tb_property_format_dir() - Formats directory to the packed XDomain format
478 * @dir: Directory to format
479 * @block: Property block where the packed data is placed
480 * @block_len: Length of the property block
482 * This function formats the directory to the packed format that can be
483 * then send over the thunderbolt fabric to receiving host. Returns %0 in
484 * case of success and negative errno on faulure. Passing %NULL in @block
485 * returns number of entries the block takes.
487 ssize_t
tb_property_format_dir(const struct tb_property_dir
*dir
, u32
*block
,
493 size_t dir_len
, data_len
= 0;
495 dir_len
= tb_property_dir_length(dir
, true, &data_len
);
496 return dir_len
+ data_len
;
499 ret
= __tb_property_format_dir(dir
, block
, 0, block_len
);
500 return ret
< 0 ? ret
: 0;
504 * tb_property_add_immediate() - Add immediate property to directory
505 * @parent: Directory to add the property
506 * @key: Key for the property
507 * @value: Immediate value to store with the property
509 int tb_property_add_immediate(struct tb_property_dir
*parent
, const char *key
,
512 struct tb_property
*property
;
514 if (!tb_property_key_valid(key
))
517 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_VALUE
);
521 property
->length
= 1;
522 property
->value
.immediate
= value
;
524 list_add_tail(&property
->list
, &parent
->properties
);
527 EXPORT_SYMBOL_GPL(tb_property_add_immediate
);
530 * tb_property_add_data() - Adds arbitrary data property to directory
531 * @parent: Directory to add the property
532 * @key: Key for the property
533 * @buf: Data buffer to add
534 * @buflen: Number of bytes in the data buffer
536 * Function takes a copy of @buf and adds it to the directory.
538 int tb_property_add_data(struct tb_property_dir
*parent
, const char *key
,
539 const void *buf
, size_t buflen
)
541 /* Need to pad to dword boundary */
542 size_t size
= round_up(buflen
, 4);
543 struct tb_property
*property
;
545 if (!tb_property_key_valid(key
))
548 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DATA
);
552 property
->length
= size
/ 4;
553 property
->value
.data
= kzalloc(size
, GFP_KERNEL
);
554 if (!property
->value
.data
) {
559 memcpy(property
->value
.data
, buf
, buflen
);
561 list_add_tail(&property
->list
, &parent
->properties
);
564 EXPORT_SYMBOL_GPL(tb_property_add_data
);
567 * tb_property_add_text() - Adds string property to directory
568 * @parent: Directory to add the property
569 * @key: Key for the property
570 * @text: String to add
572 * Function takes a copy of @text and adds it to the directory.
574 int tb_property_add_text(struct tb_property_dir
*parent
, const char *key
,
577 /* Need to pad to dword boundary */
578 size_t size
= round_up(strlen(text
) + 1, 4);
579 struct tb_property
*property
;
581 if (!tb_property_key_valid(key
))
584 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_TEXT
);
588 property
->length
= size
/ 4;
589 property
->value
.text
= kzalloc(size
, GFP_KERNEL
);
590 if (!property
->value
.text
) {
595 strcpy(property
->value
.text
, text
);
597 list_add_tail(&property
->list
, &parent
->properties
);
600 EXPORT_SYMBOL_GPL(tb_property_add_text
);
603 * tb_property_add_dir() - Adds a directory to the parent directory
604 * @parent: Directory to add the property
605 * @key: Key for the property
606 * @dir: Directory to add
608 int tb_property_add_dir(struct tb_property_dir
*parent
, const char *key
,
609 struct tb_property_dir
*dir
)
611 struct tb_property
*property
;
613 if (!tb_property_key_valid(key
))
616 property
= tb_property_alloc(key
, TB_PROPERTY_TYPE_DIRECTORY
);
620 property
->value
.dir
= dir
;
622 list_add_tail(&property
->list
, &parent
->properties
);
625 EXPORT_SYMBOL_GPL(tb_property_add_dir
);
628 * tb_property_remove() - Removes property from a parent directory
629 * @property: Property to remove
631 * Note memory for @property is released as well so it is not allowed to
632 * touch the object after call to this function.
634 void tb_property_remove(struct tb_property
*property
)
636 list_del(&property
->list
);
639 EXPORT_SYMBOL_GPL(tb_property_remove
);
642 * tb_property_find() - Find a property from a directory
643 * @dir: Directory where the property is searched
644 * @key: Key to look for
645 * @type: Type of the property
647 * Finds and returns property from the given directory. Does not recurse
648 * into sub-directories. Returns %NULL if the property was not found.
650 struct tb_property
*tb_property_find(struct tb_property_dir
*dir
,
651 const char *key
, enum tb_property_type type
)
653 struct tb_property
*property
;
655 list_for_each_entry(property
, &dir
->properties
, list
) {
656 if (property
->type
== type
&& !strcmp(property
->key
, key
))
662 EXPORT_SYMBOL_GPL(tb_property_find
);
665 * tb_property_get_next() - Get next property from directory
666 * @dir: Directory holding properties
667 * @prev: Previous property in the directory (%NULL returns the first)
669 struct tb_property
*tb_property_get_next(struct tb_property_dir
*dir
,
670 struct tb_property
*prev
)
673 if (list_is_last(&prev
->list
, &dir
->properties
))
675 return list_next_entry(prev
, list
);
677 return list_first_entry_or_null(&dir
->properties
, struct tb_property
,
680 EXPORT_SYMBOL_GPL(tb_property_get_next
);