fw_inc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (c) 2014-2016 Qualcomm Atheros, Inc.
  3. *
  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.
  7. *
  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.
  15. */
  16. /* Algorithmic part of the firmware download.
  17. * To be included in the container file providing framework
  18. */
  19. #define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
  20. #define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
  21. #define wil_hex_dump_fw(prefix_str, prefix_type, rowsize, \
  22. groupsize, buf, len, ascii) \
  23. print_hex_dump_debug("DBG[ FW ]" prefix_str, \
  24. prefix_type, rowsize, \
  25. groupsize, buf, len, ascii)
  26. #define FW_ADDR_CHECK(ioaddr, val, msg) do { \
  27. ioaddr = wmi_buffer(wil, val); \
  28. if (!ioaddr) { \
  29. wil_err_fw(wil, "bad " msg ": 0x%08x\n", \
  30. le32_to_cpu(val)); \
  31. return -EINVAL; \
  32. } \
  33. } while (0)
  34. /**
  35. * wil_fw_verify - verify firmware file validity
  36. *
  37. * perform various checks for the firmware file header.
  38. * records are not validated.
  39. *
  40. * Return file size or negative error
  41. */
  42. static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
  43. {
  44. const struct wil_fw_record_head *hdr = (const void *)data;
  45. struct wil_fw_record_file_header fh;
  46. const struct wil_fw_record_file_header *fh_;
  47. u32 crc;
  48. u32 dlen;
  49. if (size % 4) {
  50. wil_err_fw(wil, "image size not aligned: %zu\n", size);
  51. return -EINVAL;
  52. }
  53. /* have enough data for the file header? */
  54. if (size < sizeof(*hdr) + sizeof(fh)) {
  55. wil_err_fw(wil, "file too short: %zu bytes\n", size);
  56. return -EINVAL;
  57. }
  58. /* start with the file header? */
  59. if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
  60. wil_err_fw(wil, "no file header\n");
  61. return -EINVAL;
  62. }
  63. /* data_len */
  64. fh_ = (struct wil_fw_record_file_header *)&hdr[1];
  65. dlen = le32_to_cpu(fh_->data_len);
  66. if (dlen % 4) {
  67. wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
  68. return -EINVAL;
  69. }
  70. if (size < dlen) {
  71. wil_err_fw(wil, "file truncated at %zu/%lu\n",
  72. size, (ulong)dlen);
  73. return -EINVAL;
  74. }
  75. if (dlen < sizeof(*hdr) + sizeof(fh)) {
  76. wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
  77. return -EINVAL;
  78. }
  79. /* signature */
  80. if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
  81. wil_err_fw(wil, "bad header signature: 0x%08x\n",
  82. le32_to_cpu(fh_->signature));
  83. return -EINVAL;
  84. }
  85. /* version */
  86. if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
  87. wil_err_fw(wil, "unsupported header version: %d\n",
  88. le32_to_cpu(fh_->version));
  89. return -EINVAL;
  90. }
  91. /* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
  92. fh = *fh_;
  93. fh.crc = 0;
  94. crc = crc32_le(~0, (unsigned char const *)hdr, sizeof(*hdr));
  95. crc = crc32_le(crc, (unsigned char const *)&fh, sizeof(fh));
  96. crc = crc32_le(crc, (unsigned char const *)&fh_[1],
  97. dlen - sizeof(*hdr) - sizeof(fh));
  98. crc = ~crc;
  99. if (crc != le32_to_cpu(fh_->crc)) {
  100. wil_err_fw(wil, "checksum mismatch:"
  101. " calculated for %lu bytes 0x%08x != 0x%08x\n",
  102. (ulong)dlen, crc, le32_to_cpu(fh_->crc));
  103. return -EINVAL;
  104. }
  105. return (int)dlen;
  106. }
  107. static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
  108. size_t size)
  109. {
  110. return 0;
  111. }
  112. static int fw_handle_comment(struct wil6210_priv *wil, const void *data,
  113. size_t size)
  114. {
  115. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
  116. return 0;
  117. }
  118. static int
  119. fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
  120. size_t size)
  121. {
  122. const struct wil_fw_record_capabilities *rec = data;
  123. size_t capa_size;
  124. if (size < sizeof(*rec) ||
  125. le32_to_cpu(rec->magic) != WIL_FW_CAPABILITIES_MAGIC)
  126. return 0;
  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(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 fw_handle_data(struct wil6210_priv *wil, const void *data,
  137. size_t size)
  138. {
  139. const struct wil_fw_record_data *d = data;
  140. void __iomem *dst;
  141. size_t s = size - sizeof(*d);
  142. if (size < sizeof(*d) + sizeof(u32)) {
  143. wil_err_fw(wil, "data record too short: %zu\n", size);
  144. return -EINVAL;
  145. }
  146. FW_ADDR_CHECK(dst, d->addr, "address");
  147. wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(d->addr),
  148. s);
  149. wil_memcpy_toio_32(dst, d->data, s);
  150. wmb(); /* finish before processing next record */
  151. return 0;
  152. }
  153. static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
  154. size_t size)
  155. {
  156. const struct wil_fw_record_fill *d = data;
  157. void __iomem *dst;
  158. u32 v;
  159. size_t s = (size_t)le32_to_cpu(d->size);
  160. if (size != sizeof(*d)) {
  161. wil_err_fw(wil, "bad size for fill record: %zu\n", size);
  162. return -EINVAL;
  163. }
  164. if (s < sizeof(u32)) {
  165. wil_err_fw(wil, "fill size too short: %zu\n", s);
  166. return -EINVAL;
  167. }
  168. if (s % sizeof(u32)) {
  169. wil_err_fw(wil, "fill size not aligned: %zu\n", s);
  170. return -EINVAL;
  171. }
  172. FW_ADDR_CHECK(dst, d->addr, "address");
  173. v = le32_to_cpu(d->value);
  174. wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
  175. le32_to_cpu(d->addr), v, s);
  176. wil_memset_toio_32(dst, v, s);
  177. wmb(); /* finish before processing next record */
  178. return 0;
  179. }
  180. static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
  181. size_t size)
  182. {
  183. const struct wil_fw_record_file_header *d = data;
  184. if (size != sizeof(*d)) {
  185. wil_err_fw(wil, "file header length incorrect: %zu\n", size);
  186. return -EINVAL;
  187. }
  188. wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
  189. d->version, d->data_len);
  190. wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
  191. sizeof(d->comment), true);
  192. if (!memcmp(d->comment, WIL_FW_VERSION_PREFIX,
  193. WIL_FW_VERSION_PREFIX_LEN))
  194. memcpy(wil->fw_version,
  195. d->comment + WIL_FW_VERSION_PREFIX_LEN,
  196. min(sizeof(d->comment) - WIL_FW_VERSION_PREFIX_LEN,
  197. sizeof(wil->fw_version) - 1));
  198. return 0;
  199. }
  200. static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
  201. size_t size)
  202. {
  203. const struct wil_fw_record_direct_write *d = data;
  204. const struct wil_fw_data_dwrite *block = d->data;
  205. int n, i;
  206. if (size % sizeof(*block)) {
  207. wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
  208. sizeof(*block), size);
  209. return -EINVAL;
  210. }
  211. n = size / sizeof(*block);
  212. for (i = 0; i < n; i++) {
  213. void __iomem *dst;
  214. u32 m = le32_to_cpu(block[i].mask);
  215. u32 v = le32_to_cpu(block[i].value);
  216. u32 x, y;
  217. FW_ADDR_CHECK(dst, block[i].addr, "address");
  218. x = readl(dst);
  219. y = (x & m) | (v & ~m);
  220. wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
  221. "(old 0x%08x val 0x%08x mask 0x%08x)\n",
  222. le32_to_cpu(block[i].addr), y, x, v, m);
  223. writel(y, dst);
  224. wmb(); /* finish before processing next record */
  225. }
  226. return 0;
  227. }
  228. static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
  229. void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
  230. u32 a)
  231. {
  232. unsigned delay = 0;
  233. writel(a, gwa_addr);
  234. writel(gw_cmd, gwa_cmd);
  235. wmb(); /* finish before activate gw */
  236. writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
  237. do {
  238. udelay(1); /* typical time is few usec */
  239. if (delay++ > 100) {
  240. wil_err_fw(wil, "gw timeout\n");
  241. return -EINVAL;
  242. }
  243. } while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
  244. return 0;
  245. }
  246. static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
  247. size_t size)
  248. {
  249. const struct wil_fw_record_gateway_data *d = data;
  250. const struct wil_fw_data_gw *block = d->data;
  251. void __iomem *gwa_addr;
  252. void __iomem *gwa_val;
  253. void __iomem *gwa_cmd;
  254. void __iomem *gwa_ctl;
  255. u32 gw_cmd;
  256. int n, i;
  257. if (size < sizeof(*d) + sizeof(*block)) {
  258. wil_err_fw(wil, "gateway record too short: %zu\n", size);
  259. return -EINVAL;
  260. }
  261. if ((size - sizeof(*d)) % sizeof(*block)) {
  262. wil_err_fw(wil, "gateway record data size"
  263. " not aligned on %zu: %zu\n",
  264. sizeof(*block), size - sizeof(*d));
  265. return -EINVAL;
  266. }
  267. n = (size - sizeof(*d)) / sizeof(*block);
  268. gw_cmd = le32_to_cpu(d->command);
  269. wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
  270. n, gw_cmd);
  271. FW_ADDR_CHECK(gwa_addr, d->gateway_addr_addr, "gateway_addr_addr");
  272. FW_ADDR_CHECK(gwa_val, d->gateway_value_addr, "gateway_value_addr");
  273. FW_ADDR_CHECK(gwa_cmd, d->gateway_cmd_addr, "gateway_cmd_addr");
  274. FW_ADDR_CHECK(gwa_ctl, d->gateway_ctrl_address, "gateway_ctrl_address");
  275. wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
  276. " cmd 0x%08x ctl 0x%08x\n",
  277. le32_to_cpu(d->gateway_addr_addr),
  278. le32_to_cpu(d->gateway_value_addr),
  279. le32_to_cpu(d->gateway_cmd_addr),
  280. le32_to_cpu(d->gateway_ctrl_address));
  281. for (i = 0; i < n; i++) {
  282. int rc;
  283. u32 a = le32_to_cpu(block[i].addr);
  284. u32 v = le32_to_cpu(block[i].value);
  285. wil_dbg_fw(wil, " gw write[%3d] [0x%08x] <== 0x%08x\n",
  286. i, a, v);
  287. writel(v, gwa_val);
  288. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  289. if (rc)
  290. return rc;
  291. }
  292. return 0;
  293. }
  294. static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
  295. size_t size)
  296. {
  297. const struct wil_fw_record_gateway_data4 *d = data;
  298. const struct wil_fw_data_gw4 *block = d->data;
  299. void __iomem *gwa_addr;
  300. void __iomem *gwa_val[ARRAY_SIZE(block->value)];
  301. void __iomem *gwa_cmd;
  302. void __iomem *gwa_ctl;
  303. u32 gw_cmd;
  304. int n, i, k;
  305. if (size < sizeof(*d) + sizeof(*block)) {
  306. wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
  307. return -EINVAL;
  308. }
  309. if ((size - sizeof(*d)) % sizeof(*block)) {
  310. wil_err_fw(wil, "gateway4 record data size"
  311. " not aligned on %zu: %zu\n",
  312. sizeof(*block), size - sizeof(*d));
  313. return -EINVAL;
  314. }
  315. n = (size - sizeof(*d)) / sizeof(*block);
  316. gw_cmd = le32_to_cpu(d->command);
  317. wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
  318. n, gw_cmd);
  319. FW_ADDR_CHECK(gwa_addr, d->gateway_addr_addr, "gateway_addr_addr");
  320. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  321. FW_ADDR_CHECK(gwa_val[k], d->gateway_value_addr[k],
  322. "gateway_value_addr");
  323. FW_ADDR_CHECK(gwa_cmd, d->gateway_cmd_addr, "gateway_cmd_addr");
  324. FW_ADDR_CHECK(gwa_ctl, d->gateway_ctrl_address, "gateway_ctrl_address");
  325. wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
  326. le32_to_cpu(d->gateway_addr_addr),
  327. le32_to_cpu(d->gateway_cmd_addr),
  328. le32_to_cpu(d->gateway_ctrl_address));
  329. wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
  330. d->gateway_value_addr, sizeof(d->gateway_value_addr),
  331. false);
  332. for (i = 0; i < n; i++) {
  333. int rc;
  334. u32 a = le32_to_cpu(block[i].addr);
  335. u32 v[ARRAY_SIZE(block->value)];
  336. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  337. v[k] = le32_to_cpu(block[i].value[k]);
  338. wil_dbg_fw(wil, " gw4 write[%3d] [0x%08x] <==\n", i, a);
  339. wil_hex_dump_fw(" val ", DUMP_PREFIX_NONE, 16, 4, v,
  340. sizeof(v), false);
  341. for (k = 0; k < ARRAY_SIZE(block->value); k++)
  342. writel(v[k], gwa_val[k]);
  343. rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
  344. if (rc)
  345. return rc;
  346. }
  347. return 0;
  348. }
  349. static const struct {
  350. int type;
  351. int (*load_handler)(struct wil6210_priv *wil, const void *data,
  352. size_t size);
  353. int (*parse_handler)(struct wil6210_priv *wil, const void *data,
  354. size_t size);
  355. } wil_fw_handlers[] = {
  356. {wil_fw_type_comment, fw_handle_comment, fw_handle_capabilities},
  357. {wil_fw_type_data, fw_handle_data, fw_ignore_section},
  358. {wil_fw_type_fill, fw_handle_fill, fw_ignore_section},
  359. /* wil_fw_type_action */
  360. /* wil_fw_type_verify */
  361. {wil_fw_type_file_header, fw_handle_file_header,
  362. fw_handle_file_header},
  363. {wil_fw_type_direct_write, fw_handle_direct_write, fw_ignore_section},
  364. {wil_fw_type_gateway_data, fw_handle_gateway_data, fw_ignore_section},
  365. {wil_fw_type_gateway_data4, fw_handle_gateway_data4,
  366. fw_ignore_section},
  367. };
  368. static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
  369. const void *data, size_t size, bool load)
  370. {
  371. int i;
  372. for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
  373. if (wil_fw_handlers[i].type == type)
  374. return load ?
  375. wil_fw_handlers[i].load_handler(
  376. wil, data, size) :
  377. wil_fw_handlers[i].parse_handler(
  378. wil, data, size);
  379. wil_err_fw(wil, "unknown record type: %d\n", type);
  380. return -EINVAL;
  381. }
  382. /**
  383. * wil_fw_process - process section from FW file
  384. * if load is true: Load the FW and uCode code and data to the
  385. * corresponding device memory regions,
  386. * otherwise only parse and look for capabilities
  387. *
  388. * Return error code
  389. */
  390. static int wil_fw_process(struct wil6210_priv *wil, const void *data,
  391. size_t size, bool load)
  392. {
  393. int rc = 0;
  394. const struct wil_fw_record_head *hdr;
  395. size_t s, hdr_sz;
  396. for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
  397. if (size < sizeof(*hdr))
  398. break;
  399. hdr_sz = le32_to_cpu(hdr->size);
  400. s = sizeof(*hdr) + hdr_sz;
  401. if (s > size)
  402. break;
  403. if (hdr_sz % 4) {
  404. wil_err_fw(wil, "unaligned record size: %zu\n",
  405. hdr_sz);
  406. return -EINVAL;
  407. }
  408. rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
  409. &hdr[1], hdr_sz, load);
  410. if (rc)
  411. return rc;
  412. }
  413. if (size) {
  414. wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
  415. if (size >= sizeof(*hdr)) {
  416. wil_err_fw(wil, "Stop at offset %ld"
  417. " record type %d [%zd bytes]\n",
  418. (long)((const void *)hdr - data),
  419. le16_to_cpu(hdr->type), hdr_sz);
  420. }
  421. return -EINVAL;
  422. }
  423. return rc;
  424. }
  425. /**
  426. * wil_request_firmware - Request firmware
  427. *
  428. * Request firmware image from the file
  429. * If load is true, load firmware to device, otherwise
  430. * only parse and extract capabilities
  431. *
  432. * Return error code
  433. */
  434. int wil_request_firmware(struct wil6210_priv *wil, const char *name,
  435. bool load)
  436. {
  437. int rc, rc1;
  438. const struct firmware *fw;
  439. size_t sz;
  440. const void *d;
  441. rc = request_firmware(&fw, name, wil_to_dev(wil));
  442. if (rc) {
  443. wil_err_fw(wil, "Failed to load firmware %s\n", name);
  444. return rc;
  445. }
  446. wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
  447. for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
  448. rc1 = wil_fw_verify(wil, d, sz);
  449. if (rc1 < 0) {
  450. rc = rc1;
  451. goto out;
  452. }
  453. rc = wil_fw_process(wil, d, rc1, load);
  454. if (rc < 0)
  455. goto out;
  456. }
  457. out:
  458. release_firmware(fw);
  459. return rc;
  460. }