105491d2cSKalle Valo /* 205491d2cSKalle Valo * Copyright (c) 2013 Broadcom Corporation 305491d2cSKalle Valo * 405491d2cSKalle Valo * Permission to use, copy, modify, and/or distribute this software for any 505491d2cSKalle Valo * purpose with or without fee is hereby granted, provided that the above 605491d2cSKalle Valo * copyright notice and this permission notice appear in all copies. 705491d2cSKalle Valo * 805491d2cSKalle Valo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 905491d2cSKalle Valo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1005491d2cSKalle Valo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 1105491d2cSKalle Valo * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1205491d2cSKalle Valo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 1305491d2cSKalle Valo * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 1405491d2cSKalle Valo * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1505491d2cSKalle Valo */ 1605491d2cSKalle Valo 1705491d2cSKalle Valo #include <linux/kernel.h> 1805491d2cSKalle Valo #include <linux/slab.h> 1905491d2cSKalle Valo #include <linux/device.h> 2005491d2cSKalle Valo #include <linux/firmware.h> 2105491d2cSKalle Valo #include <linux/module.h> 2205491d2cSKalle Valo #include <linux/bcm47xx_nvram.h> 2305491d2cSKalle Valo 2405491d2cSKalle Valo #include "debug.h" 2505491d2cSKalle Valo #include "firmware.h" 267d34b056SHante Meuleman #include "core.h" 277d34b056SHante Meuleman #include "common.h" 28*c88cfa07SArend Van Spriel #include "chip.h" 2905491d2cSKalle Valo 3005491d2cSKalle Valo #define BRCMF_FW_MAX_NVRAM_SIZE 64000 3105491d2cSKalle Valo #define BRCMF_FW_NVRAM_DEVPATH_LEN 19 /* devpath0=pcie/1/4/ */ 3205491d2cSKalle Valo #define BRCMF_FW_NVRAM_PCIEDEV_LEN 10 /* pcie/1/4/ + \0 */ 3346f2b38aSHante Meuleman #define BRCMF_FW_DEFAULT_BOARDREV "boardrev=0xff" 3405491d2cSKalle Valo 3505491d2cSKalle Valo enum nvram_parser_state { 3605491d2cSKalle Valo IDLE, 3705491d2cSKalle Valo KEY, 3805491d2cSKalle Valo VALUE, 3905491d2cSKalle Valo COMMENT, 4005491d2cSKalle Valo END 4105491d2cSKalle Valo }; 4205491d2cSKalle Valo 4305491d2cSKalle Valo /** 4405491d2cSKalle Valo * struct nvram_parser - internal info for parser. 4505491d2cSKalle Valo * 4605491d2cSKalle Valo * @state: current parser state. 4705491d2cSKalle Valo * @data: input buffer being parsed. 4805491d2cSKalle Valo * @nvram: output buffer with parse result. 4905491d2cSKalle Valo * @nvram_len: lenght of parse result. 5005491d2cSKalle Valo * @line: current line. 5105491d2cSKalle Valo * @column: current column in line. 5205491d2cSKalle Valo * @pos: byte offset in input buffer. 5305491d2cSKalle Valo * @entry: start position of key,value entry. 5405491d2cSKalle Valo * @multi_dev_v1: detect pcie multi device v1 (compressed). 5505491d2cSKalle Valo * @multi_dev_v2: detect pcie multi device v2. 5646f2b38aSHante Meuleman * @boardrev_found: nvram contains boardrev information. 5705491d2cSKalle Valo */ 5805491d2cSKalle Valo struct nvram_parser { 5905491d2cSKalle Valo enum nvram_parser_state state; 6005491d2cSKalle Valo const u8 *data; 6105491d2cSKalle Valo u8 *nvram; 6205491d2cSKalle Valo u32 nvram_len; 6305491d2cSKalle Valo u32 line; 6405491d2cSKalle Valo u32 column; 6505491d2cSKalle Valo u32 pos; 6605491d2cSKalle Valo u32 entry; 6705491d2cSKalle Valo bool multi_dev_v1; 6805491d2cSKalle Valo bool multi_dev_v2; 6946f2b38aSHante Meuleman bool boardrev_found; 7005491d2cSKalle Valo }; 7105491d2cSKalle Valo 7205491d2cSKalle Valo /** 7305491d2cSKalle Valo * is_nvram_char() - check if char is a valid one for NVRAM entry 7405491d2cSKalle Valo * 7505491d2cSKalle Valo * It accepts all printable ASCII chars except for '#' which opens a comment. 7605491d2cSKalle Valo * Please note that ' ' (space) while accepted is not a valid key name char. 7705491d2cSKalle Valo */ 7805491d2cSKalle Valo static bool is_nvram_char(char c) 7905491d2cSKalle Valo { 8005491d2cSKalle Valo /* comment marker excluded */ 8105491d2cSKalle Valo if (c == '#') 8205491d2cSKalle Valo return false; 8305491d2cSKalle Valo 8405491d2cSKalle Valo /* key and value may have any other readable character */ 8505491d2cSKalle Valo return (c >= 0x20 && c < 0x7f); 8605491d2cSKalle Valo } 8705491d2cSKalle Valo 8805491d2cSKalle Valo static bool is_whitespace(char c) 8905491d2cSKalle Valo { 9005491d2cSKalle Valo return (c == ' ' || c == '\r' || c == '\n' || c == '\t'); 9105491d2cSKalle Valo } 9205491d2cSKalle Valo 9305491d2cSKalle Valo static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp) 9405491d2cSKalle Valo { 9505491d2cSKalle Valo char c; 9605491d2cSKalle Valo 9705491d2cSKalle Valo c = nvp->data[nvp->pos]; 9805491d2cSKalle Valo if (c == '\n') 9905491d2cSKalle Valo return COMMENT; 100d1e61b86SRafał Miłecki if (is_whitespace(c) || c == '\0') 10105491d2cSKalle Valo goto proceed; 10205491d2cSKalle Valo if (c == '#') 10305491d2cSKalle Valo return COMMENT; 10405491d2cSKalle Valo if (is_nvram_char(c)) { 10505491d2cSKalle Valo nvp->entry = nvp->pos; 10605491d2cSKalle Valo return KEY; 10705491d2cSKalle Valo } 10805491d2cSKalle Valo brcmf_dbg(INFO, "warning: ln=%d:col=%d: ignoring invalid character\n", 10905491d2cSKalle Valo nvp->line, nvp->column); 11005491d2cSKalle Valo proceed: 11105491d2cSKalle Valo nvp->column++; 11205491d2cSKalle Valo nvp->pos++; 11305491d2cSKalle Valo return IDLE; 11405491d2cSKalle Valo } 11505491d2cSKalle Valo 11605491d2cSKalle Valo static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp) 11705491d2cSKalle Valo { 11805491d2cSKalle Valo enum nvram_parser_state st = nvp->state; 11905491d2cSKalle Valo char c; 12005491d2cSKalle Valo 12105491d2cSKalle Valo c = nvp->data[nvp->pos]; 12205491d2cSKalle Valo if (c == '=') { 12305491d2cSKalle Valo /* ignore RAW1 by treating as comment */ 12405491d2cSKalle Valo if (strncmp(&nvp->data[nvp->entry], "RAW1", 4) == 0) 12505491d2cSKalle Valo st = COMMENT; 12605491d2cSKalle Valo else 12705491d2cSKalle Valo st = VALUE; 12805491d2cSKalle Valo if (strncmp(&nvp->data[nvp->entry], "devpath", 7) == 0) 12905491d2cSKalle Valo nvp->multi_dev_v1 = true; 13005491d2cSKalle Valo if (strncmp(&nvp->data[nvp->entry], "pcie/", 5) == 0) 13105491d2cSKalle Valo nvp->multi_dev_v2 = true; 13246f2b38aSHante Meuleman if (strncmp(&nvp->data[nvp->entry], "boardrev", 8) == 0) 13346f2b38aSHante Meuleman nvp->boardrev_found = true; 13405491d2cSKalle Valo } else if (!is_nvram_char(c) || c == ' ') { 13505491d2cSKalle Valo brcmf_dbg(INFO, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n", 13605491d2cSKalle Valo nvp->line, nvp->column); 13705491d2cSKalle Valo return COMMENT; 13805491d2cSKalle Valo } 13905491d2cSKalle Valo 14005491d2cSKalle Valo nvp->column++; 14105491d2cSKalle Valo nvp->pos++; 14205491d2cSKalle Valo return st; 14305491d2cSKalle Valo } 14405491d2cSKalle Valo 14505491d2cSKalle Valo static enum nvram_parser_state 14605491d2cSKalle Valo brcmf_nvram_handle_value(struct nvram_parser *nvp) 14705491d2cSKalle Valo { 14805491d2cSKalle Valo char c; 14905491d2cSKalle Valo char *skv; 15005491d2cSKalle Valo char *ekv; 15105491d2cSKalle Valo u32 cplen; 15205491d2cSKalle Valo 15305491d2cSKalle Valo c = nvp->data[nvp->pos]; 15405491d2cSKalle Valo if (!is_nvram_char(c)) { 15505491d2cSKalle Valo /* key,value pair complete */ 15605491d2cSKalle Valo ekv = (u8 *)&nvp->data[nvp->pos]; 15705491d2cSKalle Valo skv = (u8 *)&nvp->data[nvp->entry]; 15805491d2cSKalle Valo cplen = ekv - skv; 15905491d2cSKalle Valo if (nvp->nvram_len + cplen + 1 >= BRCMF_FW_MAX_NVRAM_SIZE) 16005491d2cSKalle Valo return END; 16105491d2cSKalle Valo /* copy to output buffer */ 16205491d2cSKalle Valo memcpy(&nvp->nvram[nvp->nvram_len], skv, cplen); 16305491d2cSKalle Valo nvp->nvram_len += cplen; 16405491d2cSKalle Valo nvp->nvram[nvp->nvram_len] = '\0'; 16505491d2cSKalle Valo nvp->nvram_len++; 16605491d2cSKalle Valo return IDLE; 16705491d2cSKalle Valo } 16805491d2cSKalle Valo nvp->pos++; 16905491d2cSKalle Valo nvp->column++; 17005491d2cSKalle Valo return VALUE; 17105491d2cSKalle Valo } 17205491d2cSKalle Valo 17305491d2cSKalle Valo static enum nvram_parser_state 17405491d2cSKalle Valo brcmf_nvram_handle_comment(struct nvram_parser *nvp) 17505491d2cSKalle Valo { 17605491d2cSKalle Valo char *eoc, *sol; 17705491d2cSKalle Valo 17805491d2cSKalle Valo sol = (char *)&nvp->data[nvp->pos]; 17905491d2cSKalle Valo eoc = strchr(sol, '\n'); 18005491d2cSKalle Valo if (!eoc) { 18105491d2cSKalle Valo eoc = strchr(sol, '\0'); 18205491d2cSKalle Valo if (!eoc) 18305491d2cSKalle Valo return END; 18405491d2cSKalle Valo } 18505491d2cSKalle Valo 18605491d2cSKalle Valo /* eat all moving to next line */ 18705491d2cSKalle Valo nvp->line++; 18805491d2cSKalle Valo nvp->column = 1; 18905491d2cSKalle Valo nvp->pos += (eoc - sol) + 1; 19005491d2cSKalle Valo return IDLE; 19105491d2cSKalle Valo } 19205491d2cSKalle Valo 19305491d2cSKalle Valo static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp) 19405491d2cSKalle Valo { 19505491d2cSKalle Valo /* final state */ 19605491d2cSKalle Valo return END; 19705491d2cSKalle Valo } 19805491d2cSKalle Valo 19905491d2cSKalle Valo static enum nvram_parser_state 20005491d2cSKalle Valo (*nv_parser_states[])(struct nvram_parser *nvp) = { 20105491d2cSKalle Valo brcmf_nvram_handle_idle, 20205491d2cSKalle Valo brcmf_nvram_handle_key, 20305491d2cSKalle Valo brcmf_nvram_handle_value, 20405491d2cSKalle Valo brcmf_nvram_handle_comment, 20505491d2cSKalle Valo brcmf_nvram_handle_end 20605491d2cSKalle Valo }; 20705491d2cSKalle Valo 20805491d2cSKalle Valo static int brcmf_init_nvram_parser(struct nvram_parser *nvp, 20905491d2cSKalle Valo const u8 *data, size_t data_len) 21005491d2cSKalle Valo { 21105491d2cSKalle Valo size_t size; 21205491d2cSKalle Valo 21305491d2cSKalle Valo memset(nvp, 0, sizeof(*nvp)); 21405491d2cSKalle Valo nvp->data = data; 21505491d2cSKalle Valo /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */ 21605491d2cSKalle Valo if (data_len > BRCMF_FW_MAX_NVRAM_SIZE) 21705491d2cSKalle Valo size = BRCMF_FW_MAX_NVRAM_SIZE; 21805491d2cSKalle Valo else 21905491d2cSKalle Valo size = data_len; 22005491d2cSKalle Valo /* Alloc for extra 0 byte + roundup by 4 + length field */ 22105491d2cSKalle Valo size += 1 + 3 + sizeof(u32); 22205491d2cSKalle Valo nvp->nvram = kzalloc(size, GFP_KERNEL); 22305491d2cSKalle Valo if (!nvp->nvram) 22405491d2cSKalle Valo return -ENOMEM; 22505491d2cSKalle Valo 22605491d2cSKalle Valo nvp->line = 1; 22705491d2cSKalle Valo nvp->column = 1; 22805491d2cSKalle Valo return 0; 22905491d2cSKalle Valo } 23005491d2cSKalle Valo 23105491d2cSKalle Valo /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple 23205491d2cSKalle Valo * devices. Strip it down for one device, use domain_nr/bus_nr to determine 23305491d2cSKalle Valo * which data is to be returned. v1 is the version where nvram is stored 23405491d2cSKalle Valo * compressed and "devpath" maps to index for valid entries. 23505491d2cSKalle Valo */ 23605491d2cSKalle Valo static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr, 23705491d2cSKalle Valo u16 bus_nr) 23805491d2cSKalle Valo { 23905491d2cSKalle Valo /* Device path with a leading '=' key-value separator */ 24005491d2cSKalle Valo char pci_path[] = "=pci/?/?"; 24105491d2cSKalle Valo size_t pci_len; 24205491d2cSKalle Valo char pcie_path[] = "=pcie/?/?"; 24305491d2cSKalle Valo size_t pcie_len; 24405491d2cSKalle Valo 24505491d2cSKalle Valo u32 i, j; 24605491d2cSKalle Valo bool found; 24705491d2cSKalle Valo u8 *nvram; 24805491d2cSKalle Valo u8 id; 24905491d2cSKalle Valo 25005491d2cSKalle Valo nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL); 25105491d2cSKalle Valo if (!nvram) 25205491d2cSKalle Valo goto fail; 25305491d2cSKalle Valo 25405491d2cSKalle Valo /* min length: devpath0=pcie/1/4/ + 0:x=y */ 25505491d2cSKalle Valo if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6) 25605491d2cSKalle Valo goto fail; 25705491d2cSKalle Valo 25805491d2cSKalle Valo /* First search for the devpathX and see if it is the configuration 25905491d2cSKalle Valo * for domain_nr/bus_nr. Search complete nvp 26005491d2cSKalle Valo */ 26105491d2cSKalle Valo snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr, 26205491d2cSKalle Valo bus_nr); 26305491d2cSKalle Valo pci_len = strlen(pci_path); 26405491d2cSKalle Valo snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr, 26505491d2cSKalle Valo bus_nr); 26605491d2cSKalle Valo pcie_len = strlen(pcie_path); 26705491d2cSKalle Valo found = false; 26805491d2cSKalle Valo i = 0; 26905491d2cSKalle Valo while (i < nvp->nvram_len - BRCMF_FW_NVRAM_DEVPATH_LEN) { 27005491d2cSKalle Valo /* Format: devpathX=pcie/Y/Z/ 27105491d2cSKalle Valo * Y = domain_nr, Z = bus_nr, X = virtual ID 27205491d2cSKalle Valo */ 27305491d2cSKalle Valo if (strncmp(&nvp->nvram[i], "devpath", 7) == 0 && 27405491d2cSKalle Valo (!strncmp(&nvp->nvram[i + 8], pci_path, pci_len) || 27505491d2cSKalle Valo !strncmp(&nvp->nvram[i + 8], pcie_path, pcie_len))) { 27605491d2cSKalle Valo id = nvp->nvram[i + 7] - '0'; 27705491d2cSKalle Valo found = true; 27805491d2cSKalle Valo break; 27905491d2cSKalle Valo } 28005491d2cSKalle Valo while (nvp->nvram[i] != 0) 28105491d2cSKalle Valo i++; 28205491d2cSKalle Valo i++; 28305491d2cSKalle Valo } 28405491d2cSKalle Valo if (!found) 28505491d2cSKalle Valo goto fail; 28605491d2cSKalle Valo 28705491d2cSKalle Valo /* Now copy all valid entries, release old nvram and assign new one */ 28805491d2cSKalle Valo i = 0; 28905491d2cSKalle Valo j = 0; 29005491d2cSKalle Valo while (i < nvp->nvram_len) { 29105491d2cSKalle Valo if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) { 29205491d2cSKalle Valo i += 2; 29346f2b38aSHante Meuleman if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0) 29446f2b38aSHante Meuleman nvp->boardrev_found = true; 29505491d2cSKalle Valo while (nvp->nvram[i] != 0) { 29605491d2cSKalle Valo nvram[j] = nvp->nvram[i]; 29705491d2cSKalle Valo i++; 29805491d2cSKalle Valo j++; 29905491d2cSKalle Valo } 30005491d2cSKalle Valo nvram[j] = 0; 30105491d2cSKalle Valo j++; 30205491d2cSKalle Valo } 30305491d2cSKalle Valo while (nvp->nvram[i] != 0) 30405491d2cSKalle Valo i++; 30505491d2cSKalle Valo i++; 30605491d2cSKalle Valo } 30705491d2cSKalle Valo kfree(nvp->nvram); 30805491d2cSKalle Valo nvp->nvram = nvram; 30905491d2cSKalle Valo nvp->nvram_len = j; 31005491d2cSKalle Valo return; 31105491d2cSKalle Valo 31205491d2cSKalle Valo fail: 31305491d2cSKalle Valo kfree(nvram); 31405491d2cSKalle Valo nvp->nvram_len = 0; 31505491d2cSKalle Valo } 31605491d2cSKalle Valo 31705491d2cSKalle Valo /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple 31805491d2cSKalle Valo * devices. Strip it down for one device, use domain_nr/bus_nr to determine 31905491d2cSKalle Valo * which data is to be returned. v2 is the version where nvram is stored 32005491d2cSKalle Valo * uncompressed, all relevant valid entries are identified by 32105491d2cSKalle Valo * pcie/domain_nr/bus_nr: 32205491d2cSKalle Valo */ 32305491d2cSKalle Valo static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr, 32405491d2cSKalle Valo u16 bus_nr) 32505491d2cSKalle Valo { 32605491d2cSKalle Valo char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN]; 32705491d2cSKalle Valo size_t len; 32805491d2cSKalle Valo u32 i, j; 32905491d2cSKalle Valo u8 *nvram; 33005491d2cSKalle Valo 33105491d2cSKalle Valo nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL); 33205491d2cSKalle Valo if (!nvram) 33305491d2cSKalle Valo goto fail; 33405491d2cSKalle Valo 33505491d2cSKalle Valo /* Copy all valid entries, release old nvram and assign new one. 33605491d2cSKalle Valo * Valid entries are of type pcie/X/Y/ where X = domain_nr and 33705491d2cSKalle Valo * Y = bus_nr. 33805491d2cSKalle Valo */ 33905491d2cSKalle Valo snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr); 34005491d2cSKalle Valo len = strlen(prefix); 34105491d2cSKalle Valo i = 0; 34205491d2cSKalle Valo j = 0; 34305491d2cSKalle Valo while (i < nvp->nvram_len - len) { 34405491d2cSKalle Valo if (strncmp(&nvp->nvram[i], prefix, len) == 0) { 34505491d2cSKalle Valo i += len; 34646f2b38aSHante Meuleman if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0) 34746f2b38aSHante Meuleman nvp->boardrev_found = true; 34805491d2cSKalle Valo while (nvp->nvram[i] != 0) { 34905491d2cSKalle Valo nvram[j] = nvp->nvram[i]; 35005491d2cSKalle Valo i++; 35105491d2cSKalle Valo j++; 35205491d2cSKalle Valo } 35305491d2cSKalle Valo nvram[j] = 0; 35405491d2cSKalle Valo j++; 35505491d2cSKalle Valo } 35605491d2cSKalle Valo while (nvp->nvram[i] != 0) 35705491d2cSKalle Valo i++; 35805491d2cSKalle Valo i++; 35905491d2cSKalle Valo } 36005491d2cSKalle Valo kfree(nvp->nvram); 36105491d2cSKalle Valo nvp->nvram = nvram; 36205491d2cSKalle Valo nvp->nvram_len = j; 36305491d2cSKalle Valo return; 36405491d2cSKalle Valo fail: 36505491d2cSKalle Valo kfree(nvram); 36605491d2cSKalle Valo nvp->nvram_len = 0; 36705491d2cSKalle Valo } 36805491d2cSKalle Valo 36946f2b38aSHante Meuleman static void brcmf_fw_add_defaults(struct nvram_parser *nvp) 37046f2b38aSHante Meuleman { 37146f2b38aSHante Meuleman if (nvp->boardrev_found) 37246f2b38aSHante Meuleman return; 37346f2b38aSHante Meuleman 37446f2b38aSHante Meuleman memcpy(&nvp->nvram[nvp->nvram_len], &BRCMF_FW_DEFAULT_BOARDREV, 37546f2b38aSHante Meuleman strlen(BRCMF_FW_DEFAULT_BOARDREV)); 37646f2b38aSHante Meuleman nvp->nvram_len += strlen(BRCMF_FW_DEFAULT_BOARDREV); 37746f2b38aSHante Meuleman nvp->nvram[nvp->nvram_len] = '\0'; 37846f2b38aSHante Meuleman nvp->nvram_len++; 37946f2b38aSHante Meuleman } 38046f2b38aSHante Meuleman 38105491d2cSKalle Valo /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil 38205491d2cSKalle Valo * and ending in a NUL. Removes carriage returns, empty lines, comment lines, 38305491d2cSKalle Valo * and converts newlines to NULs. Shortens buffer as needed and pads with NULs. 38405491d2cSKalle Valo * End of buffer is completed with token identifying length of buffer. 38505491d2cSKalle Valo */ 38605491d2cSKalle Valo static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len, 38705491d2cSKalle Valo u32 *new_length, u16 domain_nr, u16 bus_nr) 38805491d2cSKalle Valo { 38905491d2cSKalle Valo struct nvram_parser nvp; 39005491d2cSKalle Valo u32 pad; 39105491d2cSKalle Valo u32 token; 39205491d2cSKalle Valo __le32 token_le; 39305491d2cSKalle Valo 39405491d2cSKalle Valo if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0) 39505491d2cSKalle Valo return NULL; 39605491d2cSKalle Valo 39705491d2cSKalle Valo while (nvp.pos < data_len) { 39805491d2cSKalle Valo nvp.state = nv_parser_states[nvp.state](&nvp); 39905491d2cSKalle Valo if (nvp.state == END) 40005491d2cSKalle Valo break; 40105491d2cSKalle Valo } 40246f2b38aSHante Meuleman if (nvp.multi_dev_v1) { 40346f2b38aSHante Meuleman nvp.boardrev_found = false; 40405491d2cSKalle Valo brcmf_fw_strip_multi_v1(&nvp, domain_nr, bus_nr); 40546f2b38aSHante Meuleman } else if (nvp.multi_dev_v2) { 40646f2b38aSHante Meuleman nvp.boardrev_found = false; 40705491d2cSKalle Valo brcmf_fw_strip_multi_v2(&nvp, domain_nr, bus_nr); 40846f2b38aSHante Meuleman } 40905491d2cSKalle Valo 41005491d2cSKalle Valo if (nvp.nvram_len == 0) { 41105491d2cSKalle Valo kfree(nvp.nvram); 41205491d2cSKalle Valo return NULL; 41305491d2cSKalle Valo } 41405491d2cSKalle Valo 41546f2b38aSHante Meuleman brcmf_fw_add_defaults(&nvp); 41646f2b38aSHante Meuleman 41705491d2cSKalle Valo pad = nvp.nvram_len; 41805491d2cSKalle Valo *new_length = roundup(nvp.nvram_len + 1, 4); 41905491d2cSKalle Valo while (pad != *new_length) { 42005491d2cSKalle Valo nvp.nvram[pad] = 0; 42105491d2cSKalle Valo pad++; 42205491d2cSKalle Valo } 42305491d2cSKalle Valo 42405491d2cSKalle Valo token = *new_length / 4; 42505491d2cSKalle Valo token = (~token << 16) | (token & 0x0000FFFF); 42605491d2cSKalle Valo token_le = cpu_to_le32(token); 42705491d2cSKalle Valo 42805491d2cSKalle Valo memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le)); 42905491d2cSKalle Valo *new_length += sizeof(token_le); 43005491d2cSKalle Valo 43105491d2cSKalle Valo return nvp.nvram; 43205491d2cSKalle Valo } 43305491d2cSKalle Valo 43405491d2cSKalle Valo void brcmf_fw_nvram_free(void *nvram) 43505491d2cSKalle Valo { 43605491d2cSKalle Valo kfree(nvram); 43705491d2cSKalle Valo } 43805491d2cSKalle Valo 43905491d2cSKalle Valo struct brcmf_fw { 44005491d2cSKalle Valo struct device *dev; 44105491d2cSKalle Valo u16 flags; 44205491d2cSKalle Valo const struct firmware *code; 44305491d2cSKalle Valo const char *nvram_name; 44405491d2cSKalle Valo u16 domain_nr; 44505491d2cSKalle Valo u16 bus_nr; 4466d0507a7SArend Van Spriel void (*done)(struct device *dev, int err, const struct firmware *fw, 44705491d2cSKalle Valo void *nvram_image, u32 nvram_len); 44805491d2cSKalle Valo }; 44905491d2cSKalle Valo 45005491d2cSKalle Valo static void brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx) 45105491d2cSKalle Valo { 45205491d2cSKalle Valo struct brcmf_fw *fwctx = ctx; 45305491d2cSKalle Valo u32 nvram_length = 0; 45405491d2cSKalle Valo void *nvram = NULL; 45505491d2cSKalle Valo u8 *data = NULL; 45605491d2cSKalle Valo size_t data_len; 45705491d2cSKalle Valo bool raw_nvram; 45805491d2cSKalle Valo 45905491d2cSKalle Valo brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev)); 46005491d2cSKalle Valo if (fw && fw->data) { 46105491d2cSKalle Valo data = (u8 *)fw->data; 46205491d2cSKalle Valo data_len = fw->size; 46305491d2cSKalle Valo raw_nvram = false; 46405491d2cSKalle Valo } else { 46505491d2cSKalle Valo data = bcm47xx_nvram_get_contents(&data_len); 46605491d2cSKalle Valo if (!data && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL)) 46705491d2cSKalle Valo goto fail; 46805491d2cSKalle Valo raw_nvram = true; 46905491d2cSKalle Valo } 47005491d2cSKalle Valo 47105491d2cSKalle Valo if (data) 47205491d2cSKalle Valo nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length, 47305491d2cSKalle Valo fwctx->domain_nr, fwctx->bus_nr); 47405491d2cSKalle Valo 47505491d2cSKalle Valo if (raw_nvram) 47605491d2cSKalle Valo bcm47xx_nvram_release_contents(data); 47705491d2cSKalle Valo release_firmware(fw); 47805491d2cSKalle Valo if (!nvram && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL)) 47905491d2cSKalle Valo goto fail; 48005491d2cSKalle Valo 4816d0507a7SArend Van Spriel fwctx->done(fwctx->dev, 0, fwctx->code, nvram, nvram_length); 48205491d2cSKalle Valo kfree(fwctx); 48305491d2cSKalle Valo return; 48405491d2cSKalle Valo 48505491d2cSKalle Valo fail: 48605491d2cSKalle Valo brcmf_dbg(TRACE, "failed: dev=%s\n", dev_name(fwctx->dev)); 48705491d2cSKalle Valo release_firmware(fwctx->code); 48803fb0e83SArend Van Spriel fwctx->done(fwctx->dev, -ENOENT, NULL, NULL, 0); 48905491d2cSKalle Valo kfree(fwctx); 49005491d2cSKalle Valo } 49105491d2cSKalle Valo 49205491d2cSKalle Valo static void brcmf_fw_request_code_done(const struct firmware *fw, void *ctx) 49305491d2cSKalle Valo { 49405491d2cSKalle Valo struct brcmf_fw *fwctx = ctx; 49503fb0e83SArend Van Spriel int ret = 0; 49605491d2cSKalle Valo 49705491d2cSKalle Valo brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev)); 49803fb0e83SArend Van Spriel if (!fw) { 49903fb0e83SArend Van Spriel ret = -ENOENT; 50005491d2cSKalle Valo goto fail; 50105491d2cSKalle Valo } 50203fb0e83SArend Van Spriel /* only requested code so done here */ 50303fb0e83SArend Van Spriel if (!(fwctx->flags & BRCMF_FW_REQUEST_NVRAM)) 50403fb0e83SArend Van Spriel goto done; 50503fb0e83SArend Van Spriel 50605491d2cSKalle Valo fwctx->code = fw; 50705491d2cSKalle Valo ret = request_firmware_nowait(THIS_MODULE, true, fwctx->nvram_name, 50805491d2cSKalle Valo fwctx->dev, GFP_KERNEL, fwctx, 50905491d2cSKalle Valo brcmf_fw_request_nvram_done); 51005491d2cSKalle Valo 51103fb0e83SArend Van Spriel /* pass NULL to nvram callback for bcm47xx fallback */ 51203fb0e83SArend Van Spriel if (ret) 51305491d2cSKalle Valo brcmf_fw_request_nvram_done(NULL, fwctx); 51405491d2cSKalle Valo return; 51505491d2cSKalle Valo 51605491d2cSKalle Valo fail: 51705491d2cSKalle Valo brcmf_dbg(TRACE, "failed: dev=%s\n", dev_name(fwctx->dev)); 51803fb0e83SArend Van Spriel done: 51903fb0e83SArend Van Spriel fwctx->done(fwctx->dev, ret, fw, NULL, 0); 52005491d2cSKalle Valo kfree(fwctx); 52105491d2cSKalle Valo } 52205491d2cSKalle Valo 52305491d2cSKalle Valo int brcmf_fw_get_firmwares_pcie(struct device *dev, u16 flags, 52405491d2cSKalle Valo const char *code, const char *nvram, 5256d0507a7SArend Van Spriel void (*fw_cb)(struct device *dev, int err, 52605491d2cSKalle Valo const struct firmware *fw, 52705491d2cSKalle Valo void *nvram_image, u32 nvram_len), 52805491d2cSKalle Valo u16 domain_nr, u16 bus_nr) 52905491d2cSKalle Valo { 53005491d2cSKalle Valo struct brcmf_fw *fwctx; 53105491d2cSKalle Valo 53205491d2cSKalle Valo brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev)); 53305491d2cSKalle Valo if (!fw_cb || !code) 53405491d2cSKalle Valo return -EINVAL; 53505491d2cSKalle Valo 53605491d2cSKalle Valo if ((flags & BRCMF_FW_REQUEST_NVRAM) && !nvram) 53705491d2cSKalle Valo return -EINVAL; 53805491d2cSKalle Valo 53905491d2cSKalle Valo fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL); 54005491d2cSKalle Valo if (!fwctx) 54105491d2cSKalle Valo return -ENOMEM; 54205491d2cSKalle Valo 54305491d2cSKalle Valo fwctx->dev = dev; 54405491d2cSKalle Valo fwctx->flags = flags; 54505491d2cSKalle Valo fwctx->done = fw_cb; 54605491d2cSKalle Valo if (flags & BRCMF_FW_REQUEST_NVRAM) 54705491d2cSKalle Valo fwctx->nvram_name = nvram; 54805491d2cSKalle Valo fwctx->domain_nr = domain_nr; 54905491d2cSKalle Valo fwctx->bus_nr = bus_nr; 55005491d2cSKalle Valo 55105491d2cSKalle Valo return request_firmware_nowait(THIS_MODULE, true, code, dev, 55205491d2cSKalle Valo GFP_KERNEL, fwctx, 55305491d2cSKalle Valo brcmf_fw_request_code_done); 55405491d2cSKalle Valo } 55505491d2cSKalle Valo 55605491d2cSKalle Valo int brcmf_fw_get_firmwares(struct device *dev, u16 flags, 55705491d2cSKalle Valo const char *code, const char *nvram, 5586d0507a7SArend Van Spriel void (*fw_cb)(struct device *dev, int err, 55905491d2cSKalle Valo const struct firmware *fw, 56005491d2cSKalle Valo void *nvram_image, u32 nvram_len)) 56105491d2cSKalle Valo { 56205491d2cSKalle Valo return brcmf_fw_get_firmwares_pcie(dev, flags, code, nvram, fw_cb, 0, 56305491d2cSKalle Valo 0); 56405491d2cSKalle Valo } 56505491d2cSKalle Valo 56646d703a7SHante Meuleman int brcmf_fw_map_chip_to_name(u32 chip, u32 chiprev, 56746d703a7SHante Meuleman struct brcmf_firmware_mapping mapping_table[], 56846d703a7SHante Meuleman u32 table_size, char fw_name[BRCMF_FW_NAME_LEN], 56946d703a7SHante Meuleman char nvram_name[BRCMF_FW_NAME_LEN]) 57046d703a7SHante Meuleman { 571*c88cfa07SArend Van Spriel char chipname[12]; 57246d703a7SHante Meuleman u32 i; 57346d703a7SHante Meuleman char end; 57446d703a7SHante Meuleman 57546d703a7SHante Meuleman for (i = 0; i < table_size; i++) { 57646d703a7SHante Meuleman if (mapping_table[i].chipid == chip && 57746d703a7SHante Meuleman mapping_table[i].revmask & BIT(chiprev)) 57846d703a7SHante Meuleman break; 57946d703a7SHante Meuleman } 58046d703a7SHante Meuleman 58146d703a7SHante Meuleman if (i == table_size) { 58246d703a7SHante Meuleman brcmf_err("Unknown chipid %d [%d]\n", chip, chiprev); 58346d703a7SHante Meuleman return -ENODEV; 58446d703a7SHante Meuleman } 58546d703a7SHante Meuleman 586*c88cfa07SArend Van Spriel brcmf_chip_name(chip, chiprev, chipname, sizeof(chipname)); 587*c88cfa07SArend Van Spriel 58846d703a7SHante Meuleman /* check if firmware path is provided by module parameter */ 5897d34b056SHante Meuleman if (brcmf_mp_global.firmware_path[0] != '\0') { 5907d34b056SHante Meuleman strlcpy(fw_name, brcmf_mp_global.firmware_path, 5917d34b056SHante Meuleman BRCMF_FW_NAME_LEN); 59246d703a7SHante Meuleman if ((nvram_name) && (mapping_table[i].nvram)) 5937d34b056SHante Meuleman strlcpy(nvram_name, brcmf_mp_global.firmware_path, 59446d703a7SHante Meuleman BRCMF_FW_NAME_LEN); 59546d703a7SHante Meuleman 5967d34b056SHante Meuleman end = brcmf_mp_global.firmware_path[ 5977d34b056SHante Meuleman strlen(brcmf_mp_global.firmware_path) - 1]; 59846d703a7SHante Meuleman if (end != '/') { 59946d703a7SHante Meuleman strlcat(fw_name, "/", BRCMF_FW_NAME_LEN); 60046d703a7SHante Meuleman if ((nvram_name) && (mapping_table[i].nvram)) 60146d703a7SHante Meuleman strlcat(nvram_name, "/", BRCMF_FW_NAME_LEN); 60246d703a7SHante Meuleman } 60346d703a7SHante Meuleman } 60446d703a7SHante Meuleman strlcat(fw_name, mapping_table[i].fw, BRCMF_FW_NAME_LEN); 60546d703a7SHante Meuleman if ((nvram_name) && (mapping_table[i].nvram)) 60646d703a7SHante Meuleman strlcat(nvram_name, mapping_table[i].nvram, BRCMF_FW_NAME_LEN); 60746d703a7SHante Meuleman 608*c88cfa07SArend Van Spriel brcmf_info("using %s for chip %s\n", fw_name, chipname); 609f38966a7SHans de Goede 61046d703a7SHante Meuleman return 0; 61146d703a7SHante Meuleman } 61246d703a7SHante Meuleman 613