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 
32399da52fSBjoern A. Zeeb #include <sys/param.h>
33399da52fSBjoern A. Zeeb #include <sys/kernel.h>
34a6c2507dSBjoern A. Zeeb #include <sys/types.h>
35a6c2507dSBjoern A. Zeeb #include <sys/malloc.h>
36a6c2507dSBjoern A. Zeeb #include <sys/firmware.h>
37399da52fSBjoern A. Zeeb #include <sys/queue.h>
38399da52fSBjoern A. Zeeb #include <sys/taskqueue.h>
39a6c2507dSBjoern A. Zeeb 
40a6c2507dSBjoern A. Zeeb #include <linux/types.h>
41a6c2507dSBjoern A. Zeeb #include <linux/device.h>
42a6c2507dSBjoern A. Zeeb 
43a6c2507dSBjoern A. Zeeb #include <linux/firmware.h>
44a6c2507dSBjoern A. Zeeb #undef firmware
45a6c2507dSBjoern A. Zeeb 
46a6c2507dSBjoern A. Zeeb MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");
47a6c2507dSBjoern A. Zeeb 
48399da52fSBjoern A. Zeeb struct lkpi_fw_task {
49399da52fSBjoern A. Zeeb 	/* Task and arguments for the "nowait" callback. */
50399da52fSBjoern A. Zeeb 	struct task		fw_task;
51399da52fSBjoern A. Zeeb 	gfp_t			gfp;
52399da52fSBjoern A. Zeeb 	const char		*fw_name;
53399da52fSBjoern A. Zeeb 	struct device		*dev;
54399da52fSBjoern A. Zeeb 	void			*drv;
55399da52fSBjoern A. Zeeb 	void(*cont)(const struct linuxkpi_firmware *, void *);
56399da52fSBjoern A. Zeeb };
57399da52fSBjoern A. Zeeb 
58a6c2507dSBjoern A. Zeeb static int
_linuxkpi_request_firmware(const char * fw_name,const struct linuxkpi_firmware ** fw,struct device * dev,gfp_t gfp __unused,bool enoentok,bool warn)59a6c2507dSBjoern A. Zeeb _linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw,
60a6c2507dSBjoern A. Zeeb     struct device *dev, gfp_t gfp __unused, bool enoentok, bool warn)
61a6c2507dSBjoern A. Zeeb {
62a6c2507dSBjoern A. Zeeb 	const struct firmware *fbdfw;
63a6c2507dSBjoern A. Zeeb 	struct linuxkpi_firmware *lfw;
64a6c2507dSBjoern A. Zeeb 	const char *fwimg;
65a6c2507dSBjoern A. Zeeb 	char *p;
66a6c2507dSBjoern A. Zeeb 	uint32_t flags;
67a6c2507dSBjoern A. Zeeb 
68399da52fSBjoern A. Zeeb 	if (fw_name == NULL || fw == NULL || dev == NULL) {
69399da52fSBjoern A. Zeeb 		*fw = NULL;
70a6c2507dSBjoern A. Zeeb 		return (-EINVAL);
71399da52fSBjoern A. Zeeb 	}
72a6c2507dSBjoern A. Zeeb 
73a6c2507dSBjoern A. Zeeb 	/* Set independent on "warn". To debug, bootverbose is avail. */
74a6c2507dSBjoern A. Zeeb 	flags = FIRMWARE_GET_NOWARN;
75a6c2507dSBjoern A. Zeeb 
76a6c2507dSBjoern A. Zeeb 	KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));
77a6c2507dSBjoern A. Zeeb 	lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);
78a6c2507dSBjoern A. Zeeb 
79a6c2507dSBjoern A. Zeeb 	/*
80a6c2507dSBjoern A. Zeeb 	 * Linux can have a path in the firmware which is hard to replicate
81a6c2507dSBjoern A. Zeeb 	 * for auto-firmware-module-loading.
82a6c2507dSBjoern A. Zeeb 	 * On FreeBSD, depending on what people do, the firmware will either
83a6c2507dSBjoern A. Zeeb 	 * be called "fw", or "dir_fw", or "modname_dir_fw".  The latter the
84a6c2507dSBjoern A. Zeeb 	 * driver author has to deal with herself (requesting the special name).
85a6c2507dSBjoern A. Zeeb 	 * We also optionally flatten '/'s and '.'s as some firmware modules do.
86a6c2507dSBjoern A. Zeeb 	 * We probe in the least-of-work order avoiding memory operations.
87a6c2507dSBjoern A. Zeeb 	 * It will be preferred to build the firmware .ko in a well matching
88a6c2507dSBjoern A. Zeeb 	 * way rather than adding more name-mangling-hacks here in the future
89a6c2507dSBjoern A. Zeeb 	 * (though we could if needed).
90a6c2507dSBjoern A. Zeeb 	 */
91a6c2507dSBjoern A. Zeeb 	/* (1) Try any name removed of path. */
92a6c2507dSBjoern A. Zeeb 	fwimg = strrchr(fw_name, '/');
93a6c2507dSBjoern A. Zeeb 	if (fwimg != NULL)
94a6c2507dSBjoern A. Zeeb 		fwimg++;
95a6c2507dSBjoern A. Zeeb 	if (fwimg == NULL || *fwimg == '\0')
96a6c2507dSBjoern A. Zeeb 		fwimg = fw_name;
97a6c2507dSBjoern A. Zeeb 	fbdfw = firmware_get_flags(fwimg, flags);
98a6c2507dSBjoern A. Zeeb 	/* (2) Try the original name if we have not yet. */
99a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL && fwimg != fw_name) {
100a6c2507dSBjoern A. Zeeb 		fwimg = fw_name;
101a6c2507dSBjoern A. Zeeb 		fbdfw = firmware_get_flags(fwimg, flags);
102a6c2507dSBjoern A. Zeeb 	}
10337c3241aSBjoern A. Zeeb 	/* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */
104a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL &&
10537c3241aSBjoern A. Zeeb 	    (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||
10637c3241aSBjoern A. Zeeb 	    strchr(fw_name, '-'))) {
107a6c2507dSBjoern A. Zeeb 		fwimg = strdup(fw_name, M_LKPI_FW);
108a6c2507dSBjoern A. Zeeb 		if (fwimg != NULL) {
109a6c2507dSBjoern A. Zeeb 			while ((p = strchr(fwimg, '/')) != NULL)
110a6c2507dSBjoern A. Zeeb 				*p = '_';
111a6c2507dSBjoern A. Zeeb 			fbdfw = firmware_get_flags(fwimg, flags);
112a6c2507dSBjoern A. Zeeb 			if (fbdfw == NULL) {
113a6c2507dSBjoern A. Zeeb 				while ((p = strchr(fwimg, '.')) != NULL)
114a6c2507dSBjoern A. Zeeb 					*p = '_';
115a6c2507dSBjoern A. Zeeb 				fbdfw = firmware_get_flags(fwimg, flags);
116a6c2507dSBjoern A. Zeeb 			}
11737c3241aSBjoern A. Zeeb 			if (fbdfw == NULL) {
11837c3241aSBjoern A. Zeeb 				while ((p = strchr(fwimg, '-')) != NULL)
11937c3241aSBjoern A. Zeeb 					*p = '_';
12037c3241aSBjoern A. Zeeb 				fbdfw = firmware_get_flags(fwimg, flags);
12137c3241aSBjoern A. Zeeb 			}
122a6c2507dSBjoern A. Zeeb 			free(__DECONST(void *, fwimg), M_LKPI_FW);
123a6c2507dSBjoern A. Zeeb 		}
124a6c2507dSBjoern A. Zeeb 	}
125a6c2507dSBjoern A. Zeeb 	if (fbdfw == NULL) {
126a6c2507dSBjoern A. Zeeb 		if (enoentok)
127a6c2507dSBjoern A. Zeeb 			*fw = lfw;
128a6c2507dSBjoern A. Zeeb 		else {
129a6c2507dSBjoern A. Zeeb 			free(lfw, M_LKPI_FW);
130a6c2507dSBjoern A. Zeeb 			*fw = NULL;
131a6c2507dSBjoern A. Zeeb 		}
132a6c2507dSBjoern A. Zeeb 		if (warn)
133a6c2507dSBjoern A. Zeeb 			device_printf(dev->bsddev, "could not load firmware "
134a6c2507dSBjoern A. Zeeb 			    "image '%s'\n", fw_name);
135a6c2507dSBjoern A. Zeeb 		return (-ENOENT);
136a6c2507dSBjoern A. Zeeb 	}
137a6c2507dSBjoern A. Zeeb 
138a6c2507dSBjoern A. Zeeb 	device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
139a6c2507dSBjoern A. Zeeb 	    fw_name);
140a6c2507dSBjoern A. Zeeb 	lfw->fbdfw = fbdfw;
141a6c2507dSBjoern A. Zeeb 	lfw->data = (const uint8_t *)fbdfw->data;
142a6c2507dSBjoern A. Zeeb 	lfw->size = fbdfw->datasize;
143a6c2507dSBjoern A. Zeeb 	*fw = lfw;
144a6c2507dSBjoern A. Zeeb 	return (0);
145a6c2507dSBjoern A. Zeeb }
146a6c2507dSBjoern A. Zeeb 
147399da52fSBjoern A. Zeeb static void
lkpi_fw_task(void * ctx,int pending)148399da52fSBjoern A. Zeeb lkpi_fw_task(void *ctx, int pending)
149399da52fSBjoern A. Zeeb {
150399da52fSBjoern A. Zeeb 	struct lkpi_fw_task *lfwt;
151399da52fSBjoern A. Zeeb 	const struct linuxkpi_firmware *fw;
152399da52fSBjoern A. Zeeb 
153399da52fSBjoern A. Zeeb 	KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n",
154399da52fSBjoern A. Zeeb 	    __func__, ctx, pending));
155399da52fSBjoern A. Zeeb 
156399da52fSBjoern A. Zeeb 	lfwt = ctx;
157399da52fSBjoern A. Zeeb 	if (lfwt->cont == NULL)
158399da52fSBjoern A. Zeeb 		goto out;
159399da52fSBjoern A. Zeeb 
1607043359cSBjoern A. Zeeb 	_linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev,
161399da52fSBjoern A. Zeeb 	    lfwt->gfp, true, true);
162399da52fSBjoern A. Zeeb 
163399da52fSBjoern A. Zeeb 	/*
164399da52fSBjoern A. Zeeb 	 * Linux seems to run the callback if it cannot find the firmware.
165399da52fSBjoern A. Zeeb 	 * We call it in all cases as it is the only feedback to the requester.
166399da52fSBjoern A. Zeeb 	 */
167399da52fSBjoern A. Zeeb 	lfwt->cont(fw, lfwt->drv);
168399da52fSBjoern A. Zeeb 	/* Do not assume fw is still valid! */
169399da52fSBjoern A. Zeeb 
170399da52fSBjoern A. Zeeb out:
171399da52fSBjoern A. Zeeb 	free(lfwt, M_LKPI_FW);
172399da52fSBjoern A. Zeeb }
173399da52fSBjoern A. Zeeb 
174a6c2507dSBjoern A. Zeeb int
linuxkpi_request_firmware_nowait(struct module * mod __unused,bool _t __unused,const char * fw_name,struct device * dev,gfp_t gfp,void * drv,void (* cont)(const struct linuxkpi_firmware *,void *))175a6c2507dSBjoern A. Zeeb linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,
176a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev, gfp_t gfp, void *drv,
177a6c2507dSBjoern A. Zeeb     void(*cont)(const struct linuxkpi_firmware *, void *))
178a6c2507dSBjoern A. Zeeb {
179399da52fSBjoern A. Zeeb 	struct lkpi_fw_task *lfwt;
180a6c2507dSBjoern A. Zeeb 	int error;
181a6c2507dSBjoern A. Zeeb 
182399da52fSBjoern A. Zeeb 	lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO);
183399da52fSBjoern A. Zeeb 	lfwt->gfp = gfp;
184399da52fSBjoern A. Zeeb 	lfwt->fw_name = fw_name;
185399da52fSBjoern A. Zeeb 	lfwt->dev = dev;
186399da52fSBjoern A. Zeeb 	lfwt->drv = drv;
187399da52fSBjoern A. Zeeb 	lfwt->cont = cont;
188399da52fSBjoern A. Zeeb 	TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt);
189399da52fSBjoern A. Zeeb 	error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task);
190a6c2507dSBjoern A. Zeeb 
191399da52fSBjoern A. Zeeb 	if (error)
192399da52fSBjoern A. Zeeb 		return (-error);
193399da52fSBjoern A. Zeeb 	return (0);
194a6c2507dSBjoern A. Zeeb }
195a6c2507dSBjoern A. Zeeb 
196a6c2507dSBjoern A. Zeeb int
linuxkpi_request_firmware(const struct linuxkpi_firmware ** fw,const char * fw_name,struct device * dev)197a6c2507dSBjoern A. Zeeb linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
198a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev)
199a6c2507dSBjoern A. Zeeb {
200a6c2507dSBjoern A. Zeeb 
201a6c2507dSBjoern A. Zeeb 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
202a6c2507dSBjoern A. Zeeb 	    true));
203a6c2507dSBjoern A. Zeeb }
204a6c2507dSBjoern A. Zeeb 
205a6c2507dSBjoern A. Zeeb int
linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware ** fw,const char * fw_name,struct device * dev)206a6c2507dSBjoern A. Zeeb linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
207a6c2507dSBjoern A. Zeeb     const char *fw_name, struct device *dev)
208a6c2507dSBjoern A. Zeeb {
209a6c2507dSBjoern A. Zeeb 
210a6c2507dSBjoern A. Zeeb 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
211a6c2507dSBjoern A. Zeeb 	    false));
212a6c2507dSBjoern A. Zeeb }
213a6c2507dSBjoern A. Zeeb 
214a6c2507dSBjoern A. Zeeb void
linuxkpi_release_firmware(const struct linuxkpi_firmware * fw)215a6c2507dSBjoern A. Zeeb linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
216a6c2507dSBjoern A. Zeeb {
217a6c2507dSBjoern A. Zeeb 
218a6c2507dSBjoern A. Zeeb 	if (fw == NULL)
219a6c2507dSBjoern A. Zeeb 		return;
220a6c2507dSBjoern A. Zeeb 
221a6c2507dSBjoern A. Zeeb 	if (fw->fbdfw)
222a6c2507dSBjoern A. Zeeb 		firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
223a6c2507dSBjoern A. Zeeb 	free(__DECONST(void *, fw), M_LKPI_FW);
224a6c2507dSBjoern A. Zeeb }
225fb3c5497SBjoern A. Zeeb 
226fb3c5497SBjoern A. Zeeb int
linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware ** fw,const char * fw_name,struct device * dev,uint8_t * buf,size_t buflen,size_t offset)227fb3c5497SBjoern A. Zeeb linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,
228fb3c5497SBjoern A. Zeeb     const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen,
229fb3c5497SBjoern A. Zeeb     size_t offset)
230fb3c5497SBjoern A. Zeeb {
231fb3c5497SBjoern A. Zeeb 	const struct linuxkpi_firmware *lfw;
232fb3c5497SBjoern A. Zeeb 	int error;
233fb3c5497SBjoern A. Zeeb 
234fb3c5497SBjoern A. Zeeb 	error = linuxkpi_request_firmware(fw, fw_name, dev);
235fb3c5497SBjoern A. Zeeb 	if (error != 0)
236fb3c5497SBjoern A. Zeeb 		return (error);
237fb3c5497SBjoern A. Zeeb 
238fb3c5497SBjoern A. Zeeb 	lfw = *fw;
239fb3c5497SBjoern A. Zeeb 	if ((offset + buflen) >= lfw->size) {
240fb3c5497SBjoern A. Zeeb 		linuxkpi_release_firmware(lfw);
241fb3c5497SBjoern A. Zeeb 		return (-ERANGE);
242fb3c5497SBjoern A. Zeeb 	}
243fb3c5497SBjoern A. Zeeb 
244fb3c5497SBjoern A. Zeeb 	memcpy(buf, lfw->data + offset, buflen);
245fb3c5497SBjoern A. Zeeb 
246fb3c5497SBjoern A. Zeeb 	return (0);
247fb3c5497SBjoern A. Zeeb }
248