1 /******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright (C) 2018 - 2020 Intel Corporation
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program.
21 *
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
24 *
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 * BSD LICENSE
30 *
31 * Copyright (C) 2018 - 2020 Intel Corporation
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 *
38 * * Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * * Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in
42 * the documentation and/or other materials provided with the
43 * distribution.
44 * * Neither the name Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 *****************************************************************************/
61
62 #include <linux/firmware.h>
63 #include "iwl-drv.h"
64 #include "iwl-trans.h"
65 #include "iwl-dbg-tlv.h"
66 #include "fw/dbg.h"
67 #include "fw/runtime.h"
68
69 /**
70 * enum iwl_dbg_tlv_type - debug TLV types
71 * @IWL_DBG_TLV_TYPE_DEBUG_INFO: debug info TLV
72 * @IWL_DBG_TLV_TYPE_BUF_ALLOC: buffer allocation TLV
73 * @IWL_DBG_TLV_TYPE_HCMD: host command TLV
74 * @IWL_DBG_TLV_TYPE_REGION: region TLV
75 * @IWL_DBG_TLV_TYPE_TRIGGER: trigger TLV
76 * @IWL_DBG_TLV_TYPE_NUM: number of debug TLVs
77 */
78 enum iwl_dbg_tlv_type {
79 IWL_DBG_TLV_TYPE_DEBUG_INFO =
80 IWL_UCODE_TLV_TYPE_DEBUG_INFO - IWL_UCODE_TLV_DEBUG_BASE,
81 IWL_DBG_TLV_TYPE_BUF_ALLOC,
82 IWL_DBG_TLV_TYPE_HCMD,
83 IWL_DBG_TLV_TYPE_REGION,
84 IWL_DBG_TLV_TYPE_TRIGGER,
85 IWL_DBG_TLV_TYPE_NUM,
86 };
87
88 /**
89 * struct iwl_dbg_tlv_ver_data - debug TLV version struct
90 * @min_ver: min version supported
91 * @max_ver: max version supported
92 */
93 struct iwl_dbg_tlv_ver_data {
94 int min_ver;
95 int max_ver;
96 };
97
98 /**
99 * struct iwl_dbg_tlv_timer_node - timer node struct
100 * @list: list of &struct iwl_dbg_tlv_timer_node
101 * @timer: timer
102 * @fwrt: &struct iwl_fw_runtime
103 * @tlv: TLV attach to the timer node
104 */
105 struct iwl_dbg_tlv_timer_node {
106 struct list_head list;
107 struct timer_list timer;
108 struct iwl_fw_runtime *fwrt;
109 struct iwl_ucode_tlv *tlv;
110 };
111
112 static const struct iwl_dbg_tlv_ver_data
113 dbg_ver_table[IWL_DBG_TLV_TYPE_NUM] = {
114 [IWL_DBG_TLV_TYPE_DEBUG_INFO] = {.min_ver = 1, .max_ver = 1,},
115 [IWL_DBG_TLV_TYPE_BUF_ALLOC] = {.min_ver = 1, .max_ver = 1,},
116 [IWL_DBG_TLV_TYPE_HCMD] = {.min_ver = 1, .max_ver = 1,},
117 [IWL_DBG_TLV_TYPE_REGION] = {.min_ver = 1, .max_ver = 1,},
118 [IWL_DBG_TLV_TYPE_TRIGGER] = {.min_ver = 1, .max_ver = 1,},
119 };
120
iwl_dbg_tlv_add(struct iwl_ucode_tlv * tlv,struct list_head * list)121 static int iwl_dbg_tlv_add(struct iwl_ucode_tlv *tlv, struct list_head *list)
122 {
123 u32 len = le32_to_cpu(tlv->length);
124 struct iwl_dbg_tlv_node *node;
125
126 node = kzalloc(sizeof(*node) + len, GFP_KERNEL);
127 if (!node)
128 return -ENOMEM;
129
130 memcpy(&node->tlv, tlv, sizeof(node->tlv) + len);
131 list_add_tail(&node->list, list);
132
133 return 0;
134 }
135
iwl_dbg_tlv_ver_support(struct iwl_ucode_tlv * tlv)136 static bool iwl_dbg_tlv_ver_support(struct iwl_ucode_tlv *tlv)
137 {
138 struct iwl_fw_ini_header *hdr = (void *)&tlv->data[0];
139 u32 type = le32_to_cpu(tlv->type);
140 u32 tlv_idx = type - IWL_UCODE_TLV_DEBUG_BASE;
141 u32 ver = le32_to_cpu(hdr->version);
142
143 if (ver < dbg_ver_table[tlv_idx].min_ver ||
144 ver > dbg_ver_table[tlv_idx].max_ver)
145 return false;
146
147 return true;
148 }
149
iwl_dbg_tlv_alloc_debug_info(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv)150 static int iwl_dbg_tlv_alloc_debug_info(struct iwl_trans *trans,
151 struct iwl_ucode_tlv *tlv)
152 {
153 struct iwl_fw_ini_debug_info_tlv *debug_info = (void *)tlv->data;
154
155 if (le32_to_cpu(tlv->length) != sizeof(*debug_info))
156 return -EINVAL;
157
158 IWL_DEBUG_FW(trans, "WRT: Loading debug cfg: %s\n",
159 debug_info->debug_cfg_name);
160
161 return iwl_dbg_tlv_add(tlv, &trans->dbg.debug_info_tlv_list);
162 }
163
iwl_dbg_tlv_alloc_buf_alloc(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv)164 static int iwl_dbg_tlv_alloc_buf_alloc(struct iwl_trans *trans,
165 struct iwl_ucode_tlv *tlv)
166 {
167 struct iwl_fw_ini_allocation_tlv *alloc = (void *)tlv->data;
168 u32 buf_location;
169 u32 alloc_id;
170
171 if (le32_to_cpu(tlv->length) != sizeof(*alloc))
172 return -EINVAL;
173
174 buf_location = le32_to_cpu(alloc->buf_location);
175 alloc_id = le32_to_cpu(alloc->alloc_id);
176
177 if (buf_location == IWL_FW_INI_LOCATION_INVALID ||
178 buf_location >= IWL_FW_INI_LOCATION_NUM)
179 goto err;
180
181 if (alloc_id == IWL_FW_INI_ALLOCATION_INVALID ||
182 alloc_id >= IWL_FW_INI_ALLOCATION_NUM)
183 goto err;
184
185 if (buf_location == IWL_FW_INI_LOCATION_NPK_PATH &&
186 alloc_id != IWL_FW_INI_ALLOCATION_ID_DBGC1)
187 goto err;
188
189 if (buf_location == IWL_FW_INI_LOCATION_SRAM_PATH &&
190 alloc_id != IWL_FW_INI_ALLOCATION_ID_DBGC1 &&
191 alloc_id != IWL_FW_INI_ALLOCATION_ID_INTERNAL)
192 goto err;
193
194 trans->dbg.fw_mon_cfg[alloc_id] = *alloc;
195
196 return 0;
197 err:
198 IWL_ERR(trans,
199 "WRT: Invalid allocation id %u and/or location id %u for allocation TLV\n",
200 alloc_id, buf_location);
201 return -EINVAL;
202 }
203
iwl_dbg_tlv_alloc_hcmd(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv)204 static int iwl_dbg_tlv_alloc_hcmd(struct iwl_trans *trans,
205 struct iwl_ucode_tlv *tlv)
206 {
207 struct iwl_fw_ini_hcmd_tlv *hcmd = (void *)tlv->data;
208 u32 tp = le32_to_cpu(hcmd->time_point);
209
210 if (le32_to_cpu(tlv->length) <= sizeof(*hcmd))
211 return -EINVAL;
212
213 /* Host commands can not be sent in early time point since the FW
214 * is not ready
215 */
216 if (tp == IWL_FW_INI_TIME_POINT_INVALID ||
217 tp >= IWL_FW_INI_TIME_POINT_NUM ||
218 tp == IWL_FW_INI_TIME_POINT_EARLY) {
219 IWL_ERR(trans,
220 "WRT: Invalid time point %u for host command TLV\n",
221 tp);
222 return -EINVAL;
223 }
224
225 return iwl_dbg_tlv_add(tlv, &trans->dbg.time_point[tp].hcmd_list);
226 }
227
iwl_dbg_tlv_alloc_region(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv)228 static int iwl_dbg_tlv_alloc_region(struct iwl_trans *trans,
229 struct iwl_ucode_tlv *tlv)
230 {
231 struct iwl_fw_ini_region_tlv *reg = (void *)tlv->data;
232 struct iwl_ucode_tlv **active_reg;
233 u32 id = le32_to_cpu(reg->id);
234 u32 type = le32_to_cpu(reg->type);
235 u32 tlv_len = sizeof(*tlv) + le32_to_cpu(tlv->length);
236
237 if (le32_to_cpu(tlv->length) < sizeof(*reg))
238 return -EINVAL;
239
240 if (id >= IWL_FW_INI_MAX_REGION_ID) {
241 IWL_ERR(trans, "WRT: Invalid region id %u\n", id);
242 return -EINVAL;
243 }
244
245 if (type <= IWL_FW_INI_REGION_INVALID ||
246 type >= IWL_FW_INI_REGION_NUM) {
247 IWL_ERR(trans, "WRT: Invalid region type %u\n", type);
248 return -EINVAL;
249 }
250
251 if (type == IWL_FW_INI_REGION_PCI_IOSF_CONFIG &&
252 !trans->ops->read_config32) {
253 IWL_ERR(trans, "WRT: Unsupported region type %u\n", type);
254 return -EOPNOTSUPP;
255 }
256
257 active_reg = &trans->dbg.active_regions[id];
258 if (*active_reg) {
259 IWL_WARN(trans, "WRT: Overriding region id %u\n", id);
260
261 kfree(*active_reg);
262 }
263
264 *active_reg = kmemdup(tlv, tlv_len, GFP_KERNEL);
265 if (!*active_reg)
266 return -ENOMEM;
267
268 IWL_DEBUG_FW(trans, "WRT: Enabling region id %u type %u\n", id, type);
269
270 return 0;
271 }
272
iwl_dbg_tlv_alloc_trigger(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv)273 static int iwl_dbg_tlv_alloc_trigger(struct iwl_trans *trans,
274 struct iwl_ucode_tlv *tlv)
275 {
276 struct iwl_fw_ini_trigger_tlv *trig = (void *)tlv->data;
277 u32 tp = le32_to_cpu(trig->time_point);
278 struct iwl_ucode_tlv *dup = NULL;
279 int ret;
280
281 if (le32_to_cpu(tlv->length) < sizeof(*trig))
282 return -EINVAL;
283
284 if (tp <= IWL_FW_INI_TIME_POINT_INVALID ||
285 tp >= IWL_FW_INI_TIME_POINT_NUM) {
286 IWL_ERR(trans,
287 "WRT: Invalid time point %u for trigger TLV\n",
288 tp);
289 return -EINVAL;
290 }
291
292 if (!le32_to_cpu(trig->occurrences)) {
293 dup = kmemdup(tlv, sizeof(*tlv) + le32_to_cpu(tlv->length),
294 GFP_KERNEL);
295 if (!dup)
296 return -ENOMEM;
297 trig = (void *)dup->data;
298 trig->occurrences = cpu_to_le32(-1);
299 tlv = dup;
300 }
301
302 ret = iwl_dbg_tlv_add(tlv, &trans->dbg.time_point[tp].trig_list);
303 kfree(dup);
304
305 return ret;
306 }
307
308 static int (*dbg_tlv_alloc[])(struct iwl_trans *trans,
309 struct iwl_ucode_tlv *tlv) = {
310 [IWL_DBG_TLV_TYPE_DEBUG_INFO] = iwl_dbg_tlv_alloc_debug_info,
311 [IWL_DBG_TLV_TYPE_BUF_ALLOC] = iwl_dbg_tlv_alloc_buf_alloc,
312 [IWL_DBG_TLV_TYPE_HCMD] = iwl_dbg_tlv_alloc_hcmd,
313 [IWL_DBG_TLV_TYPE_REGION] = iwl_dbg_tlv_alloc_region,
314 [IWL_DBG_TLV_TYPE_TRIGGER] = iwl_dbg_tlv_alloc_trigger,
315 };
316
iwl_dbg_tlv_alloc(struct iwl_trans * trans,struct iwl_ucode_tlv * tlv,bool ext)317 void iwl_dbg_tlv_alloc(struct iwl_trans *trans, struct iwl_ucode_tlv *tlv,
318 bool ext)
319 {
320 struct iwl_fw_ini_header *hdr = (void *)&tlv->data[0];
321 u32 type = le32_to_cpu(tlv->type);
322 u32 tlv_idx = type - IWL_UCODE_TLV_DEBUG_BASE;
323 u32 domain = le32_to_cpu(hdr->domain);
324 enum iwl_ini_cfg_state *cfg_state = ext ?
325 &trans->dbg.external_ini_cfg : &trans->dbg.internal_ini_cfg;
326 int ret;
327
328 if (domain != IWL_FW_INI_DOMAIN_ALWAYS_ON &&
329 !(domain & trans->dbg.domains_bitmap)) {
330 IWL_DEBUG_FW(trans,
331 "WRT: Skipping TLV with disabled domain 0x%0x (0x%0x)\n",
332 domain, trans->dbg.domains_bitmap);
333 return;
334 }
335
336 if (tlv_idx >= ARRAY_SIZE(dbg_tlv_alloc) || !dbg_tlv_alloc[tlv_idx]) {
337 IWL_ERR(trans, "WRT: Unsupported TLV type 0x%x\n", type);
338 goto out_err;
339 }
340
341 if (!iwl_dbg_tlv_ver_support(tlv)) {
342 IWL_ERR(trans, "WRT: Unsupported TLV 0x%x version %u\n", type,
343 le32_to_cpu(hdr->version));
344 goto out_err;
345 }
346
347 ret = dbg_tlv_alloc[tlv_idx](trans, tlv);
348 if (ret) {
349 IWL_ERR(trans,
350 "WRT: Failed to allocate TLV 0x%x, ret %d, (ext=%d)\n",
351 type, ret, ext);
352 goto out_err;
353 }
354
355 if (*cfg_state == IWL_INI_CFG_STATE_NOT_LOADED)
356 *cfg_state = IWL_INI_CFG_STATE_LOADED;
357
358 return;
359
360 out_err:
361 *cfg_state = IWL_INI_CFG_STATE_CORRUPTED;
362 }
363
iwl_dbg_tlv_del_timers(struct iwl_trans * trans)364 void iwl_dbg_tlv_del_timers(struct iwl_trans *trans)
365 {
366 struct list_head *timer_list = &trans->dbg.periodic_trig_list;
367 struct iwl_dbg_tlv_timer_node *node, *tmp;
368
369 list_for_each_entry_safe(node, tmp, timer_list, list) {
370 del_timer(&node->timer);
371 list_del(&node->list);
372 kfree(node);
373 }
374 }
375 IWL_EXPORT_SYMBOL(iwl_dbg_tlv_del_timers);
376
iwl_dbg_tlv_fragments_free(struct iwl_trans * trans,enum iwl_fw_ini_allocation_id alloc_id)377 static void iwl_dbg_tlv_fragments_free(struct iwl_trans *trans,
378 enum iwl_fw_ini_allocation_id alloc_id)
379 {
380 struct iwl_fw_mon *fw_mon;
381 int i;
382
383 if (alloc_id <= IWL_FW_INI_ALLOCATION_INVALID ||
384 alloc_id >= IWL_FW_INI_ALLOCATION_NUM)
385 return;
386
387 fw_mon = &trans->dbg.fw_mon_ini[alloc_id];
388
389 for (i = 0; i < fw_mon->num_frags; i++) {
390 struct iwl_dram_data *frag = &fw_mon->frags[i];
391
392 dma_free_coherent(trans->dev, frag->size, frag->block,
393 frag->physical);
394
395 frag->physical = 0;
396 frag->block = NULL;
397 frag->size = 0;
398 }
399
400 kfree(fw_mon->frags);
401 fw_mon->frags = NULL;
402 fw_mon->num_frags = 0;
403 }
404
iwl_dbg_tlv_free(struct iwl_trans * trans)405 void iwl_dbg_tlv_free(struct iwl_trans *trans)
406 {
407 struct iwl_dbg_tlv_node *tlv_node, *tlv_node_tmp;
408 int i;
409
410 iwl_dbg_tlv_del_timers(trans);
411
412 for (i = 0; i < ARRAY_SIZE(trans->dbg.active_regions); i++) {
413 struct iwl_ucode_tlv **active_reg =
414 &trans->dbg.active_regions[i];
415
416 kfree(*active_reg);
417 *active_reg = NULL;
418 }
419
420 list_for_each_entry_safe(tlv_node, tlv_node_tmp,
421 &trans->dbg.debug_info_tlv_list, list) {
422 list_del(&tlv_node->list);
423 kfree(tlv_node);
424 }
425
426 for (i = 0; i < ARRAY_SIZE(trans->dbg.time_point); i++) {
427 struct iwl_dbg_tlv_time_point_data *tp =
428 &trans->dbg.time_point[i];
429
430 list_for_each_entry_safe(tlv_node, tlv_node_tmp, &tp->trig_list,
431 list) {
432 list_del(&tlv_node->list);
433 kfree(tlv_node);
434 }
435
436 list_for_each_entry_safe(tlv_node, tlv_node_tmp, &tp->hcmd_list,
437 list) {
438 list_del(&tlv_node->list);
439 kfree(tlv_node);
440 }
441
442 list_for_each_entry_safe(tlv_node, tlv_node_tmp,
443 &tp->active_trig_list, list) {
444 list_del(&tlv_node->list);
445 kfree(tlv_node);
446 }
447 }
448
449 for (i = 0; i < ARRAY_SIZE(trans->dbg.fw_mon_ini); i++)
450 iwl_dbg_tlv_fragments_free(trans, i);
451 }
452
iwl_dbg_tlv_parse_bin(struct iwl_trans * trans,const u8 * data,size_t len)453 static int iwl_dbg_tlv_parse_bin(struct iwl_trans *trans, const u8 *data,
454 size_t len)
455 {
456 struct iwl_ucode_tlv *tlv;
457 u32 tlv_len;
458
459 while (len >= sizeof(*tlv)) {
460 len -= sizeof(*tlv);
461 tlv = (void *)data;
462
463 tlv_len = le32_to_cpu(tlv->length);
464
465 if (len < tlv_len) {
466 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
467 len, tlv_len);
468 return -EINVAL;
469 }
470 len -= ALIGN(tlv_len, 4);
471 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
472
473 iwl_dbg_tlv_alloc(trans, tlv, true);
474 }
475
476 return 0;
477 }
478
iwl_dbg_tlv_load_bin(struct device * dev,struct iwl_trans * trans)479 void iwl_dbg_tlv_load_bin(struct device *dev, struct iwl_trans *trans)
480 {
481 const struct firmware *fw;
482 int res;
483
484 if (!iwlwifi_mod_params.enable_ini)
485 return;
486
487 res = firmware_request_nowarn(&fw, "iwl-debug-yoyo.bin", dev);
488 if (res)
489 return;
490
491 iwl_dbg_tlv_parse_bin(trans, fw->data, fw->size);
492
493 release_firmware(fw);
494 }
495
iwl_dbg_tlv_init(struct iwl_trans * trans)496 void iwl_dbg_tlv_init(struct iwl_trans *trans)
497 {
498 int i;
499
500 INIT_LIST_HEAD(&trans->dbg.debug_info_tlv_list);
501 INIT_LIST_HEAD(&trans->dbg.periodic_trig_list);
502
503 for (i = 0; i < ARRAY_SIZE(trans->dbg.time_point); i++) {
504 struct iwl_dbg_tlv_time_point_data *tp =
505 &trans->dbg.time_point[i];
506
507 INIT_LIST_HEAD(&tp->trig_list);
508 INIT_LIST_HEAD(&tp->hcmd_list);
509 INIT_LIST_HEAD(&tp->active_trig_list);
510 }
511 }
512
iwl_dbg_tlv_alloc_fragment(struct iwl_fw_runtime * fwrt,struct iwl_dram_data * frag,u32 pages)513 static int iwl_dbg_tlv_alloc_fragment(struct iwl_fw_runtime *fwrt,
514 struct iwl_dram_data *frag, u32 pages)
515 {
516 void *block = NULL;
517 dma_addr_t physical;
518
519 if (!frag || frag->size || !pages)
520 return -EIO;
521
522 /*
523 * We try to allocate as many pages as we can, starting with
524 * the requested amount and going down until we can allocate
525 * something. Because of DIV_ROUND_UP(), pages will never go
526 * down to 0 and stop the loop, so stop when pages reaches 1,
527 * which is too small anyway.
528 */
529 while (pages > 1) {
530 block = dma_alloc_coherent(fwrt->dev, pages * PAGE_SIZE,
531 &physical,
532 GFP_KERNEL | __GFP_NOWARN);
533 if (block)
534 break;
535
536 IWL_WARN(fwrt, "WRT: Failed to allocate fragment size %lu\n",
537 pages * PAGE_SIZE);
538
539 pages = DIV_ROUND_UP(pages, 2);
540 }
541
542 if (!block)
543 return -ENOMEM;
544
545 frag->physical = physical;
546 frag->block = block;
547 frag->size = pages * PAGE_SIZE;
548
549 return pages;
550 }
551
iwl_dbg_tlv_alloc_fragments(struct iwl_fw_runtime * fwrt,enum iwl_fw_ini_allocation_id alloc_id)552 static int iwl_dbg_tlv_alloc_fragments(struct iwl_fw_runtime *fwrt,
553 enum iwl_fw_ini_allocation_id alloc_id)
554 {
555 struct iwl_fw_mon *fw_mon;
556 struct iwl_fw_ini_allocation_tlv *fw_mon_cfg;
557 u32 num_frags, remain_pages, frag_pages;
558 int i;
559
560 if (alloc_id < IWL_FW_INI_ALLOCATION_INVALID ||
561 alloc_id >= IWL_FW_INI_ALLOCATION_NUM)
562 return -EIO;
563
564 fw_mon_cfg = &fwrt->trans->dbg.fw_mon_cfg[alloc_id];
565 fw_mon = &fwrt->trans->dbg.fw_mon_ini[alloc_id];
566
567 if (fw_mon->num_frags ||
568 fw_mon_cfg->buf_location !=
569 cpu_to_le32(IWL_FW_INI_LOCATION_DRAM_PATH))
570 return 0;
571
572 num_frags = le32_to_cpu(fw_mon_cfg->max_frags_num);
573 if (!fw_has_capa(&fwrt->fw->ucode_capa,
574 IWL_UCODE_TLV_CAPA_DBG_BUF_ALLOC_CMD_SUPP)) {
575 if (alloc_id != IWL_FW_INI_ALLOCATION_ID_DBGC1)
576 return -EIO;
577 num_frags = 1;
578 }
579
580 remain_pages = DIV_ROUND_UP(le32_to_cpu(fw_mon_cfg->req_size),
581 PAGE_SIZE);
582 num_frags = min_t(u32, num_frags, BUF_ALLOC_MAX_NUM_FRAGS);
583 num_frags = min_t(u32, num_frags, remain_pages);
584 frag_pages = DIV_ROUND_UP(remain_pages, num_frags);
585
586 fw_mon->frags = kcalloc(num_frags, sizeof(*fw_mon->frags), GFP_KERNEL);
587 if (!fw_mon->frags)
588 return -ENOMEM;
589
590 for (i = 0; i < num_frags; i++) {
591 int pages = min_t(u32, frag_pages, remain_pages);
592
593 IWL_DEBUG_FW(fwrt,
594 "WRT: Allocating DRAM buffer (alloc_id=%u, fragment=%u, size=0x%lx)\n",
595 alloc_id, i, pages * PAGE_SIZE);
596
597 pages = iwl_dbg_tlv_alloc_fragment(fwrt, &fw_mon->frags[i],
598 pages);
599 if (pages < 0) {
600 u32 alloc_size = le32_to_cpu(fw_mon_cfg->req_size) -
601 (remain_pages * PAGE_SIZE);
602
603 if (alloc_size < le32_to_cpu(fw_mon_cfg->min_size)) {
604 iwl_dbg_tlv_fragments_free(fwrt->trans,
605 alloc_id);
606 return pages;
607 }
608 break;
609 }
610
611 remain_pages -= pages;
612 fw_mon->num_frags++;
613 }
614
615 return 0;
616 }
617
iwl_dbg_tlv_apply_buffer(struct iwl_fw_runtime * fwrt,enum iwl_fw_ini_allocation_id alloc_id)618 static int iwl_dbg_tlv_apply_buffer(struct iwl_fw_runtime *fwrt,
619 enum iwl_fw_ini_allocation_id alloc_id)
620 {
621 struct iwl_fw_mon *fw_mon;
622 u32 remain_frags, num_commands;
623 int i, fw_mon_idx = 0;
624
625 if (!fw_has_capa(&fwrt->fw->ucode_capa,
626 IWL_UCODE_TLV_CAPA_DBG_BUF_ALLOC_CMD_SUPP))
627 return 0;
628
629 if (alloc_id < IWL_FW_INI_ALLOCATION_INVALID ||
630 alloc_id >= IWL_FW_INI_ALLOCATION_NUM)
631 return -EIO;
632
633 if (le32_to_cpu(fwrt->trans->dbg.fw_mon_cfg[alloc_id].buf_location) !=
634 IWL_FW_INI_LOCATION_DRAM_PATH)
635 return 0;
636
637 fw_mon = &fwrt->trans->dbg.fw_mon_ini[alloc_id];
638
639 /* the first fragment of DBGC1 is given to the FW via register
640 * or context info
641 */
642 if (alloc_id == IWL_FW_INI_ALLOCATION_ID_DBGC1)
643 fw_mon_idx++;
644
645 remain_frags = fw_mon->num_frags - fw_mon_idx;
646 if (!remain_frags)
647 return 0;
648
649 num_commands = DIV_ROUND_UP(remain_frags, BUF_ALLOC_MAX_NUM_FRAGS);
650
651 IWL_DEBUG_FW(fwrt, "WRT: Applying DRAM destination (alloc_id=%u)\n",
652 alloc_id);
653
654 for (i = 0; i < num_commands; i++) {
655 u32 num_frags = min_t(u32, remain_frags,
656 BUF_ALLOC_MAX_NUM_FRAGS);
657 struct iwl_buf_alloc_cmd data = {
658 .alloc_id = cpu_to_le32(alloc_id),
659 .num_frags = cpu_to_le32(num_frags),
660 .buf_location =
661 cpu_to_le32(IWL_FW_INI_LOCATION_DRAM_PATH),
662 };
663 struct iwl_host_cmd hcmd = {
664 .id = WIDE_ID(DEBUG_GROUP, BUFFER_ALLOCATION),
665 .data[0] = &data,
666 .len[0] = sizeof(data),
667 };
668 int ret, j;
669
670 for (j = 0; j < num_frags; j++) {
671 struct iwl_buf_alloc_frag *frag = &data.frags[j];
672 struct iwl_dram_data *fw_mon_frag =
673 &fw_mon->frags[fw_mon_idx++];
674
675 frag->addr = cpu_to_le64(fw_mon_frag->physical);
676 frag->size = cpu_to_le32(fw_mon_frag->size);
677 }
678 ret = iwl_trans_send_cmd(fwrt->trans, &hcmd);
679 if (ret)
680 return ret;
681
682 remain_frags -= num_frags;
683 }
684
685 return 0;
686 }
687
iwl_dbg_tlv_apply_buffers(struct iwl_fw_runtime * fwrt)688 static void iwl_dbg_tlv_apply_buffers(struct iwl_fw_runtime *fwrt)
689 {
690 int ret, i;
691
692 for (i = 0; i < IWL_FW_INI_ALLOCATION_NUM; i++) {
693 ret = iwl_dbg_tlv_apply_buffer(fwrt, i);
694 if (ret)
695 IWL_WARN(fwrt,
696 "WRT: Failed to apply DRAM buffer for allocation id %d, ret=%d\n",
697 i, ret);
698 }
699 }
700
iwl_dbg_tlv_send_hcmds(struct iwl_fw_runtime * fwrt,struct list_head * hcmd_list)701 static void iwl_dbg_tlv_send_hcmds(struct iwl_fw_runtime *fwrt,
702 struct list_head *hcmd_list)
703 {
704 struct iwl_dbg_tlv_node *node;
705
706 list_for_each_entry(node, hcmd_list, list) {
707 struct iwl_fw_ini_hcmd_tlv *hcmd = (void *)node->tlv.data;
708 struct iwl_fw_ini_hcmd *hcmd_data = &hcmd->hcmd;
709 u16 hcmd_len = le32_to_cpu(node->tlv.length) - sizeof(*hcmd);
710 struct iwl_host_cmd cmd = {
711 .id = WIDE_ID(hcmd_data->group, hcmd_data->id),
712 .len = { hcmd_len, },
713 .data = { hcmd_data->data, },
714 };
715
716 iwl_trans_send_cmd(fwrt->trans, &cmd);
717 }
718 }
719
iwl_dbg_tlv_periodic_trig_handler(struct timer_list * t)720 static void iwl_dbg_tlv_periodic_trig_handler(struct timer_list *t)
721 {
722 struct iwl_dbg_tlv_timer_node *timer_node =
723 from_timer(timer_node, t, timer);
724 struct iwl_fwrt_dump_data dump_data = {
725 .trig = (void *)timer_node->tlv->data,
726 };
727 int ret;
728
729 ret = iwl_fw_dbg_ini_collect(timer_node->fwrt, &dump_data);
730 if (!ret || ret == -EBUSY) {
731 u32 occur = le32_to_cpu(dump_data.trig->occurrences);
732 u32 collect_interval = le32_to_cpu(dump_data.trig->data[0]);
733
734 if (!occur)
735 return;
736
737 mod_timer(t, jiffies + msecs_to_jiffies(collect_interval));
738 }
739 }
740
iwl_dbg_tlv_set_periodic_trigs(struct iwl_fw_runtime * fwrt)741 static void iwl_dbg_tlv_set_periodic_trigs(struct iwl_fw_runtime *fwrt)
742 {
743 struct iwl_dbg_tlv_node *node;
744 struct list_head *trig_list =
745 &fwrt->trans->dbg.time_point[IWL_FW_INI_TIME_POINT_PERIODIC].active_trig_list;
746
747 list_for_each_entry(node, trig_list, list) {
748 struct iwl_fw_ini_trigger_tlv *trig = (void *)node->tlv.data;
749 struct iwl_dbg_tlv_timer_node *timer_node;
750 u32 occur = le32_to_cpu(trig->occurrences), collect_interval;
751 u32 min_interval = 100;
752
753 if (!occur)
754 continue;
755
756 /* make sure there is at least one dword of data for the
757 * interval value
758 */
759 if (le32_to_cpu(node->tlv.length) <
760 sizeof(*trig) + sizeof(__le32)) {
761 IWL_ERR(fwrt,
762 "WRT: Invalid periodic trigger data was not given\n");
763 continue;
764 }
765
766 if (le32_to_cpu(trig->data[0]) < min_interval) {
767 IWL_WARN(fwrt,
768 "WRT: Override min interval from %u to %u msec\n",
769 le32_to_cpu(trig->data[0]), min_interval);
770 trig->data[0] = cpu_to_le32(min_interval);
771 }
772
773 collect_interval = le32_to_cpu(trig->data[0]);
774
775 timer_node = kzalloc(sizeof(*timer_node), GFP_KERNEL);
776 if (!timer_node) {
777 IWL_ERR(fwrt,
778 "WRT: Failed to allocate periodic trigger\n");
779 continue;
780 }
781
782 timer_node->fwrt = fwrt;
783 timer_node->tlv = &node->tlv;
784 timer_setup(&timer_node->timer,
785 iwl_dbg_tlv_periodic_trig_handler, 0);
786
787 list_add_tail(&timer_node->list,
788 &fwrt->trans->dbg.periodic_trig_list);
789
790 IWL_DEBUG_FW(fwrt, "WRT: Enabling periodic trigger\n");
791
792 mod_timer(&timer_node->timer,
793 jiffies + msecs_to_jiffies(collect_interval));
794 }
795 }
796
is_trig_data_contained(struct iwl_ucode_tlv * new,struct iwl_ucode_tlv * old)797 static bool is_trig_data_contained(struct iwl_ucode_tlv *new,
798 struct iwl_ucode_tlv *old)
799 {
800 struct iwl_fw_ini_trigger_tlv *new_trig = (void *)new->data;
801 struct iwl_fw_ini_trigger_tlv *old_trig = (void *)old->data;
802 __le32 *new_data = new_trig->data, *old_data = old_trig->data;
803 u32 new_dwords_num = iwl_tlv_array_len(new, new_trig, data);
804 u32 old_dwords_num = iwl_tlv_array_len(old, old_trig, data);
805 int i, j;
806
807 for (i = 0; i < new_dwords_num; i++) {
808 bool match = false;
809
810 for (j = 0; j < old_dwords_num; j++) {
811 if (new_data[i] == old_data[j]) {
812 match = true;
813 break;
814 }
815 }
816 if (!match)
817 return false;
818 }
819
820 return true;
821 }
822
iwl_dbg_tlv_override_trig_node(struct iwl_fw_runtime * fwrt,struct iwl_ucode_tlv * trig_tlv,struct iwl_dbg_tlv_node * node)823 static int iwl_dbg_tlv_override_trig_node(struct iwl_fw_runtime *fwrt,
824 struct iwl_ucode_tlv *trig_tlv,
825 struct iwl_dbg_tlv_node *node)
826 {
827 struct iwl_ucode_tlv *node_tlv = &node->tlv;
828 struct iwl_fw_ini_trigger_tlv *node_trig = (void *)node_tlv->data;
829 struct iwl_fw_ini_trigger_tlv *trig = (void *)trig_tlv->data;
830 u32 policy = le32_to_cpu(trig->apply_policy);
831 u32 size = le32_to_cpu(trig_tlv->length);
832 u32 trig_data_len = size - sizeof(*trig);
833 u32 offset = 0;
834
835 if (!(policy & IWL_FW_INI_APPLY_POLICY_OVERRIDE_DATA)) {
836 u32 data_len = le32_to_cpu(node_tlv->length) -
837 sizeof(*node_trig);
838
839 IWL_DEBUG_FW(fwrt,
840 "WRT: Appending trigger data (time point %u)\n",
841 le32_to_cpu(trig->time_point));
842
843 offset += data_len;
844 size += data_len;
845 } else {
846 IWL_DEBUG_FW(fwrt,
847 "WRT: Overriding trigger data (time point %u)\n",
848 le32_to_cpu(trig->time_point));
849 }
850
851 if (size != le32_to_cpu(node_tlv->length)) {
852 struct list_head *prev = node->list.prev;
853 struct iwl_dbg_tlv_node *tmp;
854
855 list_del(&node->list);
856
857 tmp = krealloc(node, sizeof(*node) + size, GFP_KERNEL);
858 if (!tmp) {
859 IWL_WARN(fwrt,
860 "WRT: No memory to override trigger (time point %u)\n",
861 le32_to_cpu(trig->time_point));
862
863 list_add(&node->list, prev);
864
865 return -ENOMEM;
866 }
867
868 list_add(&tmp->list, prev);
869 node_tlv = &tmp->tlv;
870 node_trig = (void *)node_tlv->data;
871 }
872
873 memcpy(node_trig->data + offset, trig->data, trig_data_len);
874 node_tlv->length = cpu_to_le32(size);
875
876 if (policy & IWL_FW_INI_APPLY_POLICY_OVERRIDE_CFG) {
877 IWL_DEBUG_FW(fwrt,
878 "WRT: Overriding trigger configuration (time point %u)\n",
879 le32_to_cpu(trig->time_point));
880
881 /* the first 11 dwords are configuration related */
882 memcpy(node_trig, trig, sizeof(__le32) * 11);
883 }
884
885 if (policy & IWL_FW_INI_APPLY_POLICY_OVERRIDE_REGIONS) {
886 IWL_DEBUG_FW(fwrt,
887 "WRT: Overriding trigger regions (time point %u)\n",
888 le32_to_cpu(trig->time_point));
889
890 node_trig->regions_mask = trig->regions_mask;
891 } else {
892 IWL_DEBUG_FW(fwrt,
893 "WRT: Appending trigger regions (time point %u)\n",
894 le32_to_cpu(trig->time_point));
895
896 node_trig->regions_mask |= trig->regions_mask;
897 }
898
899 return 0;
900 }
901
902 static int
iwl_dbg_tlv_add_active_trigger(struct iwl_fw_runtime * fwrt,struct list_head * trig_list,struct iwl_ucode_tlv * trig_tlv)903 iwl_dbg_tlv_add_active_trigger(struct iwl_fw_runtime *fwrt,
904 struct list_head *trig_list,
905 struct iwl_ucode_tlv *trig_tlv)
906 {
907 struct iwl_fw_ini_trigger_tlv *trig = (void *)trig_tlv->data;
908 struct iwl_dbg_tlv_node *node, *match = NULL;
909 u32 policy = le32_to_cpu(trig->apply_policy);
910
911 list_for_each_entry(node, trig_list, list) {
912 if (!(policy & IWL_FW_INI_APPLY_POLICY_MATCH_TIME_POINT))
913 break;
914
915 if (!(policy & IWL_FW_INI_APPLY_POLICY_MATCH_DATA) ||
916 is_trig_data_contained(trig_tlv, &node->tlv)) {
917 match = node;
918 break;
919 }
920 }
921
922 if (!match) {
923 IWL_DEBUG_FW(fwrt, "WRT: Enabling trigger (time point %u)\n",
924 le32_to_cpu(trig->time_point));
925 return iwl_dbg_tlv_add(trig_tlv, trig_list);
926 }
927
928 return iwl_dbg_tlv_override_trig_node(fwrt, trig_tlv, match);
929 }
930
931 static void
iwl_dbg_tlv_gen_active_trig_list(struct iwl_fw_runtime * fwrt,struct iwl_dbg_tlv_time_point_data * tp)932 iwl_dbg_tlv_gen_active_trig_list(struct iwl_fw_runtime *fwrt,
933 struct iwl_dbg_tlv_time_point_data *tp)
934 {
935 struct iwl_dbg_tlv_node *node;
936 struct list_head *trig_list = &tp->trig_list;
937 struct list_head *active_trig_list = &tp->active_trig_list;
938
939 list_for_each_entry(node, trig_list, list) {
940 struct iwl_ucode_tlv *tlv = &node->tlv;
941
942 iwl_dbg_tlv_add_active_trigger(fwrt, active_trig_list, tlv);
943 }
944 }
945
iwl_dbg_tlv_check_fw_pkt(struct iwl_fw_runtime * fwrt,struct iwl_fwrt_dump_data * dump_data,union iwl_dbg_tlv_tp_data * tp_data,u32 trig_data)946 static bool iwl_dbg_tlv_check_fw_pkt(struct iwl_fw_runtime *fwrt,
947 struct iwl_fwrt_dump_data *dump_data,
948 union iwl_dbg_tlv_tp_data *tp_data,
949 u32 trig_data)
950 {
951 struct iwl_rx_packet *pkt = tp_data->fw_pkt;
952 struct iwl_cmd_header *wanted_hdr = (void *)&trig_data;
953
954 if (pkt && (pkt->hdr.cmd == wanted_hdr->cmd &&
955 pkt->hdr.group_id == wanted_hdr->group_id)) {
956 struct iwl_rx_packet *fw_pkt =
957 kmemdup(pkt,
958 sizeof(*pkt) + iwl_rx_packet_payload_len(pkt),
959 GFP_ATOMIC);
960
961 if (!fw_pkt)
962 return false;
963
964 dump_data->fw_pkt = fw_pkt;
965
966 return true;
967 }
968
969 return false;
970 }
971
972 static int
iwl_dbg_tlv_tp_trigger(struct iwl_fw_runtime * fwrt,struct list_head * active_trig_list,union iwl_dbg_tlv_tp_data * tp_data,bool (* data_check)(struct iwl_fw_runtime * fwrt,struct iwl_fwrt_dump_data * dump_data,union iwl_dbg_tlv_tp_data * tp_data,u32 trig_data))973 iwl_dbg_tlv_tp_trigger(struct iwl_fw_runtime *fwrt,
974 struct list_head *active_trig_list,
975 union iwl_dbg_tlv_tp_data *tp_data,
976 bool (*data_check)(struct iwl_fw_runtime *fwrt,
977 struct iwl_fwrt_dump_data *dump_data,
978 union iwl_dbg_tlv_tp_data *tp_data,
979 u32 trig_data))
980 {
981 struct iwl_dbg_tlv_node *node;
982
983 list_for_each_entry(node, active_trig_list, list) {
984 struct iwl_fwrt_dump_data dump_data = {
985 .trig = (void *)node->tlv.data,
986 };
987 u32 num_data = iwl_tlv_array_len(&node->tlv, dump_data.trig,
988 data);
989 int ret, i;
990
991 if (!num_data) {
992 ret = iwl_fw_dbg_ini_collect(fwrt, &dump_data);
993 if (ret)
994 return ret;
995 }
996
997 for (i = 0; i < num_data; i++) {
998 if (!data_check ||
999 data_check(fwrt, &dump_data, tp_data,
1000 le32_to_cpu(dump_data.trig->data[i]))) {
1001 ret = iwl_fw_dbg_ini_collect(fwrt, &dump_data);
1002 if (ret)
1003 return ret;
1004
1005 break;
1006 }
1007 }
1008 }
1009
1010 return 0;
1011 }
1012
iwl_dbg_tlv_init_cfg(struct iwl_fw_runtime * fwrt)1013 static void iwl_dbg_tlv_init_cfg(struct iwl_fw_runtime *fwrt)
1014 {
1015 enum iwl_fw_ini_buffer_location *ini_dest = &fwrt->trans->dbg.ini_dest;
1016 int ret, i;
1017
1018 if (*ini_dest != IWL_FW_INI_LOCATION_INVALID)
1019 return;
1020
1021 IWL_DEBUG_FW(fwrt,
1022 "WRT: Generating active triggers list, domain 0x%x\n",
1023 fwrt->trans->dbg.domains_bitmap);
1024
1025 for (i = 0; i < ARRAY_SIZE(fwrt->trans->dbg.time_point); i++) {
1026 struct iwl_dbg_tlv_time_point_data *tp =
1027 &fwrt->trans->dbg.time_point[i];
1028
1029 iwl_dbg_tlv_gen_active_trig_list(fwrt, tp);
1030 }
1031
1032 *ini_dest = IWL_FW_INI_LOCATION_INVALID;
1033 for (i = 0; i < IWL_FW_INI_ALLOCATION_NUM; i++) {
1034 struct iwl_fw_ini_allocation_tlv *fw_mon_cfg =
1035 &fwrt->trans->dbg.fw_mon_cfg[i];
1036 u32 dest = le32_to_cpu(fw_mon_cfg->buf_location);
1037
1038 if (dest == IWL_FW_INI_LOCATION_INVALID)
1039 continue;
1040
1041 if (*ini_dest == IWL_FW_INI_LOCATION_INVALID)
1042 *ini_dest = dest;
1043
1044 if (dest != *ini_dest)
1045 continue;
1046
1047 ret = iwl_dbg_tlv_alloc_fragments(fwrt, i);
1048 if (ret)
1049 IWL_WARN(fwrt,
1050 "WRT: Failed to allocate DRAM buffer for allocation id %d, ret=%d\n",
1051 i, ret);
1052 }
1053 }
1054
iwl_dbg_tlv_time_point(struct iwl_fw_runtime * fwrt,enum iwl_fw_ini_time_point tp_id,union iwl_dbg_tlv_tp_data * tp_data)1055 void iwl_dbg_tlv_time_point(struct iwl_fw_runtime *fwrt,
1056 enum iwl_fw_ini_time_point tp_id,
1057 union iwl_dbg_tlv_tp_data *tp_data)
1058 {
1059 struct list_head *hcmd_list, *trig_list;
1060
1061 if (!iwl_trans_dbg_ini_valid(fwrt->trans) ||
1062 tp_id == IWL_FW_INI_TIME_POINT_INVALID ||
1063 tp_id >= IWL_FW_INI_TIME_POINT_NUM)
1064 return;
1065
1066 hcmd_list = &fwrt->trans->dbg.time_point[tp_id].hcmd_list;
1067 trig_list = &fwrt->trans->dbg.time_point[tp_id].active_trig_list;
1068
1069 switch (tp_id) {
1070 case IWL_FW_INI_TIME_POINT_EARLY:
1071 iwl_dbg_tlv_init_cfg(fwrt);
1072 iwl_dbg_tlv_tp_trigger(fwrt, trig_list, tp_data, NULL);
1073 break;
1074 case IWL_FW_INI_TIME_POINT_AFTER_ALIVE:
1075 iwl_dbg_tlv_apply_buffers(fwrt);
1076 iwl_dbg_tlv_send_hcmds(fwrt, hcmd_list);
1077 iwl_dbg_tlv_tp_trigger(fwrt, trig_list, tp_data, NULL);
1078 break;
1079 case IWL_FW_INI_TIME_POINT_PERIODIC:
1080 iwl_dbg_tlv_set_periodic_trigs(fwrt);
1081 iwl_dbg_tlv_send_hcmds(fwrt, hcmd_list);
1082 break;
1083 case IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF:
1084 case IWL_FW_INI_TIME_POINT_MISSED_BEACONS:
1085 case IWL_FW_INI_TIME_POINT_FW_DHC_NOTIFICATION:
1086 iwl_dbg_tlv_send_hcmds(fwrt, hcmd_list);
1087 iwl_dbg_tlv_tp_trigger(fwrt, trig_list, tp_data,
1088 iwl_dbg_tlv_check_fw_pkt);
1089 break;
1090 default:
1091 iwl_dbg_tlv_send_hcmds(fwrt, hcmd_list);
1092 iwl_dbg_tlv_tp_trigger(fwrt, trig_list, tp_data, NULL);
1093 break;
1094 }
1095 }
1096 IWL_EXPORT_SYMBOL(iwl_dbg_tlv_time_point);
1097