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