xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_coredump.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
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 			data = buf + BNXT_SEG_HDR_LEN + extra_hlen;
375 		seg_len = bnxt_copy_ctx_mem(bp, ctxm, data, 0) + extra_hlen;
376 		if (buf) {
377 			bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, seg_len,
378 						   0, 0, 0, comp_id, seg_id);
379 			memcpy(buf, &seg_hdr, BNXT_SEG_HDR_LEN);
380 			buf += BNXT_SEG_HDR_LEN;
381 			if (trace) {
382 				u16 trace_type = bnxt_bstore_to_trace[type];
383 
384 				bnxt_fill_drv_seg_record(bp, &record, ctxm,
385 							 trace_type);
386 				memcpy(buf, &record, BNXT_SEG_RCD_LEN);
387 			}
388 			buf += seg_len;
389 		}
390 		len += BNXT_SEG_HDR_LEN + seg_len;
391 		*segs += 1;
392 	}
393 	return len;
394 }
395 
__bnxt_get_coredump(struct bnxt * bp,u16 dump_type,void * buf,u32 * dump_len)396 static int __bnxt_get_coredump(struct bnxt *bp, u16 dump_type, void *buf,
397 			       u32 *dump_len)
398 {
399 	u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
400 	u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
401 	struct coredump_segment_record *seg_record = NULL;
402 	struct bnxt_coredump_segment_hdr seg_hdr;
403 	struct bnxt_coredump coredump = {NULL};
404 	time64_t start_time;
405 	u16 start_utc;
406 	int rc = 0, i;
407 
408 	if (buf)
409 		buf_len = *dump_len;
410 
411 	start_time = ktime_get_real_seconds();
412 	start_utc = sys_tz.tz_minuteswest * 60;
413 	seg_hdr_len = sizeof(seg_hdr);
414 
415 	/* First segment should be hwrm_ver_get response.
416 	 * For hwrm_ver_get response Component id = 2 and Segment id = 0.
417 	 */
418 	*dump_len = seg_hdr_len + ver_get_resp_len;
419 	if (buf) {
420 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
421 					   0, 0, 0, BNXT_VER_GET_COMP_ID, 0);
422 		memcpy(buf + offset, &seg_hdr, seg_hdr_len);
423 		offset += seg_hdr_len;
424 		memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
425 		offset += ver_get_resp_len;
426 	}
427 
428 	if (dump_type == BNXT_DUMP_DRIVER) {
429 		u32 drv_len, segs = 0;
430 
431 		drv_len = bnxt_get_ctx_coredump(bp, buf, offset, &segs);
432 		*dump_len += drv_len;
433 		offset += drv_len;
434 		if (buf)
435 			coredump.total_segs += segs;
436 		goto err;
437 	}
438 
439 	seg_record_len = sizeof(*seg_record);
440 	rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
441 	if (rc) {
442 		netdev_err(bp->dev, "Failed to get coredump segment list\n");
443 		goto err;
444 	}
445 
446 	*dump_len += seg_hdr_len * coredump.total_segs;
447 
448 	seg_record = (struct coredump_segment_record *)coredump.data;
449 	seg_record_len = sizeof(*seg_record);
450 
451 	for (i = 0; i < coredump.total_segs; i++) {
452 		u16 comp_id = le16_to_cpu(seg_record->component_id);
453 		u16 seg_id = le16_to_cpu(seg_record->segment_id);
454 		u32 duration = 0, seg_len = 0;
455 		unsigned long start, end;
456 
457 		if (buf && ((offset + seg_hdr_len) >
458 			    BNXT_COREDUMP_BUF_LEN(buf_len))) {
459 			rc = -ENOBUFS;
460 			goto err;
461 		}
462 
463 		start = jiffies;
464 
465 		rc = bnxt_hwrm_dbg_coredump_initiate(bp, dump_type, comp_id,
466 						     seg_id);
467 		if (rc) {
468 			netdev_err(bp->dev,
469 				   "Failed to initiate coredump for seg = %d\n",
470 				   seg_record->segment_id);
471 			goto next_seg;
472 		}
473 
474 		/* Write segment data into the buffer */
475 		rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
476 						     &seg_len, buf, buf_len,
477 						     offset + seg_hdr_len);
478 		if (rc && rc == -ENOBUFS)
479 			goto err;
480 		else if (rc)
481 			netdev_err(bp->dev,
482 				   "Failed to retrieve coredump for seg = %d\n",
483 				   seg_record->segment_id);
484 
485 next_seg:
486 		end = jiffies;
487 		duration = jiffies_to_msecs(end - start);
488 		bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
489 					   rc, duration, 0, 0, 0);
490 
491 		if (buf) {
492 			/* Write segment header into the buffer */
493 			memcpy(buf + offset, &seg_hdr, seg_hdr_len);
494 			offset += seg_hdr_len + seg_len;
495 		}
496 
497 		*dump_len += seg_len;
498 		seg_record =
499 			(struct coredump_segment_record *)((u8 *)seg_record +
500 							   seg_record_len);
501 	}
502 
503 err:
504 	if (buf)
505 		bnxt_fill_coredump_record(bp, buf + offset, start_time,
506 					  start_utc, coredump.total_segs + 1,
507 					  rc);
508 	kfree(coredump.data);
509 	if (!rc) {
510 		*dump_len += sizeof(struct bnxt_coredump_record);
511 		/* The actual coredump length can be smaller than the FW
512 		 * reported length earlier.  Use the ethtool provided length.
513 		 */
514 		if (buf_len)
515 			*dump_len = buf_len;
516 	} else if (rc == -ENOBUFS) {
517 		netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
518 	}
519 	return rc;
520 }
521 
bnxt_copy_crash_data(struct bnxt_ring_mem_info * rmem,void * buf,u32 dump_len)522 static u32 bnxt_copy_crash_data(struct bnxt_ring_mem_info *rmem, void *buf,
523 				u32 dump_len)
524 {
525 	u32 data_copied = 0;
526 	u32 data_len;
527 	int i;
528 
529 	for (i = 0; i < rmem->nr_pages; i++) {
530 		data_len = rmem->page_size;
531 		if (data_copied + data_len > dump_len)
532 			data_len = dump_len - data_copied;
533 		memcpy(buf + data_copied, rmem->pg_arr[i], data_len);
534 		data_copied += data_len;
535 		if (data_copied >= dump_len)
536 			break;
537 	}
538 	return data_copied;
539 }
540 
bnxt_copy_crash_dump(struct bnxt * bp,void * buf,u32 dump_len)541 static int bnxt_copy_crash_dump(struct bnxt *bp, void *buf, u32 dump_len)
542 {
543 	struct bnxt_ring_mem_info *rmem;
544 	u32 offset = 0;
545 
546 	if (!bp->fw_crash_mem)
547 		return -ENOENT;
548 
549 	rmem = &bp->fw_crash_mem->ring_mem;
550 
551 	if (rmem->depth > 1) {
552 		int i;
553 
554 		for (i = 0; i < rmem->nr_pages; i++) {
555 			struct bnxt_ctx_pg_info *pg_tbl;
556 
557 			pg_tbl = bp->fw_crash_mem->ctx_pg_tbl[i];
558 			offset += bnxt_copy_crash_data(&pg_tbl->ring_mem,
559 						       buf + offset,
560 						       dump_len - offset);
561 			if (offset >= dump_len)
562 				break;
563 		}
564 	} else {
565 		bnxt_copy_crash_data(rmem, buf, dump_len);
566 	}
567 
568 	return 0;
569 }
570 
bnxt_crash_dump_avail(struct bnxt * bp)571 static bool bnxt_crash_dump_avail(struct bnxt *bp)
572 {
573 	u32 sig = 0;
574 
575 	/* First 4 bytes(signature) of crash dump is always non-zero */
576 	bnxt_copy_crash_dump(bp, &sig, sizeof(sig));
577 	return !!sig;
578 }
579 
bnxt_get_coredump(struct bnxt * bp,u16 dump_type,void * buf,u32 * dump_len)580 int bnxt_get_coredump(struct bnxt *bp, u16 dump_type, void *buf, u32 *dump_len)
581 {
582 	if (dump_type == BNXT_DUMP_CRASH) {
583 		if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)
584 			return bnxt_copy_crash_dump(bp, buf, *dump_len);
585 #ifdef CONFIG_TEE_BNXT_FW
586 		else if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
587 			return tee_bnxt_copy_coredump(buf, 0, *dump_len);
588 #endif
589 		else
590 			return -EOPNOTSUPP;
591 	} else {
592 		return __bnxt_get_coredump(bp, dump_type, buf, dump_len);
593 	}
594 }
595 
bnxt_hwrm_get_dump_len(struct bnxt * bp,u16 dump_type,u32 * dump_len)596 int bnxt_hwrm_get_dump_len(struct bnxt *bp, u16 dump_type, u32 *dump_len)
597 {
598 	struct hwrm_dbg_qcfg_output *resp;
599 	struct hwrm_dbg_qcfg_input *req;
600 	int rc, hdr_len = 0;
601 
602 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
603 		return -EOPNOTSUPP;
604 
605 	if (dump_type == BNXT_DUMP_CRASH &&
606 	    !(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR ||
607 	     (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)))
608 		return -EOPNOTSUPP;
609 
610 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCFG);
611 	if (rc)
612 		return rc;
613 
614 	req->fid = cpu_to_le16(0xffff);
615 	if (dump_type == BNXT_DUMP_CRASH) {
616 		if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
617 			req->flags = cpu_to_le16(BNXT_DBG_FL_CR_DUMP_SIZE_SOC);
618 		else
619 			req->flags = cpu_to_le16(BNXT_DBG_FL_CR_DUMP_SIZE_HOST);
620 	}
621 
622 	resp = hwrm_req_hold(bp, req);
623 	rc = hwrm_req_send(bp, req);
624 	if (rc)
625 		goto get_dump_len_exit;
626 
627 	if (dump_type == BNXT_DUMP_CRASH) {
628 		if (bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR)
629 			*dump_len = BNXT_CRASH_DUMP_LEN;
630 		else
631 			*dump_len = le32_to_cpu(resp->crashdump_size);
632 	} else {
633 		/* Driver adds coredump header and "HWRM_VER_GET response"
634 		 * segment additionally to coredump.
635 		 */
636 		hdr_len = sizeof(struct bnxt_coredump_segment_hdr) +
637 		sizeof(struct hwrm_ver_get_output) +
638 		sizeof(struct bnxt_coredump_record);
639 		*dump_len = le32_to_cpu(resp->coredump_size) + hdr_len;
640 	}
641 	if (*dump_len <= hdr_len)
642 		rc = -EINVAL;
643 
644 get_dump_len_exit:
645 	hwrm_req_drop(bp, req);
646 	return rc;
647 }
648 
bnxt_get_coredump_length(struct bnxt * bp,u16 dump_type)649 u32 bnxt_get_coredump_length(struct bnxt *bp, u16 dump_type)
650 {
651 	u32 len = 0;
652 
653 	if (dump_type == BNXT_DUMP_CRASH &&
654 	    bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR &&
655 	    bp->fw_crash_mem) {
656 		if (!bnxt_crash_dump_avail(bp))
657 			return 0;
658 
659 		return bp->fw_crash_len;
660 	}
661 
662 	if (dump_type != BNXT_DUMP_DRIVER) {
663 		if (!bnxt_hwrm_get_dump_len(bp, dump_type, &len))
664 			return len;
665 	}
666 	if (dump_type != BNXT_DUMP_CRASH)
667 		__bnxt_get_coredump(bp, dump_type, NULL, &len);
668 
669 	return len;
670 }
671