fw_inc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* Algorithmic part of the firmware download.
  18. * To be included in the container file providing framework
  19. */
  20. #define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
  21. #define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
  22. #define wil_hex_dump_fw(prefix_str, prefix_type, rowsize, \
  23. groupsize, buf, len, ascii) \
  24. print_hex_dump_debug("DBG[ FW ]" prefix_str, \
  25. prefix_type, rowsize, \
  26. groupsize, buf, len, ascii)
  27. static bool wil_fw_addr_check(struct wil6210_priv *wil,
  28. void __iomem **ioaddr, __le32 val,
  29. u32 size, const char *msg)
  30. {
  31. *ioaddr = wmi_buffer_block(wil, val, size);
  32. if (!(*ioaddr)) {
  33. wil_err_fw(wil, "bad %s: 0x%08x\n", msg, le32_to_cpu(val));
  34. return false;
  35. }
  36. return true;
  37. }
  38. /**
  39. * wil_fw_verify - verify firmware file validity
  40. *
  41. * perform various checks for the firmware file header.
  42. * records are not validated.
  43. *
  44. * Return file size or negative error
  45. */
  46. static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
  47. {
  48. const struct wil_fw_record_head *hdr = (const void *)data;
  49. struct wil_fw_record_file_header fh;
  50. const struct wil_fw_record_file_header *fh_;
  51. u32 crc;
  52. u32 dlen;
  53. if (size % 4) {
  54. wil_err_fw(wil, "image size not aligned: %zu\n", size);
  55. return -EINVAL;
  56. }
  57. /* have enough data for the file header? */
  58. if (size < sizeof(*hdr) + sizeof(fh)) {
  59. wil_err_fw(wil, "file too short: %zu bytes\n", size);
  60. return -EINVAL;
  61. }
  62. /* start with the file header? */
  63. if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
  64. wil_err_fw(wil, "no file header\n");
  65. return -EINVAL;
  66. }
  67. /* data_len */
  68. fh_ = (struct wil_fw_record_file_header *)&hdr[1];
  69. dlen = le32_to_cpu(fh_->data_len);
  70. if (dlen % 4) {
  71. wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
  72. return -EINVAL;
  73. }
  74. if (size < dlen) {
  75. wil_err_fw(wil, "file truncated at %zu/%lu\n",
  76. size, (ulong)dlen);
  77. return -EINVAL;
  78. }
  79. if (dlen < sizeof(*hdr) + sizeof(fh)) {
  80. wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
  81. return -EINVAL;
  82. }
  83. /* signature */
  84. if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
  85. wil_err_fw(wil, "bad header signature: 0x%08x\n",
  86. le32_to_cpu(fh_->signature));
  87. return -EINVAL;
  88. }
  89. /* version */
  90. if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
  91. wil_err_fw(wil, "unsupported header version: %d\n",
  92. le32_to_cpu(fh_->version));
  93. return -EINVAL;
  94. }
  95. /* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
  96. fh = *fh_;
  97. fh.crc = 0;
  98. crc = crc32_le(~0, (unsigned char const *)hdr, sizeof(*hdr));
  99. crc = crc32_le(crc, (unsigned char const *)&fh, sizeof(fh));
  100. crc = crc32_le(crc, (unsigned char const *)&fh_[1],
  101. dlen - sizeof(*hdr) - sizeof(fh));
  102. crc = ~crc;
  103. if (crc != le32_to_cpu(fh_->crc)) {
  104. wil_err_fw(wil, "checksum mismatch:"
  105. " calculated for %lu bytes 0x%08x != 0x%08x\n",
  106. (ulong)dlen, crc, le32_to_cpu(fh_->crc));
  107. return -EINVAL;
  108. }
  109. return (int)dlen;
  110. }
  111. static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
  112. size_t size)
  113. {
  114. return 0;
  115. }
  116. static int
  117. fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
  118. size_t size)
  119. {
  120. const struct wil_fw_record_capabilities *rec = data;
  121. size_t capa_size;
  122. if (size < sizeof(*rec)) {
  123. wil_err_fw(wil, "capabilities record too short: %zu\n", size);
  124. /* let the FW load anyway */
  125. return 0;
  126. }
  127. capa_size = size - offsetof(struct wil_fw_record_capabilities,
  128. capabilities);
  129. bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
  130. memcpy(wil->fw_capabilities, rec->capabilities,
  131. min_t(size_t, sizeof(wil->fw_capabilities), capa_size));
  132. wil_hex_dump_fw("CAPA", DUMP_PREFIX_OFFSET, 16, 1,
  133. rec->capabilities, capa_size, false);
  134. return 0;
  135. }
  136. static int
  137. fw_handle_brd_file(struct wil6210_priv *wil, const void *data,
  138. size_t size)
  139. {
  140. const struct wil_fw_record_brd_file *rec = data;
  141. if (size < sizeof(*rec)) {
  142. wil_err_fw(wil, "brd_file record too short: %zu\n", size);
  143. return 0;
  144. }
  145. wil->brd_file_addr = le32_to_cpu(rec->base_addr);
  146. wil->brd_file_max_size = le32_to_cpu(rec->max_size_bytes);
  147. wil_dbg_fw(wil, "brd_file_addr 0x%x, brd_file_max_size %d\n",
  148. wil->brd_file_addr, wil->brd_file_max_size);
  149. return 0;
  150. }
  151. static int
  152. fw_handle_concurrency(struct wil6210_priv *wil, const void *data,
  153. size_t size)
  154. {
  155. const struct wil_fw_record_concurrency *rec = data;
  156. const struct wil_fw_concurrency_combo *combo;
  157. const struct wil_fw_concurrency_limit *limit;
  158. size_t remain, lsize;
  159. int i, n_combos;
  160. if (size < sizeof(*rec)) {
  161. wil_err_fw(wil, "concurrency record too short: %zu\n", size);
  162. /* continue, let the FW load anyway */
  163. return 0;
  164. }
  165. n_combos = le16_to_cpu(rec->n_combos);
  166. remain = size - offsetof(struct wil_fw_record_concurrency, combos);
  167. combo = rec->combos;
  168. for (i = 0; i < n_combos; i++) {
  169. if (remain < sizeof(*combo))
  170. goto out_short;
  171. remain -= sizeof(*combo);
  172. limit = combo->limits;
  173. lsize = combo->n_limits * sizeof(*limit);
  174. if (remain < lsize)
  175. goto out_short;
  176. remain -= lsize;
  177. limit += combo->n_limits;
  178. combo = (struct wil_fw_concurrency_combo *)limit;
  179. }
  180. return wil_cfg80211_iface_combinations_from_fw(wil, rec);
  181. out_short:
  182. wil_err_fw(wil, "concurrency record truncated\n");
  183. return 0;
  184. }
  185. static int
  186. fw_handle_comment(struct wil6210_priv *wil, const void *data,
  187. size_t size)
  188. {
  189. const struct wil_fw_record_comment_hdr *hdr = data;
  190. u32 magic;
  191. int rc = 0;
  192. if (size < sizeof(*hdr))
  193. return 0;
  194. magic = le32_to_cpu(hdr->magic);
  195. switch (magic) {
  196. case WIL_FW_CAPABILITIES_MAGIC:
  197. wil_dbg_fw(wil, "magic is WIL_FW_CAPABILITIES_MAGIC\n");
  198. rc = fw_handle_capabilities(wil, data, size);
  199. break;
  200. case WIL_BRD_FILE_MAGIC:
  201. wil_dbg_fw(wil, "magic is WIL_BRD_FILE_MAGIC\n");
  202. rc = fw_handle_brd_file(wil, data, size);
  203. break;
  204. case WIL_FW_CONCURRENCY_MAGIC:
  205. wil_dbg_fw(wil, "magic is WIL_FW_CONCURRENCY_MAGIC\n");
  206. rc = fw_handle_concurrency(wil, data, size);
  207. break;
  208. default:
  209. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1,
  210. data, size, true);
  211. }
  212. return rc;
  213. }
  214. static int __fw_handle_data(struct wil6210_priv *wil, const void *data,
  215. size_t size, __le32 addr)
  216. {
  217. const struct wil_fw_record_data *d = data;
  218. void __iomem *dst;
  219. size_t s = size - sizeof(*d);
  220. if (size < sizeof(*d) + sizeof(u32)) {
  221. wil_err_fw(wil, "data record too short: %zu\n", size);
  222. return -EINVAL;
  223. }
  224. if (!wil_fw_addr_check(wil, &dst, addr, s, "address"))
  225. return -EINVAL;
  226. wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(addr), s);
  227. wil_memcpy_toio_32(dst, d->data, s);
  228. wmb(); /* finish before processing next record */
  229. return 0;
  230. }
  231. static int fw_handle_data(struct wil6210_priv *wil, const void *data,
  232. size_t size)
  233. {
  234. const struct wil_fw_record_data *d = data;
  235. return __fw_handle_data(wil, data, size, d->addr);
  236. }
  237. static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
  238. size_t size)
  239. {
  240. const struct wil_fw_record_fill *d = data;
  241. void __iomem *dst;
  242. u32 v;
  243. size_t s = (size_t)le32_to_cpu(d->size);
  244. if (size != sizeof(*d)) {
  245. wil_err_fw(wil, "bad size for fill record: %zu\n", size);
  246. return -EINVAL;
  247. }
  248. if (s < sizeof(u32)) {
  249. wil_err_fw(wil, "fill size too short: %zu\n", s);
  250. return -EINVAL;
  251. }
  252. if (s % sizeof(u32)) {
  253. wil_err_fw(wil, "fill size not aligned: %zu\n", s);
  254. return -EINVAL;
  255. }
  256. if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
  257. return -EINVAL;
  258. v = le32_to_cpu(d->value);
  259. wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
  260. le32_to_cpu(d->addr), v, s);
  261. wil_memset_toio_32(dst, v, s);
  262. wmb(); /* finish before processing next record */
  263. return 0;
  264. }
  265. static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
  266. size_t size)
  267. {
  268. const struct wil_fw_record_file_header *d = data;
  269. if (size != sizeof(*d)) {
  270. wil_err_fw(wil, "file header length incorrect: %zu\n", size);
  271. return -EINVAL;
  272. }
  273. wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
  274. d->version, d->data_len);
  275. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
  276. sizeof(d->comment), true);
  277. if (!memcmp(d->comment, WIL_FW_VERSION_PREFIX,
  278. WIL_FW_VERSION_PREFIX_LEN))
  279. memcpy(wil->fw_version,
  280. d->comment + WIL_FW_VERSION_PREFIX_LEN,
  281. min(sizeof(d->comment) - WIL_FW_VERSION_PREFIX_LEN,
  282. sizeof(wil->fw_version) - 1));
  283. return 0;
  284. }
  285. static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
  286. size_t size)
  287. {
  288. const struct wil_fw_record_direct_write *d = data;
  289. const struct wil_fw_data_dwrite *block = d->data;
  290. int n, i;
  291. if (size % sizeof(*block)) {
  292. wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
  293. sizeof(*block), size);
  294. return -EINVAL;
  295. }
  296. n = size / sizeof(*block);
  297. for (i = 0; i < n; i++) {
  298. void __iomem *dst;
  299. u32 m = le32_to_cpu(block[i].mask);
  300. u32 v = le32_to_cpu(block[i].value);
  301. u32 x, y;
  302. if (!wil_fw_addr_check(wil, &dst, block[i].addr, 0, "address"))
  303. return -EINVAL;
  304. x = readl(dst);
  305. y = (x & m) | (v & ~m);
  306. wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
  307. "(old 0x%08x val 0x%08x mask 0x%08x)\n",
  308. le32_to_cpu(block[i].addr), y, x, v, m);
  309. writel(y, dst);
  310. wmb(); /* finish before processing next record */
  311. }
  312. return 0;
  313. }
  314. static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
  315. void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
  316. u32 a)
  317. {
  318. unsigned delay = 0;
  319. writel(a, gwa_addr);
  320. writel(gw_cmd, gwa_cmd);
  321. wmb(); /* finish before activate gw */
  322. writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
  323. do {
  324. udelay(1); /* typical time is few usec */
  325. if (delay++ > 100) {
  326. wil_err_fw(wil, "gw timeout\n");
  327. return -EINVAL;
  328. }
  329. } while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
  330. return 0;
  331. }
  332. static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
  333. size_t size)
  334. {
  335. const struct wil_fw_record_gateway_data *d = data;
  336. const struct wil_fw_data_gw *block = d->data;
  337. void __iomem *gwa_addr;
  338. void __iomem *gwa_val;
  339. void __iomem *gwa_cmd;
  340. void __iomem *gwa_ctl;
  341. u32 gw_cmd;
  342. int n, i;
  343. if (size < sizeof(*d) + sizeof(*block)) {
  344. wil_err_fw(wil, "gateway record too short: %zu\n", size);
  345. return -EINVAL;
  346. }
  347. if ((size - sizeof(*d)) % sizeof(*block)) {
  348. wil_err_fw(wil, "gateway record data size"
  349. " not aligned on %zu: %zu\n",
  350. sizeof(*block), size - sizeof(*d));
  351. return -EINVAL;
  352. }
  353. n = (size - sizeof(*d)) / sizeof(*block);
  354. gw_cmd = le32_to_cpu(d->command);
  355. wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
  356. n, gw_cmd);
  357. if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
  358. "gateway_addr_addr") ||
  359. !wil_fw_addr_check(wil, &gwa_val, d->gateway_value_addr, 0,
  360. "gateway_value_addr") ||
  361. !wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
  362. "gateway_cmd_addr") ||
  363. !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
  364. "gateway_ctrl_address"))
  365. return -EINVAL;
  366. wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
  367. " cmd 0x%08x ctl 0x%08x\n",
  368. le32_to_cpu(d->gateway_addr_addr),
  369. le32_to_cpu(d->gateway_value_addr),
  370. le32_to_cpu(d->gateway_cmd_addr),
  371. le32_to_cpu(d->gateway_ctrl_address));
  372. for (i = 0; i < n; i++) {
  373. int rc;
  374. u32 a = le32_to_cpu(block[i].addr);
  375. u32 v = le32_to_cpu(block[i].value);
  376. wil_dbg_fw(wil, " gw write[%3d] [0x%08x] <== 0x%08x\n",
  377. i, a, v);
  378. writel(v, gwa_val);
  379. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  380. if (rc)
  381. return rc;
  382. }
  383. return 0;
  384. }
  385. static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
  386. size_t size)
  387. {
  388. const struct wil_fw_record_gateway_data4 *d = data;
  389. const struct wil_fw_data_gw4 *block = d->data;
  390. void __iomem *gwa_addr;
  391. void __iomem *gwa_val[ARRAY_SIZE(block->value)];
  392. void __iomem *gwa_cmd;
  393. void __iomem *gwa_ctl;
  394. u32 gw_cmd;
  395. int n, i, k;
  396. if (size < sizeof(*d) + sizeof(*block)) {
  397. wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
  398. return -EINVAL;
  399. }
  400. if ((size - sizeof(*d)) % sizeof(*block)) {
  401. wil_err_fw(wil, "gateway4 record data size"
  402. " not aligned on %zu: %zu\n",
  403. sizeof(*block), size - sizeof(*d));
  404. return -EINVAL;
  405. }
  406. n = (size - sizeof(*d)) / sizeof(*block);
  407. gw_cmd = le32_to_cpu(d->command);
  408. wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
  409. n, gw_cmd);
  410. if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
  411. "gateway_addr_addr"))
  412. return -EINVAL;
  413. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  414. if (!wil_fw_addr_check(wil, &gwa_val[k],
  415. d->gateway_value_addr[k],
  416. 0, "gateway_value_addr"))
  417. return -EINVAL;
  418. if (!wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
  419. "gateway_cmd_addr") ||
  420. !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
  421. "gateway_ctrl_address"))
  422. return -EINVAL;
  423. wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
  424. le32_to_cpu(d->gateway_addr_addr),
  425. le32_to_cpu(d->gateway_cmd_addr),
  426. le32_to_cpu(d->gateway_ctrl_address));
  427. wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
  428. d->gateway_value_addr, sizeof(d->gateway_value_addr),
  429. false);
  430. for (i = 0; i < n; i++) {
  431. int rc;
  432. u32 a = le32_to_cpu(block[i].addr);
  433. u32 v[ARRAY_SIZE(block->value)];
  434. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  435. v[k] = le32_to_cpu(block[i].value[k]);
  436. wil_dbg_fw(wil, " gw4 write[%3d] [0x%08x] <==\n", i, a);
  437. wil_hex_dump_fw(" val ", DUMP_PREFIX_NONE, 16, 4, v,
  438. sizeof(v), false);
  439. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  440. writel(v[k], gwa_val[k]);
  441. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  442. if (rc)
  443. return rc;
  444. }
  445. return 0;
  446. }
  447. static const struct {
  448. int type;
  449. int (*load_handler)(struct wil6210_priv *wil, const void *data,
  450. size_t size);
  451. int (*parse_handler)(struct wil6210_priv *wil, const void *data,
  452. size_t size);
  453. } wil_fw_handlers[] = {
  454. {wil_fw_type_comment, fw_handle_comment, fw_handle_comment},
  455. {wil_fw_type_data, fw_handle_data, fw_ignore_section},
  456. {wil_fw_type_fill, fw_handle_fill, fw_ignore_section},
  457. /* wil_fw_type_action */
  458. /* wil_fw_type_verify */
  459. {wil_fw_type_file_header, fw_handle_file_header,
  460. fw_handle_file_header},
  461. {wil_fw_type_direct_write, fw_handle_direct_write, fw_ignore_section},
  462. {wil_fw_type_gateway_data, fw_handle_gateway_data, fw_ignore_section},
  463. {wil_fw_type_gateway_data4, fw_handle_gateway_data4,
  464. fw_ignore_section},
  465. };
  466. static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
  467. const void *data, size_t size, bool load)
  468. {
  469. int i;
  470. for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
  471. if (wil_fw_handlers[i].type == type)
  472. return load ?
  473. wil_fw_handlers[i].load_handler(
  474. wil, data, size) :
  475. wil_fw_handlers[i].parse_handler(
  476. wil, data, size);
  477. wil_err_fw(wil, "unknown record type: %d\n", type);
  478. return -EINVAL;
  479. }
  480. /**
  481. * wil_fw_process - process section from FW file
  482. * if load is true: Load the FW and uCode code and data to the
  483. * corresponding device memory regions,
  484. * otherwise only parse and look for capabilities
  485. *
  486. * Return error code
  487. */
  488. static int wil_fw_process(struct wil6210_priv *wil, const void *data,
  489. size_t size, bool load)
  490. {
  491. int rc = 0;
  492. const struct wil_fw_record_head *hdr;
  493. size_t s, hdr_sz;
  494. for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
  495. if (size < sizeof(*hdr))
  496. break;
  497. hdr_sz = le32_to_cpu(hdr->size);
  498. s = sizeof(*hdr) + hdr_sz;
  499. if (s > size)
  500. break;
  501. if (hdr_sz % 4) {
  502. wil_err_fw(wil, "unaligned record size: %zu\n",
  503. hdr_sz);
  504. return -EINVAL;
  505. }
  506. rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
  507. &hdr[1], hdr_sz, load);
  508. if (rc)
  509. return rc;
  510. }
  511. if (size) {
  512. wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
  513. if (size >= sizeof(*hdr)) {
  514. wil_err_fw(wil, "Stop at offset %ld"
  515. " record type %d [%zd bytes]\n",
  516. (long)((const void *)hdr - data),
  517. le16_to_cpu(hdr->type), hdr_sz);
  518. }
  519. return -EINVAL;
  520. }
  521. return rc;
  522. }
  523. /**
  524. * wil_request_firmware - Request firmware
  525. *
  526. * Request firmware image from the file
  527. * If load is true, load firmware to device, otherwise
  528. * only parse and extract capabilities
  529. *
  530. * Return error code
  531. */
  532. int wil_request_firmware(struct wil6210_priv *wil, const char *name,
  533. bool load)
  534. {
  535. int rc, rc1;
  536. const struct firmware *fw;
  537. size_t sz;
  538. const void *d;
  539. rc = request_firmware(&fw, name, wil_to_dev(wil));
  540. if (rc) {
  541. wil_err_fw(wil, "Failed to load firmware %s rc %d\n", name, rc);
  542. return rc;
  543. }
  544. wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
  545. for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
  546. rc1 = wil_fw_verify(wil, d, sz);
  547. if (rc1 < 0) {
  548. rc = rc1;
  549. goto out;
  550. }
  551. rc = wil_fw_process(wil, d, rc1, load);
  552. if (rc < 0)
  553. goto out;
  554. }
  555. out:
  556. release_firmware(fw);
  557. return rc;
  558. }
  559. /**
  560. * wil_brd_process - process section from BRD file
  561. *
  562. * Return error code
  563. */
  564. static int wil_brd_process(struct wil6210_priv *wil, const void *data,
  565. size_t size)
  566. {
  567. int rc = 0;
  568. const struct wil_fw_record_head *hdr = data;
  569. size_t s, hdr_sz;
  570. u16 type;
  571. /* Assuming the board file includes only one header record and one data
  572. * record. Each record starts with wil_fw_record_head.
  573. */
  574. if (size < sizeof(*hdr))
  575. return -EINVAL;
  576. s = sizeof(*hdr) + le32_to_cpu(hdr->size);
  577. if (s > size)
  578. return -EINVAL;
  579. /* Skip the header record and handle the data record */
  580. hdr = (const void *)hdr + s;
  581. size -= s;
  582. if (size < sizeof(*hdr))
  583. return -EINVAL;
  584. hdr_sz = le32_to_cpu(hdr->size);
  585. if (wil->brd_file_max_size && hdr_sz > wil->brd_file_max_size)
  586. return -EINVAL;
  587. if (sizeof(*hdr) + hdr_sz > size)
  588. return -EINVAL;
  589. if (hdr_sz % 4) {
  590. wil_err_fw(wil, "unaligned record size: %zu\n",
  591. hdr_sz);
  592. return -EINVAL;
  593. }
  594. type = le16_to_cpu(hdr->type);
  595. if (type != wil_fw_type_data) {
  596. wil_err_fw(wil, "invalid record type for board file: %d\n",
  597. type);
  598. return -EINVAL;
  599. }
  600. if (hdr_sz < sizeof(struct wil_fw_record_data)) {
  601. wil_err_fw(wil, "data record too short: %zu\n", hdr_sz);
  602. return -EINVAL;
  603. }
  604. wil_dbg_fw(wil, "using addr from fw file: [0x%08x]\n",
  605. wil->brd_file_addr);
  606. rc = __fw_handle_data(wil, &hdr[1], hdr_sz,
  607. cpu_to_le32(wil->brd_file_addr));
  608. return rc;
  609. }
  610. /**
  611. * wil_request_board - Request board file
  612. *
  613. * Request board image from the file
  614. * board file address and max size are read from FW file
  615. * during initialization.
  616. * brd file shall include one header and one data section.
  617. *
  618. * Return error code
  619. */
  620. int wil_request_board(struct wil6210_priv *wil, const char *name)
  621. {
  622. int rc, dlen;
  623. const struct firmware *brd;
  624. rc = request_firmware(&brd, name, wil_to_dev(wil));
  625. if (rc) {
  626. wil_err_fw(wil, "Failed to load brd %s\n", name);
  627. return rc;
  628. }
  629. wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, brd->size);
  630. /* Verify the header */
  631. dlen = wil_fw_verify(wil, brd->data, brd->size);
  632. if (dlen < 0) {
  633. rc = dlen;
  634. goto out;
  635. }
  636. /* Process the data record */
  637. rc = wil_brd_process(wil, brd->data, dlen);
  638. out:
  639. release_firmware(brd);
  640. return rc;
  641. }
  642. /**
  643. * wil_fw_verify_file_exists - checks if firmware file exist
  644. *
  645. * @wil: driver context
  646. * @name: firmware file name
  647. *
  648. * return value - boolean, true for success, false for failure
  649. */
  650. bool wil_fw_verify_file_exists(struct wil6210_priv *wil, const char *name)
  651. {
  652. const struct firmware *fw;
  653. int rc;
  654. rc = request_firmware(&fw, name, wil_to_dev(wil));
  655. if (!rc)
  656. release_firmware(fw);
  657. else
  658. wil_dbg_fw(wil, "<%s> not available: %d\n", name, rc);
  659. return !rc;
  660. }