1 /* Broadcom NetXtreme-C/E network driver.
2 *
3 * Copyright (c) 2021 Broadcom Limited
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/pci.h>
13 #include "bnxt_hsi.h"
14 #include "bnxt.h"
15 #include "bnxt_hwrm.h"
16 #include "bnxt_coredump.h"
17
18 static const u16 bnxt_bstore_to_seg_id[] = {
19 [BNXT_CTX_QP] = BNXT_CTX_MEM_SEG_QP,
20 [BNXT_CTX_SRQ] = BNXT_CTX_MEM_SEG_SRQ,
21 [BNXT_CTX_CQ] = BNXT_CTX_MEM_SEG_CQ,
22 [BNXT_CTX_VNIC] = BNXT_CTX_MEM_SEG_VNIC,
23 [BNXT_CTX_STAT] = BNXT_CTX_MEM_SEG_STAT,
24 [BNXT_CTX_STQM] = BNXT_CTX_MEM_SEG_STQM,
25 [BNXT_CTX_FTQM] = BNXT_CTX_MEM_SEG_FTQM,
26 [BNXT_CTX_MRAV] = BNXT_CTX_MEM_SEG_MRAV,
27 [BNXT_CTX_TIM] = BNXT_CTX_MEM_SEG_TIM,
28 [BNXT_CTX_SRT] = BNXT_CTX_MEM_SEG_SRT,
29 [BNXT_CTX_SRT2] = BNXT_CTX_MEM_SEG_SRT2,
30 [BNXT_CTX_CRT] = BNXT_CTX_MEM_SEG_CRT,
31 [BNXT_CTX_CRT2] = BNXT_CTX_MEM_SEG_CRT2,
32 [BNXT_CTX_RIGP0] = BNXT_CTX_MEM_SEG_RIGP0,
33 [BNXT_CTX_L2HWRM] = BNXT_CTX_MEM_SEG_L2HWRM,
34 [BNXT_CTX_REHWRM] = BNXT_CTX_MEM_SEG_REHWRM,
35 [BNXT_CTX_CA0] = BNXT_CTX_MEM_SEG_CA0,
36 [BNXT_CTX_CA1] = BNXT_CTX_MEM_SEG_CA1,
37 [BNXT_CTX_CA2] = BNXT_CTX_MEM_SEG_CA2,
38 [BNXT_CTX_RIGP1] = BNXT_CTX_MEM_SEG_RIGP1,
39 };
40
bnxt_dbg_hwrm_log_buffer_flush(struct bnxt * bp,u16 type,u32 flags,u32 * offset)41 static int bnxt_dbg_hwrm_log_buffer_flush(struct bnxt *bp, u16 type, u32 flags,
42 u32 *offset)
43 {
44 struct hwrm_dbg_log_buffer_flush_output *resp;
45 struct hwrm_dbg_log_buffer_flush_input *req;
46 int rc;
47
48 rc = hwrm_req_init(bp, req, HWRM_DBG_LOG_BUFFER_FLUSH);
49 if (rc)
50 return rc;
51
52 req->flags = cpu_to_le32(flags);
53 req->type = cpu_to_le16(type);
54 resp = hwrm_req_hold(bp, req);
55 rc = hwrm_req_send(bp, req);
56 if (!rc)
57 *offset = le32_to_cpu(resp->current_buffer_offset);
58 hwrm_req_drop(bp, req);
59 return rc;
60 }
61
bnxt_hwrm_dbg_dma_data(struct bnxt * bp,void * msg,struct bnxt_hwrm_dbg_dma_info * info)62 static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg,
63 struct bnxt_hwrm_dbg_dma_info *info)
64 {
65 struct hwrm_dbg_cmn_input *cmn_req = msg;
66 __le16 *seq_ptr = msg + info->seq_off;
67 struct hwrm_dbg_cmn_output *cmn_resp;
68 u16 seq = 0, len, segs_off;
69 dma_addr_t dma_handle;
70 void *dma_buf, *resp;
71 int rc, off = 0;
72
73 dma_buf = hwrm_req_dma_slice(bp, msg, info->dma_len, &dma_handle);
74 if (!dma_buf) {
75 hwrm_req_drop(bp, msg);
76 return -ENOMEM;
77 }
78
79 hwrm_req_timeout(bp, msg, bp->hwrm_cmd_max_timeout);
80 cmn_resp = hwrm_req_hold(bp, msg);
81 resp = cmn_resp;
82
83 segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
84 total_segments);
85 cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
86 cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
87 while (1) {
88 *seq_ptr = cpu_to_le16(seq);
89 rc = hwrm_req_send(bp, msg);
90 if (rc)
91 break;
92
93 len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
94 if (!seq &&
95 cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
96 info->segs = le16_to_cpu(*((__le16 *)(resp +
97 segs_off)));
98 if (!info->segs) {
99 rc = -EIO;
100 break;
101 }
102
103 info->dest_buf_size = info->segs *
104 sizeof(struct coredump_segment_record);
105 info->dest_buf = kmalloc(info->dest_buf_size,
106 GFP_KERNEL);
107 if (!info->dest_buf) {
108 rc = -ENOMEM;
109 break;
110 }
111 }
112
113 if (cmn_req->req_type ==
114 cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
115 info->dest_buf_size += len;
116
117 if (info->dest_buf) {
118 if ((info->seg_start + off + len) <=
119 BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
120 u16 copylen = min_t(u16, len,
121 info->dest_buf_size - off);
122
123 memcpy(info->dest_buf + off, dma_buf, copylen);
124 if (copylen < len)
125 break;
126 } else {
127 rc = -ENOBUFS;
128 if (cmn_req->req_type ==
129 cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
130 kfree(info->dest_buf);
131 info->dest_buf = NULL;
132 }
133 break;
134 }
135 }
136
137 if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
138 break;
139
140 seq++;
141 off += len;
142 }
143 hwrm_req_drop(bp, msg);
144 return rc;
145 }
146
bnxt_hwrm_dbg_coredump_list(struct bnxt * bp,struct bnxt_coredump * coredump)147 static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
148 struct bnxt_coredump *coredump)
149 {
150 struct bnxt_hwrm_dbg_dma_info info = {NULL};
151 struct hwrm_dbg_coredump_list_input *req;
152 int rc;
153
154 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_LIST);
155 if (rc)
156 return rc;
157
158 info.dma_len = COREDUMP_LIST_BUF_LEN;
159 info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
160 info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
161 data_len);
162
163 rc = bnxt_hwrm_dbg_dma_data(bp, req, &info);
164 if (!rc) {
165 coredump->data = info.dest_buf;
166 coredump->data_size = info.dest_buf_size;
167 coredump->total_segs = info.segs;
168 }
169 return rc;
170 }
171
bnxt_hwrm_dbg_coredump_initiate(struct bnxt * bp,u16 dump_type,u16 component_id,u16 segment_id)172 static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 dump_type,
173 u16 component_id, u16 segment_id)
174 {
175 struct hwrm_dbg_coredump_initiate_input *req;
176 int rc;
177
178 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_INITIATE);
179 if (rc)
180 return rc;
181
182 hwrm_req_timeout(bp, req, bp->hwrm_cmd_max_timeout);
183 req->component_id = cpu_to_le16(component_id);
184 req->segment_id = cpu_to_le16(segment_id);
185 if (dump_type == BNXT_DUMP_LIVE_WITH_CTX_L1_CACHE)
186 req->seg_flags = DBG_COREDUMP_INITIATE_REQ_SEG_FLAGS_COLLECT_CTX_L1_CACHE;
187
188 return hwrm_req_send(bp, req);
189 }
190
bnxt_hwrm_dbg_coredump_retrieve(struct bnxt * bp,u16 component_id,u16 segment_id,u32 * seg_len,void * buf,u32 buf_len,u32 offset)191 static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
192 u16 segment_id, u32 *seg_len,
193 void *buf, u32 buf_len, u32 offset)
194 {
195 struct hwrm_dbg_coredump_retrieve_input *req;
196 struct bnxt_hwrm_dbg_dma_info info = {NULL};
197 int rc;
198
199 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_RETRIEVE);
200 if (rc)
201 return rc;
202
203 req->component_id = cpu_to_le16(component_id);
204 req->segment_id = cpu_to_le16(segment_id);
205
206 info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
207 info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
208 seq_no);
209 info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
210 data_len);
211 if (buf) {
212 info.dest_buf = buf + offset;
213 info.buf_len = buf_len;
214 info.seg_start = offset;
215 }
216
217 rc = bnxt_hwrm_dbg_dma_data(bp, req, &info);
218 if (!rc)
219 *seg_len = info.dest_buf_size;
220
221 return rc;
222 }
223
224 void
bnxt_fill_coredump_seg_hdr(struct bnxt * bp,struct bnxt_coredump_segment_hdr * seg_hdr,struct coredump_segment_record * seg_rec,u32 seg_len,int status,u32 duration,u32 instance,u32 comp_id,u32 seg_id)225 bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
226 struct bnxt_coredump_segment_hdr *seg_hdr,
227 struct coredump_segment_record *seg_rec, u32 seg_len,
228 int status, u32 duration, u32 instance, u32 comp_id,
229 u32 seg_id)
230 {
231 memset(seg_hdr, 0, sizeof(*seg_hdr));
232 memcpy(seg_hdr->signature, "sEgM", 4);
233 if (seg_rec) {
234 seg_hdr->component_id = (__force __le32)seg_rec->component_id;
235 seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
236 seg_hdr->low_version = seg_rec->version_low;
237 seg_hdr->high_version = seg_rec->version_hi;
238 seg_hdr->flags = cpu_to_le32(seg_rec->compress_flags);
239 } else {
240 seg_hdr->component_id = cpu_to_le32(comp_id);
241 seg_hdr->segment_id = cpu_to_le32(seg_id);
242 }
243 seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
244 seg_hdr->length = cpu_to_le32(seg_len);
245 seg_hdr->status = cpu_to_le32(status);
246 seg_hdr->duration = cpu_to_le32(duration);
247 seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
248 seg_hdr->instance = cpu_to_le32(instance);
249 }
250
bnxt_fill_cmdline(struct bnxt_coredump_record * record)251 static void bnxt_fill_cmdline(struct bnxt_coredump_record *record)
252 {
253 struct mm_struct *mm = current->mm;
254 int i, len, last = 0;
255
256 if (mm) {
257 len = min_t(int, mm->arg_end - mm->arg_start,
258 sizeof(record->commandline) - 1);
259 if (len && !copy_from_user(record->commandline,
260 (char __user *)mm->arg_start, len)) {
261 for (i = 0; i < len; i++) {
262 if (record->commandline[i])
263 last = i;
264 else
265 record->commandline[i] = ' ';
266 }
267 record->commandline[last + 1] = 0;
268 return;
269 }
270 }
271
272 strscpy(record->commandline, current->comm, TASK_COMM_LEN);
273 }
274
275 static void
bnxt_fill_coredump_record(struct bnxt * bp,struct bnxt_coredump_record * record,time64_t start,s16 start_utc,u16 total_segs,int status)276 bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
277 time64_t start, s16 start_utc, u16 total_segs,
278 int status)
279 {
280 time64_t end = ktime_get_real_seconds();
281 u32 os_ver_major = 0, os_ver_minor = 0;
282 struct tm tm;
283
284 time64_to_tm(start, 0, &tm);
285 memset(record, 0, sizeof(*record));
286 memcpy(record->signature, "cOrE", 4);
287 record->flags = 0;
288 record->low_version = 0;
289 record->high_version = 1;
290 record->asic_state = 0;
291 strscpy(record->system_name, utsname()->nodename,
292 sizeof(record->system_name));
293 record->year = cpu_to_le16(tm.tm_year + 1900);
294 record->month = cpu_to_le16(tm.tm_mon + 1);
295 record->day = cpu_to_le16(tm.tm_mday);
296 record->hour = cpu_to_le16(tm.tm_hour);
297 record->minute = cpu_to_le16(tm.tm_min);
298 record->second = cpu_to_le16(tm.tm_sec);
299 record->utc_bias = cpu_to_le16(start_utc);
300 bnxt_fill_cmdline(record);
301 record->total_segments = cpu_to_le32(total_segs);
302
303 if (sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor) != 2)
304 netdev_warn(bp->dev, "Unknown OS release in coredump\n");
305 record->os_ver_major = cpu_to_le32(os_ver_major);
306 record->os_ver_minor = cpu_to_le32(os_ver_minor);
307
308 strscpy(record->os_name, utsname()->sysname, sizeof(record->os_name));
309 time64_to_tm(end, 0, &tm);
310 record->end_year = cpu_to_le16(tm.tm_year + 1900);
311 record->end_month = cpu_to_le16(tm.tm_mon + 1);
312 record->end_day = cpu_to_le16(tm.tm_mday);
313 record->end_hour = cpu_to_le16(tm.tm_hour);
314 record->end_minute = cpu_to_le16(tm.tm_min);
315 record->end_second = cpu_to_le16(tm.tm_sec);
316 record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
317 record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
318 bp->ver_resp.chip_rev << 8 |
319 bp->ver_resp.chip_metal);
320 record->asic_id2 = 0;
321 record->coredump_status = cpu_to_le32(status);
322 record->ioctl_low_version = 0;
323 record->ioctl_high_version = 0;
324 }
325
bnxt_fill_drv_seg_record(struct bnxt * bp,struct bnxt_driver_segment_record * record,struct bnxt_ctx_mem_type * ctxm,u16 type)326 static void bnxt_fill_drv_seg_record(struct bnxt *bp,
327 struct bnxt_driver_segment_record *record,
328 struct bnxt_ctx_mem_type *ctxm, u16 type)
329 {
330 struct bnxt_bs_trace_info *bs_trace = &bp->bs_trace[type];
331 u32 offset = 0;
332 int rc = 0;
333
334 rc = bnxt_dbg_hwrm_log_buffer_flush(bp, type, 0, &offset);
335 if (rc)
336 return;
337
338 bnxt_bs_trace_check_wrap(bs_trace, offset);
339 record->max_entries = cpu_to_le32(ctxm->max_entries);
340 record->entry_size = cpu_to_le32(ctxm->entry_size);
341 record->offset = cpu_to_le32(bs_trace->last_offset);
342 record->wrapped = bs_trace->wrapped;
343 }
344
bnxt_get_ctx_coredump(struct bnxt * bp,void * buf,u32 offset,u32 * segs)345 static u32 bnxt_get_ctx_coredump(struct bnxt *bp, void *buf, u32 offset,
346 u32 *segs)
347 {
348 struct bnxt_driver_segment_record record = {};
349 struct bnxt_coredump_segment_hdr seg_hdr;
350 struct bnxt_ctx_mem_info *ctx = bp->ctx;
351 u32 comp_id = BNXT_DRV_COMP_ID;
352 void *data = NULL;
353 size_t len = 0;
354 u16 type;
355
356 *segs = 0;
357 if (!ctx)
358 return 0;
359
360 if (buf)
361 buf += offset;
362 for (type = 0 ; type <= BNXT_CTX_RIGP1; type++) {
363 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
364 bool trace = bnxt_bs_trace_avail(bp, type);
365 u32 seg_id = bnxt_bstore_to_seg_id[type];
366 size_t seg_len, extra_hlen = 0;
367
368 if (!ctxm->mem_valid || !seg_id)
369 continue;
370
371 if (trace) {
372 extra_hlen = BNXT_SEG_RCD_LEN;
373 if (buf) {
374 u16 trace_type = bnxt_bstore_to_trace[type];
375
376 bnxt_fill_drv_seg_record(bp, &record, ctxm,
377 trace_type);
378 }
379 }
380
381 if (buf)
382 data = buf + BNXT_SEG_HDR_LEN + extra_hlen;
383
384 seg_len = bnxt_copy_ctx_mem(bp, ctxm, data, 0) + extra_hlen;
385 if (buf) {
386 bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, seg_len,
387 0, 0, 0, comp_id, seg_id);
388 memcpy(buf, &seg_hdr, BNXT_SEG_HDR_LEN);
389 buf += BNXT_SEG_HDR_LEN;
390 if (trace)
391 memcpy(buf, &record, BNXT_SEG_RCD_LEN);
392 buf += seg_len;
393 }
394 len += BNXT_SEG_HDR_LEN + seg_len;
395 *segs += 1;
396 }
397 return len;
398 }
399
__bnxt_get_coredump(struct bnxt * bp,u16 dump_type,void * buf,u32 * dump_len)400 static int __bnxt_get_coredump(struct bnxt *bp, u16 dump_type, void *buf,
401 u32 *dump_len)
402 {
403 u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
404 u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
405 struct coredump_segment_record *seg_record = NULL;
406 struct bnxt_coredump_segment_hdr seg_hdr;
407 struct bnxt_coredump coredump = {NULL};
408 time64_t start_time;
409 u16 start_utc;
410 int rc = 0, i;
411
412 if (buf)
413 buf_len = *dump_len;
414
415 start_time = ktime_get_real_seconds();
416 start_utc = sys_tz.tz_minuteswest * 60;
417 seg_hdr_len = sizeof(seg_hdr);
418
419 /* First segment should be hwrm_ver_get response.
420 * For hwrm_ver_get response Component id = 2 and Segment id = 0.
421 */
422 *dump_len = seg_hdr_len + ver_get_resp_len;
423 if (buf) {
424 bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
425 0, 0, 0, BNXT_VER_GET_COMP_ID, 0);
426 memcpy(buf + offset, &seg_hdr, seg_hdr_len);
427 offset += seg_hdr_len;
428 memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
429 offset += ver_get_resp_len;
430 }
431
432 if (dump_type == BNXT_DUMP_DRIVER) {
433 u32 drv_len, segs = 0;
434
435 drv_len = bnxt_get_ctx_coredump(bp, buf, offset, &segs);
436 *dump_len += drv_len;
437 offset += drv_len;
438 if (buf)
439 coredump.total_segs += segs;
440 goto err;
441 }
442
443 seg_record_len = sizeof(*seg_record);
444 rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
445 if (rc) {
446 netdev_err(bp->dev, "Failed to get coredump segment list\n");
447 goto err;
448 }
449
450 *dump_len += seg_hdr_len * coredump.total_segs;
451
452 seg_record = (struct coredump_segment_record *)coredump.data;
453 seg_record_len = sizeof(*seg_record);
454
455 for (i = 0; i < coredump.total_segs; i++) {
456 u16 comp_id = le16_to_cpu(seg_record->component_id);
457 u16 seg_id = le16_to_cpu(seg_record->segment_id);
458 u32 duration = 0, seg_len = 0;
459 unsigned long start, end;
460
461 if (buf && ((offset + seg_hdr_len) >
462 BNXT_COREDUMP_BUF_LEN(buf_len))) {
463 rc = -ENOBUFS;
464 goto err;
465 }
466
467 start = jiffies;
468
469 rc = bnxt_hwrm_dbg_coredump_initiate(bp, dump_type, comp_id,
470 seg_id);
471 if (rc) {
472 netdev_err(bp->dev,
473 "Failed to initiate coredump for seg = %d\n",
474 seg_record->segment_id);
475 goto next_seg;
476 }
477
478 /* Write segment data into the buffer */
479 rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
480 &seg_len, buf, buf_len,
481 offset + seg_hdr_len);
482 if (rc && rc == -ENOBUFS)
483 goto err;
484 else if (rc)
485 netdev_err(bp->dev,
486 "Failed to retrieve coredump for seg = %d\n",
487 seg_record->segment_id);
488
489 next_seg:
490 end = jiffies;
491 duration = jiffies_to_msecs(end - start);
492 bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
493 rc, duration, 0, 0, 0);
494
495 if (buf) {
496 /* Write segment header into the buffer */
497 memcpy(buf + offset, &seg_hdr, seg_hdr_len);
498 offset += seg_hdr_len + seg_len;
499 }
500
501 *dump_len += seg_len;
502 seg_record =
503 (struct coredump_segment_record *)((u8 *)seg_record +
504 seg_record_len);
505 }
506
507 err:
508 if (buf)
509 bnxt_fill_coredump_record(bp, buf + offset, start_time,
510 start_utc, coredump.total_segs + 1,
511 rc);
512 kfree(coredump.data);
513 if (!rc) {
514 *dump_len += sizeof(struct bnxt_coredump_record);
515 /* The actual coredump length can be smaller than the FW
516 * reported length earlier. Use the ethtool provided length.
517 */
518 if (buf_len)
519 *dump_len = buf_len;
520 } else if (rc == -ENOBUFS) {
521 netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
522 }
523 return rc;
524 }
525
bnxt_copy_crash_data(struct bnxt_ring_mem_info * rmem,void * buf,u32 dump_len)526 static u32 bnxt_copy_crash_data(struct bnxt_ring_mem_info *rmem, void *buf,
527 u32 dump_len)
528 {
529 u32 data_copied = 0;
530 u32 data_len;
531 int i;
532
533 for (i = 0; i < rmem->nr_pages; i++) {
534 data_len = rmem->page_size;
535 if (data_copied + data_len > dump_len)
536 data_len = dump_len - data_copied;
537 memcpy(buf + data_copied, rmem->pg_arr[i], data_len);
538 data_copied += data_len;
539 if (data_copied >= dump_len)
540 break;
541 }
542 return data_copied;
543 }
544
bnxt_copy_crash_dump(struct bnxt * bp,void * buf,u32 dump_len)545 static int bnxt_copy_crash_dump(struct bnxt *bp, void *buf, u32 dump_len)
546 {
547 struct bnxt_ring_mem_info *rmem;
548 u32 offset = 0;
549
550 if (!bp->fw_crash_mem)
551 return -ENOENT;
552
553 rmem = &bp->fw_crash_mem->ring_mem;
554
555 if (rmem->depth > 1) {
556 int i;
557
558 for (i = 0; i < rmem->nr_pages; i++) {
559 struct bnxt_ctx_pg_info *pg_tbl;
560
561 pg_tbl = bp->fw_crash_mem->ctx_pg_tbl[i];
562 offset += bnxt_copy_crash_data(&pg_tbl->ring_mem,
563 buf + offset,
564 dump_len - offset);
565 if (offset >= dump_len)
566 break;
567 }
568 } else {
569 bnxt_copy_crash_data(rmem, buf, dump_len);
570 }
571
572 return 0;
573 }
574
bnxt_crash_dump_avail(struct bnxt * bp)575 static bool bnxt_crash_dump_avail(struct bnxt *bp)
576 {
577 u32 sig = 0;
578
579 /* First 4 bytes(signature) of crash dump is always non-zero */
580 bnxt_copy_crash_dump(bp, &sig, sizeof(sig));
581 return !!sig;
582 }
583
bnxt_get_coredump(struct bnxt * bp,u16 dump_type,void * buf,u32 * dump_len)584 int bnxt_get_coredump(struct bnxt *bp, u16 dump_type, void *buf, u32 *dump_len)
585 {
586 if (dump_type == BNXT_DUMP_CRASH) {
587 if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)
588 return bnxt_copy_crash_dump(bp, buf, *dump_len);
589 #ifdef CONFIG_TEE_BNXT_FW
590 else if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
591 return tee_bnxt_copy_coredump(buf, 0, *dump_len);
592 #endif
593 else
594 return -EOPNOTSUPP;
595 } else {
596 return __bnxt_get_coredump(bp, dump_type, buf, dump_len);
597 }
598 }
599
bnxt_hwrm_get_dump_len(struct bnxt * bp,u16 dump_type,u32 * dump_len)600 int bnxt_hwrm_get_dump_len(struct bnxt *bp, u16 dump_type, u32 *dump_len)
601 {
602 struct hwrm_dbg_qcfg_output *resp;
603 struct hwrm_dbg_qcfg_input *req;
604 int rc, hdr_len = 0;
605
606 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
607 return -EOPNOTSUPP;
608
609 if (dump_type == BNXT_DUMP_CRASH &&
610 !(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR ||
611 (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)))
612 return -EOPNOTSUPP;
613
614 rc = hwrm_req_init(bp, req, HWRM_DBG_QCFG);
615 if (rc)
616 return rc;
617
618 req->fid = cpu_to_le16(0xffff);
619 if (dump_type == BNXT_DUMP_CRASH) {
620 if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
621 req->flags = cpu_to_le16(BNXT_DBG_FL_CR_DUMP_SIZE_SOC);
622 else
623 req->flags = cpu_to_le16(BNXT_DBG_FL_CR_DUMP_SIZE_HOST);
624 }
625
626 resp = hwrm_req_hold(bp, req);
627 rc = hwrm_req_send(bp, req);
628 if (rc)
629 goto get_dump_len_exit;
630
631 if (dump_type == BNXT_DUMP_CRASH) {
632 if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
633 *dump_len = BNXT_CRASH_DUMP_LEN;
634 else
635 *dump_len = le32_to_cpu(resp->crashdump_size);
636 } else {
637 /* Driver adds coredump header and "HWRM_VER_GET response"
638 * segment additionally to coredump.
639 */
640 hdr_len = sizeof(struct bnxt_coredump_segment_hdr) +
641 sizeof(struct hwrm_ver_get_output) +
642 sizeof(struct bnxt_coredump_record);
643 *dump_len = le32_to_cpu(resp->coredump_size) + hdr_len;
644 }
645 if (*dump_len <= hdr_len)
646 rc = -EINVAL;
647
648 get_dump_len_exit:
649 hwrm_req_drop(bp, req);
650 return rc;
651 }
652
bnxt_get_coredump_length(struct bnxt * bp,u16 dump_type)653 u32 bnxt_get_coredump_length(struct bnxt *bp, u16 dump_type)
654 {
655 u32 len = 0;
656
657 if (dump_type == BNXT_DUMP_CRASH &&
658 bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR &&
659 bp->fw_crash_mem) {
660 if (!bnxt_crash_dump_avail(bp))
661 return 0;
662
663 return bp->fw_crash_len;
664 }
665
666 if (dump_type != BNXT_DUMP_DRIVER) {
667 if (!bnxt_hwrm_get_dump_len(bp, dump_type, &len))
668 return len;
669 }
670 if (dump_type != BNXT_DUMP_CRASH)
671 __bnxt_get_coredump(bp, dump_type, NULL, &len);
672
673 return len;
674 }
675