1a6c2507dSBjoern A. Zeeb /*-
2a6c2507dSBjoern A. Zeeb  * SPDX-License-Identifier: BSD-2-Clause
3a6c2507dSBjoern A. Zeeb  *
4a6c2507dSBjoern A. Zeeb  * Copyright (c) 2020-2021 The FreeBSD Foundation
5fb3c5497SBjoern A. Zeeb  * Copyright (c) 2022 Bjoern A. Zeeb
6a6c2507dSBjoern A. Zeeb  *
7a6c2507dSBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
8a6c2507dSBjoern A. Zeeb  * the FreeBSD Foundation.
9a6c2507dSBjoern A. Zeeb  *
10a6c2507dSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
11a6c2507dSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
12a6c2507dSBjoern A. Zeeb  * are met:
13a6c2507dSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
14a6c2507dSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
15a6c2507dSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
16a6c2507dSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
17a6c2507dSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
18a6c2507dSBjoern A. Zeeb  *
19a6c2507dSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20a6c2507dSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a6c2507dSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a6c2507dSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23a6c2507dSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a6c2507dSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a6c2507dSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a6c2507dSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a6c2507dSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a6c2507dSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a6c2507dSBjoern A. Zeeb  * SUCH DAMAGE.
30a6c2507dSBjoern A. Zeeb  *
31a6c2507dSBjoern A. Zeeb  * $FreeBSD$
32a6c2507dSBjoern A. Zeeb  */
33a6c2507dSBjoern A. Zeeb 
34399da52fSBjoern A. Zeeb #include <sys/param.h>
35399da52fSBjoern A. Zeeb #include <sys/kernel.h>
36a6c2507dSBjoern A. Zeeb #include <sys/types.h>
37a6c2507dSBjoern A. Zeeb #include <sys/malloc.h>
38a6c2507dSBjoern A. Zeeb #include <sys/firmware.h>
39399da52fSBjoern A. Zeeb #include <sys/queue.h>
40399da52fSBjoern A. Zeeb #include <sys/taskqueue.h>
41a6c2507dSBjoern A. Zeeb 
42a6c2507dSBjoern A. Zeeb #include <linux/types.h>
43a6c2507dSBjoern A. Zeeb #include <linux/device.h>
44a6c2507dSBjoern A. Zeeb 
45a6c2507dSBjoern A. Zeeb #include <linux/firmware.h>
46a6c2507dSBjoern A. Zeeb #undef firmware
47a6c2507dSBjoern A. Zeeb 
48a6c2507dSBjoern A. Zeeb MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");
49a6c2507dSBjoern A. Zeeb 
50399da52fSBjoern A. Zeeb struct lkpi_fw_task {
51399da52fSBjoern A. Zeeb 	/* Task and arguments for the "nowait" callback. */
52399da52fSBjoern A. Zeeb 	struct task		fw_task;
53399da52fSBjoern A. Zeeb 	gfp_t			gfp;
54399da52fSBjoern A. Zeeb 	const char		*fw_name;
55399da52fSBjoern A. Zeeb 	struct device		*dev;
56399da52fSBjoern A. Zeeb 	void			*drv;
57399da52fSBjoern A. Zeeb 	void(*cont)(const struct linuxkpi_firmware *, void *);
58399da52fSBjoern A. Zeeb };
59399da52fSBjoern A. Zeeb 
60a6c2507dSBjoern A. Zeeb static int
61a6c2507dSBjoern A. Zeeb _linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw,
62a6c2507dSBjoern A. Zeeb     struct device *dev, gfp_t gfp __unused, bool enoentok, bool warn)
63a6c2507dSBjoern A. Zeeb {
64a6c2507dSBjoern A. Zeeb 	const struct firmware *fbdfw;
65a6c2507dSBjoern A. Zeeb 	struct linuxkpi_firmware *lfw;
66a6c2507dSBjoern A. Zeeb 	const char *fwimg;
67a6c2507dSBjoern A. Zeeb 	char *p;
68a6c2507dSBjoern A. Zeeb 	uint32_t flags;
69a6c2507dSBjoern A. Zeeb 
70399da52fSBjoern A. Zeeb 	if (fw_name == NULL || fw == NULL || dev == NULL) {
71399da52fSBjoern A. Zeeb 		*fw = NULL;
72a6c2507dSBjoern A. Zeeb 		return (-EINVAL);
73399da52fSBjoern A. Zeeb 	}
74a6c2507dSBjoern A. Zeeb 
75a6c2507dSBjoern A. Zeeb 	/* Set independent on "warn". To debug, bootverbose is avail. */
76a6c2507dSBjoern A. Zeeb 	flags = FIRMWARE_GET_NOWARN;
77a6c2507dSBjoern A. Zeeb 
78a6c2507dSBjoern A. Zeeb 	KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));
79a6c2507dSBjoern A. Zeeb 	lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);
80a6c2507dSBjoern A. Zeeb 
81a6c2507dSBjoern A. Zeeb 	/*
82a6c2507dSBjoern A. Zeeb 	 * Linux can have a path in the firmware which is hard to replicate
83a6c2507dSBjoern A. Zeeb 	 * for auto-firmware-module-loading.
84a6c2507dSBjoern A. Zeeb 	 * On FreeBSD, depending on what people do, the firmware will either
85a6c2507dSBjoern A. Zeeb 	 * be called "fw", or "dir_fw", or "modname_dir_fw".  The latter the
86a6c2507dSBjoern A. Zeeb 	 * driver author has to deal with herself (requesting the special name).
87a6c2507dSBjoern A. Zeeb 	 * We also optionally flatten '/'s and '.'s as some firmware modules do.
88a6c2507dSBjoern A. Zeeb 	 * We probe in the least-of-work order avoiding memory operations.
89a6c2507dSBjoern A. Zeeb 	 * It will be preferred to build the firmware .ko in a well matching
90a6c2507dSBjoern A. Zeeb 	 * way rather than adding more name-mangling-hacks here in the future
91a6c2507dSBjoern A. Zeeb 	 * (though we could if needed).
92a6c2507dSBjoern A. Zeeb 	 */
93a6c2507dSBjoern A. Zeeb 	/* (1) Try any name removed of path. */
94a6c2507dSBjoern A. Zeeb 	fwimg = strrchr(fw_name, '/');
95a6c2507dSBjoern A. Zeeb 	if (fwimg != NULL)
96a6c2507dSBjoern A. Zeeb 		fwimg++;
97a6c2507dSBjoern A. Zeeb 	if (fwimg == NULL || *fwimg == '\0')
98a6c2507dSBjoern A. Zeeb 		fwimg = fw_name;
99a6c2507dSBjoern A. Zeeb 	fbdfw = firmware_get_flags(fwimg, flags);
100a6c2507dSBjoern A. Zeeb 	/* (2) Try the original name if we have not yet. */
101a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL && fwimg != fw_name) {
102a6c2507dSBjoern A. Zeeb 		fwimg = fw_name;
103a6c2507dSBjoern A. Zeeb 		fbdfw = firmware_get_flags(fwimg, flags);
104a6c2507dSBjoern A. Zeeb 	}
10537c3241aSBjoern A. Zeeb 	/* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */
106a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL &&
10737c3241aSBjoern A. Zeeb 	    (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||
10837c3241aSBjoern A. Zeeb 	    strchr(fw_name, '-'))) {
109a6c2507dSBjoern A. Zeeb 		fwimg = strdup(fw_name, M_LKPI_FW);
110a6c2507dSBjoern A. Zeeb 		if (fwimg != NULL) {
111a6c2507dSBjoern A. Zeeb 			while ((p = strchr(fwimg, '/')) != NULL)
112a6c2507dSBjoern A. Zeeb 				*p = '_';
113a6c2507dSBjoern A. Zeeb 			fbdfw = firmware_get_flags(fwimg, flags);
114a6c2507dSBjoern A. Zeeb 			if (fbdfw == NULL) {
115a6c2507dSBjoern A. Zeeb 				while ((p = strchr(fwimg, '.')) != NULL)
116a6c2507dSBjoern A. Zeeb 					*p = '_';
117a6c2507dSBjoern A. Zeeb 				fbdfw = firmware_get_flags(fwimg, flags);
118a6c2507dSBjoern A. Zeeb 			}
11937c3241aSBjoern A. Zeeb 			if (fbdfw == NULL) {
12037c3241aSBjoern A. Zeeb 				while ((p = strchr(fwimg, '-')) != NULL)
12137c3241aSBjoern A. Zeeb 					*p = '_';
12237c3241aSBjoern A. Zeeb 				fbdfw = firmware_get_flags(fwimg, flags);
12337c3241aSBjoern A. Zeeb 			}
124a6c2507dSBjoern A. Zeeb 			free(__DECONST(void *, fwimg), M_LKPI_FW);
125a6c2507dSBjoern A. Zeeb 		}
126a6c2507dSBjoern A. Zeeb 	}
127a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL) {
128a6c2507dSBjoern A. Zeeb 		if (enoentok)
129a6c2507dSBjoern A. Zeeb 			*fw = lfw;
130a6c2507dSBjoern A. Zeeb 		else {
131a6c2507dSBjoern A. Zeeb 			free(lfw, M_LKPI_FW);
132a6c2507dSBjoern A. Zeeb 			*fw = NULL;
133a6c2507dSBjoern A. Zeeb 		}
134a6c2507dSBjoern A. Zeeb 		if (warn)
135a6c2507dSBjoern A. Zeeb 			device_printf(dev->bsddev, "could not load firmware "
136a6c2507dSBjoern A. Zeeb 			    "image '%s'\n", fw_name);
137a6c2507dSBjoern A. Zeeb 		return (-ENOENT);
138a6c2507dSBjoern A. Zeeb 	}
139a6c2507dSBjoern A. Zeeb 
140a6c2507dSBjoern A. Zeeb 	device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
141a6c2507dSBjoern A. Zeeb 	    fw_name);
142a6c2507dSBjoern A. Zeeb 	lfw->fbdfw = fbdfw;
143a6c2507dSBjoern A. Zeeb 	lfw->data = (const uint8_t *)fbdfw->data;
144a6c2507dSBjoern A. Zeeb 	lfw->size = fbdfw->datasize;
145a6c2507dSBjoern A. Zeeb 	*fw = lfw;
146a6c2507dSBjoern A. Zeeb 	return (0);
147a6c2507dSBjoern A. Zeeb }
148a6c2507dSBjoern A. Zeeb 
149399da52fSBjoern A. Zeeb static void
150399da52fSBjoern A. Zeeb lkpi_fw_task(void *ctx, int pending)
151399da52fSBjoern A. Zeeb {
152399da52fSBjoern A. Zeeb 	struct lkpi_fw_task *lfwt;
153399da52fSBjoern A. Zeeb 	const struct linuxkpi_firmware *fw;
154399da52fSBjoern A. Zeeb 
155399da52fSBjoern A. Zeeb 	KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n",
156399da52fSBjoern A. Zeeb 	    __func__, ctx, pending));
157399da52fSBjoern A. Zeeb 
158399da52fSBjoern A. Zeeb 	lfwt = ctx;
159399da52fSBjoern A. Zeeb 	if (lfwt->cont == NULL)
160399da52fSBjoern A. Zeeb 		goto out;
161399da52fSBjoern A. Zeeb 
1627043359cSBjoern A. Zeeb 	_linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev,
163399da52fSBjoern A. Zeeb 	    lfwt->gfp, true, true);
164399da52fSBjoern A. Zeeb 
165399da52fSBjoern A. Zeeb 	/*
166399da52fSBjoern A. Zeeb 	 * Linux seems to run the callback if it cannot find the firmware.
167399da52fSBjoern A. Zeeb 	 * We call it in all cases as it is the only feedback to the requester.
168399da52fSBjoern A. Zeeb 	 */
169399da52fSBjoern A. Zeeb 	lfwt->cont(fw, lfwt->drv);
170399da52fSBjoern A. Zeeb 	/* Do not assume fw is still valid! */
171399da52fSBjoern A. Zeeb 
172399da52fSBjoern A. Zeeb out:
173399da52fSBjoern A. Zeeb 	free(lfwt, M_LKPI_FW);
174399da52fSBjoern A. Zeeb }
175399da52fSBjoern A. Zeeb 
176a6c2507dSBjoern A. Zeeb int
177a6c2507dSBjoern A. Zeeb linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,
178a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev, gfp_t gfp, void *drv,
179a6c2507dSBjoern A. Zeeb     void(*cont)(const struct linuxkpi_firmware *, void *))
180a6c2507dSBjoern A. Zeeb {
181399da52fSBjoern A. Zeeb 	struct lkpi_fw_task *lfwt;
182a6c2507dSBjoern A. Zeeb 	int error;
183a6c2507dSBjoern A. Zeeb 
184399da52fSBjoern A. Zeeb 	lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO);
185399da52fSBjoern A. Zeeb 	lfwt->gfp = gfp;
186399da52fSBjoern A. Zeeb 	lfwt->fw_name = fw_name;
187399da52fSBjoern A. Zeeb 	lfwt->dev = dev;
188399da52fSBjoern A. Zeeb 	lfwt->drv = drv;
189399da52fSBjoern A. Zeeb 	lfwt->cont = cont;
190399da52fSBjoern A. Zeeb 	TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt);
191399da52fSBjoern A. Zeeb 	error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task);
192a6c2507dSBjoern A. Zeeb 
193399da52fSBjoern A. Zeeb 	if (error)
194399da52fSBjoern A. Zeeb 		return (-error);
195399da52fSBjoern A. Zeeb 	return (0);
196a6c2507dSBjoern A. Zeeb }
197a6c2507dSBjoern A. Zeeb 
198a6c2507dSBjoern A. Zeeb int
199a6c2507dSBjoern A. Zeeb linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
200a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev)
201a6c2507dSBjoern A. Zeeb {
202a6c2507dSBjoern A. Zeeb 
203a6c2507dSBjoern A. Zeeb 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
204a6c2507dSBjoern A. Zeeb 	    true));
205a6c2507dSBjoern A. Zeeb }
206a6c2507dSBjoern A. Zeeb 
207a6c2507dSBjoern A. Zeeb int
208a6c2507dSBjoern A. Zeeb linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
209a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev)
210a6c2507dSBjoern A. Zeeb {
211a6c2507dSBjoern A. Zeeb 
212a6c2507dSBjoern A. Zeeb 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
213a6c2507dSBjoern A. Zeeb 	    false));
214a6c2507dSBjoern A. Zeeb }
215a6c2507dSBjoern A. Zeeb 
216a6c2507dSBjoern A. Zeeb void
217a6c2507dSBjoern A. Zeeb linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
218a6c2507dSBjoern A. Zeeb {
219a6c2507dSBjoern A. Zeeb 
220a6c2507dSBjoern A. Zeeb 	if (fw == NULL)
221a6c2507dSBjoern A. Zeeb 		return;
222a6c2507dSBjoern A. Zeeb 
223a6c2507dSBjoern A. Zeeb 	if (fw->fbdfw)
224a6c2507dSBjoern A. Zeeb 		firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
225a6c2507dSBjoern A. Zeeb 	free(__DECONST(void *, fw), M_LKPI_FW);
226a6c2507dSBjoern A. Zeeb }
227fb3c5497SBjoern A. Zeeb 
228fb3c5497SBjoern A. Zeeb int
229fb3c5497SBjoern A. Zeeb linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,
230fb3c5497SBjoern A. Zeeb     const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen,
231fb3c5497SBjoern A. Zeeb     size_t offset)
232fb3c5497SBjoern A. Zeeb {
233fb3c5497SBjoern A. Zeeb 	const struct linuxkpi_firmware *lfw;
234fb3c5497SBjoern A. Zeeb 	int error;
235fb3c5497SBjoern A. Zeeb 
236fb3c5497SBjoern A. Zeeb 	error = linuxkpi_request_firmware(fw, fw_name, dev);
237fb3c5497SBjoern A. Zeeb 	if (error != 0)
238fb3c5497SBjoern A. Zeeb 		return (error);
239fb3c5497SBjoern A. Zeeb 
240fb3c5497SBjoern A. Zeeb 	lfw = *fw;
241fb3c5497SBjoern A. Zeeb 	if ((offset + buflen) >= lfw->size) {
242fb3c5497SBjoern A. Zeeb 		linuxkpi_release_firmware(lfw);
243fb3c5497SBjoern A. Zeeb 		return (-ERANGE);
244fb3c5497SBjoern A. Zeeb 	}
245fb3c5497SBjoern A. Zeeb 
246fb3c5497SBjoern A. Zeeb 	memcpy(buf, lfw->data + offset, buflen);
247fb3c5497SBjoern A. Zeeb 
248fb3c5497SBjoern A. Zeeb 	return (0);
249fb3c5497SBjoern A. Zeeb }
250