1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2013 Broadcom Corporation
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/device.h>
10 #include <linux/firmware.h>
11 #include <linux/module.h>
12 #include <linux/bcm47xx_nvram.h>
20 #define BRCMF_FW_MAX_NVRAM_SIZE 64000
21 #define BRCMF_FW_NVRAM_DEVPATH_LEN 19 /* devpath0=pcie/1/4/ */
22 #define BRCMF_FW_NVRAM_PCIEDEV_LEN 10 /* pcie/1/4/ + \0 */
23 #define BRCMF_FW_DEFAULT_BOARDREV "boardrev=0xff"
25 enum nvram_parser_state {
34 * struct nvram_parser - internal info for parser.
36 * @state: current parser state.
37 * @data: input buffer being parsed.
38 * @nvram: output buffer with parse result.
39 * @nvram_len: length of parse result.
40 * @line: current line.
41 * @column: current column in line.
42 * @pos: byte offset in input buffer.
43 * @entry: start position of key,value entry.
44 * @multi_dev_v1: detect pcie multi device v1 (compressed).
45 * @multi_dev_v2: detect pcie multi device v2.
46 * @boardrev_found: nvram contains boardrev information.
49 enum nvram_parser_state state;
63 * is_nvram_char() - check if char is a valid one for NVRAM entry
65 * It accepts all printable ASCII chars except for '#' which opens a comment.
66 * Please note that ' ' (space) while accepted is not a valid key name char.
68 static bool is_nvram_char(char c)
70 /* comment marker excluded */
74 /* key and value may have any other readable character */
75 return (c >= 0x20 && c < 0x7f);
78 static bool is_whitespace(char c)
80 return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
83 static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp)
87 c = nvp->data[nvp->pos];
90 if (is_whitespace(c) || c == '\0')
94 if (is_nvram_char(c)) {
95 nvp->entry = nvp->pos;
98 brcmf_dbg(INFO, "warning: ln=%d:col=%d: ignoring invalid character\n",
99 nvp->line, nvp->column);
106 static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp)
108 enum nvram_parser_state st = nvp->state;
111 c = nvp->data[nvp->pos];
113 /* ignore RAW1 by treating as comment */
114 if (strncmp(&nvp->data[nvp->entry], "RAW1", 4) == 0)
118 if (strncmp(&nvp->data[nvp->entry], "devpath", 7) == 0)
119 nvp->multi_dev_v1 = true;
120 if (strncmp(&nvp->data[nvp->entry], "pcie/", 5) == 0)
121 nvp->multi_dev_v2 = true;
122 if (strncmp(&nvp->data[nvp->entry], "boardrev", 8) == 0)
123 nvp->boardrev_found = true;
124 } else if (!is_nvram_char(c) || c == ' ') {
125 brcmf_dbg(INFO, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
126 nvp->line, nvp->column);
135 static enum nvram_parser_state
136 brcmf_nvram_handle_value(struct nvram_parser *nvp)
143 c = nvp->data[nvp->pos];
144 if (!is_nvram_char(c)) {
145 /* key,value pair complete */
146 ekv = (u8 *)&nvp->data[nvp->pos];
147 skv = (u8 *)&nvp->data[nvp->entry];
149 if (nvp->nvram_len + cplen + 1 >= BRCMF_FW_MAX_NVRAM_SIZE)
151 /* copy to output buffer */
152 memcpy(&nvp->nvram[nvp->nvram_len], skv, cplen);
153 nvp->nvram_len += cplen;
154 nvp->nvram[nvp->nvram_len] = '\0';
163 static enum nvram_parser_state
164 brcmf_nvram_handle_comment(struct nvram_parser *nvp)
168 sol = (char *)&nvp->data[nvp->pos];
169 eoc = strchr(sol, '\n');
171 eoc = strchr(sol, '\0');
176 /* eat all moving to next line */
179 nvp->pos += (eoc - sol) + 1;
183 static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp)
189 static enum nvram_parser_state
190 (*nv_parser_states[])(struct nvram_parser *nvp) = {
191 brcmf_nvram_handle_idle,
192 brcmf_nvram_handle_key,
193 brcmf_nvram_handle_value,
194 brcmf_nvram_handle_comment,
195 brcmf_nvram_handle_end
198 static int brcmf_init_nvram_parser(struct nvram_parser *nvp,
199 const u8 *data, size_t data_len)
203 memset(nvp, 0, sizeof(*nvp));
205 /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */
206 if (data_len > BRCMF_FW_MAX_NVRAM_SIZE)
207 size = BRCMF_FW_MAX_NVRAM_SIZE;
210 /* Alloc for extra 0 byte + roundup by 4 + length field */
211 size += 1 + 3 + sizeof(u32);
212 nvp->nvram = kzalloc(size, GFP_KERNEL);
221 /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple
222 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
223 * which data is to be returned. v1 is the version where nvram is stored
224 * compressed and "devpath" maps to index for valid entries.
226 static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr,
229 /* Device path with a leading '=' key-value separator */
230 char pci_path[] = "=pci/?/?";
232 char pcie_path[] = "=pcie/?/?";
240 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
244 /* min length: devpath0=pcie/1/4/ + 0:x=y */
245 if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6)
248 /* First search for the devpathX and see if it is the configuration
249 * for domain_nr/bus_nr. Search complete nvp
251 snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr,
253 pci_len = strlen(pci_path);
254 snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr,
256 pcie_len = strlen(pcie_path);
259 while (i < nvp->nvram_len - BRCMF_FW_NVRAM_DEVPATH_LEN) {
260 /* Format: devpathX=pcie/Y/Z/
261 * Y = domain_nr, Z = bus_nr, X = virtual ID
263 if (strncmp(&nvp->nvram[i], "devpath", 7) == 0 &&
264 (!strncmp(&nvp->nvram[i + 8], pci_path, pci_len) ||
265 !strncmp(&nvp->nvram[i + 8], pcie_path, pcie_len))) {
266 id = nvp->nvram[i + 7] - '0';
270 while (nvp->nvram[i] != 0)
277 /* Now copy all valid entries, release old nvram and assign new one */
280 while (i < nvp->nvram_len) {
281 if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) {
283 if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
284 nvp->boardrev_found = true;
285 while (nvp->nvram[i] != 0) {
286 nvram[j] = nvp->nvram[i];
293 while (nvp->nvram[i] != 0)
307 /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple
308 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
309 * which data is to be returned. v2 is the version where nvram is stored
310 * uncompressed, all relevant valid entries are identified by
311 * pcie/domain_nr/bus_nr:
313 static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr,
316 char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN];
321 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
327 /* Copy all valid entries, release old nvram and assign new one.
328 * Valid entries are of type pcie/X/Y/ where X = domain_nr and
331 snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr);
332 len = strlen(prefix);
335 while (i < nvp->nvram_len - len) {
336 if (strncmp(&nvp->nvram[i], prefix, len) == 0) {
338 if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
339 nvp->boardrev_found = true;
340 while (nvp->nvram[i] != 0) {
341 nvram[j] = nvp->nvram[i];
348 while (nvp->nvram[i] != 0)
357 static void brcmf_fw_add_defaults(struct nvram_parser *nvp)
359 if (nvp->boardrev_found)
362 memcpy(&nvp->nvram[nvp->nvram_len], &BRCMF_FW_DEFAULT_BOARDREV,
363 strlen(BRCMF_FW_DEFAULT_BOARDREV));
364 nvp->nvram_len += strlen(BRCMF_FW_DEFAULT_BOARDREV);
365 nvp->nvram[nvp->nvram_len] = '\0';
369 /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
370 * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
371 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
372 * End of buffer is completed with token identifying length of buffer.
374 static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len,
375 u32 *new_length, u16 domain_nr, u16 bus_nr)
377 struct nvram_parser nvp;
382 if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0)
385 while (nvp.pos < data_len) {
386 nvp.state = nv_parser_states[nvp.state](&nvp);
387 if (nvp.state == END)
390 if (nvp.multi_dev_v1) {
391 nvp.boardrev_found = false;
392 brcmf_fw_strip_multi_v1(&nvp, domain_nr, bus_nr);
393 } else if (nvp.multi_dev_v2) {
394 nvp.boardrev_found = false;
395 brcmf_fw_strip_multi_v2(&nvp, domain_nr, bus_nr);
398 if (nvp.nvram_len == 0) {
403 brcmf_fw_add_defaults(&nvp);
406 *new_length = roundup(nvp.nvram_len + 1, 4);
407 while (pad != *new_length) {
412 token = *new_length / 4;
413 token = (~token << 16) | (token & 0x0000FFFF);
414 token_le = cpu_to_le32(token);
416 memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le));
417 *new_length += sizeof(token_le);
422 void brcmf_fw_nvram_free(void *nvram)
429 struct brcmf_fw_request *req;
431 void (*done)(struct device *dev, int err, struct brcmf_fw_request *req);
435 /* In some cases the EFI-var stored nvram contains "ccode=ALL" or "ccode=XV"
436 * to specify "worldwide" compatible settings, but these 2 ccode-s do not work
437 * properly. "ccode=ALL" causes channels 12 and 13 to not be available,
438 * "ccode=XV" causes all 5GHz channels to not be available. So we replace both
439 * with "ccode=X2" which allows channels 12+13 and 5Ghz channels in
440 * no-Initiate-Radiation mode. This means that we will never send on these
441 * channels without first having received valid wifi traffic on the channel.
443 static void brcmf_fw_fix_efi_nvram_ccode(char *data, unsigned long data_len)
447 ccode = strnstr((char *)data, "ccode=ALL", data_len);
449 ccode = strnstr((char *)data, "ccode=XV\r", data_len);
458 static u8 *brcmf_fw_nvram_from_efi(size_t *data_len_ret)
460 const u16 name[] = { 'n', 'v', 'r', 'a', 'm', 0 };
461 struct efivar_entry *nvram_efivar;
462 unsigned long data_len = 0;
466 nvram_efivar = kzalloc(sizeof(*nvram_efivar), GFP_KERNEL);
470 memcpy(&nvram_efivar->var.VariableName, name, sizeof(name));
471 nvram_efivar->var.VendorGuid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61,
472 0xb5, 0x1f, 0x43, 0x26,
473 0x81, 0x23, 0xd1, 0x13);
475 err = efivar_entry_size(nvram_efivar, &data_len);
479 data = kmalloc(data_len, GFP_KERNEL);
483 err = efivar_entry_get(nvram_efivar, NULL, &data_len, data);
487 brcmf_fw_fix_efi_nvram_ccode(data, data_len);
488 brcmf_info("Using nvram EFI variable\n");
491 *data_len_ret = data_len;
500 static inline u8 *brcmf_fw_nvram_from_efi(size_t *data_len) { return NULL; }
503 static void brcmf_fw_free_request(struct brcmf_fw_request *req)
505 struct brcmf_fw_item *item;
508 for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
509 if (item->type == BRCMF_FW_TYPE_BINARY)
510 release_firmware(item->binary);
511 else if (item->type == BRCMF_FW_TYPE_NVRAM)
512 brcmf_fw_nvram_free(item->nv_data.data);
517 static int brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx)
519 struct brcmf_fw *fwctx = ctx;
520 struct brcmf_fw_item *cur;
521 bool free_bcm47xx_nvram = false;
522 bool kfree_nvram = false;
523 u32 nvram_length = 0;
528 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
530 cur = &fwctx->req->items[fwctx->curpos];
532 if (fw && fw->data) {
533 data = (u8 *)fw->data;
536 if ((data = bcm47xx_nvram_get_contents(&data_len)))
537 free_bcm47xx_nvram = true;
538 else if ((data = brcmf_fw_nvram_from_efi(&data_len)))
540 else if (!(cur->flags & BRCMF_FW_REQF_OPTIONAL))
545 nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length,
546 fwctx->req->domain_nr,
549 if (free_bcm47xx_nvram)
550 bcm47xx_nvram_release_contents(data);
554 release_firmware(fw);
555 if (!nvram && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
558 brcmf_dbg(TRACE, "nvram %p len %d\n", nvram, nvram_length);
559 cur->nv_data.data = nvram;
560 cur->nv_data.len = nvram_length;
567 static int brcmf_fw_complete_request(const struct firmware *fw,
568 struct brcmf_fw *fwctx)
570 struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos];
573 brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path, fw ? "" : "not ");
576 case BRCMF_FW_TYPE_NVRAM:
577 ret = brcmf_fw_request_nvram_done(fw, fwctx);
579 case BRCMF_FW_TYPE_BINARY:
586 /* something fishy here so bail out early */
587 brcmf_err("unknown fw type: %d\n", cur->type);
588 release_firmware(fw);
592 return (cur->flags & BRCMF_FW_REQF_OPTIONAL) ? 0 : ret;
595 static char *brcm_alt_fw_path(const char *path, const char *board_type)
597 char alt_path[BRCMF_FW_NAME_LEN];
600 strscpy(alt_path, path, BRCMF_FW_NAME_LEN);
601 /* At least one character + suffix */
602 if (strlen(alt_path) < 5)
605 /* strip .txt or .bin at the end */
606 strscpy(suffix, alt_path + strlen(alt_path) - 4, 5);
607 alt_path[strlen(alt_path) - 4] = 0;
608 strlcat(alt_path, ".", BRCMF_FW_NAME_LEN);
609 strlcat(alt_path, board_type, BRCMF_FW_NAME_LEN);
610 strlcat(alt_path, suffix, BRCMF_FW_NAME_LEN);
612 return kstrdup(alt_path, GFP_KERNEL);
615 static int brcmf_fw_request_firmware(const struct firmware **fw,
616 struct brcmf_fw *fwctx)
618 struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos];
621 /* Files can be board-specific, first try a board-specific path */
622 if (cur->type == BRCMF_FW_TYPE_NVRAM && fwctx->req->board_type) {
625 alt_path = brcm_alt_fw_path(cur->path, fwctx->req->board_type);
629 ret = request_firmware(fw, alt_path, fwctx->dev);
636 return request_firmware(fw, cur->path, fwctx->dev);
639 static void brcmf_fw_request_done(const struct firmware *fw, void *ctx)
641 struct brcmf_fw *fwctx = ctx;
644 ret = brcmf_fw_complete_request(fw, fwctx);
646 while (ret == 0 && ++fwctx->curpos < fwctx->req->n_items) {
647 brcmf_fw_request_firmware(&fw, fwctx);
648 ret = brcmf_fw_complete_request(fw, ctx);
652 brcmf_fw_free_request(fwctx->req);
655 fwctx->done(fwctx->dev, ret, fwctx->req);
659 static void brcmf_fw_request_done_alt_path(const struct firmware *fw, void *ctx)
661 struct brcmf_fw *fwctx = ctx;
662 struct brcmf_fw_item *first = &fwctx->req->items[0];
665 /* Fall back to canonical path if board firmware not found */
667 ret = request_firmware_nowait(THIS_MODULE, true, first->path,
668 fwctx->dev, GFP_KERNEL, fwctx,
669 brcmf_fw_request_done);
672 brcmf_fw_request_done(fw, ctx);
675 static bool brcmf_fw_request_is_valid(struct brcmf_fw_request *req)
677 struct brcmf_fw_item *item;
683 for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
690 int brcmf_fw_get_firmwares(struct device *dev, struct brcmf_fw_request *req,
691 void (*fw_cb)(struct device *dev, int err,
692 struct brcmf_fw_request *req))
694 struct brcmf_fw_item *first = &req->items[0];
695 struct brcmf_fw *fwctx;
699 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
703 if (!brcmf_fw_request_is_valid(req))
706 fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL);
714 /* First try alternative board-specific path if any */
715 alt_path = brcm_alt_fw_path(first->path, fwctx->req->board_type);
717 ret = request_firmware_nowait(THIS_MODULE, true, alt_path,
718 fwctx->dev, GFP_KERNEL, fwctx,
719 brcmf_fw_request_done_alt_path);
722 ret = request_firmware_nowait(THIS_MODULE, true, first->path,
723 fwctx->dev, GFP_KERNEL, fwctx,
724 brcmf_fw_request_done);
727 brcmf_fw_request_done(NULL, fwctx);
732 struct brcmf_fw_request *
733 brcmf_fw_alloc_request(u32 chip, u32 chiprev,
734 const struct brcmf_firmware_mapping mapping_table[],
735 u32 table_size, struct brcmf_fw_name *fwnames,
738 struct brcmf_fw_request *fwreq;
745 for (i = 0; i < table_size; i++) {
746 if (mapping_table[i].chipid == chip &&
747 mapping_table[i].revmask & BIT(chiprev))
751 brcmf_chip_name(chip, chiprev, chipname, sizeof(chipname));
753 if (i == table_size) {
754 brcmf_err("Unknown chip %s\n", chipname);
758 fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL);
762 brcmf_info("using %s for chip %s\n",
763 mapping_table[i].fw_base, chipname);
765 mp_path = brcmf_mp_global.firmware_path;
766 mp_path_len = strnlen(mp_path, BRCMF_FW_ALTPATH_LEN);
768 end = mp_path[mp_path_len - 1];
770 fwreq->n_items = n_fwnames;
772 for (j = 0; j < n_fwnames; j++) {
773 fwreq->items[j].path = fwnames[j].path;
774 fwnames[j].path[0] = '\0';
775 /* check if firmware path is provided by module parameter */
776 if (brcmf_mp_global.firmware_path[0] != '\0') {
777 strlcpy(fwnames[j].path, mp_path,
781 strlcat(fwnames[j].path, "/",
785 strlcat(fwnames[j].path, mapping_table[i].fw_base,
787 strlcat(fwnames[j].path, fwnames[j].extension,
789 fwreq->items[j].path = fwnames[j].path;