Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: ISC
  2/*
  3 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
  4 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7/* Algorithmic part of the firmware download.
  8 * To be included in the container file providing framework
  9 */
 10
 11#define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
 12#define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
 13#define wil_hex_dump_fw(prefix_str, prefix_type, rowsize,		\
 14			groupsize, buf, len, ascii)			\
 15			print_hex_dump_debug("DBG[ FW ]" prefix_str,	\
 16					     prefix_type, rowsize,	\
 17					     groupsize, buf, len, ascii)
 18
 19static bool wil_fw_addr_check(struct wil6210_priv *wil,
 20			      void __iomem **ioaddr, __le32 val,
 21			      u32 size, const char *msg)
 22{
 23	*ioaddr = wmi_buffer_block(wil, val, size);
 24	if (!(*ioaddr)) {
 25		wil_err_fw(wil, "bad %s: 0x%08x\n", msg, le32_to_cpu(val));
 26		return false;
 27	}
 28	return true;
 29}
 30
 31/**
 32 * wil_fw_verify - verify firmware file validity
 33 *
 34 * perform various checks for the firmware file header.
 35 * records are not validated.
 36 *
 37 * Return file size or negative error
 38 */
 39static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
 40{
 41	const struct wil_fw_record_head *hdr = (const void *)data;
 42	struct wil_fw_record_file_header fh;
 43	const struct wil_fw_record_file_header *fh_;
 44	u32 crc;
 45	u32 dlen;
 46
 47	if (size % 4) {
 48		wil_err_fw(wil, "image size not aligned: %zu\n", size);
 49		return -EINVAL;
 50	}
 51	/* have enough data for the file header? */
 52	if (size < sizeof(*hdr) + sizeof(fh)) {
 53		wil_err_fw(wil, "file too short: %zu bytes\n", size);
 54		return -EINVAL;
 55	}
 56
 57	/* start with the file header? */
 58	if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
 59		wil_err_fw(wil, "no file header\n");
 60		return -EINVAL;
 61	}
 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
 80	/* signature */
 81	if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
 82		wil_err_fw(wil, "bad header signature: 0x%08x\n",
 83			   le32_to_cpu(fh_->signature));
 84		return -EINVAL;
 85	}
 86
 87	/* version */
 88	if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
 89		wil_err_fw(wil, "unsupported header version: %d\n",
 90			   le32_to_cpu(fh_->version));
 91		return -EINVAL;
 92	}
 93
 94	/* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
 95	fh = *fh_;
 96	fh.crc = 0;
 97
 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
104	if (crc != le32_to_cpu(fh_->crc)) {
105		wil_err_fw(wil, "checksum mismatch:"
106			   " calculated for %lu bytes 0x%08x != 0x%08x\n",
107			   (ulong)dlen, crc, le32_to_cpu(fh_->crc));
108		return -EINVAL;
109	}
110
111	return (int)dlen;
112}
113
114static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
115			     size_t size)
116{
117	return 0;
118}
119
120static int
121fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
122		       size_t size)
123{
124	const struct wil_fw_record_capabilities *rec = data;
125	size_t capa_size;
126
127	if (size < sizeof(*rec)) {
128		wil_err_fw(wil, "capabilities record too short: %zu\n", size);
129		/* let the FW load anyway */
130		return 0;
131	}
132
133	capa_size = size - offsetof(struct wil_fw_record_capabilities,
134				    capabilities);
135	bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
136	memcpy(wil->fw_capabilities, rec->capabilities,
137	       min_t(size_t, sizeof(wil->fw_capabilities), capa_size));
138	wil_hex_dump_fw("CAPA", DUMP_PREFIX_OFFSET, 16, 1,
139			rec->capabilities, capa_size, false);
140	return 0;
141}
142
143static int
144fw_handle_brd_file(struct wil6210_priv *wil, const void *data,
145		   size_t size)
146{
147	const struct wil_fw_record_brd_file *rec = data;
148	u32 max_num_ent, i, ent_size;
149
150	if (size <= offsetof(struct wil_fw_record_brd_file, brd_info)) {
151		wil_err(wil, "board record too short, size %zu\n", size);
152		return -EINVAL;
153	}
154
155	ent_size = size - offsetof(struct wil_fw_record_brd_file, brd_info);
156	max_num_ent = ent_size / sizeof(struct brd_info);
157
158	if (!max_num_ent) {
159		wil_err(wil, "brd info entries are missing\n");
160		return -EINVAL;
161	}
162
163	wil->brd_info = kcalloc(max_num_ent, sizeof(struct wil_brd_info),
164				GFP_KERNEL);
165	if (!wil->brd_info)
166		return -ENOMEM;
167
168	for (i = 0; i < max_num_ent; i++) {
169		wil->brd_info[i].file_addr =
170			le32_to_cpu(rec->brd_info[i].base_addr);
171		wil->brd_info[i].file_max_size =
172			le32_to_cpu(rec->brd_info[i].max_size_bytes);
173
174		if (!wil->brd_info[i].file_addr)
175			break;
176
177		wil_dbg_fw(wil,
178			   "brd info %d: file_addr 0x%x, file_max_size %d\n",
179			   i, wil->brd_info[i].file_addr,
180			   wil->brd_info[i].file_max_size);
181	}
182
183	wil->num_of_brd_entries = i;
184	if (wil->num_of_brd_entries == 0) {
185		kfree(wil->brd_info);
186		wil->brd_info = NULL;
187		wil_dbg_fw(wil,
188			   "no valid brd info entries, using brd file addr\n");
189
190	} else {
191		wil_dbg_fw(wil, "num of brd info entries %d\n",
192			   wil->num_of_brd_entries);
193	}
194
195	return 0;
196}
197
198static int
199fw_handle_concurrency(struct wil6210_priv *wil, const void *data,
200		      size_t size)
201{
202	const struct wil_fw_record_concurrency *rec = data;
203	const struct wil_fw_concurrency_combo *combo;
204	const struct wil_fw_concurrency_limit *limit;
205	size_t remain, lsize;
206	int i, n_combos;
207
208	if (size < sizeof(*rec)) {
209		wil_err_fw(wil, "concurrency record too short: %zu\n", size);
210		/* continue, let the FW load anyway */
211		return 0;
212	}
213
214	n_combos = le16_to_cpu(rec->n_combos);
215	remain = size - offsetof(struct wil_fw_record_concurrency, combos);
216	combo = rec->combos;
217	for (i = 0; i < n_combos; i++) {
218		if (remain < sizeof(*combo))
219			goto out_short;
220		remain -= sizeof(*combo);
221		limit = combo->limits;
222		lsize = combo->n_limits * sizeof(*limit);
223		if (remain < lsize)
224			goto out_short;
225		remain -= lsize;
226		limit += combo->n_limits;
227		combo = (struct wil_fw_concurrency_combo *)limit;
228	}
229
230	return wil_cfg80211_iface_combinations_from_fw(wil, rec);
231out_short:
232	wil_err_fw(wil, "concurrency record truncated\n");
233	return 0;
234}
235
236static int
237fw_handle_comment(struct wil6210_priv *wil, const void *data,
238		  size_t size)
239{
240	const struct wil_fw_record_comment_hdr *hdr = data;
241	u32 magic;
242	int rc = 0;
243
244	if (size < sizeof(*hdr))
245		return 0;
246
247	magic = le32_to_cpu(hdr->magic);
248
249	switch (magic) {
250	case WIL_FW_CAPABILITIES_MAGIC:
251		wil_dbg_fw(wil, "magic is WIL_FW_CAPABILITIES_MAGIC\n");
252		rc = fw_handle_capabilities(wil, data, size);
253		break;
254	case WIL_BRD_FILE_MAGIC:
255		wil_dbg_fw(wil, "magic is WIL_BRD_FILE_MAGIC\n");
256		rc = fw_handle_brd_file(wil, data, size);
257		break;
258	case WIL_FW_CONCURRENCY_MAGIC:
259		wil_dbg_fw(wil, "magic is WIL_FW_CONCURRENCY_MAGIC\n");
260		rc = fw_handle_concurrency(wil, data, size);
261		break;
262	default:
263		wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1,
264				data, size, true);
265	}
266
267	return rc;
268}
269
270static int __fw_handle_data(struct wil6210_priv *wil, const void *data,
271			    size_t size, __le32 addr)
272{
273	const struct wil_fw_record_data *d = data;
274	void __iomem *dst;
275	size_t s = size - sizeof(*d);
276
277	if (size < sizeof(*d) + sizeof(u32)) {
278		wil_err_fw(wil, "data record too short: %zu\n", size);
279		return -EINVAL;
280	}
281
282	if (!wil_fw_addr_check(wil, &dst, addr, s, "address"))
283		return -EINVAL;
284	wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(addr), s);
285	wil_memcpy_toio_32(dst, d->data, s);
286	wmb(); /* finish before processing next record */
287
288	return 0;
289}
290
291static int fw_handle_data(struct wil6210_priv *wil, const void *data,
292			  size_t size)
293{
294	const struct wil_fw_record_data *d = data;
295
296	return __fw_handle_data(wil, data, size, d->addr);
297}
298
299static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
300			  size_t size)
301{
302	const struct wil_fw_record_fill *d = data;
303	void __iomem *dst;
304	u32 v;
305	size_t s = (size_t)le32_to_cpu(d->size);
306
307	if (size != sizeof(*d)) {
308		wil_err_fw(wil, "bad size for fill record: %zu\n", size);
309		return -EINVAL;
310	}
311
312	if (s < sizeof(u32)) {
313		wil_err_fw(wil, "fill size too short: %zu\n", s);
314		return -EINVAL;
315	}
316
317	if (s % sizeof(u32)) {
318		wil_err_fw(wil, "fill size not aligned: %zu\n", s);
319		return -EINVAL;
320	}
321
322	if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
323		return -EINVAL;
324
325	v = le32_to_cpu(d->value);
326	wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
327		   le32_to_cpu(d->addr), v, s);
328	wil_memset_toio_32(dst, v, s);
329	wmb(); /* finish before processing next record */
330
331	return 0;
332}
333
334static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
335				 size_t size)
336{
337	const struct wil_fw_record_file_header *d = data;
338
339	if (size != sizeof(*d)) {
340		wil_err_fw(wil, "file header length incorrect: %zu\n", size);
341		return -EINVAL;
342	}
343
344	wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
345		   d->version, d->data_len);
346	wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
347			sizeof(d->comment), true);
348
349	if (!memcmp(d->comment, WIL_FW_VERSION_PREFIX,
350		    WIL_FW_VERSION_PREFIX_LEN))
351		memcpy(wil->fw_version,
352		       d->comment + WIL_FW_VERSION_PREFIX_LEN,
353		       min(sizeof(d->comment) - WIL_FW_VERSION_PREFIX_LEN,
354			   sizeof(wil->fw_version) - 1));
355
356	return 0;
357}
358
359static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
360				  size_t size)
361{
362	const struct wil_fw_record_direct_write *d = data;
363	const struct wil_fw_data_dwrite *block = d->data;
364	int n, i;
365
366	if (size % sizeof(*block)) {
367		wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
368			   sizeof(*block), size);
369		return -EINVAL;
370	}
371	n = size / sizeof(*block);
372
373	for (i = 0; i < n; i++) {
374		void __iomem *dst;
375		u32 m = le32_to_cpu(block[i].mask);
376		u32 v = le32_to_cpu(block[i].value);
377		u32 x, y;
378
379		if (!wil_fw_addr_check(wil, &dst, block[i].addr, 0, "address"))
380			return -EINVAL;
381
382		x = readl(dst);
383		y = (x & m) | (v & ~m);
384		wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
385			   "(old 0x%08x val 0x%08x mask 0x%08x)\n",
386			   le32_to_cpu(block[i].addr), y, x, v, m);
387		writel(y, dst);
388		wmb(); /* finish before processing next record */
389	}
390
391	return 0;
392}
393
394static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
395		    void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
396		    u32 a)
397{
398	unsigned delay = 0;
399
400	writel(a, gwa_addr);
401	writel(gw_cmd, gwa_cmd);
402	wmb(); /* finish before activate gw */
403
404	writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
405	do {
406		udelay(1); /* typical time is few usec */
407		if (delay++ > 100) {
408			wil_err_fw(wil, "gw timeout\n");
409			return -EINVAL;
410		}
411	} while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
412
413	return 0;
414}
415
416static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
417				  size_t size)
418{
419	const struct wil_fw_record_gateway_data *d = data;
420	const struct wil_fw_data_gw *block = d->data;
421	void __iomem *gwa_addr;
422	void __iomem *gwa_val;
423	void __iomem *gwa_cmd;
424	void __iomem *gwa_ctl;
425	u32 gw_cmd;
426	int n, i;
427
428	if (size < sizeof(*d) + sizeof(*block)) {
429		wil_err_fw(wil, "gateway record too short: %zu\n", size);
430		return -EINVAL;
431	}
432
433	if ((size - sizeof(*d)) % sizeof(*block)) {
434		wil_err_fw(wil, "gateway record data size"
435			   " not aligned on %zu: %zu\n",
436			   sizeof(*block), size - sizeof(*d));
437		return -EINVAL;
438	}
439	n = (size - sizeof(*d)) / sizeof(*block);
440
441	gw_cmd = le32_to_cpu(d->command);
442
443	wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
444		   n, gw_cmd);
445
446	if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
447			       "gateway_addr_addr") ||
448	    !wil_fw_addr_check(wil, &gwa_val, d->gateway_value_addr, 0,
449			       "gateway_value_addr") ||
450	    !wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
451			       "gateway_cmd_addr") ||
452	    !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
453			       "gateway_ctrl_address"))
454		return -EINVAL;
455
456	wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
457		   " cmd 0x%08x ctl 0x%08x\n",
458		   le32_to_cpu(d->gateway_addr_addr),
459		   le32_to_cpu(d->gateway_value_addr),
460		   le32_to_cpu(d->gateway_cmd_addr),
461		   le32_to_cpu(d->gateway_ctrl_address));
462
463	for (i = 0; i < n; i++) {
464		int rc;
465		u32 a = le32_to_cpu(block[i].addr);
466		u32 v = le32_to_cpu(block[i].value);
467
468		wil_dbg_fw(wil, "  gw write[%3d] [0x%08x] <== 0x%08x\n",
469			   i, a, v);
470
471		writel(v, gwa_val);
472		rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
473		if (rc)
474			return rc;
475	}
476
477	return 0;
478}
479
480static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
481				   size_t size)
482{
483	const struct wil_fw_record_gateway_data4 *d = data;
484	const struct wil_fw_data_gw4 *block = d->data;
485	void __iomem *gwa_addr;
486	void __iomem *gwa_val[ARRAY_SIZE(block->value)];
487	void __iomem *gwa_cmd;
488	void __iomem *gwa_ctl;
489	u32 gw_cmd;
490	int n, i, k;
491
492	if (size < sizeof(*d) + sizeof(*block)) {
493		wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
494		return -EINVAL;
495	}
496
497	if ((size - sizeof(*d)) % sizeof(*block)) {
498		wil_err_fw(wil, "gateway4 record data size"
499			   " not aligned on %zu: %zu\n",
500			   sizeof(*block), size - sizeof(*d));
501		return -EINVAL;
502	}
503	n = (size - sizeof(*d)) / sizeof(*block);
504
505	gw_cmd = le32_to_cpu(d->command);
506
507	wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
508		   n, gw_cmd);
509
510	if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
511			       "gateway_addr_addr"))
512		return -EINVAL;
513	for (k = 0; k < ARRAY_SIZE(block->value); k++)
514		if (!wil_fw_addr_check(wil, &gwa_val[k],
515				       d->gateway_value_addr[k],
516				       0, "gateway_value_addr"))
517			return -EINVAL;
518	if (!wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
519			       "gateway_cmd_addr") ||
520	    !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
521			       "gateway_ctrl_address"))
522		return -EINVAL;
523
524	wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
525		   le32_to_cpu(d->gateway_addr_addr),
526		   le32_to_cpu(d->gateway_cmd_addr),
527		   le32_to_cpu(d->gateway_ctrl_address));
528	wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
529			d->gateway_value_addr, sizeof(d->gateway_value_addr),
530			false);
531
532	for (i = 0; i < n; i++) {
533		int rc;
534		u32 a = le32_to_cpu(block[i].addr);
535		u32 v[ARRAY_SIZE(block->value)];
536
537		for (k = 0; k < ARRAY_SIZE(block->value); k++)
538			v[k] = le32_to_cpu(block[i].value[k]);
539
540		wil_dbg_fw(wil, "  gw4 write[%3d] [0x%08x] <==\n", i, a);
541		wil_hex_dump_fw("    val ", DUMP_PREFIX_NONE, 16, 4, v,
542				sizeof(v), false);
543
544		for (k = 0; k < ARRAY_SIZE(block->value); k++)
545			writel(v[k], gwa_val[k]);
546		rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
547		if (rc)
548			return rc;
549	}
550
551	return 0;
552}
553
554static const struct {
555	int type;
556	int (*load_handler)(struct wil6210_priv *wil, const void *data,
557			    size_t size);
558	int (*parse_handler)(struct wil6210_priv *wil, const void *data,
559			     size_t size);
560} wil_fw_handlers[] = {
561	{wil_fw_type_comment, fw_handle_comment, fw_handle_comment},
562	{wil_fw_type_data, fw_handle_data, fw_ignore_section},
563	{wil_fw_type_fill, fw_handle_fill, fw_ignore_section},
564	/* wil_fw_type_action */
565	/* wil_fw_type_verify */
566	{wil_fw_type_file_header, fw_handle_file_header,
567		fw_handle_file_header},
568	{wil_fw_type_direct_write, fw_handle_direct_write, fw_ignore_section},
569	{wil_fw_type_gateway_data, fw_handle_gateway_data, fw_ignore_section},
570	{wil_fw_type_gateway_data4, fw_handle_gateway_data4,
571		fw_ignore_section},
572};
573
574static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
575				const void *data, size_t size, bool load)
576{
577	int i;
578
579	for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
580		if (wil_fw_handlers[i].type == type)
581			return load ?
582				wil_fw_handlers[i].load_handler(
583					wil, data, size) :
584				wil_fw_handlers[i].parse_handler(
585					wil, data, size);
586
587	wil_err_fw(wil, "unknown record type: %d\n", type);
588	return -EINVAL;
589}
590
591/**
592 * wil_fw_process - process section from FW file
593 * if load is true: Load the FW and uCode code and data to the
594 * corresponding device memory regions,
595 * otherwise only parse and look for capabilities
596 *
597 * Return error code
598 */
599static int wil_fw_process(struct wil6210_priv *wil, const void *data,
600			  size_t size, bool load)
601{
602	int rc = 0;
603	const struct wil_fw_record_head *hdr;
604	size_t s, hdr_sz;
605
606	for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
607		if (size < sizeof(*hdr))
608			break;
609		hdr_sz = le32_to_cpu(hdr->size);
610		s = sizeof(*hdr) + hdr_sz;
611		if (s > size)
612			break;
613		if (hdr_sz % 4) {
614			wil_err_fw(wil, "unaligned record size: %zu\n",
615				   hdr_sz);
616			return -EINVAL;
617		}
618		rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
619					  &hdr[1], hdr_sz, load);
620		if (rc)
621			return rc;
622	}
623	if (size) {
624		wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
625		if (size >= sizeof(*hdr)) {
626			wil_err_fw(wil, "Stop at offset %ld"
627				   " record type %d [%zd bytes]\n",
628				   (long)((const void *)hdr - data),
629				   le16_to_cpu(hdr->type), hdr_sz);
630		}
631		return -EINVAL;
632	}
633
634	return rc;
635}
636
637/**
638 * wil_request_firmware - Request firmware
639 *
640 * Request firmware image from the file
641 * If load is true, load firmware to device, otherwise
642 * only parse and extract capabilities
643 *
644 * Return error code
645 */
646int wil_request_firmware(struct wil6210_priv *wil, const char *name,
647			 bool load)
648{
649	int rc, rc1;
650	const struct firmware *fw;
651	size_t sz;
652	const void *d;
653
654	rc = request_firmware(&fw, name, wil_to_dev(wil));
655	if (rc) {
656		wil_err_fw(wil, "Failed to load firmware %s rc %d\n", name, rc);
657		return rc;
658	}
659	wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
660
661	/* re-initialize board info params */
662	wil->num_of_brd_entries = 0;
663	kfree(wil->brd_info);
664	wil->brd_info = NULL;
665
666	for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
667		rc1 = wil_fw_verify(wil, d, sz);
668		if (rc1 < 0) {
669			rc = rc1;
670			goto out;
671		}
672		rc = wil_fw_process(wil, d, rc1, load);
673		if (rc < 0)
674			goto out;
675	}
676
677out:
678	release_firmware(fw);
679	if (rc)
680		wil_err_fw(wil, "Loading <%s> failed, rc %d\n", name, rc);
681	return rc;
682}
683
684/**
685 * wil_brd_process - process section from BRD file
686 *
687 * Return error code
688 */
689static int wil_brd_process(struct wil6210_priv *wil, const void *data,
690			   size_t size)
691{
692	int rc = 0;
693	const struct wil_fw_record_head *hdr = data;
694	size_t s, hdr_sz = 0;
695	u16 type;
696	int i = 0;
697
698	/* Assuming the board file includes only one file header
699	 * and one or several data records.
700	 * Each record starts with wil_fw_record_head.
701	 */
702	if (size < sizeof(*hdr))
703		return -EINVAL;
704	s = sizeof(*hdr) + le32_to_cpu(hdr->size);
705	if (s > size)
706		return -EINVAL;
707
708	/* Skip the header record and handle the data records */
709	size -= s;
710
711	for (hdr = data + s;; hdr = (const void *)hdr + s, size -= s, i++) {
712		if (size < sizeof(*hdr))
713			break;
714
715		if (i >= wil->num_of_brd_entries) {
716			wil_err_fw(wil,
717				   "Too many brd records: %d, num of expected entries %d\n",
718				   i, wil->num_of_brd_entries);
719			break;
720		}
721
722		hdr_sz = le32_to_cpu(hdr->size);
723		s = sizeof(*hdr) + hdr_sz;
724		if (wil->brd_info[i].file_max_size &&
725		    hdr_sz > wil->brd_info[i].file_max_size)
726			return -EINVAL;
727		if (sizeof(*hdr) + hdr_sz > size)
728			return -EINVAL;
729		if (hdr_sz % 4) {
730			wil_err_fw(wil, "unaligned record size: %zu\n",
731				   hdr_sz);
732			return -EINVAL;
733		}
734		type = le16_to_cpu(hdr->type);
735		if (type != wil_fw_type_data) {
736			wil_err_fw(wil,
737				   "invalid record type for board file: %d\n",
738				   type);
739			return -EINVAL;
740		}
741		if (hdr_sz < sizeof(struct wil_fw_record_data)) {
742			wil_err_fw(wil, "data record too short: %zu\n", hdr_sz);
743			return -EINVAL;
744		}
745
746		wil_dbg_fw(wil,
747			   "using info from fw file for record %d: addr[0x%08x], max size %d\n",
748			   i, wil->brd_info[i].file_addr,
749			   wil->brd_info[i].file_max_size);
750
751		rc = __fw_handle_data(wil, &hdr[1], hdr_sz,
752				      cpu_to_le32(wil->brd_info[i].file_addr));
753		if (rc)
754			return rc;
755	}
756
757	if (size) {
758		wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
759		if (size >= sizeof(*hdr)) {
760			wil_err_fw(wil,
761				   "Stop at offset %ld record type %d [%zd bytes]\n",
762				   (long)((const void *)hdr - data),
763				   le16_to_cpu(hdr->type), hdr_sz);
764		}
765		return -EINVAL;
766	}
767
768	return 0;
769}
770
771/**
772 * wil_request_board - Request board file
773 *
774 * Request board image from the file
775 * board file address and max size are read from FW file
776 * during initialization.
777 * brd file shall include one header and one data section.
778 *
779 * Return error code
780 */
781int wil_request_board(struct wil6210_priv *wil, const char *name)
782{
783	int rc, dlen;
784	const struct firmware *brd;
785
786	rc = request_firmware(&brd, name, wil_to_dev(wil));
787	if (rc) {
788		wil_err_fw(wil, "Failed to load brd %s\n", name);
789		return rc;
790	}
791	wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, brd->size);
792
793	/* Verify the header */
794	dlen = wil_fw_verify(wil, brd->data, brd->size);
795	if (dlen < 0) {
796		rc = dlen;
797		goto out;
798	}
799
800	/* Process the data records */
801	rc = wil_brd_process(wil, brd->data, dlen);
802
803out:
804	release_firmware(brd);
805	if (rc)
806		wil_err_fw(wil, "Loading <%s> failed, rc %d\n", name, rc);
807	return rc;
808}
809
810/**
811 * wil_fw_verify_file_exists - checks if firmware file exist
812 *
813 * @wil: driver context
814 * @name: firmware file name
815 *
816 * return value - boolean, true for success, false for failure
817 */
818bool wil_fw_verify_file_exists(struct wil6210_priv *wil, const char *name)
819{
820	const struct firmware *fw;
821	int rc;
822
823	rc = request_firmware(&fw, name, wil_to_dev(wil));
824	if (!rc)
825		release_firmware(fw);
826	else
827		wil_dbg_fw(wil, "<%s> not available: %d\n", name, rc);
828	return !rc;
829}
v5.4
 
  1/*
  2 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
  3 * Copyright (c) 2018-2019, 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
 18/* Algorithmic part of the firmware download.
 19 * To be included in the container file providing framework
 20 */
 21
 22#define wil_err_fw(wil, fmt, arg...) wil_err(wil, "ERR[ FW ]" fmt, ##arg)
 23#define wil_dbg_fw(wil, fmt, arg...) wil_dbg(wil, "DBG[ FW ]" fmt, ##arg)
 24#define wil_hex_dump_fw(prefix_str, prefix_type, rowsize,		\
 25			groupsize, buf, len, ascii)			\
 26			print_hex_dump_debug("DBG[ FW ]" prefix_str,	\
 27					     prefix_type, rowsize,	\
 28					     groupsize, buf, len, ascii)
 29
 30static bool wil_fw_addr_check(struct wil6210_priv *wil,
 31			      void __iomem **ioaddr, __le32 val,
 32			      u32 size, const char *msg)
 33{
 34	*ioaddr = wmi_buffer_block(wil, val, size);
 35	if (!(*ioaddr)) {
 36		wil_err_fw(wil, "bad %s: 0x%08x\n", msg, le32_to_cpu(val));
 37		return false;
 38	}
 39	return true;
 40}
 41
 42/**
 43 * wil_fw_verify - verify firmware file validity
 44 *
 45 * perform various checks for the firmware file header.
 46 * records are not validated.
 47 *
 48 * Return file size or negative error
 49 */
 50static int wil_fw_verify(struct wil6210_priv *wil, const u8 *data, size_t size)
 51{
 52	const struct wil_fw_record_head *hdr = (const void *)data;
 53	struct wil_fw_record_file_header fh;
 54	const struct wil_fw_record_file_header *fh_;
 55	u32 crc;
 56	u32 dlen;
 57
 58	if (size % 4) {
 59		wil_err_fw(wil, "image size not aligned: %zu\n", size);
 60		return -EINVAL;
 61	}
 62	/* have enough data for the file header? */
 63	if (size < sizeof(*hdr) + sizeof(fh)) {
 64		wil_err_fw(wil, "file too short: %zu bytes\n", size);
 65		return -EINVAL;
 66	}
 67
 68	/* start with the file header? */
 69	if (le16_to_cpu(hdr->type) != wil_fw_type_file_header) {
 70		wil_err_fw(wil, "no file header\n");
 71		return -EINVAL;
 72	}
 73
 74	/* data_len */
 75	fh_ = (struct wil_fw_record_file_header *)&hdr[1];
 76	dlen = le32_to_cpu(fh_->data_len);
 77	if (dlen % 4) {
 78		wil_err_fw(wil, "data length not aligned: %lu\n", (ulong)dlen);
 79		return -EINVAL;
 80	}
 81	if (size < dlen) {
 82		wil_err_fw(wil, "file truncated at %zu/%lu\n",
 83			   size, (ulong)dlen);
 84		return -EINVAL;
 85	}
 86	if (dlen < sizeof(*hdr) + sizeof(fh)) {
 87		wil_err_fw(wil, "data length too short: %lu\n", (ulong)dlen);
 88		return -EINVAL;
 89	}
 90
 91	/* signature */
 92	if (le32_to_cpu(fh_->signature) != WIL_FW_SIGNATURE) {
 93		wil_err_fw(wil, "bad header signature: 0x%08x\n",
 94			   le32_to_cpu(fh_->signature));
 95		return -EINVAL;
 96	}
 97
 98	/* version */
 99	if (le32_to_cpu(fh_->version) > WIL_FW_FMT_VERSION) {
100		wil_err_fw(wil, "unsupported header version: %d\n",
101			   le32_to_cpu(fh_->version));
102		return -EINVAL;
103	}
104
105	/* checksum. ~crc32(~0, data, size) when fh.crc set to 0*/
106	fh = *fh_;
107	fh.crc = 0;
108
109	crc = crc32_le(~0, (unsigned char const *)hdr, sizeof(*hdr));
110	crc = crc32_le(crc, (unsigned char const *)&fh, sizeof(fh));
111	crc = crc32_le(crc, (unsigned char const *)&fh_[1],
112		       dlen - sizeof(*hdr) - sizeof(fh));
113	crc = ~crc;
114
115	if (crc != le32_to_cpu(fh_->crc)) {
116		wil_err_fw(wil, "checksum mismatch:"
117			   " calculated for %lu bytes 0x%08x != 0x%08x\n",
118			   (ulong)dlen, crc, le32_to_cpu(fh_->crc));
119		return -EINVAL;
120	}
121
122	return (int)dlen;
123}
124
125static int fw_ignore_section(struct wil6210_priv *wil, const void *data,
126			     size_t size)
127{
128	return 0;
129}
130
131static int
132fw_handle_capabilities(struct wil6210_priv *wil, const void *data,
133		       size_t size)
134{
135	const struct wil_fw_record_capabilities *rec = data;
136	size_t capa_size;
137
138	if (size < sizeof(*rec)) {
139		wil_err_fw(wil, "capabilities record too short: %zu\n", size);
140		/* let the FW load anyway */
141		return 0;
142	}
143
144	capa_size = size - offsetof(struct wil_fw_record_capabilities,
145				    capabilities);
146	bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
147	memcpy(wil->fw_capabilities, rec->capabilities,
148	       min_t(size_t, sizeof(wil->fw_capabilities), capa_size));
149	wil_hex_dump_fw("CAPA", DUMP_PREFIX_OFFSET, 16, 1,
150			rec->capabilities, capa_size, false);
151	return 0;
152}
153
154static int
155fw_handle_brd_file(struct wil6210_priv *wil, const void *data,
156		   size_t size)
157{
158	const struct wil_fw_record_brd_file *rec = data;
159	u32 max_num_ent, i, ent_size;
160
161	if (size <= offsetof(struct wil_fw_record_brd_file, brd_info)) {
162		wil_err(wil, "board record too short, size %zu\n", size);
163		return -EINVAL;
164	}
165
166	ent_size = size - offsetof(struct wil_fw_record_brd_file, brd_info);
167	max_num_ent = ent_size / sizeof(struct brd_info);
168
169	if (!max_num_ent) {
170		wil_err(wil, "brd info entries are missing\n");
171		return -EINVAL;
172	}
173
174	wil->brd_info = kcalloc(max_num_ent, sizeof(struct wil_brd_info),
175				GFP_KERNEL);
176	if (!wil->brd_info)
177		return -ENOMEM;
178
179	for (i = 0; i < max_num_ent; i++) {
180		wil->brd_info[i].file_addr =
181			le32_to_cpu(rec->brd_info[i].base_addr);
182		wil->brd_info[i].file_max_size =
183			le32_to_cpu(rec->brd_info[i].max_size_bytes);
184
185		if (!wil->brd_info[i].file_addr)
186			break;
187
188		wil_dbg_fw(wil,
189			   "brd info %d: file_addr 0x%x, file_max_size %d\n",
190			   i, wil->brd_info[i].file_addr,
191			   wil->brd_info[i].file_max_size);
192	}
193
194	wil->num_of_brd_entries = i;
195	if (wil->num_of_brd_entries == 0) {
196		kfree(wil->brd_info);
197		wil->brd_info = NULL;
198		wil_dbg_fw(wil,
199			   "no valid brd info entries, using brd file addr\n");
200
201	} else {
202		wil_dbg_fw(wil, "num of brd info entries %d\n",
203			   wil->num_of_brd_entries);
204	}
205
206	return 0;
207}
208
209static int
210fw_handle_concurrency(struct wil6210_priv *wil, const void *data,
211		      size_t size)
212{
213	const struct wil_fw_record_concurrency *rec = data;
214	const struct wil_fw_concurrency_combo *combo;
215	const struct wil_fw_concurrency_limit *limit;
216	size_t remain, lsize;
217	int i, n_combos;
218
219	if (size < sizeof(*rec)) {
220		wil_err_fw(wil, "concurrency record too short: %zu\n", size);
221		/* continue, let the FW load anyway */
222		return 0;
223	}
224
225	n_combos = le16_to_cpu(rec->n_combos);
226	remain = size - offsetof(struct wil_fw_record_concurrency, combos);
227	combo = rec->combos;
228	for (i = 0; i < n_combos; i++) {
229		if (remain < sizeof(*combo))
230			goto out_short;
231		remain -= sizeof(*combo);
232		limit = combo->limits;
233		lsize = combo->n_limits * sizeof(*limit);
234		if (remain < lsize)
235			goto out_short;
236		remain -= lsize;
237		limit += combo->n_limits;
238		combo = (struct wil_fw_concurrency_combo *)limit;
239	}
240
241	return wil_cfg80211_iface_combinations_from_fw(wil, rec);
242out_short:
243	wil_err_fw(wil, "concurrency record truncated\n");
244	return 0;
245}
246
247static int
248fw_handle_comment(struct wil6210_priv *wil, const void *data,
249		  size_t size)
250{
251	const struct wil_fw_record_comment_hdr *hdr = data;
252	u32 magic;
253	int rc = 0;
254
255	if (size < sizeof(*hdr))
256		return 0;
257
258	magic = le32_to_cpu(hdr->magic);
259
260	switch (magic) {
261	case WIL_FW_CAPABILITIES_MAGIC:
262		wil_dbg_fw(wil, "magic is WIL_FW_CAPABILITIES_MAGIC\n");
263		rc = fw_handle_capabilities(wil, data, size);
264		break;
265	case WIL_BRD_FILE_MAGIC:
266		wil_dbg_fw(wil, "magic is WIL_BRD_FILE_MAGIC\n");
267		rc = fw_handle_brd_file(wil, data, size);
268		break;
269	case WIL_FW_CONCURRENCY_MAGIC:
270		wil_dbg_fw(wil, "magic is WIL_FW_CONCURRENCY_MAGIC\n");
271		rc = fw_handle_concurrency(wil, data, size);
272		break;
273	default:
274		wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1,
275				data, size, true);
276	}
277
278	return rc;
279}
280
281static int __fw_handle_data(struct wil6210_priv *wil, const void *data,
282			    size_t size, __le32 addr)
283{
284	const struct wil_fw_record_data *d = data;
285	void __iomem *dst;
286	size_t s = size - sizeof(*d);
287
288	if (size < sizeof(*d) + sizeof(u32)) {
289		wil_err_fw(wil, "data record too short: %zu\n", size);
290		return -EINVAL;
291	}
292
293	if (!wil_fw_addr_check(wil, &dst, addr, s, "address"))
294		return -EINVAL;
295	wil_dbg_fw(wil, "write [0x%08x] <== %zu bytes\n", le32_to_cpu(addr), s);
296	wil_memcpy_toio_32(dst, d->data, s);
297	wmb(); /* finish before processing next record */
298
299	return 0;
300}
301
302static int fw_handle_data(struct wil6210_priv *wil, const void *data,
303			  size_t size)
304{
305	const struct wil_fw_record_data *d = data;
306
307	return __fw_handle_data(wil, data, size, d->addr);
308}
309
310static int fw_handle_fill(struct wil6210_priv *wil, const void *data,
311			  size_t size)
312{
313	const struct wil_fw_record_fill *d = data;
314	void __iomem *dst;
315	u32 v;
316	size_t s = (size_t)le32_to_cpu(d->size);
317
318	if (size != sizeof(*d)) {
319		wil_err_fw(wil, "bad size for fill record: %zu\n", size);
320		return -EINVAL;
321	}
322
323	if (s < sizeof(u32)) {
324		wil_err_fw(wil, "fill size too short: %zu\n", s);
325		return -EINVAL;
326	}
327
328	if (s % sizeof(u32)) {
329		wil_err_fw(wil, "fill size not aligned: %zu\n", s);
330		return -EINVAL;
331	}
332
333	if (!wil_fw_addr_check(wil, &dst, d->addr, s, "address"))
334		return -EINVAL;
335
336	v = le32_to_cpu(d->value);
337	wil_dbg_fw(wil, "fill [0x%08x] <== 0x%08x, %zu bytes\n",
338		   le32_to_cpu(d->addr), v, s);
339	wil_memset_toio_32(dst, v, s);
340	wmb(); /* finish before processing next record */
341
342	return 0;
343}
344
345static int fw_handle_file_header(struct wil6210_priv *wil, const void *data,
346				 size_t size)
347{
348	const struct wil_fw_record_file_header *d = data;
349
350	if (size != sizeof(*d)) {
351		wil_err_fw(wil, "file header length incorrect: %zu\n", size);
352		return -EINVAL;
353	}
354
355	wil_dbg_fw(wil, "new file, ver. %d, %i bytes\n",
356		   d->version, d->data_len);
357	wil_hex_dump_fw("", DUMP_PREFIX_OFFSET, 16, 1, d->comment,
358			sizeof(d->comment), true);
359
360	if (!memcmp(d->comment, WIL_FW_VERSION_PREFIX,
361		    WIL_FW_VERSION_PREFIX_LEN))
362		memcpy(wil->fw_version,
363		       d->comment + WIL_FW_VERSION_PREFIX_LEN,
364		       min(sizeof(d->comment) - WIL_FW_VERSION_PREFIX_LEN,
365			   sizeof(wil->fw_version) - 1));
366
367	return 0;
368}
369
370static int fw_handle_direct_write(struct wil6210_priv *wil, const void *data,
371				  size_t size)
372{
373	const struct wil_fw_record_direct_write *d = data;
374	const struct wil_fw_data_dwrite *block = d->data;
375	int n, i;
376
377	if (size % sizeof(*block)) {
378		wil_err_fw(wil, "record size not aligned on %zu: %zu\n",
379			   sizeof(*block), size);
380		return -EINVAL;
381	}
382	n = size / sizeof(*block);
383
384	for (i = 0; i < n; i++) {
385		void __iomem *dst;
386		u32 m = le32_to_cpu(block[i].mask);
387		u32 v = le32_to_cpu(block[i].value);
388		u32 x, y;
389
390		if (!wil_fw_addr_check(wil, &dst, block[i].addr, 0, "address"))
391			return -EINVAL;
392
393		x = readl(dst);
394		y = (x & m) | (v & ~m);
395		wil_dbg_fw(wil, "write [0x%08x] <== 0x%08x "
396			   "(old 0x%08x val 0x%08x mask 0x%08x)\n",
397			   le32_to_cpu(block[i].addr), y, x, v, m);
398		writel(y, dst);
399		wmb(); /* finish before processing next record */
400	}
401
402	return 0;
403}
404
405static int gw_write(struct wil6210_priv *wil, void __iomem *gwa_addr,
406		    void __iomem *gwa_cmd, void __iomem *gwa_ctl, u32 gw_cmd,
407		    u32 a)
408{
409	unsigned delay = 0;
410
411	writel(a, gwa_addr);
412	writel(gw_cmd, gwa_cmd);
413	wmb(); /* finish before activate gw */
414
415	writel(WIL_FW_GW_CTL_RUN, gwa_ctl); /* activate gw */
416	do {
417		udelay(1); /* typical time is few usec */
418		if (delay++ > 100) {
419			wil_err_fw(wil, "gw timeout\n");
420			return -EINVAL;
421		}
422	} while (readl(gwa_ctl) & WIL_FW_GW_CTL_BUSY); /* gw done? */
423
424	return 0;
425}
426
427static int fw_handle_gateway_data(struct wil6210_priv *wil, const void *data,
428				  size_t size)
429{
430	const struct wil_fw_record_gateway_data *d = data;
431	const struct wil_fw_data_gw *block = d->data;
432	void __iomem *gwa_addr;
433	void __iomem *gwa_val;
434	void __iomem *gwa_cmd;
435	void __iomem *gwa_ctl;
436	u32 gw_cmd;
437	int n, i;
438
439	if (size < sizeof(*d) + sizeof(*block)) {
440		wil_err_fw(wil, "gateway record too short: %zu\n", size);
441		return -EINVAL;
442	}
443
444	if ((size - sizeof(*d)) % sizeof(*block)) {
445		wil_err_fw(wil, "gateway record data size"
446			   " not aligned on %zu: %zu\n",
447			   sizeof(*block), size - sizeof(*d));
448		return -EINVAL;
449	}
450	n = (size - sizeof(*d)) / sizeof(*block);
451
452	gw_cmd = le32_to_cpu(d->command);
453
454	wil_dbg_fw(wil, "gw write record [%3d] blocks, cmd 0x%08x\n",
455		   n, gw_cmd);
456
457	if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
458			       "gateway_addr_addr") ||
459	    !wil_fw_addr_check(wil, &gwa_val, d->gateway_value_addr, 0,
460			       "gateway_value_addr") ||
461	    !wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
462			       "gateway_cmd_addr") ||
463	    !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
464			       "gateway_ctrl_address"))
465		return -EINVAL;
466
467	wil_dbg_fw(wil, "gw addresses: addr 0x%08x val 0x%08x"
468		   " cmd 0x%08x ctl 0x%08x\n",
469		   le32_to_cpu(d->gateway_addr_addr),
470		   le32_to_cpu(d->gateway_value_addr),
471		   le32_to_cpu(d->gateway_cmd_addr),
472		   le32_to_cpu(d->gateway_ctrl_address));
473
474	for (i = 0; i < n; i++) {
475		int rc;
476		u32 a = le32_to_cpu(block[i].addr);
477		u32 v = le32_to_cpu(block[i].value);
478
479		wil_dbg_fw(wil, "  gw write[%3d] [0x%08x] <== 0x%08x\n",
480			   i, a, v);
481
482		writel(v, gwa_val);
483		rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
484		if (rc)
485			return rc;
486	}
487
488	return 0;
489}
490
491static int fw_handle_gateway_data4(struct wil6210_priv *wil, const void *data,
492				   size_t size)
493{
494	const struct wil_fw_record_gateway_data4 *d = data;
495	const struct wil_fw_data_gw4 *block = d->data;
496	void __iomem *gwa_addr;
497	void __iomem *gwa_val[ARRAY_SIZE(block->value)];
498	void __iomem *gwa_cmd;
499	void __iomem *gwa_ctl;
500	u32 gw_cmd;
501	int n, i, k;
502
503	if (size < sizeof(*d) + sizeof(*block)) {
504		wil_err_fw(wil, "gateway4 record too short: %zu\n", size);
505		return -EINVAL;
506	}
507
508	if ((size - sizeof(*d)) % sizeof(*block)) {
509		wil_err_fw(wil, "gateway4 record data size"
510			   " not aligned on %zu: %zu\n",
511			   sizeof(*block), size - sizeof(*d));
512		return -EINVAL;
513	}
514	n = (size - sizeof(*d)) / sizeof(*block);
515
516	gw_cmd = le32_to_cpu(d->command);
517
518	wil_dbg_fw(wil, "gw4 write record [%3d] blocks, cmd 0x%08x\n",
519		   n, gw_cmd);
520
521	if (!wil_fw_addr_check(wil, &gwa_addr, d->gateway_addr_addr, 0,
522			       "gateway_addr_addr"))
523		return -EINVAL;
524	for (k = 0; k < ARRAY_SIZE(block->value); k++)
525		if (!wil_fw_addr_check(wil, &gwa_val[k],
526				       d->gateway_value_addr[k],
527				       0, "gateway_value_addr"))
528			return -EINVAL;
529	if (!wil_fw_addr_check(wil, &gwa_cmd, d->gateway_cmd_addr, 0,
530			       "gateway_cmd_addr") ||
531	    !wil_fw_addr_check(wil, &gwa_ctl, d->gateway_ctrl_address, 0,
532			       "gateway_ctrl_address"))
533		return -EINVAL;
534
535	wil_dbg_fw(wil, "gw4 addresses: addr 0x%08x cmd 0x%08x ctl 0x%08x\n",
536		   le32_to_cpu(d->gateway_addr_addr),
537		   le32_to_cpu(d->gateway_cmd_addr),
538		   le32_to_cpu(d->gateway_ctrl_address));
539	wil_hex_dump_fw("val addresses: ", DUMP_PREFIX_NONE, 16, 4,
540			d->gateway_value_addr, sizeof(d->gateway_value_addr),
541			false);
542
543	for (i = 0; i < n; i++) {
544		int rc;
545		u32 a = le32_to_cpu(block[i].addr);
546		u32 v[ARRAY_SIZE(block->value)];
547
548		for (k = 0; k < ARRAY_SIZE(block->value); k++)
549			v[k] = le32_to_cpu(block[i].value[k]);
550
551		wil_dbg_fw(wil, "  gw4 write[%3d] [0x%08x] <==\n", i, a);
552		wil_hex_dump_fw("    val ", DUMP_PREFIX_NONE, 16, 4, v,
553				sizeof(v), false);
554
555		for (k = 0; k < ARRAY_SIZE(block->value); k++)
556			writel(v[k], gwa_val[k]);
557		rc = gw_write(wil, gwa_addr, gwa_cmd, gwa_ctl, gw_cmd, a);
558		if (rc)
559			return rc;
560	}
561
562	return 0;
563}
564
565static const struct {
566	int type;
567	int (*load_handler)(struct wil6210_priv *wil, const void *data,
568			    size_t size);
569	int (*parse_handler)(struct wil6210_priv *wil, const void *data,
570			     size_t size);
571} wil_fw_handlers[] = {
572	{wil_fw_type_comment, fw_handle_comment, fw_handle_comment},
573	{wil_fw_type_data, fw_handle_data, fw_ignore_section},
574	{wil_fw_type_fill, fw_handle_fill, fw_ignore_section},
575	/* wil_fw_type_action */
576	/* wil_fw_type_verify */
577	{wil_fw_type_file_header, fw_handle_file_header,
578		fw_handle_file_header},
579	{wil_fw_type_direct_write, fw_handle_direct_write, fw_ignore_section},
580	{wil_fw_type_gateway_data, fw_handle_gateway_data, fw_ignore_section},
581	{wil_fw_type_gateway_data4, fw_handle_gateway_data4,
582		fw_ignore_section},
583};
584
585static int wil_fw_handle_record(struct wil6210_priv *wil, int type,
586				const void *data, size_t size, bool load)
587{
588	int i;
589
590	for (i = 0; i < ARRAY_SIZE(wil_fw_handlers); i++)
591		if (wil_fw_handlers[i].type == type)
592			return load ?
593				wil_fw_handlers[i].load_handler(
594					wil, data, size) :
595				wil_fw_handlers[i].parse_handler(
596					wil, data, size);
597
598	wil_err_fw(wil, "unknown record type: %d\n", type);
599	return -EINVAL;
600}
601
602/**
603 * wil_fw_process - process section from FW file
604 * if load is true: Load the FW and uCode code and data to the
605 * corresponding device memory regions,
606 * otherwise only parse and look for capabilities
607 *
608 * Return error code
609 */
610static int wil_fw_process(struct wil6210_priv *wil, const void *data,
611			  size_t size, bool load)
612{
613	int rc = 0;
614	const struct wil_fw_record_head *hdr;
615	size_t s, hdr_sz;
616
617	for (hdr = data;; hdr = (const void *)hdr + s, size -= s) {
618		if (size < sizeof(*hdr))
619			break;
620		hdr_sz = le32_to_cpu(hdr->size);
621		s = sizeof(*hdr) + hdr_sz;
622		if (s > size)
623			break;
624		if (hdr_sz % 4) {
625			wil_err_fw(wil, "unaligned record size: %zu\n",
626				   hdr_sz);
627			return -EINVAL;
628		}
629		rc = wil_fw_handle_record(wil, le16_to_cpu(hdr->type),
630					  &hdr[1], hdr_sz, load);
631		if (rc)
632			return rc;
633	}
634	if (size) {
635		wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
636		if (size >= sizeof(*hdr)) {
637			wil_err_fw(wil, "Stop at offset %ld"
638				   " record type %d [%zd bytes]\n",
639				   (long)((const void *)hdr - data),
640				   le16_to_cpu(hdr->type), hdr_sz);
641		}
642		return -EINVAL;
643	}
644
645	return rc;
646}
647
648/**
649 * wil_request_firmware - Request firmware
650 *
651 * Request firmware image from the file
652 * If load is true, load firmware to device, otherwise
653 * only parse and extract capabilities
654 *
655 * Return error code
656 */
657int wil_request_firmware(struct wil6210_priv *wil, const char *name,
658			 bool load)
659{
660	int rc, rc1;
661	const struct firmware *fw;
662	size_t sz;
663	const void *d;
664
665	rc = request_firmware(&fw, name, wil_to_dev(wil));
666	if (rc) {
667		wil_err_fw(wil, "Failed to load firmware %s rc %d\n", name, rc);
668		return rc;
669	}
670	wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, fw->size);
671
672	/* re-initialize board info params */
673	wil->num_of_brd_entries = 0;
674	kfree(wil->brd_info);
675	wil->brd_info = NULL;
676
677	for (sz = fw->size, d = fw->data; sz; sz -= rc1, d += rc1) {
678		rc1 = wil_fw_verify(wil, d, sz);
679		if (rc1 < 0) {
680			rc = rc1;
681			goto out;
682		}
683		rc = wil_fw_process(wil, d, rc1, load);
684		if (rc < 0)
685			goto out;
686	}
687
688out:
689	release_firmware(fw);
690	if (rc)
691		wil_err_fw(wil, "Loading <%s> failed, rc %d\n", name, rc);
692	return rc;
693}
694
695/**
696 * wil_brd_process - process section from BRD file
697 *
698 * Return error code
699 */
700static int wil_brd_process(struct wil6210_priv *wil, const void *data,
701			   size_t size)
702{
703	int rc = 0;
704	const struct wil_fw_record_head *hdr = data;
705	size_t s, hdr_sz = 0;
706	u16 type;
707	int i = 0;
708
709	/* Assuming the board file includes only one file header
710	 * and one or several data records.
711	 * Each record starts with wil_fw_record_head.
712	 */
713	if (size < sizeof(*hdr))
714		return -EINVAL;
715	s = sizeof(*hdr) + le32_to_cpu(hdr->size);
716	if (s > size)
717		return -EINVAL;
718
719	/* Skip the header record and handle the data records */
720	size -= s;
721
722	for (hdr = data + s;; hdr = (const void *)hdr + s, size -= s, i++) {
723		if (size < sizeof(*hdr))
724			break;
725
726		if (i >= wil->num_of_brd_entries) {
727			wil_err_fw(wil,
728				   "Too many brd records: %d, num of expected entries %d\n",
729				   i, wil->num_of_brd_entries);
730			break;
731		}
732
733		hdr_sz = le32_to_cpu(hdr->size);
734		s = sizeof(*hdr) + hdr_sz;
735		if (wil->brd_info[i].file_max_size &&
736		    hdr_sz > wil->brd_info[i].file_max_size)
737			return -EINVAL;
738		if (sizeof(*hdr) + hdr_sz > size)
739			return -EINVAL;
740		if (hdr_sz % 4) {
741			wil_err_fw(wil, "unaligned record size: %zu\n",
742				   hdr_sz);
743			return -EINVAL;
744		}
745		type = le16_to_cpu(hdr->type);
746		if (type != wil_fw_type_data) {
747			wil_err_fw(wil,
748				   "invalid record type for board file: %d\n",
749				   type);
750			return -EINVAL;
751		}
752		if (hdr_sz < sizeof(struct wil_fw_record_data)) {
753			wil_err_fw(wil, "data record too short: %zu\n", hdr_sz);
754			return -EINVAL;
755		}
756
757		wil_dbg_fw(wil,
758			   "using info from fw file for record %d: addr[0x%08x], max size %d\n",
759			   i, wil->brd_info[i].file_addr,
760			   wil->brd_info[i].file_max_size);
761
762		rc = __fw_handle_data(wil, &hdr[1], hdr_sz,
763				      cpu_to_le32(wil->brd_info[i].file_addr));
764		if (rc)
765			return rc;
766	}
767
768	if (size) {
769		wil_err_fw(wil, "unprocessed bytes: %zu\n", size);
770		if (size >= sizeof(*hdr)) {
771			wil_err_fw(wil,
772				   "Stop at offset %ld record type %d [%zd bytes]\n",
773				   (long)((const void *)hdr - data),
774				   le16_to_cpu(hdr->type), hdr_sz);
775		}
776		return -EINVAL;
777	}
778
779	return 0;
780}
781
782/**
783 * wil_request_board - Request board file
784 *
785 * Request board image from the file
786 * board file address and max size are read from FW file
787 * during initialization.
788 * brd file shall include one header and one data section.
789 *
790 * Return error code
791 */
792int wil_request_board(struct wil6210_priv *wil, const char *name)
793{
794	int rc, dlen;
795	const struct firmware *brd;
796
797	rc = request_firmware(&brd, name, wil_to_dev(wil));
798	if (rc) {
799		wil_err_fw(wil, "Failed to load brd %s\n", name);
800		return rc;
801	}
802	wil_dbg_fw(wil, "Loading <%s>, %zu bytes\n", name, brd->size);
803
804	/* Verify the header */
805	dlen = wil_fw_verify(wil, brd->data, brd->size);
806	if (dlen < 0) {
807		rc = dlen;
808		goto out;
809	}
810
811	/* Process the data records */
812	rc = wil_brd_process(wil, brd->data, dlen);
813
814out:
815	release_firmware(brd);
816	if (rc)
817		wil_err_fw(wil, "Loading <%s> failed, rc %d\n", name, rc);
818	return rc;
819}
820
821/**
822 * wil_fw_verify_file_exists - checks if firmware file exist
823 *
824 * @wil: driver context
825 * @name: firmware file name
826 *
827 * return value - boolean, true for success, false for failure
828 */
829bool wil_fw_verify_file_exists(struct wil6210_priv *wil, const char *name)
830{
831	const struct firmware *fw;
832	int rc;
833
834	rc = request_firmware(&fw, name, wil_to_dev(wil));
835	if (!rc)
836		release_firmware(fw);
837	else
838		wil_dbg_fw(wil, "<%s> not available: %d\n", name, rc);
839	return !rc;
840}