7e12732f470548a5b6c2384f9d90cd9ea704af8b
[linux-2.6-block.git] / drivers / base / firmware_loader / main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * main.c - Multi purpose firmware loading support
4  *
5  * Copyright (c) 2003 Manuel Estrada Sainz
6  *
7  * Please see Documentation/firmware_class/ for more information.
8  *
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/capability.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/timer.h>
18 #include <linux/vmalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/bitops.h>
21 #include <linux/mutex.h>
22 #include <linux/workqueue.h>
23 #include <linux/highmem.h>
24 #include <linux/firmware.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/file.h>
28 #include <linux/list.h>
29 #include <linux/fs.h>
30 #include <linux/async.h>
31 #include <linux/pm.h>
32 #include <linux/suspend.h>
33 #include <linux/syscore_ops.h>
34 #include <linux/reboot.h>
35 #include <linux/security.h>
36
37 #include <generated/utsrelease.h>
38
39 #include "../base.h"
40 #include "firmware.h"
41 #include "fallback.h"
42
43 MODULE_AUTHOR("Manuel Estrada Sainz");
44 MODULE_DESCRIPTION("Multi purpose firmware loading support");
45 MODULE_LICENSE("GPL");
46
47 struct firmware_cache {
48         /* firmware_buf instance will be added into the below list */
49         spinlock_t lock;
50         struct list_head head;
51         int state;
52
53 #ifdef CONFIG_PM_SLEEP
54         /*
55          * Names of firmware images which have been cached successfully
56          * will be added into the below list so that device uncache
57          * helper can trace which firmware images have been cached
58          * before.
59          */
60         spinlock_t name_lock;
61         struct list_head fw_names;
62
63         struct delayed_work work;
64
65         struct notifier_block   pm_notify;
66 #endif
67 };
68
69 struct fw_cache_entry {
70         struct list_head list;
71         const char *name;
72 };
73
74 struct fw_name_devm {
75         unsigned long magic;
76         const char *name;
77 };
78
79 static inline struct fw_priv *to_fw_priv(struct kref *ref)
80 {
81         return container_of(ref, struct fw_priv, ref);
82 }
83
84 #define FW_LOADER_NO_CACHE      0
85 #define FW_LOADER_START_CACHE   1
86
87 /* fw_lock could be moved to 'struct fw_sysfs' but since it is just
88  * guarding for corner cases a global lock should be OK */
89 DEFINE_MUTEX(fw_lock);
90
91 static struct firmware_cache fw_cache;
92
93 /* Builtin firmware support */
94
95 #ifdef CONFIG_FW_LOADER
96
97 extern struct builtin_fw __start_builtin_fw[];
98 extern struct builtin_fw __end_builtin_fw[];
99
100 static void fw_copy_to_prealloc_buf(struct firmware *fw,
101                                     void *buf, size_t size)
102 {
103         if (!buf || size < fw->size)
104                 return;
105         memcpy(buf, fw->data, fw->size);
106 }
107
108 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
109                                     void *buf, size_t size)
110 {
111         struct builtin_fw *b_fw;
112
113         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
114                 if (strcmp(name, b_fw->name) == 0) {
115                         fw->size = b_fw->size;
116                         fw->data = b_fw->data;
117                         fw_copy_to_prealloc_buf(fw, buf, size);
118
119                         return true;
120                 }
121         }
122
123         return false;
124 }
125
126 static bool fw_is_builtin_firmware(const struct firmware *fw)
127 {
128         struct builtin_fw *b_fw;
129
130         for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
131                 if (fw->data == b_fw->data)
132                         return true;
133
134         return false;
135 }
136
137 #else /* Module case - no builtin firmware support */
138
139 static inline bool fw_get_builtin_firmware(struct firmware *fw,
140                                            const char *name, void *buf,
141                                            size_t size)
142 {
143         return false;
144 }
145
146 static inline bool fw_is_builtin_firmware(const struct firmware *fw)
147 {
148         return false;
149 }
150 #endif
151
152 static void fw_state_init(struct fw_priv *fw_priv)
153 {
154         struct fw_state *fw_st = &fw_priv->fw_st;
155
156         init_completion(&fw_st->completion);
157         fw_st->status = FW_STATUS_UNKNOWN;
158 }
159
160 static inline int fw_state_wait(struct fw_priv *fw_priv)
161 {
162         return __fw_state_wait_common(fw_priv, MAX_SCHEDULE_TIMEOUT);
163 }
164
165 static int fw_cache_piggyback_on_request(const char *name);
166
167 static struct fw_priv *__allocate_fw_priv(const char *fw_name,
168                                           struct firmware_cache *fwc,
169                                           void *dbuf, size_t size)
170 {
171         struct fw_priv *fw_priv;
172
173         fw_priv = kzalloc(sizeof(*fw_priv), GFP_ATOMIC);
174         if (!fw_priv)
175                 return NULL;
176
177         fw_priv->fw_name = kstrdup_const(fw_name, GFP_ATOMIC);
178         if (!fw_priv->fw_name) {
179                 kfree(fw_priv);
180                 return NULL;
181         }
182
183         kref_init(&fw_priv->ref);
184         fw_priv->fwc = fwc;
185         fw_priv->data = dbuf;
186         fw_priv->allocated_size = size;
187         fw_state_init(fw_priv);
188 #ifdef CONFIG_FW_LOADER_USER_HELPER
189         INIT_LIST_HEAD(&fw_priv->pending_list);
190 #endif
191
192         pr_debug("%s: fw-%s fw_priv=%p\n", __func__, fw_name, fw_priv);
193
194         return fw_priv;
195 }
196
197 static struct fw_priv *__lookup_fw_priv(const char *fw_name)
198 {
199         struct fw_priv *tmp;
200         struct firmware_cache *fwc = &fw_cache;
201
202         list_for_each_entry(tmp, &fwc->head, list)
203                 if (!strcmp(tmp->fw_name, fw_name))
204                         return tmp;
205         return NULL;
206 }
207
208 /* Returns 1 for batching firmware requests with the same name */
209 static int alloc_lookup_fw_priv(const char *fw_name,
210                                 struct firmware_cache *fwc,
211                                 struct fw_priv **fw_priv, void *dbuf,
212                                 size_t size, enum fw_opt opt_flags)
213 {
214         struct fw_priv *tmp;
215
216         spin_lock(&fwc->lock);
217         if (!(opt_flags & FW_OPT_NOCACHE)) {
218                 tmp = __lookup_fw_priv(fw_name);
219                 if (tmp) {
220                         kref_get(&tmp->ref);
221                         spin_unlock(&fwc->lock);
222                         *fw_priv = tmp;
223                         pr_debug("batched request - sharing the same struct fw_priv and lookup for multiple requests\n");
224                         return 1;
225                 }
226         }
227
228         tmp = __allocate_fw_priv(fw_name, fwc, dbuf, size);
229         if (tmp) {
230                 INIT_LIST_HEAD(&tmp->list);
231                 if (!(opt_flags & FW_OPT_NOCACHE))
232                         list_add(&tmp->list, &fwc->head);
233         }
234         spin_unlock(&fwc->lock);
235
236         *fw_priv = tmp;
237
238         return tmp ? 0 : -ENOMEM;
239 }
240
241 static void __free_fw_priv(struct kref *ref)
242         __releases(&fwc->lock)
243 {
244         struct fw_priv *fw_priv = to_fw_priv(ref);
245         struct firmware_cache *fwc = fw_priv->fwc;
246
247         pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
248                  __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
249                  (unsigned int)fw_priv->size);
250
251         list_del(&fw_priv->list);
252         spin_unlock(&fwc->lock);
253
254         fw_free_paged_buf(fw_priv); /* free leftover pages */
255         if (!fw_priv->allocated_size)
256                 vfree(fw_priv->data);
257         kfree_const(fw_priv->fw_name);
258         kfree(fw_priv);
259 }
260
261 static void free_fw_priv(struct fw_priv *fw_priv)
262 {
263         struct firmware_cache *fwc = fw_priv->fwc;
264         spin_lock(&fwc->lock);
265         if (!kref_put(&fw_priv->ref, __free_fw_priv))
266                 spin_unlock(&fwc->lock);
267 }
268
269 #ifdef CONFIG_FW_LOADER_USER_HELPER
270 void fw_free_paged_buf(struct fw_priv *fw_priv)
271 {
272         int i;
273
274         if (!fw_priv->pages)
275                 return;
276
277         for (i = 0; i < fw_priv->nr_pages; i++)
278                 __free_page(fw_priv->pages[i]);
279         kvfree(fw_priv->pages);
280         fw_priv->pages = NULL;
281         fw_priv->page_array_size = 0;
282         fw_priv->nr_pages = 0;
283 }
284
285 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
286 {
287         /* If the array of pages is too small, grow it */
288         if (fw_priv->page_array_size < pages_needed) {
289                 int new_array_size = max(pages_needed,
290                                          fw_priv->page_array_size * 2);
291                 struct page **new_pages;
292
293                 new_pages = kvmalloc_array(new_array_size, sizeof(void *),
294                                            GFP_KERNEL);
295                 if (!new_pages)
296                         return -ENOMEM;
297                 memcpy(new_pages, fw_priv->pages,
298                        fw_priv->page_array_size * sizeof(void *));
299                 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) *
300                        (new_array_size - fw_priv->page_array_size));
301                 kvfree(fw_priv->pages);
302                 fw_priv->pages = new_pages;
303                 fw_priv->page_array_size = new_array_size;
304         }
305
306         while (fw_priv->nr_pages < pages_needed) {
307                 fw_priv->pages[fw_priv->nr_pages] =
308                         alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
309
310                 if (!fw_priv->pages[fw_priv->nr_pages])
311                         return -ENOMEM;
312                 fw_priv->nr_pages++;
313         }
314
315         return 0;
316 }
317
318 int fw_map_paged_buf(struct fw_priv *fw_priv)
319 {
320         /* one pages buffer should be mapped/unmapped only once */
321         if (!fw_priv->pages)
322                 return 0;
323
324         vunmap(fw_priv->data);
325         fw_priv->data = vmap(fw_priv->pages, fw_priv->nr_pages, 0,
326                              PAGE_KERNEL_RO);
327         if (!fw_priv->data)
328                 return -ENOMEM;
329
330         /* page table is no longer needed after mapping, let's free */
331         kvfree(fw_priv->pages);
332         fw_priv->pages = NULL;
333
334         return 0;
335 }
336 #endif
337
338 /* direct firmware loading support */
339 static char fw_path_para[256];
340 static const char * const fw_path[] = {
341         fw_path_para,
342         "/lib/firmware/updates/" UTS_RELEASE,
343         "/lib/firmware/updates",
344         "/lib/firmware/" UTS_RELEASE,
345         "/lib/firmware"
346 };
347
348 /*
349  * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
350  * from kernel command line because firmware_class is generally built in
351  * kernel instead of module.
352  */
353 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
354 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
355
356 static int
357 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv)
358 {
359         loff_t size;
360         int i, len;
361         int rc = -ENOENT;
362         char *path;
363         enum kernel_read_file_id id = READING_FIRMWARE;
364         size_t msize = INT_MAX;
365
366         /* Already populated data member means we're loading into a buffer */
367         if (fw_priv->data) {
368                 id = READING_FIRMWARE_PREALLOC_BUFFER;
369                 msize = fw_priv->allocated_size;
370         }
371
372         path = __getname();
373         if (!path)
374                 return -ENOMEM;
375
376         for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
377                 /* skip the unset customized path */
378                 if (!fw_path[i][0])
379                         continue;
380
381                 len = snprintf(path, PATH_MAX, "%s/%s",
382                                fw_path[i], fw_priv->fw_name);
383                 if (len >= PATH_MAX) {
384                         rc = -ENAMETOOLONG;
385                         break;
386                 }
387
388                 fw_priv->size = 0;
389                 rc = kernel_read_file_from_path(path, &fw_priv->data, &size,
390                                                 msize, id);
391                 if (rc) {
392                         if (rc != -ENOENT)
393                                 dev_warn(device, "loading %s failed with error %d\n",
394                                          path, rc);
395                         else
396                                 dev_dbg(device, "loading %s failed for no such file or directory.\n",
397                                          path);
398                         continue;
399                 }
400                 dev_dbg(device, "direct-loading %s\n", fw_priv->fw_name);
401                 fw_priv->size = size;
402                 fw_state_done(fw_priv);
403                 break;
404         }
405         __putname(path);
406
407         return rc;
408 }
409
410 /* firmware holds the ownership of pages */
411 static void firmware_free_data(const struct firmware *fw)
412 {
413         /* Loaded directly? */
414         if (!fw->priv) {
415                 vfree(fw->data);
416                 return;
417         }
418         free_fw_priv(fw->priv);
419 }
420
421 /* store the pages buffer info firmware from buf */
422 static void fw_set_page_data(struct fw_priv *fw_priv, struct firmware *fw)
423 {
424         fw->priv = fw_priv;
425 #ifdef CONFIG_FW_LOADER_USER_HELPER
426         fw->pages = fw_priv->pages;
427 #endif
428         fw->size = fw_priv->size;
429         fw->data = fw_priv->data;
430
431         pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
432                  __func__, fw_priv->fw_name, fw_priv, fw_priv->data,
433                  (unsigned int)fw_priv->size);
434 }
435
436 #ifdef CONFIG_PM_SLEEP
437 static void fw_name_devm_release(struct device *dev, void *res)
438 {
439         struct fw_name_devm *fwn = res;
440
441         if (fwn->magic == (unsigned long)&fw_cache)
442                 pr_debug("%s: fw_name-%s devm-%p released\n",
443                                 __func__, fwn->name, res);
444         kfree_const(fwn->name);
445 }
446
447 static int fw_devm_match(struct device *dev, void *res,
448                 void *match_data)
449 {
450         struct fw_name_devm *fwn = res;
451
452         return (fwn->magic == (unsigned long)&fw_cache) &&
453                 !strcmp(fwn->name, match_data);
454 }
455
456 static struct fw_name_devm *fw_find_devm_name(struct device *dev,
457                 const char *name)
458 {
459         struct fw_name_devm *fwn;
460
461         fwn = devres_find(dev, fw_name_devm_release,
462                           fw_devm_match, (void *)name);
463         return fwn;
464 }
465
466 static bool fw_cache_is_setup(struct device *dev, const char *name)
467 {
468         struct fw_name_devm *fwn;
469
470         fwn = fw_find_devm_name(dev, name);
471         if (fwn)
472                 return true;
473
474         return false;
475 }
476
477 /* add firmware name into devres list */
478 static int fw_add_devm_name(struct device *dev, const char *name)
479 {
480         struct fw_name_devm *fwn;
481
482         if (fw_cache_is_setup(dev, name))
483                 return 0;
484
485         fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm),
486                            GFP_KERNEL);
487         if (!fwn)
488                 return -ENOMEM;
489         fwn->name = kstrdup_const(name, GFP_KERNEL);
490         if (!fwn->name) {
491                 devres_free(fwn);
492                 return -ENOMEM;
493         }
494
495         fwn->magic = (unsigned long)&fw_cache;
496         devres_add(dev, fwn);
497
498         return 0;
499 }
500 #else
501 static bool fw_cache_is_setup(struct device *dev, const char *name)
502 {
503         return false;
504 }
505
506 static int fw_add_devm_name(struct device *dev, const char *name)
507 {
508         return 0;
509 }
510 #endif
511
512 int assign_fw(struct firmware *fw, struct device *device,
513               enum fw_opt opt_flags)
514 {
515         struct fw_priv *fw_priv = fw->priv;
516         int ret;
517
518         mutex_lock(&fw_lock);
519         if (!fw_priv->size || fw_state_is_aborted(fw_priv)) {
520                 mutex_unlock(&fw_lock);
521                 return -ENOENT;
522         }
523
524         /*
525          * add firmware name into devres list so that we can auto cache
526          * and uncache firmware for device.
527          *
528          * device may has been deleted already, but the problem
529          * should be fixed in devres or driver core.
530          */
531         /* don't cache firmware handled without uevent */
532         if (device && (opt_flags & FW_OPT_UEVENT) &&
533             !(opt_flags & FW_OPT_NOCACHE)) {
534                 ret = fw_add_devm_name(device, fw_priv->fw_name);
535                 if (ret) {
536                         mutex_unlock(&fw_lock);
537                         return ret;
538                 }
539         }
540
541         /*
542          * After caching firmware image is started, let it piggyback
543          * on request firmware.
544          */
545         if (!(opt_flags & FW_OPT_NOCACHE) &&
546             fw_priv->fwc->state == FW_LOADER_START_CACHE) {
547                 if (fw_cache_piggyback_on_request(fw_priv->fw_name))
548                         kref_get(&fw_priv->ref);
549         }
550
551         /* pass the pages buffer to driver at the last minute */
552         fw_set_page_data(fw_priv, fw);
553         mutex_unlock(&fw_lock);
554         return 0;
555 }
556
557 /* prepare firmware and firmware_buf structs;
558  * return 0 if a firmware is already assigned, 1 if need to load one,
559  * or a negative error code
560  */
561 static int
562 _request_firmware_prepare(struct firmware **firmware_p, const char *name,
563                           struct device *device, void *dbuf, size_t size,
564                           enum fw_opt opt_flags)
565 {
566         struct firmware *firmware;
567         struct fw_priv *fw_priv;
568         int ret;
569
570         *firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
571         if (!firmware) {
572                 dev_err(device, "%s: kmalloc(struct firmware) failed\n",
573                         __func__);
574                 return -ENOMEM;
575         }
576
577         if (fw_get_builtin_firmware(firmware, name, dbuf, size)) {
578                 dev_dbg(device, "using built-in %s\n", name);
579                 return 0; /* assigned */
580         }
581
582         ret = alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, dbuf, size,
583                                   opt_flags);
584
585         /*
586          * bind with 'priv' now to avoid warning in failure path
587          * of requesting firmware.
588          */
589         firmware->priv = fw_priv;
590
591         if (ret > 0) {
592                 ret = fw_state_wait(fw_priv);
593                 if (!ret) {
594                         fw_set_page_data(fw_priv, firmware);
595                         return 0; /* assigned */
596                 }
597         }
598
599         if (ret < 0)
600                 return ret;
601         return 1; /* need to load */
602 }
603
604 /*
605  * Batched requests need only one wake, we need to do this step last due to the
606  * fallback mechanism. The buf is protected with kref_get(), and it won't be
607  * released until the last user calls release_firmware().
608  *
609  * Failed batched requests are possible as well, in such cases we just share
610  * the struct fw_priv and won't release it until all requests are woken
611  * and have gone through this same path.
612  */
613 static void fw_abort_batch_reqs(struct firmware *fw)
614 {
615         struct fw_priv *fw_priv;
616
617         /* Loaded directly? */
618         if (!fw || !fw->priv)
619                 return;
620
621         fw_priv = fw->priv;
622         if (!fw_state_is_aborted(fw_priv))
623                 fw_state_aborted(fw_priv);
624 }
625
626 /* called from request_firmware() and request_firmware_work_func() */
627 static int
628 _request_firmware(const struct firmware **firmware_p, const char *name,
629                   struct device *device, void *buf, size_t size,
630                   enum fw_opt opt_flags)
631 {
632         struct firmware *fw = NULL;
633         int ret;
634
635         if (!firmware_p)
636                 return -EINVAL;
637
638         if (!name || name[0] == '\0') {
639                 ret = -EINVAL;
640                 goto out;
641         }
642
643         ret = _request_firmware_prepare(&fw, name, device, buf, size,
644                                         opt_flags);
645         if (ret <= 0) /* error or already assigned */
646                 goto out;
647
648         ret = fw_get_filesystem_firmware(device, fw->priv);
649         if (ret) {
650                 if (!(opt_flags & FW_OPT_NO_WARN))
651                         dev_warn(device,
652                                  "Direct firmware load for %s failed with error %d\n",
653                                  name, ret);
654                 ret = firmware_fallback_sysfs(fw, name, device, opt_flags, ret);
655         } else
656                 ret = assign_fw(fw, device, opt_flags);
657
658  out:
659         if (ret < 0) {
660                 fw_abort_batch_reqs(fw);
661                 release_firmware(fw);
662                 fw = NULL;
663         }
664
665         *firmware_p = fw;
666         return ret;
667 }
668
669 /**
670  * request_firmware() - send firmware request and wait for it
671  * @firmware_p: pointer to firmware image
672  * @name: name of firmware file
673  * @device: device for which firmware is being loaded
674  *
675  *      @firmware_p will be used to return a firmware image by the name
676  *      of @name for device @device.
677  *
678  *      Should be called from user context where sleeping is allowed.
679  *
680  *      @name will be used as $FIRMWARE in the uevent environment and
681  *      should be distinctive enough not to be confused with any other
682  *      firmware image for this or any other device.
683  *
684  *      Caller must hold the reference count of @device.
685  *
686  *      The function can be called safely inside device's suspend and
687  *      resume callback.
688  **/
689 int
690 request_firmware(const struct firmware **firmware_p, const char *name,
691                  struct device *device)
692 {
693         int ret;
694
695         /* Need to pin this module until return */
696         __module_get(THIS_MODULE);
697         ret = _request_firmware(firmware_p, name, device, NULL, 0,
698                                 FW_OPT_UEVENT);
699         module_put(THIS_MODULE);
700         return ret;
701 }
702 EXPORT_SYMBOL(request_firmware);
703
704 /**
705  * firmware_request_nowarn() - request for an optional fw module
706  * @firmware: pointer to firmware image
707  * @name: name of firmware file
708  * @device: device for which firmware is being loaded
709  *
710  * This function is similar in behaviour to request_firmware(), except
711  * it doesn't produce warning messages when the file is not found.
712  * The sysfs fallback mechanism is enabled if direct filesystem lookup fails,
713  * however, however failures to find the firmware file with it are still
714  * suppressed. It is therefore up to the driver to check for the return value
715  * of this call and to decide when to inform the users of errors.
716  **/
717 int firmware_request_nowarn(const struct firmware **firmware, const char *name,
718                             struct device *device)
719 {
720         int ret;
721
722         /* Need to pin this module until return */
723         __module_get(THIS_MODULE);
724         ret = _request_firmware(firmware, name, device, NULL, 0,
725                                 FW_OPT_UEVENT | FW_OPT_NO_WARN);
726         module_put(THIS_MODULE);
727         return ret;
728 }
729 EXPORT_SYMBOL_GPL(firmware_request_nowarn);
730
731 /**
732  * request_firmware_direct() - load firmware directly without usermode helper
733  * @firmware_p: pointer to firmware image
734  * @name: name of firmware file
735  * @device: device for which firmware is being loaded
736  *
737  * This function works pretty much like request_firmware(), but this doesn't
738  * fall back to usermode helper even if the firmware couldn't be loaded
739  * directly from fs.  Hence it's useful for loading optional firmwares, which
740  * aren't always present, without extra long timeouts of udev.
741  **/
742 int request_firmware_direct(const struct firmware **firmware_p,
743                             const char *name, struct device *device)
744 {
745         int ret;
746
747         __module_get(THIS_MODULE);
748         ret = _request_firmware(firmware_p, name, device, NULL, 0,
749                                 FW_OPT_UEVENT | FW_OPT_NO_WARN |
750                                 FW_OPT_NOFALLBACK);
751         module_put(THIS_MODULE);
752         return ret;
753 }
754 EXPORT_SYMBOL_GPL(request_firmware_direct);
755
756 /**
757  * firmware_request_cache() - cache firmware for suspend so resume can use it
758  * @name: name of firmware file
759  * @device: device for which firmware should be cached for
760  *
761  * There are some devices with an optimization that enables the device to not
762  * require loading firmware on system reboot. This optimization may still
763  * require the firmware present on resume from suspend. This routine can be
764  * used to ensure the firmware is present on resume from suspend in these
765  * situations. This helper is not compatible with drivers which use
766  * request_firmware_into_buf() or request_firmware_nowait() with no uevent set.
767  **/
768 int firmware_request_cache(struct device *device, const char *name)
769 {
770         int ret;
771
772         mutex_lock(&fw_lock);
773         ret = fw_add_devm_name(device, name);
774         mutex_unlock(&fw_lock);
775
776         return ret;
777 }
778 EXPORT_SYMBOL_GPL(firmware_request_cache);
779
780 /**
781  * request_firmware_into_buf() - load firmware into a previously allocated buffer
782  * @firmware_p: pointer to firmware image
783  * @name: name of firmware file
784  * @device: device for which firmware is being loaded and DMA region allocated
785  * @buf: address of buffer to load firmware into
786  * @size: size of buffer
787  *
788  * This function works pretty much like request_firmware(), but it doesn't
789  * allocate a buffer to hold the firmware data. Instead, the firmware
790  * is loaded directly into the buffer pointed to by @buf and the @firmware_p
791  * data member is pointed at @buf.
792  *
793  * This function doesn't cache firmware either.
794  */
795 int
796 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
797                           struct device *device, void *buf, size_t size)
798 {
799         int ret;
800
801         if (fw_cache_is_setup(device, name))
802                 return -EOPNOTSUPP;
803
804         __module_get(THIS_MODULE);
805         ret = _request_firmware(firmware_p, name, device, buf, size,
806                                 FW_OPT_UEVENT | FW_OPT_NOCACHE);
807         module_put(THIS_MODULE);
808         return ret;
809 }
810 EXPORT_SYMBOL(request_firmware_into_buf);
811
812 /**
813  * release_firmware() - release the resource associated with a firmware image
814  * @fw: firmware resource to release
815  **/
816 void release_firmware(const struct firmware *fw)
817 {
818         if (fw) {
819                 if (!fw_is_builtin_firmware(fw))
820                         firmware_free_data(fw);
821                 kfree(fw);
822         }
823 }
824 EXPORT_SYMBOL(release_firmware);
825
826 /* Async support */
827 struct firmware_work {
828         struct work_struct work;
829         struct module *module;
830         const char *name;
831         struct device *device;
832         void *context;
833         void (*cont)(const struct firmware *fw, void *context);
834         enum fw_opt opt_flags;
835 };
836
837 static void request_firmware_work_func(struct work_struct *work)
838 {
839         struct firmware_work *fw_work;
840         const struct firmware *fw;
841
842         fw_work = container_of(work, struct firmware_work, work);
843
844         _request_firmware(&fw, fw_work->name, fw_work->device, NULL, 0,
845                           fw_work->opt_flags);
846         fw_work->cont(fw, fw_work->context);
847         put_device(fw_work->device); /* taken in request_firmware_nowait() */
848
849         module_put(fw_work->module);
850         kfree_const(fw_work->name);
851         kfree(fw_work);
852 }
853
854 /**
855  * request_firmware_nowait() - asynchronous version of request_firmware
856  * @module: module requesting the firmware
857  * @uevent: sends uevent to copy the firmware image if this flag
858  *      is non-zero else the firmware copy must be done manually.
859  * @name: name of firmware file
860  * @device: device for which firmware is being loaded
861  * @gfp: allocation flags
862  * @context: will be passed over to @cont, and
863  *      @fw may be %NULL if firmware request fails.
864  * @cont: function will be called asynchronously when the firmware
865  *      request is over.
866  *
867  *      Caller must hold the reference count of @device.
868  *
869  *      Asynchronous variant of request_firmware() for user contexts:
870  *              - sleep for as small periods as possible since it may
871  *                increase kernel boot time of built-in device drivers
872  *                requesting firmware in their ->probe() methods, if
873  *                @gfp is GFP_KERNEL.
874  *
875  *              - can't sleep at all if @gfp is GFP_ATOMIC.
876  **/
877 int
878 request_firmware_nowait(
879         struct module *module, bool uevent,
880         const char *name, struct device *device, gfp_t gfp, void *context,
881         void (*cont)(const struct firmware *fw, void *context))
882 {
883         struct firmware_work *fw_work;
884
885         fw_work = kzalloc(sizeof(struct firmware_work), gfp);
886         if (!fw_work)
887                 return -ENOMEM;
888
889         fw_work->module = module;
890         fw_work->name = kstrdup_const(name, gfp);
891         if (!fw_work->name) {
892                 kfree(fw_work);
893                 return -ENOMEM;
894         }
895         fw_work->device = device;
896         fw_work->context = context;
897         fw_work->cont = cont;
898         fw_work->opt_flags = FW_OPT_NOWAIT |
899                 (uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
900
901         if (!uevent && fw_cache_is_setup(device, name)) {
902                 kfree_const(fw_work->name);
903                 kfree(fw_work);
904                 return -EOPNOTSUPP;
905         }
906
907         if (!try_module_get(module)) {
908                 kfree_const(fw_work->name);
909                 kfree(fw_work);
910                 return -EFAULT;
911         }
912
913         get_device(fw_work->device);
914         INIT_WORK(&fw_work->work, request_firmware_work_func);
915         schedule_work(&fw_work->work);
916         return 0;
917 }
918 EXPORT_SYMBOL(request_firmware_nowait);
919
920 #ifdef CONFIG_PM_SLEEP
921 static ASYNC_DOMAIN_EXCLUSIVE(fw_cache_domain);
922
923 /**
924  * cache_firmware() - cache one firmware image in kernel memory space
925  * @fw_name: the firmware image name
926  *
927  * Cache firmware in kernel memory so that drivers can use it when
928  * system isn't ready for them to request firmware image from userspace.
929  * Once it returns successfully, driver can use request_firmware or its
930  * nowait version to get the cached firmware without any interacting
931  * with userspace
932  *
933  * Return 0 if the firmware image has been cached successfully
934  * Return !0 otherwise
935  *
936  */
937 static int cache_firmware(const char *fw_name)
938 {
939         int ret;
940         const struct firmware *fw;
941
942         pr_debug("%s: %s\n", __func__, fw_name);
943
944         ret = request_firmware(&fw, fw_name, NULL);
945         if (!ret)
946                 kfree(fw);
947
948         pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
949
950         return ret;
951 }
952
953 static struct fw_priv *lookup_fw_priv(const char *fw_name)
954 {
955         struct fw_priv *tmp;
956         struct firmware_cache *fwc = &fw_cache;
957
958         spin_lock(&fwc->lock);
959         tmp = __lookup_fw_priv(fw_name);
960         spin_unlock(&fwc->lock);
961
962         return tmp;
963 }
964
965 /**
966  * uncache_firmware() - remove one cached firmware image
967  * @fw_name: the firmware image name
968  *
969  * Uncache one firmware image which has been cached successfully
970  * before.
971  *
972  * Return 0 if the firmware cache has been removed successfully
973  * Return !0 otherwise
974  *
975  */
976 static int uncache_firmware(const char *fw_name)
977 {
978         struct fw_priv *fw_priv;
979         struct firmware fw;
980
981         pr_debug("%s: %s\n", __func__, fw_name);
982
983         if (fw_get_builtin_firmware(&fw, fw_name, NULL, 0))
984                 return 0;
985
986         fw_priv = lookup_fw_priv(fw_name);
987         if (fw_priv) {
988                 free_fw_priv(fw_priv);
989                 return 0;
990         }
991
992         return -EINVAL;
993 }
994
995 static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
996 {
997         struct fw_cache_entry *fce;
998
999         fce = kzalloc(sizeof(*fce), GFP_ATOMIC);
1000         if (!fce)
1001                 goto exit;
1002
1003         fce->name = kstrdup_const(name, GFP_ATOMIC);
1004         if (!fce->name) {
1005                 kfree(fce);
1006                 fce = NULL;
1007                 goto exit;
1008         }
1009 exit:
1010         return fce;
1011 }
1012
1013 static int __fw_entry_found(const char *name)
1014 {
1015         struct firmware_cache *fwc = &fw_cache;
1016         struct fw_cache_entry *fce;
1017
1018         list_for_each_entry(fce, &fwc->fw_names, list) {
1019                 if (!strcmp(fce->name, name))
1020                         return 1;
1021         }
1022         return 0;
1023 }
1024
1025 static int fw_cache_piggyback_on_request(const char *name)
1026 {
1027         struct firmware_cache *fwc = &fw_cache;
1028         struct fw_cache_entry *fce;
1029         int ret = 0;
1030
1031         spin_lock(&fwc->name_lock);
1032         if (__fw_entry_found(name))
1033                 goto found;
1034
1035         fce = alloc_fw_cache_entry(name);
1036         if (fce) {
1037                 ret = 1;
1038                 list_add(&fce->list, &fwc->fw_names);
1039                 pr_debug("%s: fw: %s\n", __func__, name);
1040         }
1041 found:
1042         spin_unlock(&fwc->name_lock);
1043         return ret;
1044 }
1045
1046 static void free_fw_cache_entry(struct fw_cache_entry *fce)
1047 {
1048         kfree_const(fce->name);
1049         kfree(fce);
1050 }
1051
1052 static void __async_dev_cache_fw_image(void *fw_entry,
1053                                        async_cookie_t cookie)
1054 {
1055         struct fw_cache_entry *fce = fw_entry;
1056         struct firmware_cache *fwc = &fw_cache;
1057         int ret;
1058
1059         ret = cache_firmware(fce->name);
1060         if (ret) {
1061                 spin_lock(&fwc->name_lock);
1062                 list_del(&fce->list);
1063                 spin_unlock(&fwc->name_lock);
1064
1065                 free_fw_cache_entry(fce);
1066         }
1067 }
1068
1069 /* called with dev->devres_lock held */
1070 static void dev_create_fw_entry(struct device *dev, void *res,
1071                                 void *data)
1072 {
1073         struct fw_name_devm *fwn = res;
1074         const char *fw_name = fwn->name;
1075         struct list_head *head = data;
1076         struct fw_cache_entry *fce;
1077
1078         fce = alloc_fw_cache_entry(fw_name);
1079         if (fce)
1080                 list_add(&fce->list, head);
1081 }
1082
1083 static int devm_name_match(struct device *dev, void *res,
1084                            void *match_data)
1085 {
1086         struct fw_name_devm *fwn = res;
1087         return (fwn->magic == (unsigned long)match_data);
1088 }
1089
1090 static void dev_cache_fw_image(struct device *dev, void *data)
1091 {
1092         LIST_HEAD(todo);
1093         struct fw_cache_entry *fce;
1094         struct fw_cache_entry *fce_next;
1095         struct firmware_cache *fwc = &fw_cache;
1096
1097         devres_for_each_res(dev, fw_name_devm_release,
1098                             devm_name_match, &fw_cache,
1099                             dev_create_fw_entry, &todo);
1100
1101         list_for_each_entry_safe(fce, fce_next, &todo, list) {
1102                 list_del(&fce->list);
1103
1104                 spin_lock(&fwc->name_lock);
1105                 /* only one cache entry for one firmware */
1106                 if (!__fw_entry_found(fce->name)) {
1107                         list_add(&fce->list, &fwc->fw_names);
1108                 } else {
1109                         free_fw_cache_entry(fce);
1110                         fce = NULL;
1111                 }
1112                 spin_unlock(&fwc->name_lock);
1113
1114                 if (fce)
1115                         async_schedule_domain(__async_dev_cache_fw_image,
1116                                               (void *)fce,
1117                                               &fw_cache_domain);
1118         }
1119 }
1120
1121 static void __device_uncache_fw_images(void)
1122 {
1123         struct firmware_cache *fwc = &fw_cache;
1124         struct fw_cache_entry *fce;
1125
1126         spin_lock(&fwc->name_lock);
1127         while (!list_empty(&fwc->fw_names)) {
1128                 fce = list_entry(fwc->fw_names.next,
1129                                 struct fw_cache_entry, list);
1130                 list_del(&fce->list);
1131                 spin_unlock(&fwc->name_lock);
1132
1133                 uncache_firmware(fce->name);
1134                 free_fw_cache_entry(fce);
1135
1136                 spin_lock(&fwc->name_lock);
1137         }
1138         spin_unlock(&fwc->name_lock);
1139 }
1140
1141 /**
1142  * device_cache_fw_images() - cache devices' firmware
1143  *
1144  * If one device called request_firmware or its nowait version
1145  * successfully before, the firmware names are recored into the
1146  * device's devres link list, so device_cache_fw_images can call
1147  * cache_firmware() to cache these firmwares for the device,
1148  * then the device driver can load its firmwares easily at
1149  * time when system is not ready to complete loading firmware.
1150  */
1151 static void device_cache_fw_images(void)
1152 {
1153         struct firmware_cache *fwc = &fw_cache;
1154         DEFINE_WAIT(wait);
1155
1156         pr_debug("%s\n", __func__);
1157
1158         /* cancel uncache work */
1159         cancel_delayed_work_sync(&fwc->work);
1160
1161         fw_fallback_set_cache_timeout();
1162
1163         mutex_lock(&fw_lock);
1164         fwc->state = FW_LOADER_START_CACHE;
1165         dpm_for_each_dev(NULL, dev_cache_fw_image);
1166         mutex_unlock(&fw_lock);
1167
1168         /* wait for completion of caching firmware for all devices */
1169         async_synchronize_full_domain(&fw_cache_domain);
1170
1171         fw_fallback_set_default_timeout();
1172 }
1173
1174 /**
1175  * device_uncache_fw_images() - uncache devices' firmware
1176  *
1177  * uncache all firmwares which have been cached successfully
1178  * by device_uncache_fw_images earlier
1179  */
1180 static void device_uncache_fw_images(void)
1181 {
1182         pr_debug("%s\n", __func__);
1183         __device_uncache_fw_images();
1184 }
1185
1186 static void device_uncache_fw_images_work(struct work_struct *work)
1187 {
1188         device_uncache_fw_images();
1189 }
1190
1191 /**
1192  * device_uncache_fw_images_delay() - uncache devices firmwares
1193  * @delay: number of milliseconds to delay uncache device firmwares
1194  *
1195  * uncache all devices's firmwares which has been cached successfully
1196  * by device_cache_fw_images after @delay milliseconds.
1197  */
1198 static void device_uncache_fw_images_delay(unsigned long delay)
1199 {
1200         queue_delayed_work(system_power_efficient_wq, &fw_cache.work,
1201                            msecs_to_jiffies(delay));
1202 }
1203
1204 static int fw_pm_notify(struct notifier_block *notify_block,
1205                         unsigned long mode, void *unused)
1206 {
1207         switch (mode) {
1208         case PM_HIBERNATION_PREPARE:
1209         case PM_SUSPEND_PREPARE:
1210         case PM_RESTORE_PREPARE:
1211                 /*
1212                  * kill pending fallback requests with a custom fallback
1213                  * to avoid stalling suspend.
1214                  */
1215                 kill_pending_fw_fallback_reqs(true);
1216                 device_cache_fw_images();
1217                 break;
1218
1219         case PM_POST_SUSPEND:
1220         case PM_POST_HIBERNATION:
1221         case PM_POST_RESTORE:
1222                 /*
1223                  * In case that system sleep failed and syscore_suspend is
1224                  * not called.
1225                  */
1226                 mutex_lock(&fw_lock);
1227                 fw_cache.state = FW_LOADER_NO_CACHE;
1228                 mutex_unlock(&fw_lock);
1229
1230                 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1231                 break;
1232         }
1233
1234         return 0;
1235 }
1236
1237 /* stop caching firmware once syscore_suspend is reached */
1238 static int fw_suspend(void)
1239 {
1240         fw_cache.state = FW_LOADER_NO_CACHE;
1241         return 0;
1242 }
1243
1244 static struct syscore_ops fw_syscore_ops = {
1245         .suspend = fw_suspend,
1246 };
1247
1248 static int __init register_fw_pm_ops(void)
1249 {
1250         int ret;
1251
1252         spin_lock_init(&fw_cache.name_lock);
1253         INIT_LIST_HEAD(&fw_cache.fw_names);
1254
1255         INIT_DELAYED_WORK(&fw_cache.work,
1256                           device_uncache_fw_images_work);
1257
1258         fw_cache.pm_notify.notifier_call = fw_pm_notify;
1259         ret = register_pm_notifier(&fw_cache.pm_notify);
1260         if (ret)
1261                 return ret;
1262
1263         register_syscore_ops(&fw_syscore_ops);
1264
1265         return ret;
1266 }
1267
1268 static inline void unregister_fw_pm_ops(void)
1269 {
1270         unregister_syscore_ops(&fw_syscore_ops);
1271         unregister_pm_notifier(&fw_cache.pm_notify);
1272 }
1273 #else
1274 static int fw_cache_piggyback_on_request(const char *name)
1275 {
1276         return 0;
1277 }
1278 static inline int register_fw_pm_ops(void)
1279 {
1280         return 0;
1281 }
1282 static inline void unregister_fw_pm_ops(void)
1283 {
1284 }
1285 #endif
1286
1287 static void __init fw_cache_init(void)
1288 {
1289         spin_lock_init(&fw_cache.lock);
1290         INIT_LIST_HEAD(&fw_cache.head);
1291         fw_cache.state = FW_LOADER_NO_CACHE;
1292 }
1293
1294 static int fw_shutdown_notify(struct notifier_block *unused1,
1295                               unsigned long unused2, void *unused3)
1296 {
1297         /*
1298          * Kill all pending fallback requests to avoid both stalling shutdown,
1299          * and avoid a deadlock with the usermode_lock.
1300          */
1301         kill_pending_fw_fallback_reqs(false);
1302
1303         return NOTIFY_DONE;
1304 }
1305
1306 static struct notifier_block fw_shutdown_nb = {
1307         .notifier_call = fw_shutdown_notify,
1308 };
1309
1310 static int __init firmware_class_init(void)
1311 {
1312         int ret;
1313
1314         /* No need to unfold these on exit */
1315         fw_cache_init();
1316
1317         ret = register_fw_pm_ops();
1318         if (ret)
1319                 return ret;
1320
1321         ret = register_reboot_notifier(&fw_shutdown_nb);
1322         if (ret)
1323                 goto out;
1324
1325         return register_sysfs_loader();
1326
1327 out:
1328         unregister_fw_pm_ops();
1329         return ret;
1330 }
1331
1332 static void __exit firmware_class_exit(void)
1333 {
1334         unregister_fw_pm_ops();
1335         unregister_reboot_notifier(&fw_shutdown_nb);
1336         unregister_sysfs_loader();
1337 }
1338
1339 fs_initcall(firmware_class_init);
1340 module_exit(firmware_class_exit);