1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020-2021 The FreeBSD Foundation 5 * Copyright (c) 2022 Bjoern A. Zeeb 6 * 7 * This software was developed by Björn Zeeb under sponsorship from 8 * the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/types.h> 35 #include <sys/malloc.h> 36 #include <sys/firmware.h> 37 #include <sys/queue.h> 38 #include <sys/taskqueue.h> 39 40 #include <linux/types.h> 41 #include <linux/device.h> 42 43 #include <linux/firmware.h> 44 #undef firmware 45 46 MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware"); 47 48 struct lkpi_fw_task { 49 /* Task and arguments for the "nowait" callback. */ 50 struct task fw_task; 51 gfp_t gfp; 52 const char *fw_name; 53 struct device *dev; 54 void *drv; 55 void(*cont)(const struct linuxkpi_firmware *, void *); 56 }; 57 58 static int 59 _linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw, 60 struct device *dev, gfp_t gfp __unused, bool warn) 61 { 62 const struct firmware *fbdfw; 63 struct linuxkpi_firmware *lfw; 64 const char *fwimg; 65 char *p; 66 uint32_t flags; 67 68 if (fw_name == NULL || fw == NULL || dev == NULL) { 69 if (fw != NULL) 70 *fw = NULL; 71 return (-EINVAL); 72 } 73 74 /* Set independent on "warn". To debug, bootverbose is avail. */ 75 flags = FIRMWARE_GET_NOWARN; 76 77 KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp)); 78 lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO); 79 80 /* 81 * Linux can have a path in the firmware which is hard to replicate 82 * for auto-firmware-module-loading. 83 * On FreeBSD, depending on what people do, the firmware will either 84 * be called "fw", or "dir_fw", or "modname_dir_fw". The latter the 85 * driver author has to deal with herself (requesting the special name). 86 * We also optionally flatten '/'s and '.'s as some firmware modules do. 87 * We probe in the least-of-work order avoiding memory operations. 88 * It will be preferred to build the firmware .ko in a well matching 89 * way rather than adding more name-mangling-hacks here in the future 90 * (though we could if needed). 91 */ 92 /* (1) Try the original name. */ 93 fbdfw = firmware_get_flags(fw_name, flags); 94 /* (2) Try any name removed of path, if we have not yet. */ 95 if (fbdfw == NULL) { 96 fwimg = strrchr(fw_name, '/'); 97 if (fwimg != NULL) 98 fwimg++; 99 if (fwimg == NULL || *fwimg == '\0') 100 fwimg = fw_name; 101 if (fwimg != fw_name) 102 fbdfw = firmware_get_flags(fwimg, flags); 103 } 104 /* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */ 105 if (fbdfw == NULL && 106 (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL || 107 strchr(fw_name, '-'))) { 108 fwimg = strdup(fw_name, M_LKPI_FW); 109 if (fwimg != NULL) { 110 while ((p = strchr(fwimg, '/')) != NULL) 111 *p = '_'; 112 fbdfw = firmware_get_flags(fwimg, flags); 113 if (fbdfw == NULL) { 114 while ((p = strchr(fwimg, '.')) != NULL) 115 *p = '_'; 116 fbdfw = firmware_get_flags(fwimg, flags); 117 } 118 if (fbdfw == NULL) { 119 while ((p = strchr(fwimg, '-')) != NULL) 120 *p = '_'; 121 fbdfw = firmware_get_flags(fwimg, flags); 122 } 123 free(__DECONST(void *, fwimg), M_LKPI_FW); 124 } 125 } 126 if (fbdfw == NULL) { 127 linuxkpi_release_firmware(lfw); 128 *fw = NULL; 129 if (warn) 130 device_printf(dev->bsddev, 131 "could not load firmware image '%s'\n", 132 fw_name); 133 return (-ENOENT); 134 } 135 136 device_printf(dev->bsddev, "successfully loaded firmware image '%s'\n", 137 fw_name); 138 lfw->fbdfw = fbdfw; 139 lfw->data = (const uint8_t *)fbdfw->data; 140 lfw->size = fbdfw->datasize; 141 *fw = lfw; 142 return (0); 143 } 144 145 static void 146 lkpi_fw_task(void *ctx, int pending) 147 { 148 struct lkpi_fw_task *lfwt; 149 const struct linuxkpi_firmware *fw; 150 151 KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n", 152 __func__, ctx, pending)); 153 154 lfwt = ctx; 155 if (lfwt->cont == NULL) 156 goto out; 157 158 _linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev, lfwt->gfp, 159 true); 160 161 /* 162 * Linux seems to run the callback if it cannot find the firmware. 163 * We call it in all cases as it is the only feedback to the requester. 164 */ 165 lfwt->cont(fw, lfwt->drv); 166 /* Do not assume fw is still valid! */ 167 168 out: 169 free(lfwt, M_LKPI_FW); 170 } 171 172 int 173 linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused, 174 const char *fw_name, struct device *dev, gfp_t gfp, void *drv, 175 void(*cont)(const struct linuxkpi_firmware *, void *)) 176 { 177 struct lkpi_fw_task *lfwt; 178 int error; 179 180 lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO); 181 lfwt->gfp = gfp; 182 lfwt->fw_name = fw_name; 183 lfwt->dev = dev; 184 lfwt->drv = drv; 185 lfwt->cont = cont; 186 TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt); 187 error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task); 188 189 if (error) 190 return (-error); 191 return (0); 192 } 193 194 int 195 linuxkpi_request_firmware(const struct linuxkpi_firmware **fw, 196 const char *fw_name, struct device *dev) 197 { 198 199 return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, true)); 200 } 201 202 int 203 linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw, 204 const char *fw_name, struct device *dev) 205 { 206 207 return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, 208 false)); 209 } 210 211 void 212 linuxkpi_release_firmware(const struct linuxkpi_firmware *fw) 213 { 214 215 if (fw == NULL) 216 return; 217 218 if (fw->fbdfw) 219 firmware_put(fw->fbdfw, FIRMWARE_UNLOAD); 220 free(__DECONST(void *, fw), M_LKPI_FW); 221 } 222 223 int 224 linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw, 225 const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen, 226 size_t offset) 227 { 228 const struct linuxkpi_firmware *lfw; 229 int error; 230 231 error = linuxkpi_request_firmware(fw, fw_name, dev); 232 if (error != 0) 233 return (error); 234 235 lfw = *fw; 236 if ((offset + buflen) >= lfw->size) { 237 linuxkpi_release_firmware(lfw); 238 return (-ERANGE); 239 } 240 241 memcpy(buf, lfw->data + offset, buflen); 242 243 return (0); 244 } 245