blob: 1e907dd3b4fe4e50385a458994a4ef4be9d8b70e [file] [log] [blame]
Kees Cook0a8adf52014-07-14 14:38:12 -07001/*
2 * This module provides an interface to trigger and test firmware loading.
3 *
4 * It is designed to be used for basic evaluation of the firmware loading
5 * subsystem (for example when validating firmware verification). It lacks
6 * any extra dependencies, and will not normally be loaded by the system
7 * unless explicitly requested by name.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/printk.h>
Brian Norriseb910942015-12-09 14:50:27 -080015#include <linux/completion.h>
Kees Cook0a8adf52014-07-14 14:38:12 -070016#include <linux/firmware.h>
17#include <linux/device.h>
18#include <linux/fs.h>
19#include <linux/miscdevice.h>
20#include <linux/slab.h>
21#include <linux/uaccess.h>
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -070022#include <linux/delay.h>
23#include <linux/kthread.h>
24
25#define TEST_FIRMWARE_NAME "test-firmware.bin"
26#define TEST_FIRMWARE_NUM_REQS 4
Kees Cook0a8adf52014-07-14 14:38:12 -070027
28static DEFINE_MUTEX(test_fw_mutex);
29static const struct firmware *test_firmware;
30
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -070031struct test_batched_req {
32 u8 idx;
33 int rc;
34 bool sent;
35 const struct firmware *fw;
36 const char *name;
37 struct completion completion;
38 struct task_struct *task;
39 struct device *dev;
40};
41
42/**
43 * test_config - represents configuration for the test for different triggers
44 *
45 * @name: the name of the firmware file to look for
46 * @sync_direct: when the sync trigger is used if this is true
47 * request_firmware_direct() will be used instead.
48 * @send_uevent: whether or not to send a uevent for async requests
49 * @num_requests: number of requests to try per test case. This is trigger
50 * specific.
51 * @reqs: stores all requests information
52 * @read_fw_idx: index of thread from which we want to read firmware results
53 * from through the read_fw trigger.
54 * @test_result: a test may use this to collect the result from the call
55 * of the request_firmware*() calls used in their tests. In order of
56 * priority we always keep first any setup error. If no setup errors were
57 * found then we move on to the first error encountered while running the
58 * API. Note that for async calls this typically will be a successful
59 * result (0) unless of course you've used bogus parameters, or the system
60 * is out of memory. In the async case the callback is expected to do a
61 * bit more homework to figure out what happened, unfortunately the only
62 * information passed today on error is the fact that no firmware was
63 * found so we can only assume -ENOENT on async calls if the firmware is
64 * NULL.
65 *
66 * Errors you can expect:
67 *
68 * API specific:
69 *
70 * 0: success for sync, for async it means request was sent
71 * -EINVAL: invalid parameters or request
72 * -ENOENT: files not found
73 *
74 * System environment:
75 *
76 * -ENOMEM: memory pressure on system
77 * -ENODEV: out of number of devices to test
78 * -EINVAL: an unexpected error has occurred
79 * @req_firmware: if @sync_direct is true this is set to
80 * request_firmware_direct(), otherwise request_firmware()
81 */
82struct test_config {
83 char *name;
84 bool sync_direct;
85 bool send_uevent;
86 u8 num_requests;
87 u8 read_fw_idx;
88
89 /*
90 * These below don't belong her but we'll move them once we create
91 * a struct fw_test_device and stuff the misc_dev under there later.
92 */
93 struct test_batched_req *reqs;
94 int test_result;
95 int (*req_firmware)(const struct firmware **fw, const char *name,
96 struct device *device);
97};
98
Wei Yongjun76f8ab12018-01-11 11:13:45 +000099static struct test_config *test_fw_config;
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700100
Kees Cook0a8adf52014-07-14 14:38:12 -0700101static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
102 size_t size, loff_t *offset)
103{
104 ssize_t rc = 0;
105
106 mutex_lock(&test_fw_mutex);
107 if (test_firmware)
108 rc = simple_read_from_buffer(buf, size, offset,
109 test_firmware->data,
110 test_firmware->size);
111 mutex_unlock(&test_fw_mutex);
112 return rc;
113}
114
115static const struct file_operations test_fw_fops = {
116 .owner = THIS_MODULE,
117 .read = test_fw_misc_read,
118};
119
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700120static void __test_release_all_firmware(void)
121{
122 struct test_batched_req *req;
123 u8 i;
124
125 if (!test_fw_config->reqs)
126 return;
127
128 for (i = 0; i < test_fw_config->num_requests; i++) {
129 req = &test_fw_config->reqs[i];
130 if (req->fw)
131 release_firmware(req->fw);
132 }
133
134 vfree(test_fw_config->reqs);
135 test_fw_config->reqs = NULL;
136}
137
138static void test_release_all_firmware(void)
139{
140 mutex_lock(&test_fw_mutex);
141 __test_release_all_firmware();
142 mutex_unlock(&test_fw_mutex);
143}
144
145
146static void __test_firmware_config_free(void)
147{
148 __test_release_all_firmware();
149 kfree_const(test_fw_config->name);
150 test_fw_config->name = NULL;
151}
152
153/*
154 * XXX: move to kstrncpy() once merged.
155 *
156 * Users should use kfree_const() when freeing these.
157 */
158static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
159{
160 *dst = kstrndup(name, count, gfp);
161 if (!*dst)
162 return -ENOSPC;
163 return count;
164}
165
166static int __test_firmware_config_init(void)
167{
168 int ret;
169
170 ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
171 strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
172 if (ret < 0)
173 goto out;
174
175 test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
176 test_fw_config->send_uevent = true;
177 test_fw_config->sync_direct = false;
178 test_fw_config->req_firmware = request_firmware;
179 test_fw_config->test_result = 0;
180 test_fw_config->reqs = NULL;
181
182 return 0;
183
184out:
185 __test_firmware_config_free();
186 return ret;
187}
188
189static ssize_t reset_store(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
192{
193 int ret;
194
195 mutex_lock(&test_fw_mutex);
196
197 __test_firmware_config_free();
198
199 ret = __test_firmware_config_init();
200 if (ret < 0) {
201 ret = -ENOMEM;
202 pr_err("could not alloc settings for config trigger: %d\n",
203 ret);
204 goto out;
205 }
206
207 pr_info("reset\n");
208 ret = count;
209
210out:
211 mutex_unlock(&test_fw_mutex);
212
213 return ret;
214}
215static DEVICE_ATTR_WO(reset);
216
217static ssize_t config_show(struct device *dev,
218 struct device_attribute *attr,
219 char *buf)
220{
221 int len = 0;
222
223 mutex_lock(&test_fw_mutex);
224
225 len += snprintf(buf, PAGE_SIZE,
226 "Custom trigger configuration for: %s\n",
227 dev_name(dev));
228
229 if (test_fw_config->name)
230 len += snprintf(buf+len, PAGE_SIZE,
231 "name:\t%s\n",
232 test_fw_config->name);
233 else
234 len += snprintf(buf+len, PAGE_SIZE,
235 "name:\tEMTPY\n");
236
237 len += snprintf(buf+len, PAGE_SIZE,
238 "num_requests:\t%u\n", test_fw_config->num_requests);
239
240 len += snprintf(buf+len, PAGE_SIZE,
241 "send_uevent:\t\t%s\n",
242 test_fw_config->send_uevent ?
243 "FW_ACTION_HOTPLUG" :
244 "FW_ACTION_NOHOTPLUG");
245 len += snprintf(buf+len, PAGE_SIZE,
246 "sync_direct:\t\t%s\n",
247 test_fw_config->sync_direct ? "true" : "false");
248 len += snprintf(buf+len, PAGE_SIZE,
249 "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
250
251 mutex_unlock(&test_fw_mutex);
252
253 return len;
254}
255static DEVICE_ATTR_RO(config);
256
257static ssize_t config_name_store(struct device *dev,
258 struct device_attribute *attr,
259 const char *buf, size_t count)
260{
261 int ret;
262
263 mutex_lock(&test_fw_mutex);
264 kfree_const(test_fw_config->name);
265 ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
266 mutex_unlock(&test_fw_mutex);
267
268 return ret;
269}
270
271/*
272 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
273 */
274static ssize_t config_test_show_str(char *dst,
275 char *src)
276{
277 int len;
278
279 mutex_lock(&test_fw_mutex);
280 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
281 mutex_unlock(&test_fw_mutex);
282
283 return len;
284}
285
286static int test_dev_config_update_bool(const char *buf, size_t size,
287 bool *cfg)
288{
289 int ret;
290
291 mutex_lock(&test_fw_mutex);
292 if (strtobool(buf, cfg) < 0)
293 ret = -EINVAL;
294 else
295 ret = size;
296 mutex_unlock(&test_fw_mutex);
297
298 return ret;
299}
300
301static ssize_t
302test_dev_config_show_bool(char *buf,
303 bool config)
304{
305 bool val;
306
307 mutex_lock(&test_fw_mutex);
308 val = config;
309 mutex_unlock(&test_fw_mutex);
310
311 return snprintf(buf, PAGE_SIZE, "%d\n", val);
312}
313
314static ssize_t test_dev_config_show_int(char *buf, int cfg)
315{
316 int val;
317
318 mutex_lock(&test_fw_mutex);
319 val = cfg;
320 mutex_unlock(&test_fw_mutex);
321
322 return snprintf(buf, PAGE_SIZE, "%d\n", val);
323}
324
325static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
326{
327 int ret;
328 long new;
329
330 ret = kstrtol(buf, 10, &new);
331 if (ret)
332 return ret;
333
334 if (new > U8_MAX)
335 return -EINVAL;
336
337 mutex_lock(&test_fw_mutex);
338 *(u8 *)cfg = new;
339 mutex_unlock(&test_fw_mutex);
340
341 /* Always return full write size even if we didn't consume all */
342 return size;
343}
344
345static ssize_t test_dev_config_show_u8(char *buf, u8 cfg)
346{
347 u8 val;
348
349 mutex_lock(&test_fw_mutex);
350 val = cfg;
351 mutex_unlock(&test_fw_mutex);
352
353 return snprintf(buf, PAGE_SIZE, "%u\n", val);
354}
355
356static ssize_t config_name_show(struct device *dev,
357 struct device_attribute *attr,
358 char *buf)
359{
360 return config_test_show_str(buf, test_fw_config->name);
361}
Joe Perchesb6b996b2017-12-19 10:15:07 -0800362static DEVICE_ATTR_RW(config_name);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700363
364static ssize_t config_num_requests_store(struct device *dev,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
367{
368 int rc;
369
370 mutex_lock(&test_fw_mutex);
371 if (test_fw_config->reqs) {
372 pr_err("Must call release_all_firmware prior to changing config\n");
373 rc = -EINVAL;
374 goto out;
375 }
376 mutex_unlock(&test_fw_mutex);
377
378 rc = test_dev_config_update_u8(buf, count,
379 &test_fw_config->num_requests);
380
381out:
382 return rc;
383}
384
385static ssize_t config_num_requests_show(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
388{
389 return test_dev_config_show_u8(buf, test_fw_config->num_requests);
390}
Joe Perchesb6b996b2017-12-19 10:15:07 -0800391static DEVICE_ATTR_RW(config_num_requests);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700392
393static ssize_t config_sync_direct_store(struct device *dev,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
396{
397 int rc = test_dev_config_update_bool(buf, count,
398 &test_fw_config->sync_direct);
399
400 if (rc == count)
401 test_fw_config->req_firmware = test_fw_config->sync_direct ?
402 request_firmware_direct :
403 request_firmware;
404 return rc;
405}
406
407static ssize_t config_sync_direct_show(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
410{
411 return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
412}
Joe Perchesb6b996b2017-12-19 10:15:07 -0800413static DEVICE_ATTR_RW(config_sync_direct);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700414
415static ssize_t config_send_uevent_store(struct device *dev,
416 struct device_attribute *attr,
417 const char *buf, size_t count)
418{
419 return test_dev_config_update_bool(buf, count,
420 &test_fw_config->send_uevent);
421}
422
423static ssize_t config_send_uevent_show(struct device *dev,
424 struct device_attribute *attr,
425 char *buf)
426{
427 return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
428}
Joe Perchesb6b996b2017-12-19 10:15:07 -0800429static DEVICE_ATTR_RW(config_send_uevent);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700430
431static ssize_t config_read_fw_idx_store(struct device *dev,
432 struct device_attribute *attr,
433 const char *buf, size_t count)
434{
435 return test_dev_config_update_u8(buf, count,
436 &test_fw_config->read_fw_idx);
437}
438
439static ssize_t config_read_fw_idx_show(struct device *dev,
440 struct device_attribute *attr,
441 char *buf)
442{
443 return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
444}
Joe Perchesb6b996b2017-12-19 10:15:07 -0800445static DEVICE_ATTR_RW(config_read_fw_idx);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700446
447
Kees Cook0a8adf52014-07-14 14:38:12 -0700448static ssize_t trigger_request_store(struct device *dev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
451{
452 int rc;
453 char *name;
454
Brian Norrisbe4a1322015-12-09 14:50:26 -0800455 name = kstrndup(buf, count, GFP_KERNEL);
Kees Cook0a8adf52014-07-14 14:38:12 -0700456 if (!name)
457 return -ENOSPC;
Kees Cook0a8adf52014-07-14 14:38:12 -0700458
459 pr_info("loading '%s'\n", name);
460
461 mutex_lock(&test_fw_mutex);
462 release_firmware(test_firmware);
463 test_firmware = NULL;
464 rc = request_firmware(&test_firmware, name, dev);
Brian Norris47e0bbb2015-12-09 14:50:25 -0800465 if (rc) {
Kees Cook0a8adf52014-07-14 14:38:12 -0700466 pr_info("load of '%s' failed: %d\n", name, rc);
Brian Norris47e0bbb2015-12-09 14:50:25 -0800467 goto out;
468 }
469 pr_info("loaded: %zu\n", test_firmware->size);
470 rc = count;
471
472out:
Kees Cook0a8adf52014-07-14 14:38:12 -0700473 mutex_unlock(&test_fw_mutex);
474
475 kfree(name);
476
Brian Norris47e0bbb2015-12-09 14:50:25 -0800477 return rc;
Kees Cook0a8adf52014-07-14 14:38:12 -0700478}
479static DEVICE_ATTR_WO(trigger_request);
480
Brian Norriseb910942015-12-09 14:50:27 -0800481static DECLARE_COMPLETION(async_fw_done);
482
483static void trigger_async_request_cb(const struct firmware *fw, void *context)
484{
485 test_firmware = fw;
486 complete(&async_fw_done);
487}
488
489static ssize_t trigger_async_request_store(struct device *dev,
490 struct device_attribute *attr,
491 const char *buf, size_t count)
492{
493 int rc;
494 char *name;
495
496 name = kstrndup(buf, count, GFP_KERNEL);
497 if (!name)
498 return -ENOSPC;
499
500 pr_info("loading '%s'\n", name);
501
502 mutex_lock(&test_fw_mutex);
503 release_firmware(test_firmware);
504 test_firmware = NULL;
505 rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
506 NULL, trigger_async_request_cb);
507 if (rc) {
508 pr_info("async load of '%s' failed: %d\n", name, rc);
509 kfree(name);
510 goto out;
511 }
512 /* Free 'name' ASAP, to test for race conditions */
513 kfree(name);
514
515 wait_for_completion(&async_fw_done);
516
517 if (test_firmware) {
518 pr_info("loaded: %zu\n", test_firmware->size);
519 rc = count;
520 } else {
521 pr_err("failed to async load firmware\n");
522 rc = -ENODEV;
523 }
524
525out:
526 mutex_unlock(&test_fw_mutex);
527
528 return rc;
529}
530static DEVICE_ATTR_WO(trigger_async_request);
531
Luis R. Rodriguez061132d2017-01-23 08:11:10 -0800532static ssize_t trigger_custom_fallback_store(struct device *dev,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
535{
536 int rc;
537 char *name;
538
539 name = kstrndup(buf, count, GFP_KERNEL);
540 if (!name)
541 return -ENOSPC;
542
543 pr_info("loading '%s' using custom fallback mechanism\n", name);
544
545 mutex_lock(&test_fw_mutex);
546 release_firmware(test_firmware);
547 test_firmware = NULL;
548 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
549 dev, GFP_KERNEL, NULL,
550 trigger_async_request_cb);
551 if (rc) {
552 pr_info("async load of '%s' failed: %d\n", name, rc);
553 kfree(name);
554 goto out;
555 }
556 /* Free 'name' ASAP, to test for race conditions */
557 kfree(name);
558
559 wait_for_completion(&async_fw_done);
560
561 if (test_firmware) {
562 pr_info("loaded: %zu\n", test_firmware->size);
563 rc = count;
564 } else {
565 pr_err("failed to async load firmware\n");
566 rc = -ENODEV;
567 }
568
569out:
570 mutex_unlock(&test_fw_mutex);
571
572 return rc;
573}
574static DEVICE_ATTR_WO(trigger_custom_fallback);
575
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700576static int test_fw_run_batch_request(void *data)
577{
578 struct test_batched_req *req = data;
579
580 if (!req) {
581 test_fw_config->test_result = -EINVAL;
582 return -EINVAL;
583 }
584
585 req->rc = test_fw_config->req_firmware(&req->fw, req->name, req->dev);
586 if (req->rc) {
587 pr_info("#%u: batched sync load failed: %d\n",
588 req->idx, req->rc);
589 if (!test_fw_config->test_result)
590 test_fw_config->test_result = req->rc;
591 } else if (req->fw) {
592 req->sent = true;
593 pr_info("#%u: batched sync loaded %zu\n",
594 req->idx, req->fw->size);
595 }
596 complete(&req->completion);
597
598 req->task = NULL;
599
600 return 0;
601}
602
603/*
604 * We use a kthread as otherwise the kernel serializes all our sync requests
605 * and we would not be able to mimic batched requests on a sync call. Batched
606 * requests on a sync call can for instance happen on a device driver when
607 * multiple cards are used and firmware loading happens outside of probe.
608 */
609static ssize_t trigger_batched_requests_store(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf, size_t count)
612{
613 struct test_batched_req *req;
614 int rc;
615 u8 i;
616
617 mutex_lock(&test_fw_mutex);
618
619 test_fw_config->reqs = vzalloc(sizeof(struct test_batched_req) *
620 test_fw_config->num_requests * 2);
621 if (!test_fw_config->reqs) {
622 rc = -ENOMEM;
623 goto out_unlock;
624 }
625
626 pr_info("batched sync firmware loading '%s' %u times\n",
627 test_fw_config->name, test_fw_config->num_requests);
628
629 for (i = 0; i < test_fw_config->num_requests; i++) {
630 req = &test_fw_config->reqs[i];
631 if (!req) {
632 WARN_ON(1);
633 rc = -ENOMEM;
634 goto out_bail;
635 }
636 req->fw = NULL;
637 req->idx = i;
638 req->name = test_fw_config->name;
639 req->dev = dev;
640 init_completion(&req->completion);
641 req->task = kthread_run(test_fw_run_batch_request, req,
642 "%s-%u", KBUILD_MODNAME, req->idx);
643 if (!req->task || IS_ERR(req->task)) {
644 pr_err("Setting up thread %u failed\n", req->idx);
645 req->task = NULL;
646 rc = -ENOMEM;
647 goto out_bail;
648 }
649 }
650
651 rc = count;
652
653 /*
654 * We require an explicit release to enable more time and delay of
655 * calling release_firmware() to improve our chances of forcing a
656 * batched request. If we instead called release_firmware() right away
657 * then we might miss on an opportunity of having a successful firmware
658 * request pass on the opportunity to be come a batched request.
659 */
660
661out_bail:
662 for (i = 0; i < test_fw_config->num_requests; i++) {
663 req = &test_fw_config->reqs[i];
664 if (req->task || req->sent)
665 wait_for_completion(&req->completion);
666 }
667
668 /* Override any worker error if we had a general setup error */
669 if (rc < 0)
670 test_fw_config->test_result = rc;
671
672out_unlock:
673 mutex_unlock(&test_fw_mutex);
674
675 return rc;
676}
677static DEVICE_ATTR_WO(trigger_batched_requests);
678
679/*
680 * We wait for each callback to return with the lock held, no need to lock here
681 */
682static void trigger_batched_cb(const struct firmware *fw, void *context)
683{
684 struct test_batched_req *req = context;
685
686 if (!req) {
687 test_fw_config->test_result = -EINVAL;
688 return;
689 }
690
691 /* forces *some* batched requests to queue up */
692 if (!req->idx)
693 ssleep(2);
694
695 req->fw = fw;
696
697 /*
698 * Unfortunately the firmware API gives us nothing other than a null FW
699 * if the firmware was not found on async requests. Best we can do is
700 * just assume -ENOENT. A better API would pass the actual return
701 * value to the callback.
702 */
703 if (!fw && !test_fw_config->test_result)
704 test_fw_config->test_result = -ENOENT;
705
706 complete(&req->completion);
707}
708
709static
710ssize_t trigger_batched_requests_async_store(struct device *dev,
711 struct device_attribute *attr,
712 const char *buf, size_t count)
713{
714 struct test_batched_req *req;
715 bool send_uevent;
716 int rc;
717 u8 i;
718
719 mutex_lock(&test_fw_mutex);
720
721 test_fw_config->reqs = vzalloc(sizeof(struct test_batched_req) *
722 test_fw_config->num_requests * 2);
723 if (!test_fw_config->reqs) {
724 rc = -ENOMEM;
725 goto out;
726 }
727
728 pr_info("batched loading '%s' custom fallback mechanism %u times\n",
729 test_fw_config->name, test_fw_config->num_requests);
730
731 send_uevent = test_fw_config->send_uevent ? FW_ACTION_HOTPLUG :
732 FW_ACTION_NOHOTPLUG;
733
734 for (i = 0; i < test_fw_config->num_requests; i++) {
735 req = &test_fw_config->reqs[i];
736 if (!req) {
737 WARN_ON(1);
738 goto out_bail;
739 }
740 req->name = test_fw_config->name;
741 req->fw = NULL;
742 req->idx = i;
743 init_completion(&req->completion);
744 rc = request_firmware_nowait(THIS_MODULE, send_uevent,
745 req->name,
746 dev, GFP_KERNEL, req,
747 trigger_batched_cb);
748 if (rc) {
749 pr_info("#%u: batched async load failed setup: %d\n",
750 i, rc);
751 req->rc = rc;
752 goto out_bail;
753 } else
754 req->sent = true;
755 }
756
757 rc = count;
758
759out_bail:
760
761 /*
762 * We require an explicit release to enable more time and delay of
763 * calling release_firmware() to improve our chances of forcing a
764 * batched request. If we instead called release_firmware() right away
765 * then we might miss on an opportunity of having a successful firmware
766 * request pass on the opportunity to be come a batched request.
767 */
768
769 for (i = 0; i < test_fw_config->num_requests; i++) {
770 req = &test_fw_config->reqs[i];
771 if (req->sent)
772 wait_for_completion(&req->completion);
773 }
774
775 /* Override any worker error if we had a general setup error */
776 if (rc < 0)
777 test_fw_config->test_result = rc;
778
779out:
780 mutex_unlock(&test_fw_mutex);
781
782 return rc;
783}
784static DEVICE_ATTR_WO(trigger_batched_requests_async);
785
786static ssize_t test_result_show(struct device *dev,
787 struct device_attribute *attr,
788 char *buf)
789{
790 return test_dev_config_show_int(buf, test_fw_config->test_result);
791}
792static DEVICE_ATTR_RO(test_result);
793
794static ssize_t release_all_firmware_store(struct device *dev,
795 struct device_attribute *attr,
796 const char *buf, size_t count)
797{
798 test_release_all_firmware();
799 return count;
800}
801static DEVICE_ATTR_WO(release_all_firmware);
802
803static ssize_t read_firmware_show(struct device *dev,
804 struct device_attribute *attr,
805 char *buf)
806{
807 struct test_batched_req *req;
808 u8 idx;
809 ssize_t rc = 0;
810
811 mutex_lock(&test_fw_mutex);
812
813 idx = test_fw_config->read_fw_idx;
814 if (idx >= test_fw_config->num_requests) {
815 rc = -ERANGE;
816 goto out;
817 }
818
819 if (!test_fw_config->reqs) {
820 rc = -EINVAL;
821 goto out;
822 }
823
824 req = &test_fw_config->reqs[idx];
825 if (!req->fw) {
826 pr_err("#%u: failed to async load firmware\n", idx);
827 rc = -ENOENT;
828 goto out;
829 }
830
831 pr_info("#%u: loaded %zu\n", idx, req->fw->size);
832
833 if (req->fw->size > PAGE_SIZE) {
834 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
835 rc = -EINVAL;
836 }
837 memcpy(buf, req->fw->data, req->fw->size);
838
839 rc = req->fw->size;
840out:
841 mutex_unlock(&test_fw_mutex);
842
843 return rc;
844}
845static DEVICE_ATTR_RO(read_firmware);
846
Luis R. Rodriguez083a93b2017-01-23 08:11:06 -0800847#define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
848
849static struct attribute *test_dev_attrs[] = {
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700850 TEST_FW_DEV_ATTR(reset),
851
852 TEST_FW_DEV_ATTR(config),
853 TEST_FW_DEV_ATTR(config_name),
854 TEST_FW_DEV_ATTR(config_num_requests),
855 TEST_FW_DEV_ATTR(config_sync_direct),
856 TEST_FW_DEV_ATTR(config_send_uevent),
857 TEST_FW_DEV_ATTR(config_read_fw_idx),
858
859 /* These don't use the config at all - they could be ported! */
Luis R. Rodriguez083a93b2017-01-23 08:11:06 -0800860 TEST_FW_DEV_ATTR(trigger_request),
861 TEST_FW_DEV_ATTR(trigger_async_request),
Luis R. Rodriguez061132d2017-01-23 08:11:10 -0800862 TEST_FW_DEV_ATTR(trigger_custom_fallback),
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700863
864 /* These use the config and can use the test_result */
865 TEST_FW_DEV_ATTR(trigger_batched_requests),
866 TEST_FW_DEV_ATTR(trigger_batched_requests_async),
867
868 TEST_FW_DEV_ATTR(release_all_firmware),
869 TEST_FW_DEV_ATTR(test_result),
870 TEST_FW_DEV_ATTR(read_firmware),
Luis R. Rodriguez083a93b2017-01-23 08:11:06 -0800871 NULL,
872};
873
874ATTRIBUTE_GROUPS(test_dev);
875
Luis R. Rodriguez67fd5532017-01-23 08:11:05 -0800876static struct miscdevice test_fw_misc_device = {
877 .minor = MISC_DYNAMIC_MINOR,
878 .name = "test_firmware",
879 .fops = &test_fw_fops,
Luis R. Rodriguez083a93b2017-01-23 08:11:06 -0800880 .groups = test_dev_groups,
Luis R. Rodriguez67fd5532017-01-23 08:11:05 -0800881};
882
Kees Cook0a8adf52014-07-14 14:38:12 -0700883static int __init test_firmware_init(void)
884{
885 int rc;
886
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700887 test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
888 if (!test_fw_config)
889 return -ENOMEM;
890
891 rc = __test_firmware_config_init();
892 if (rc)
893 return rc;
894
Kees Cook0a8adf52014-07-14 14:38:12 -0700895 rc = misc_register(&test_fw_misc_device);
896 if (rc) {
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700897 kfree(test_fw_config);
Kees Cook0a8adf52014-07-14 14:38:12 -0700898 pr_err("could not register misc device: %d\n", rc);
899 return rc;
900 }
Brian Norriseb910942015-12-09 14:50:27 -0800901
Kees Cook0a8adf52014-07-14 14:38:12 -0700902 pr_warn("interface ready\n");
903
904 return 0;
Kees Cook0a8adf52014-07-14 14:38:12 -0700905}
906
907module_init(test_firmware_init);
908
909static void __exit test_firmware_exit(void)
910{
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700911 mutex_lock(&test_fw_mutex);
Kees Cook0a8adf52014-07-14 14:38:12 -0700912 release_firmware(test_firmware);
Kees Cook0a8adf52014-07-14 14:38:12 -0700913 misc_deregister(&test_fw_misc_device);
Luis R. Rodriguezc92316b2017-07-20 13:13:42 -0700914 __test_firmware_config_free();
915 kfree(test_fw_config);
916 mutex_unlock(&test_fw_mutex);
917
Kees Cook0a8adf52014-07-14 14:38:12 -0700918 pr_warn("removed interface\n");
919}
920
921module_exit(test_firmware_exit);
922
923MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
924MODULE_LICENSE("GPL");