blk-throttle: fix lockdep warning of "cgroup_mutex or RCU read lock required!"
[linux-block.git] / drivers / greybus / manifest.c
CommitLineData
eb50fd3a 1// SPDX-License-Identifier: GPL-2.0
b09c94a1 2/*
a93db2d1 3 * Greybus manifest parsing
b09c94a1 4 *
d8187aa2
AE
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
b09c94a1
AE
7 */
8
ec0ad868 9#include <linux/greybus.h>
b09c94a1 10
19b3b2c2
VK
11static const char *get_descriptor_type_string(u8 type)
12{
3dd22268 13 switch (type) {
19b3b2c2
VK
14 case GREYBUS_TYPE_INVALID:
15 return "invalid";
19b3b2c2
VK
16 case GREYBUS_TYPE_STRING:
17 return "string";
18 case GREYBUS_TYPE_INTERFACE:
19 return "interface";
20 case GREYBUS_TYPE_CPORT:
21 return "cport";
83a0cb59
VK
22 case GREYBUS_TYPE_BUNDLE:
23 return "bundle";
19b3b2c2
VK
24 default:
25 WARN_ON(1);
26 return "unknown";
27 }
28}
29
b09c94a1
AE
30/*
31 * We scan the manifest once to identify where all the descriptors
32 * are. The result is a list of these manifest_desc structures. We
33 * then pick through them for what we're looking for (starting with
a93db2d1 34 * the interface descriptor). As each is processed we remove it from
b09c94a1
AE
35 * the list. When we're done the list should (probably) be empty.
36 */
37struct manifest_desc {
38 struct list_head links;
39
40 size_t size;
41 void *data;
42 enum greybus_descriptor_type type;
43};
44
b09c94a1
AE
45static void release_manifest_descriptor(struct manifest_desc *descriptor)
46{
47 list_del(&descriptor->links);
48 kfree(descriptor);
49}
50
86cad666 51static void release_manifest_descriptors(struct gb_interface *intf)
b09c94a1
AE
52{
53 struct manifest_desc *descriptor;
54 struct manifest_desc *next;
55
86cad666 56 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
b09c94a1
AE
57 release_manifest_descriptor(descriptor);
58}
59
f2152eb3
JH
60static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
61{
62 struct manifest_desc *desc, *tmp;
63 struct greybus_descriptor_cport *desc_cport;
64
65 list_for_each_entry_safe(desc, tmp, head, links) {
66 desc_cport = desc->data;
67
68 if (desc->type != GREYBUS_TYPE_CPORT)
69 continue;
70
71 if (desc_cport->bundle == bundle_id)
72 release_manifest_descriptor(desc);
73 }
74}
75
5c864e77
RMS
76static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
77{
78 struct manifest_desc *descriptor;
79 struct manifest_desc *next;
80
81 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
82 if (descriptor->type == GREYBUS_TYPE_BUNDLE)
83 return descriptor;
84
85 return NULL;
86}
87
b09c94a1
AE
88/*
89 * Validate the given descriptor. Its reported size must fit within
696e0cca 90 * the number of bytes remaining, and it must have a recognized
b09c94a1
AE
91 * type. Check that the reported size is at least as big as what
92 * we expect to see. (It could be bigger, perhaps for a new version
93 * of the format.)
94 *
d393c98f
AE
95 * Returns the (non-zero) number of bytes consumed by the descriptor,
96 * or a negative errno.
b09c94a1 97 */
86cad666
GKH
98static int identify_descriptor(struct gb_interface *intf,
99 struct greybus_descriptor *desc, size_t size)
b09c94a1
AE
100{
101 struct greybus_descriptor_header *desc_header = &desc->header;
102 struct manifest_desc *descriptor;
d8187aa2 103 size_t desc_size;
b09c94a1
AE
104 size_t expected_size;
105
106 if (size < sizeof(*desc_header)) {
ee989b02
GKH
107 dev_err(&intf->dev, "manifest too small (%zu < %zu)\n", size,
108 sizeof(*desc_header));
b09c94a1
AE
109 return -EINVAL; /* Must at least have header */
110 }
111
d8187aa2
AE
112 desc_size = le16_to_cpu(desc_header->size);
113 if (desc_size > size) {
a0b5542d 114 dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
ee989b02 115 desc_size, size);
b09c94a1
AE
116 return -EINVAL;
117 }
118
19b3b2c2
VK
119 /* Descriptor needs to at least have a header */
120 expected_size = sizeof(*desc_header);
121
b09c94a1 122 switch (desc_header->type) {
b09c94a1 123 case GREYBUS_TYPE_STRING:
b09c94a1 124 expected_size += sizeof(struct greybus_descriptor_string);
19b3b2c2 125 expected_size += desc->string.length;
fa2fbf16
VK
126
127 /* String descriptors are padded to 4 byte boundaries */
128 expected_size = ALIGN(expected_size, 4);
b09c94a1 129 break;
63cc932b 130 case GREYBUS_TYPE_INTERFACE:
a93db2d1 131 expected_size += sizeof(struct greybus_descriptor_interface);
63cc932b 132 break;
7c183f70
VK
133 case GREYBUS_TYPE_BUNDLE:
134 expected_size += sizeof(struct greybus_descriptor_bundle);
135 break;
b09c94a1 136 case GREYBUS_TYPE_CPORT:
19b3b2c2 137 expected_size += sizeof(struct greybus_descriptor_cport);
b09c94a1
AE
138 break;
139 case GREYBUS_TYPE_INVALID:
140 default:
a0b5542d 141 dev_err(&intf->dev, "invalid descriptor type (%u)\n",
ee989b02 142 desc_header->type);
b09c94a1
AE
143 return -EINVAL;
144 }
145
19b3b2c2 146 if (desc_size < expected_size) {
a0b5542d 147 dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
ee989b02
GKH
148 get_descriptor_type_string(desc_header->type),
149 desc_size, expected_size);
19b3b2c2
VK
150 return -EINVAL;
151 }
152
55b930cd
VK
153 /* Descriptor bigger than what we expect */
154 if (desc_size > expected_size) {
a0b5542d 155 dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
ee989b02
GKH
156 get_descriptor_type_string(desc_header->type),
157 expected_size, desc_size);
55b930cd
VK
158 }
159
b09c94a1
AE
160 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
161 if (!descriptor)
162 return -ENOMEM;
163
164 descriptor->size = desc_size;
d393c98f 165 descriptor->data = (char *)desc + sizeof(*desc_header);
b09c94a1 166 descriptor->type = desc_header->type;
86cad666 167 list_add_tail(&descriptor->links, &intf->manifest_descs);
b09c94a1 168
d393c98f 169 /* desc_size is positive and is known to fit in a signed int */
d8187aa2 170
b09c94a1
AE
171 return desc_size;
172}
173
174/*
175 * Find the string descriptor having the given id, validate it, and
176 * allocate a duplicate copy of it. The duplicate has an extra byte
177 * which guarantees the returned string is NUL-terminated.
178 *
179 * String index 0 is valid (it represents "no string"), and for
180 * that a null pointer is returned.
181 *
182 * Otherwise returns a pointer to a newly-allocated copy of the
183 * descriptor string, or an error-coded pointer on failure.
184 */
86cad666 185static char *gb_string_get(struct gb_interface *intf, u8 string_id)
b09c94a1
AE
186{
187 struct greybus_descriptor_string *desc_string;
188 struct manifest_desc *descriptor;
189 bool found = false;
190 char *string;
191
192 /* A zero string id means no string (but no error) */
193 if (!string_id)
194 return NULL;
195
86cad666 196 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
b09c94a1
AE
197 if (descriptor->type != GREYBUS_TYPE_STRING)
198 continue;
199
7a13e2f6 200 desc_string = descriptor->data;
b09c94a1
AE
201 if (desc_string->id == string_id) {
202 found = true;
203 break;
204 }
205 }
206 if (!found)
207 return ERR_PTR(-ENOENT);
208
209 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
d393c98f 210 string = kmemdup(&desc_string->string, desc_string->length + 1,
ee989b02 211 GFP_KERNEL);
b09c94a1
AE
212 if (!string)
213 return ERR_PTR(-ENOMEM);
214 string[desc_string->length] = '\0';
215
216 /* Ok we've used this string, so we're done with it */
217 release_manifest_descriptor(descriptor);
218
219 return string;
220}
221
c095bbcf 222/*
d393c98f
AE
223 * Find cport descriptors in the manifest associated with the given
224 * bundle, and set up data structures for the functions that use
225 * them. Returns the number of cports set up for the bundle, or 0
226 * if there is an error.
c095bbcf 227 */
c46839d1 228static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
c095bbcf 229{
c46839d1 230 struct gb_interface *intf = bundle->intf;
98fdf5a0 231 struct greybus_descriptor_cport *desc_cport;
d6fba3db 232 struct manifest_desc *desc, *next, *tmp;
98fdf5a0 233 LIST_HEAD(list);
a6b13eb6 234 u8 bundle_id = bundle->id;
b38fe347 235 u16 cport_id;
c095bbcf 236 u32 count = 0;
98fdf5a0 237 int i;
c095bbcf 238
a6b13eb6
AE
239 /* Set up all cport descriptors associated with this bundle */
240 list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
a6b13eb6
AE
241 if (desc->type != GREYBUS_TYPE_CPORT)
242 continue;
243
244 desc_cport = desc->data;
245 if (desc_cport->bundle != bundle_id)
246 continue;
c095bbcf 247
fb690ca9
AE
248 cport_id = le16_to_cpu(desc_cport->id);
249 if (cport_id > CPORT_ID_MAX)
b38fe347 250 goto exit;
fb690ca9 251
42830f7f
VK
252 /* Nothing else should have its cport_id as control cport id */
253 if (cport_id == GB_CONTROL_CPORT_ID) {
254 dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
255 cport_id);
256 goto exit;
257 }
258
d6fba3db
JH
259 /*
260 * Found one, move it to our temporary list after checking for
261 * duplicates.
262 */
263 list_for_each_entry(tmp, &list, links) {
264 desc_cport = tmp->data;
d1a9c056 265 if (cport_id == le16_to_cpu(desc_cport->id)) {
d6fba3db 266 dev_err(&bundle->dev,
ee989b02 267 "duplicate CPort %u found\n", cport_id);
d6fba3db
JH
268 goto exit;
269 }
270 }
4a7908cb 271 list_move_tail(&desc->links, &list);
98fdf5a0
JH
272 count++;
273 }
730a2f6d 274
98fdf5a0
JH
275 if (!count)
276 return 0;
c095bbcf 277
98fdf5a0 278 bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
ee989b02 279 GFP_KERNEL);
98fdf5a0
JH
280 if (!bundle->cport_desc)
281 goto exit;
282
283 bundle->num_cports = count;
284
285 i = 0;
286 list_for_each_entry_safe(desc, next, &list, links) {
287 desc_cport = desc->data;
288 memcpy(&bundle->cport_desc[i++], desc_cport,
ee989b02 289 sizeof(*desc_cport));
a6b13eb6 290
c095bbcf 291 /* Release the cport descriptor */
a6b13eb6 292 release_manifest_descriptor(desc);
c095bbcf
AE
293 }
294
295 return count;
b38fe347 296exit:
98fdf5a0 297 release_cport_descriptors(&list, bundle_id);
4317f874
VK
298 /*
299 * Free all cports for this bundle to avoid 'excess descriptors'
300 * warnings.
301 */
f2152eb3 302 release_cport_descriptors(&intf->manifest_descs, bundle_id);
4317f874 303
52e8ce31 304 return 0; /* Error; count should also be 0 */
c095bbcf
AE
305}
306
d88bfb5b 307/*
1db0a5ff
GKH
308 * Find bundle descriptors in the manifest and set up their data
309 * structures. Returns the number of bundles set up for the
7c183f70 310 * given interface.
d88bfb5b 311 */
4ab9b3c2 312static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
d88bfb5b 313{
c27a253f 314 struct manifest_desc *desc;
2a64fb0e
AE
315 struct gb_bundle *bundle;
316 struct gb_bundle *bundle_next;
d88bfb5b 317 u32 count = 0;
98d7fbca 318 u8 bundle_id;
47091af9 319 u8 class;
d88bfb5b 320
5c864e77 321 while ((desc = get_next_bundle_desc(intf))) {
7c183f70 322 struct greybus_descriptor_bundle *desc_bundle;
c27a253f 323
1db0a5ff 324 /* Found one. Set up its bundle structure*/
c27a253f 325 desc_bundle = desc->data;
98d7fbca 326 bundle_id = desc_bundle->id;
47091af9
JH
327 class = desc_bundle->class;
328
329 /* Done with this bundle descriptor */
330 release_manifest_descriptor(desc);
6c68da26 331
47091af9 332 /* Ignore any legacy control bundles */
98d7fbca 333 if (bundle_id == GB_CONTROL_BUNDLE_ID) {
47091af9 334 dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
ee989b02 335 __func__);
47091af9 336 release_cport_descriptors(&intf->manifest_descs,
ee989b02 337 bundle_id);
47091af9 338 continue;
6c68da26
VK
339 }
340
730a2f6d 341 /* Nothing else should have its class set to control class */
47091af9 342 if (class == GREYBUS_CLASS_CONTROL) {
b38fe347 343 dev_err(&intf->dev,
100e9000 344 "bundle %u cannot use control class\n",
98d7fbca 345 bundle_id);
b38fe347
BD
346 goto cleanup;
347 }
730a2f6d 348
47091af9 349 bundle = gb_bundle_create(intf, bundle_id, class);
1db0a5ff 350 if (!bundle)
2a64fb0e 351 goto cleanup;
c095bbcf 352
98d7fbca
VK
353 /*
354 * Now go set up this bundle's functions and cports.
355 *
356 * A 'bundle' represents a device in greybus. It may require
357 * multiple cports for its functioning. If we fail to setup any
358 * cport of a bundle, we better reject the complete bundle as
359 * the device may not be able to function properly then.
360 *
361 * But, failing to setup a cport of bundle X doesn't mean that
362 * the device corresponding to bundle Y will not work properly.
363 * Bundles should be treated as separate independent devices.
364 *
365 * While parsing manifest for an interface, treat bundles as
366 * separate entities and don't reject entire interface and its
367 * bundles on failing to initialize a cport. But make sure the
368 * bundle which needs the cport, gets destroyed properly.
98d7fbca
VK
369 */
370 if (!gb_manifest_parse_cports(bundle)) {
98d7fbca
VK
371 gb_bundle_destroy(bundle);
372 continue;
373 }
374
b38fe347 375 count++;
d88bfb5b
AE
376 }
377
378 return count;
2a64fb0e
AE
379cleanup:
380 /* An error occurred; undo any changes we've made */
381 list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
382 gb_bundle_destroy(bundle);
383 count--;
384 }
385 return 0; /* Error; count should also be 0 */
d88bfb5b
AE
386}
387
a93db2d1
VK
388static bool gb_manifest_parse_interface(struct gb_interface *intf,
389 struct manifest_desc *interface_desc)
b09c94a1 390{
a93db2d1 391 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
7c8eb12d 392 struct gb_control *control = intf->control;
7d963cbe 393 char *str;
b09c94a1
AE
394
395 /* Handle the strings first--they can fail */
7d963cbe
JH
396 str = gb_string_get(intf, desc_intf->vendor_stringid);
397 if (IS_ERR(str))
937d0da8 398 return false;
7c8eb12d 399 control->vendor_string = str;
937d0da8 400
7d963cbe
JH
401 str = gb_string_get(intf, desc_intf->product_stringid);
402 if (IS_ERR(str))
50fc08f8 403 goto out_free_vendor_string;
7c8eb12d 404 control->product_string = str;
b09c94a1 405
8c81d460
BD
406 /* Assign feature flags communicated via manifest */
407 intf->features = desc_intf->features;
408
a93db2d1
VK
409 /* Release the interface descriptor, now that we're done with it */
410 release_manifest_descriptor(interface_desc);
b09c94a1 411
1db0a5ff 412 /* An interface must have at least one bundle descriptor */
4ab9b3c2 413 if (!gb_manifest_parse_bundles(intf)) {
09fb10fe 414 dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
937d0da8 415 goto out_err;
d88bfb5b
AE
416 }
417
937d0da8
AE
418 return true;
419out_err:
7c8eb12d
JH
420 kfree(control->product_string);
421 control->product_string = NULL;
50fc08f8 422out_free_vendor_string:
7c8eb12d
JH
423 kfree(control->vendor_string);
424 control->vendor_string = NULL;
937d0da8
AE
425
426 return false;
b09c94a1
AE
427}
428
429/*
d393c98f 430 * Parse a buffer containing an interface manifest.
b09c94a1
AE
431 *
432 * If we find anything wrong with the content/format of the buffer
433 * we reject it.
434 *
435 * The first requirement is that the manifest's version is
436 * one we can parse.
437 *
438 * We make an initial pass through the buffer and identify all of
439 * the descriptors it contains, keeping track for each its type
440 * and the location size of its data in the buffer.
441 *
d393c98f 442 * Next we scan the descriptors, looking for an interface descriptor;
b09c94a1
AE
443 * there must be exactly one of those. When found, we record the
444 * information it contains, and then remove that descriptor (and any
445 * string descriptors it refers to) from further consideration.
446 *
4ab9b3c2 447 * After that we look for the interface's bundles--there must be at
b09c94a1
AE
448 * least one of those.
449 *
937d0da8 450 * Returns true if parsing was successful, false otherwise.
b09c94a1 451 */
4ab9b3c2 452bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
b09c94a1
AE
453{
454 struct greybus_manifest *manifest;
455 struct greybus_manifest_header *header;
456 struct greybus_descriptor *desc;
457 struct manifest_desc *descriptor;
a93db2d1 458 struct manifest_desc *interface_desc = NULL;
b09c94a1
AE
459 u16 manifest_size;
460 u32 found = 0;
43d9431f 461 bool result;
b09c94a1 462
1dd90df4 463 /* Manifest descriptor list should be empty here */
86cad666 464 if (WARN_ON(!list_empty(&intf->manifest_descs)))
1dd90df4
VK
465 return false;
466
b09c94a1 467 /* we have to have at _least_ the manifest header */
d393c98f 468 if (size < sizeof(*header)) {
a0b5542d 469 dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
ee989b02 470 size, sizeof(*header));
937d0da8 471 return false;
b09c94a1
AE
472 }
473
474 /* Make sure the size is right */
475 manifest = data;
476 header = &manifest->header;
477 manifest_size = le16_to_cpu(header->size);
478 if (manifest_size != size) {
a0b5542d 479 dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
ee989b02 480 size, manifest_size);
937d0da8 481 return false;
b09c94a1
AE
482 }
483
484 /* Validate major/minor number */
485 if (header->version_major > GREYBUS_VERSION_MAJOR) {
a0b5542d 486 dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
ee989b02
GKH
487 header->version_major, header->version_minor,
488 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
937d0da8 489 return false;
b09c94a1
AE
490 }
491
492 /* OK, find all the descriptors */
fc25d906 493 desc = manifest->descriptors;
b09c94a1
AE
494 size -= sizeof(*header);
495 while (size) {
496 int desc_size;
497
86cad666 498 desc_size = identify_descriptor(intf, desc, size);
13fe6a9a 499 if (desc_size < 0) {
ff8aed52 500 result = false;
937d0da8 501 goto out;
b09c94a1
AE
502 }
503 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
504 size -= desc_size;
86bf33af 505 }
b09c94a1 506
a93db2d1 507 /* There must be a single interface descriptor */
86cad666 508 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
a93db2d1 509 if (descriptor->type == GREYBUS_TYPE_INTERFACE)
86bf33af 510 if (!found++)
a93db2d1 511 interface_desc = descriptor;
86bf33af
GKH
512 }
513 if (found != 1) {
a0b5542d 514 dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
ee989b02 515 found);
86bf33af
GKH
516 result = false;
517 goto out;
b09c94a1
AE
518 }
519
a93db2d1
VK
520 /* Parse the manifest, starting with the interface descriptor */
521 result = gb_manifest_parse_interface(intf, interface_desc);
b09c94a1
AE
522
523 /*
524 * We really should have no remaining descriptors, but we
525 * don't know what newer format manifests might leave.
526 */
86cad666 527 if (result && !list_empty(&intf->manifest_descs))
a0b5542d 528 dev_info(&intf->dev, "excess descriptors in interface manifest\n");
937d0da8 529out:
86cad666 530 release_manifest_descriptors(intf);
b09c94a1 531
ff8aed52 532 return result;
b09c94a1 533}