GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / net / wireless / ath / wil6210 / fw_inc.c
1 /*
2  * Copyright (c) 2014-2017 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
17 /* Algorithmic part of the firmware download.
18  * To be included in the container file providing framework
19  */
20
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)
28
29 static bool wil_fw_addr_check(struct wil6210_priv *wil,
30                               void __iomem **ioaddr, __le32 val,
31                               u32 size, const char *msg)
32 {
33         *ioaddr = wmi_buffer_block(wil, val, size);
34         if (!(*ioaddr)) {
35                 wil_err_fw(wil, "bad %s: 0x%08x\n", msg, le32_to_cpu(val));
36                 return false;
37         }
38         return true;
39 }
40
41 /**
42  * wil_fw_verify - verify firmware file validity
43  *
44  * perform various checks for the firmware file header.
45  * records are not validated.
46  *
47  * Return file size or negative error
48  */
49 static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
50 {
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_;
54         u32 crc;
55         u32 dlen;
56
57         if (size % 4) {
58                 wil_err_fw(wil, "image size not aligned: %zu\n", size);
59                 return -EINVAL;
60         }
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);
64                 return -EINVAL;
65         }
66
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");
70                 return -EINVAL;
71         }
72
73         /* data_len */
74         fh_ = (struct wil_fw_record_file_header *)&hdr[1];
75         dlen = le32_to_cpu(fh_->data_len);
76         if (dlen % 4) {
77                 wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
78                 return -EINVAL;
79         }
80         if (size < dlen) {
81                 wil_err_fw(wil, "file truncated at %zu/%lu\n",
82                            size, (ulong)dlen);
83                 return -EINVAL;
84         }
85         if (dlen < sizeof(*hdr) + sizeof(fh)) {
86                 wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
87                 return -EINVAL;
88         }
89
90         /* signature */
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));
94                 return -EINVAL;
95         }
96
97         /* version */
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));
101                 return -EINVAL;
102         }
103
104         /* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
105         fh = *fh_;
106         fh.crc = 0;
107
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));
112         crc = ~crc;
113
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));
118                 return -EINVAL;
119         }
120
121         return (int)dlen;
122 }
123
124 static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
125                              size_t size)
126 {
127         return 0;
128 }
129
130 static int fw_handle_comment(struct wil6210_priv *wil, const void *data,
131                              size_t size)
132 {
133         wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, data, size, true);
134
135         return 0;
136 }
137
138 static int
139 fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
140                        size_t size)
141 {
142         const struct wil_fw_record_capabilities *rec = data;
143         size_t capa_size;
144
145         if (size < sizeof(*rec) ||
146             le32_to_cpu(rec->magic) != WIL_FW_CAPABILITIES_MAGIC)
147                 return 0;
148
149         capa_size = size - offsetof(struct wil_fw_record_capabilities,
150                                     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);
156         return 0;
157 }
158
159 static int fw_handle_data(struct wil6210_priv *wil, const void *data,
160                           size_t size)
161 {
162         const struct wil_fw_record_data *d = data;
163         void __iomem *dst;
164         size_t s = size - sizeof(*d);
165
166         if (size < sizeof(*d) + sizeof(u32)) {
167                 wil_err_fw(wil, "data record too short: %zu\n", size);
168                 return -EINVAL;
169         }
170
171         if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
172                 return -EINVAL;
173         wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(d->addr),
174                    s);
175         wil_memcpy_toio_32(dst, d->data, s);
176         wmb(); /* finish before processing next record */
177
178         return 0;
179 }
180
181 static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
182                           size_t size)
183 {
184         const struct wil_fw_record_fill *d = data;
185         void __iomem *dst;
186         u32 v;
187         size_t s = (size_t)le32_to_cpu(d->size);
188
189         if (size != sizeof(*d)) {
190                 wil_err_fw(wil, "bad size for fill record: %zu\n", size);
191                 return -EINVAL;
192         }
193
194         if (s < sizeof(u32)) {
195                 wil_err_fw(wil, "fill size too short: %zu\n", s);
196                 return -EINVAL;
197         }
198
199         if (s % sizeof(u32)) {
200                 wil_err_fw(wil, "fill size not aligned: %zu\n", s);
201                 return -EINVAL;
202         }
203
204         if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
205                 return -EINVAL;
206
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 */
212
213         return 0;
214 }
215
216 static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
217                                  size_t size)
218 {
219         const struct wil_fw_record_file_header *d = data;
220
221         if (size != sizeof(*d)) {
222                 wil_err_fw(wil, "file header length incorrect: %zu\n", size);
223                 return -EINVAL;
224         }
225
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);
230
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));
237
238         return 0;
239 }
240
241 static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
242                                   size_t size)
243 {
244         const struct wil_fw_record_direct_write *d = data;
245         const struct wil_fw_data_dwrite *block = d->data;
246         int n, i;
247
248         if (size % sizeof(*block)) {
249                 wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
250                            sizeof(*block), size);
251                 return -EINVAL;
252         }
253         n = size / sizeof(*block);
254
255         for (i = 0; i < n; i++) {
256                 void __iomem *dst;
257                 u32 m = le32_to_cpu(block[i].mask);
258                 u32 v = le32_to_cpu(block[i].value);
259                 u32 x, y;
260
261                 if (!wil_fw_addr_check(wil, &dst, block[i].addr, 0, "address"))
262                         return -EINVAL;
263
264                 x = readl(dst);
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);
269                 writel(y, dst);
270                 wmb(); /* finish before processing next record */
271         }
272
273         return 0;
274 }
275
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,
278                     u32 a)
279 {
280         unsigned delay = 0;
281
282         writel(a, gwa_addr);
283         writel(gw_cmd, gwa_cmd);
284         wmb(); /* finish before activate gw */
285
286         writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
287         do {
288                 udelay(1); /* typical time is few usec */
289                 if (delay++ > 100) {
290                         wil_err_fw(wil, "gw timeout\n");
291                         return -EINVAL;
292                 }
293         } while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
294
295         return 0;
296 }
297
298 static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
299                                   size_t size)
300 {
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;
307         u32 gw_cmd;
308         int n, i;
309
310         if (size < sizeof(*d) + sizeof(*block)) {
311                 wil_err_fw(wil, "gateway record too short: %zu\n", size);
312                 return -EINVAL;
313         }
314
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));
319                 return -EINVAL;
320         }
321         n = (size - sizeof(*d)) / sizeof(*block);
322
323         gw_cmd = le32_to_cpu(d->command);
324
325         wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
326                    n, gw_cmd);
327
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"))
336                 return -EINVAL;
337
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));
344
345         for (i = 0; i < n; i++) {
346                 int rc;
347                 u32 a = le32_to_cpu(block[i].addr);
348                 u32 v = le32_to_cpu(block[i].value);
349
350                 wil_dbg_fw(wil, "  gw write[%3d] [0x%08x] <== 0x%08x\n",
351                            i, a, v);
352
353                 writel(v, gwa_val);
354                 rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
355                 if (rc)
356                         return rc;
357         }
358
359         return 0;
360 }
361
362 static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
363                                    size_t size)
364 {
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;
371         u32 gw_cmd;
372         int n, i, k;
373
374         if (size < sizeof(*d) + sizeof(*block)) {
375                 wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
376                 return -EINVAL;
377         }
378
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));
383                 return -EINVAL;
384         }
385         n = (size - sizeof(*d)) / sizeof(*block);
386
387         gw_cmd = le32_to_cpu(d->command);
388
389         wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
390                    n, gw_cmd);
391
392         if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
393                                "gateway_addr_addr"))
394                 return -EINVAL;
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"))
399                         return -EINVAL;
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"))
404                 return -EINVAL;
405
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),
412                         false);
413
414         for (i = 0; i < n; i++) {
415                 int rc;
416                 u32 a = le32_to_cpu(block[i].addr);
417                 u32 v[ARRAY_SIZE(block->value)];
418
419                 for (k = 0; k < ARRAY_SIZE(block->value); k++)
420                         v[k] = le32_to_cpu(block[i].value[k]);
421
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,
424                                 sizeof(v), false);
425
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);
429                 if (rc)
430                         return rc;
431         }
432
433         return 0;
434 }
435
436 static const struct {
437         int type;
438         int (*load_handler)(struct wil6210_priv *wil, const void *data,
439                             size_t size);
440         int (*parse_handler)(struct wil6210_priv *wil, const void *data,
441                              size_t size);
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,
453                 fw_ignore_section},
454 };
455
456 static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
457                                 const void *data, size_t size, bool load)
458 {
459         int i;
460
461         for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
462                 if (wil_fw_handlers[i].type == type)
463                         return load ?
464                                 wil_fw_handlers[i].load_handler(
465                                         wil, data, size) :
466                                 wil_fw_handlers[i].parse_handler(
467                                         wil, data, size);
468
469         wil_err_fw(wil, "unknown record type: %d\n", type);
470         return -EINVAL;
471 }
472
473 /**
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
478  *
479  * Return error code
480  */
481 static int wil_fw_process(struct wil6210_priv *wil, const void *data,
482                           size_t size, bool load)
483 {
484         int rc = 0;
485         const struct wil_fw_record_head *hdr;
486         size_t s, hdr_sz;
487
488         for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
489                 if (size < sizeof(*hdr))
490                         break;
491                 hdr_sz = le32_to_cpu(hdr->size);
492                 s = sizeof(*hdr) + hdr_sz;
493                 if (s > size)
494                         break;
495                 if (hdr_sz % 4) {
496                         wil_err_fw(wil, "unaligned record size: %zu\n",
497                                    hdr_sz);
498                         return -EINVAL;
499                 }
500                 rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
501                                           &hdr[1], hdr_sz, load);
502                 if (rc)
503                         return rc;
504         }
505         if (size) {
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);
512                 }
513                 return -EINVAL;
514         }
515
516         return rc;
517 }
518
519 /**
520  * wil_request_firmware - Request firmware
521  *
522  * Request firmware image from the file
523  * If load is true, load firmware to device, otherwise
524  * only parse and extract capabilities
525  *
526  * Return error code
527  */
528 int wil_request_firmware(struct wil6210_priv *wil, const char *name,
529                          bool load)
530 {
531         int rc, rc1;
532         const struct firmware *fw;
533         size_t sz;
534         const void *d;
535
536         rc = reject_firmware(&fw, name, wil_to_dev(wil));
537         if (rc) {
538                 wil_err_fw(wil, "Failed to load firmware %s\n", name);
539                 return rc;
540         }
541         wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
542
543         for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
544                 rc1 = wil_fw_verify(wil, d, sz);
545                 if (rc1 < 0) {
546                         rc = rc1;
547                         goto out;
548                 }
549                 rc = wil_fw_process(wil, d, rc1, load);
550                 if (rc < 0)
551                         goto out;
552         }
553
554 out:
555         release_firmware(fw);
556         return rc;
557 }
558
559 /**
560  * wil_fw_verify_file_exists - checks if firmware file exist
561  *
562  * @wil: driver context
563  * @name: firmware file name
564  *
565  * return value - boolean, true for success, false for failure
566  */
567 bool wil_fw_verify_file_exists(struct wil6210_priv *wil, const char *name)
568 {
569         const struct firmware *fw;
570         int rc;
571
572         rc = reject_firmware(&fw, name, wil_to_dev(wil));
573         if (!rc)
574                 release_firmware(fw);
575         else
576                 wil_dbg_fw(wil, "<%s> not available: %d\n", name, rc);
577         return !rc;
578 }