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