2 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 /* Algorithmic part of the firmware download.
18 * To be included in the container file providing framework
21 #define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
22 #define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
23 #define wil_hex_dump_fw(prefix_str, prefix_type, rowsize, \
24 groupsize, buf, len, ascii) \
25 print_hex_dump_debug("DBG[ FW ]" prefix_str, \
26 prefix_type, rowsize, \
27 groupsize, buf, len, ascii)
29 static bool wil_fw_addr_check(struct wil6210_priv *wil,
30 void __iomem **ioaddr, __le32 val,
31 u32 size, const char *msg)
33 *ioaddr = wmi_buffer_block(wil, val, size);
35 wil_err_fw(wil, "bad %s: 0x%08x\n", msg, le32_to_cpu(val));
42 * wil_fw_verify - verify firmware file validity
44 * perform various checks for the firmware file header.
45 * records are not validated.
47 * Return file size or negative error
49 static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
51 const struct wil_fw_record_head *hdr = (const void *)data;
52 struct wil_fw_record_file_header fh;
53 const struct wil_fw_record_file_header *fh_;
58 wil_err_fw(wil, "image size not aligned: %zu\n", size);
61 /* have enough data for the file header? */
62 if (size < sizeof(*hdr) + sizeof(fh)) {
63 wil_err_fw(wil, "file too short: %zu bytes\n", size);
67 /* start with the file header? */
68 if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
69 wil_err_fw(wil, "no file header\n");
74 fh_ = (struct wil_fw_record_file_header *)&hdr[1];
75 dlen = le32_to_cpu(fh_->data_len);
77 wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
81 wil_err_fw(wil, "file truncated at %zu/%lu\n",
85 if (dlen < sizeof(*hdr) + sizeof(fh)) {
86 wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
91 if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
92 wil_err_fw(wil, "bad header signature: 0x%08x\n",
93 le32_to_cpu(fh_->signature));
98 if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
99 wil_err_fw(wil, "unsupported header version: %d\n",
100 le32_to_cpu(fh_->version));
104 /* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
108 crc = crc32_le(~0, (unsigned char const *)hdr, sizeof(*hdr));
109 crc = crc32_le(crc, (unsigned char const *)&fh, sizeof(fh));
110 crc = crc32_le(crc, (unsigned char const *)&fh_[1],
111 dlen - sizeof(*hdr) - sizeof(fh));
114 if (crc != le32_to_cpu(fh_->crc)) {
115 wil_err_fw(wil, "checksum mismatch:"
116 " calculated for %lu bytes 0x%08x != 0x%08x\n",
117 (ulong)dlen, crc, le32_to_cpu(fh_->crc));
124 static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
130 static int fw_handle_comment(struct wil6210_priv *wil, const void *data,
133 wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
139 fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
142 const struct wil_fw_record_capabilities *rec = data;
145 if (size < sizeof(*rec) ||
146 le32_to_cpu(rec->magic) != WIL_FW_CAPABILITIES_MAGIC)
149 capa_size = size - offsetof(struct wil_fw_record_capabilities,
151 bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
152 memcpy(wil->fw_capabilities, rec->capabilities,
153 min(sizeof(wil->fw_capabilities), capa_size));
154 wil_hex_dump_fw("CAPA", DUMP_PREFIX_OFFSET, 16, 1,
155 rec->capabilities, capa_size, false);
159 static int fw_handle_data(struct wil6210_priv *wil, const void *data,
162 const struct wil_fw_record_data *d = data;
164 size_t s = size - sizeof(*d);
166 if (size < sizeof(*d) + sizeof(u32)) {
167 wil_err_fw(wil, "data record too short: %zu\n", size);
171 if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
173 wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(d->addr),
175 wil_memcpy_toio_32(dst, d->data, s);
176 wmb(); /* finish before processing next record */
181 static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
184 const struct wil_fw_record_fill *d = data;
187 size_t s = (size_t)le32_to_cpu(d->size);
189 if (size != sizeof(*d)) {
190 wil_err_fw(wil, "bad size for fill record: %zu\n", size);
194 if (s < sizeof(u32)) {
195 wil_err_fw(wil, "fill size too short: %zu\n", s);
199 if (s % sizeof(u32)) {
200 wil_err_fw(wil, "fill size not aligned: %zu\n", s);
204 if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
207 v = le32_to_cpu(d->value);
208 wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
209 le32_to_cpu(d->addr), v, s);
210 wil_memset_toio_32(dst, v, s);
211 wmb(); /* finish before processing next record */
216 static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
219 const struct wil_fw_record_file_header *d = data;
221 if (size != sizeof(*d)) {
222 wil_err_fw(wil, "file header length incorrect: %zu\n", size);
226 wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
227 d->version, d->data_len);
228 wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
229 sizeof(d->comment), true);
231 if (!memcmp(d->comment, WIL_FW_VERSION_PREFIX,
232 WIL_FW_VERSION_PREFIX_LEN))
233 memcpy(wil->fw_version,
234 d->comment + WIL_FW_VERSION_PREFIX_LEN,
235 min(sizeof(d->comment) - WIL_FW_VERSION_PREFIX_LEN,
236 sizeof(wil->fw_version) - 1));
241 static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
244 const struct wil_fw_record_direct_write *d = data;
245 const struct wil_fw_data_dwrite *block = d->data;
248 if (size % sizeof(*block)) {
249 wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
250 sizeof(*block), size);
253 n = size / sizeof(*block);
255 for (i = 0; i < n; i++) {
257 u32 m = le32_to_cpu(block[i].mask);
258 u32 v = le32_to_cpu(block[i].value);
261 if (!wil_fw_addr_check(wil, &dst, block[i].addr, 0, "address"))
265 y = (x & m) | (v & ~m);
266 wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
267 "(old 0x%08x val 0x%08x mask 0x%08x)\n",
268 le32_to_cpu(block[i].addr), y, x, v, m);
270 wmb(); /* finish before processing next record */
276 static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
277 void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
283 writel(gw_cmd, gwa_cmd);
284 wmb(); /* finish before activate gw */
286 writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
288 udelay(1); /* typical time is few usec */
290 wil_err_fw(wil, "gw timeout\n");
293 } while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
298 static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
301 const struct wil_fw_record_gateway_data *d = data;
302 const struct wil_fw_data_gw *block = d->data;
303 void __iomem *gwa_addr;
304 void __iomem *gwa_val;
305 void __iomem *gwa_cmd;
306 void __iomem *gwa_ctl;
310 if (size < sizeof(*d) + sizeof(*block)) {
311 wil_err_fw(wil, "gateway record too short: %zu\n", size);
315 if ((size - sizeof(*d)) % sizeof(*block)) {
316 wil_err_fw(wil, "gateway record data size"
317 " not aligned on %zu: %zu\n",
318 sizeof(*block), size - sizeof(*d));
321 n = (size - sizeof(*d)) / sizeof(*block);
323 gw_cmd = le32_to_cpu(d->command);
325 wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
328 if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
329 "gateway_addr_addr") ||
330 !wil_fw_addr_check(wil, &gwa_val, d->gateway_value_addr, 0,
331 "gateway_value_addr") ||
332 !wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
333 "gateway_cmd_addr") ||
334 !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
335 "gateway_ctrl_address"))
338 wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
339 " cmd 0x%08x ctl 0x%08x\n",
340 le32_to_cpu(d->gateway_addr_addr),
341 le32_to_cpu(d->gateway_value_addr),
342 le32_to_cpu(d->gateway_cmd_addr),
343 le32_to_cpu(d->gateway_ctrl_address));
345 for (i = 0; i < n; i++) {
347 u32 a = le32_to_cpu(block[i].addr);
348 u32 v = le32_to_cpu(block[i].value);
350 wil_dbg_fw(wil, " gw write[%3d] [0x%08x] <== 0x%08x\n",
354 rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
362 static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
365 const struct wil_fw_record_gateway_data4 *d = data;
366 const struct wil_fw_data_gw4 *block = d->data;
367 void __iomem *gwa_addr;
368 void __iomem *gwa_val[ARRAY_SIZE(block->value)];
369 void __iomem *gwa_cmd;
370 void __iomem *gwa_ctl;
374 if (size < sizeof(*d) + sizeof(*block)) {
375 wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
379 if ((size - sizeof(*d)) % sizeof(*block)) {
380 wil_err_fw(wil, "gateway4 record data size"
381 " not aligned on %zu: %zu\n",
382 sizeof(*block), size - sizeof(*d));
385 n = (size - sizeof(*d)) / sizeof(*block);
387 gw_cmd = le32_to_cpu(d->command);
389 wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
392 if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
393 "gateway_addr_addr"))
395 for (k = 0; k < ARRAY_SIZE(block->value); k++)
396 if (!wil_fw_addr_check(wil, &gwa_val[k],
397 d->gateway_value_addr[k],
398 0, "gateway_value_addr"))
400 if (!wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
401 "gateway_cmd_addr") ||
402 !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
403 "gateway_ctrl_address"))
406 wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
407 le32_to_cpu(d->gateway_addr_addr),
408 le32_to_cpu(d->gateway_cmd_addr),
409 le32_to_cpu(d->gateway_ctrl_address));
410 wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
411 d->gateway_value_addr, sizeof(d->gateway_value_addr),
414 for (i = 0; i < n; i++) {
416 u32 a = le32_to_cpu(block[i].addr);
417 u32 v[ARRAY_SIZE(block->value)];
419 for (k = 0; k < ARRAY_SIZE(block->value); k++)
420 v[k] = le32_to_cpu(block[i].value[k]);
422 wil_dbg_fw(wil, " gw4 write[%3d] [0x%08x] <==\n", i, a);
423 wil_hex_dump_fw(" val ", DUMP_PREFIX_NONE, 16, 4, v,
426 for (k = 0; k < ARRAY_SIZE(block->value); k++)
427 writel(v[k], gwa_val[k]);
428 rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
436 static const struct {
438 int (*load_handler)(struct wil6210_priv *wil, const void *data,
440 int (*parse_handler)(struct wil6210_priv *wil, const void *data,
442 } wil_fw_handlers[] = {
443 {wil_fw_type_comment, fw_handle_comment, fw_handle_capabilities},
444 {wil_fw_type_data, fw_handle_data, fw_ignore_section},
445 {wil_fw_type_fill, fw_handle_fill, fw_ignore_section},
446 /* wil_fw_type_action */
447 /* wil_fw_type_verify */
448 {wil_fw_type_file_header, fw_handle_file_header,
449 fw_handle_file_header},
450 {wil_fw_type_direct_write, fw_handle_direct_write, fw_ignore_section},
451 {wil_fw_type_gateway_data, fw_handle_gateway_data, fw_ignore_section},
452 {wil_fw_type_gateway_data4, fw_handle_gateway_data4,
456 static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
457 const void *data, size_t size, bool load)
461 for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
462 if (wil_fw_handlers[i].type == type)
464 wil_fw_handlers[i].load_handler(
466 wil_fw_handlers[i].parse_handler(
469 wil_err_fw(wil, "unknown record type: %d\n", type);
474 * wil_fw_process - process section from FW file
475 * if load is true: Load the FW and uCode code and data to the
476 * corresponding device memory regions,
477 * otherwise only parse and look for capabilities
481 static int wil_fw_process(struct wil6210_priv *wil, const void *data,
482 size_t size, bool load)
485 const struct wil_fw_record_head *hdr;
488 for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
489 if (size < sizeof(*hdr))
491 hdr_sz = le32_to_cpu(hdr->size);
492 s = sizeof(*hdr) + hdr_sz;
496 wil_err_fw(wil, "unaligned record size: %zu\n",
500 rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
501 &hdr[1], hdr_sz, load);
506 wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
507 if (size >= sizeof(*hdr)) {
508 wil_err_fw(wil, "Stop at offset %ld"
509 " record type %d [%zd bytes]\n",
510 (long)((const void *)hdr - data),
511 le16_to_cpu(hdr->type), hdr_sz);
520 * wil_request_firmware - Request firmware
522 * Request firmware image from the file
523 * If load is true, load firmware to device, otherwise
524 * only parse and extract capabilities
528 int wil_request_firmware(struct wil6210_priv *wil, const char *name,
532 const struct firmware *fw;
536 rc = reject_firmware(&fw, name, wil_to_dev(wil));
538 wil_err_fw(wil, "Failed to load firmware %s\n", name);
541 wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
543 for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
544 rc1 = wil_fw_verify(wil, d, sz);
549 rc = wil_fw_process(wil, d, rc1, load);
555 release_firmware(fw);
560 * wil_fw_verify_file_exists - checks if firmware file exist
562 * @wil: driver context
563 * @name: firmware file name
565 * return value - boolean, true for success, false for failure
567 bool wil_fw_verify_file_exists(struct wil6210_priv *wil, const char *name)
569 const struct firmware *fw;
572 rc = reject_firmware(&fw, name, wil_to_dev(wil));
574 release_firmware(fw);
576 wil_dbg_fw(wil, "<%s> not available: %d\n", name, rc);