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
bnxt_hwrm_dbg_dma_data(struct bnxt * bp,void * msg,struct bnxt_hwrm_dbg_dma_info * info)18 static int bnxt_hwrm_dbg_dma_data(struct bnxt *bp, void *msg,
19 struct bnxt_hwrm_dbg_dma_info *info)
20 {
21 struct hwrm_dbg_cmn_input *cmn_req = msg;
22 __le16 *seq_ptr = msg + info->seq_off;
23 struct hwrm_dbg_cmn_output *cmn_resp;
24 u16 seq = 0, len, segs_off;
25 dma_addr_t dma_handle;
26 void *dma_buf, *resp;
27 int rc, off = 0;
28
29 dma_buf = hwrm_req_dma_slice(bp, msg, info->dma_len, &dma_handle);
30 if (!dma_buf) {
31 hwrm_req_drop(bp, msg);
32 return -ENOMEM;
33 }
34
35 hwrm_req_timeout(bp, msg, bp->hwrm_cmd_max_timeout);
36 cmn_resp = hwrm_req_hold(bp, msg);
37 resp = cmn_resp;
38
39 segs_off = offsetof(struct hwrm_dbg_coredump_list_output,
40 total_segments);
41 cmn_req->host_dest_addr = cpu_to_le64(dma_handle);
42 cmn_req->host_buf_len = cpu_to_le32(info->dma_len);
43 while (1) {
44 *seq_ptr = cpu_to_le16(seq);
45 rc = hwrm_req_send(bp, msg);
46 if (rc)
47 break;
48
49 len = le16_to_cpu(*((__le16 *)(resp + info->data_len_off)));
50 if (!seq &&
51 cmn_req->req_type == cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
52 info->segs = le16_to_cpu(*((__le16 *)(resp +
53 segs_off)));
54 if (!info->segs) {
55 rc = -EIO;
56 break;
57 }
58
59 info->dest_buf_size = info->segs *
60 sizeof(struct coredump_segment_record);
61 info->dest_buf = kmalloc(info->dest_buf_size,
62 GFP_KERNEL);
63 if (!info->dest_buf) {
64 rc = -ENOMEM;
65 break;
66 }
67 }
68
69 if (cmn_req->req_type ==
70 cpu_to_le16(HWRM_DBG_COREDUMP_RETRIEVE))
71 info->dest_buf_size += len;
72
73 if (info->dest_buf) {
74 if ((info->seg_start + off + len) <=
75 BNXT_COREDUMP_BUF_LEN(info->buf_len)) {
76 u16 copylen = min_t(u16, len,
77 info->dest_buf_size - off);
78
79 memcpy(info->dest_buf + off, dma_buf, copylen);
80 if (copylen < len)
81 break;
82 } else {
83 rc = -ENOBUFS;
84 if (cmn_req->req_type ==
85 cpu_to_le16(HWRM_DBG_COREDUMP_LIST)) {
86 kfree(info->dest_buf);
87 info->dest_buf = NULL;
88 }
89 break;
90 }
91 }
92
93 if (!(cmn_resp->flags & HWRM_DBG_CMN_FLAGS_MORE))
94 break;
95
96 seq++;
97 off += len;
98 }
99 hwrm_req_drop(bp, msg);
100 return rc;
101 }
102
bnxt_hwrm_dbg_coredump_list(struct bnxt * bp,struct bnxt_coredump * coredump)103 static int bnxt_hwrm_dbg_coredump_list(struct bnxt *bp,
104 struct bnxt_coredump *coredump)
105 {
106 struct bnxt_hwrm_dbg_dma_info info = {NULL};
107 struct hwrm_dbg_coredump_list_input *req;
108 int rc;
109
110 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_LIST);
111 if (rc)
112 return rc;
113
114 info.dma_len = COREDUMP_LIST_BUF_LEN;
115 info.seq_off = offsetof(struct hwrm_dbg_coredump_list_input, seq_no);
116 info.data_len_off = offsetof(struct hwrm_dbg_coredump_list_output,
117 data_len);
118
119 rc = bnxt_hwrm_dbg_dma_data(bp, req, &info);
120 if (!rc) {
121 coredump->data = info.dest_buf;
122 coredump->data_size = info.dest_buf_size;
123 coredump->total_segs = info.segs;
124 }
125 return rc;
126 }
127
bnxt_hwrm_dbg_coredump_initiate(struct bnxt * bp,u16 component_id,u16 segment_id)128 static int bnxt_hwrm_dbg_coredump_initiate(struct bnxt *bp, u16 component_id,
129 u16 segment_id)
130 {
131 struct hwrm_dbg_coredump_initiate_input *req;
132 int rc;
133
134 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_INITIATE);
135 if (rc)
136 return rc;
137
138 hwrm_req_timeout(bp, req, bp->hwrm_cmd_max_timeout);
139 req->component_id = cpu_to_le16(component_id);
140 req->segment_id = cpu_to_le16(segment_id);
141
142 return hwrm_req_send(bp, req);
143 }
144
bnxt_hwrm_dbg_coredump_retrieve(struct bnxt * bp,u16 component_id,u16 segment_id,u32 * seg_len,void * buf,u32 buf_len,u32 offset)145 static int bnxt_hwrm_dbg_coredump_retrieve(struct bnxt *bp, u16 component_id,
146 u16 segment_id, u32 *seg_len,
147 void *buf, u32 buf_len, u32 offset)
148 {
149 struct hwrm_dbg_coredump_retrieve_input *req;
150 struct bnxt_hwrm_dbg_dma_info info = {NULL};
151 int rc;
152
153 rc = hwrm_req_init(bp, req, HWRM_DBG_COREDUMP_RETRIEVE);
154 if (rc)
155 return rc;
156
157 req->component_id = cpu_to_le16(component_id);
158 req->segment_id = cpu_to_le16(segment_id);
159
160 info.dma_len = COREDUMP_RETRIEVE_BUF_LEN;
161 info.seq_off = offsetof(struct hwrm_dbg_coredump_retrieve_input,
162 seq_no);
163 info.data_len_off = offsetof(struct hwrm_dbg_coredump_retrieve_output,
164 data_len);
165 if (buf) {
166 info.dest_buf = buf + offset;
167 info.buf_len = buf_len;
168 info.seg_start = offset;
169 }
170
171 rc = bnxt_hwrm_dbg_dma_data(bp, req, &info);
172 if (!rc)
173 *seg_len = info.dest_buf_size;
174
175 return rc;
176 }
177
178 static 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)179 bnxt_fill_coredump_seg_hdr(struct bnxt *bp,
180 struct bnxt_coredump_segment_hdr *seg_hdr,
181 struct coredump_segment_record *seg_rec, u32 seg_len,
182 int status, u32 duration, u32 instance)
183 {
184 memset(seg_hdr, 0, sizeof(*seg_hdr));
185 memcpy(seg_hdr->signature, "sEgM", 4);
186 if (seg_rec) {
187 seg_hdr->component_id = (__force __le32)seg_rec->component_id;
188 seg_hdr->segment_id = (__force __le32)seg_rec->segment_id;
189 seg_hdr->low_version = seg_rec->version_low;
190 seg_hdr->high_version = seg_rec->version_hi;
191 seg_hdr->flags = cpu_to_le32(seg_rec->compress_flags);
192 } else {
193 /* For hwrm_ver_get response Component id = 2
194 * and Segment id = 0
195 */
196 seg_hdr->component_id = cpu_to_le32(2);
197 seg_hdr->segment_id = 0;
198 }
199 seg_hdr->function_id = cpu_to_le16(bp->pdev->devfn);
200 seg_hdr->length = cpu_to_le32(seg_len);
201 seg_hdr->status = cpu_to_le32(status);
202 seg_hdr->duration = cpu_to_le32(duration);
203 seg_hdr->data_offset = cpu_to_le32(sizeof(*seg_hdr));
204 seg_hdr->instance = cpu_to_le32(instance);
205 }
206
bnxt_fill_cmdline(struct bnxt_coredump_record * record)207 static void bnxt_fill_cmdline(struct bnxt_coredump_record *record)
208 {
209 struct mm_struct *mm = current->mm;
210 int i, len, last = 0;
211
212 if (mm) {
213 len = min_t(int, mm->arg_end - mm->arg_start,
214 sizeof(record->commandline) - 1);
215 if (len && !copy_from_user(record->commandline,
216 (char __user *)mm->arg_start, len)) {
217 for (i = 0; i < len; i++) {
218 if (record->commandline[i])
219 last = i;
220 else
221 record->commandline[i] = ' ';
222 }
223 record->commandline[last + 1] = 0;
224 return;
225 }
226 }
227
228 strscpy(record->commandline, current->comm, TASK_COMM_LEN);
229 }
230
231 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)232 bnxt_fill_coredump_record(struct bnxt *bp, struct bnxt_coredump_record *record,
233 time64_t start, s16 start_utc, u16 total_segs,
234 int status)
235 {
236 time64_t end = ktime_get_real_seconds();
237 u32 os_ver_major = 0, os_ver_minor = 0;
238 struct tm tm;
239
240 time64_to_tm(start, 0, &tm);
241 memset(record, 0, sizeof(*record));
242 memcpy(record->signature, "cOrE", 4);
243 record->flags = 0;
244 record->low_version = 0;
245 record->high_version = 1;
246 record->asic_state = 0;
247 strscpy(record->system_name, utsname()->nodename,
248 sizeof(record->system_name));
249 record->year = cpu_to_le16(tm.tm_year + 1900);
250 record->month = cpu_to_le16(tm.tm_mon + 1);
251 record->day = cpu_to_le16(tm.tm_mday);
252 record->hour = cpu_to_le16(tm.tm_hour);
253 record->minute = cpu_to_le16(tm.tm_min);
254 record->second = cpu_to_le16(tm.tm_sec);
255 record->utc_bias = cpu_to_le16(start_utc);
256 bnxt_fill_cmdline(record);
257 record->total_segments = cpu_to_le32(total_segs);
258
259 if (sscanf(utsname()->release, "%u.%u", &os_ver_major, &os_ver_minor) != 2)
260 netdev_warn(bp->dev, "Unknown OS release in coredump\n");
261 record->os_ver_major = cpu_to_le32(os_ver_major);
262 record->os_ver_minor = cpu_to_le32(os_ver_minor);
263
264 strscpy(record->os_name, utsname()->sysname, sizeof(record->os_name));
265 time64_to_tm(end, 0, &tm);
266 record->end_year = cpu_to_le16(tm.tm_year + 1900);
267 record->end_month = cpu_to_le16(tm.tm_mon + 1);
268 record->end_day = cpu_to_le16(tm.tm_mday);
269 record->end_hour = cpu_to_le16(tm.tm_hour);
270 record->end_minute = cpu_to_le16(tm.tm_min);
271 record->end_second = cpu_to_le16(tm.tm_sec);
272 record->end_utc_bias = cpu_to_le16(sys_tz.tz_minuteswest * 60);
273 record->asic_id1 = cpu_to_le32(bp->chip_num << 16 |
274 bp->ver_resp.chip_rev << 8 |
275 bp->ver_resp.chip_metal);
276 record->asic_id2 = 0;
277 record->coredump_status = cpu_to_le32(status);
278 record->ioctl_low_version = 0;
279 record->ioctl_high_version = 0;
280 }
281
__bnxt_get_coredump(struct bnxt * bp,void * buf,u32 * dump_len)282 static int __bnxt_get_coredump(struct bnxt *bp, void *buf, u32 *dump_len)
283 {
284 u32 ver_get_resp_len = sizeof(struct hwrm_ver_get_output);
285 u32 offset = 0, seg_hdr_len, seg_record_len, buf_len = 0;
286 struct coredump_segment_record *seg_record = NULL;
287 struct bnxt_coredump_segment_hdr seg_hdr;
288 struct bnxt_coredump coredump = {NULL};
289 time64_t start_time;
290 u16 start_utc;
291 int rc = 0, i;
292
293 if (buf)
294 buf_len = *dump_len;
295
296 start_time = ktime_get_real_seconds();
297 start_utc = sys_tz.tz_minuteswest * 60;
298 seg_hdr_len = sizeof(seg_hdr);
299
300 /* First segment should be hwrm_ver_get response */
301 *dump_len = seg_hdr_len + ver_get_resp_len;
302 if (buf) {
303 bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, NULL, ver_get_resp_len,
304 0, 0, 0);
305 memcpy(buf + offset, &seg_hdr, seg_hdr_len);
306 offset += seg_hdr_len;
307 memcpy(buf + offset, &bp->ver_resp, ver_get_resp_len);
308 offset += ver_get_resp_len;
309 }
310
311 rc = bnxt_hwrm_dbg_coredump_list(bp, &coredump);
312 if (rc) {
313 netdev_err(bp->dev, "Failed to get coredump segment list\n");
314 goto err;
315 }
316
317 *dump_len += seg_hdr_len * coredump.total_segs;
318
319 seg_record = (struct coredump_segment_record *)coredump.data;
320 seg_record_len = sizeof(*seg_record);
321
322 for (i = 0; i < coredump.total_segs; i++) {
323 u16 comp_id = le16_to_cpu(seg_record->component_id);
324 u16 seg_id = le16_to_cpu(seg_record->segment_id);
325 u32 duration = 0, seg_len = 0;
326 unsigned long start, end;
327
328 if (buf && ((offset + seg_hdr_len) >
329 BNXT_COREDUMP_BUF_LEN(buf_len))) {
330 rc = -ENOBUFS;
331 goto err;
332 }
333
334 start = jiffies;
335
336 rc = bnxt_hwrm_dbg_coredump_initiate(bp, comp_id, seg_id);
337 if (rc) {
338 netdev_err(bp->dev,
339 "Failed to initiate coredump for seg = %d\n",
340 seg_record->segment_id);
341 goto next_seg;
342 }
343
344 /* Write segment data into the buffer */
345 rc = bnxt_hwrm_dbg_coredump_retrieve(bp, comp_id, seg_id,
346 &seg_len, buf, buf_len,
347 offset + seg_hdr_len);
348 if (rc && rc == -ENOBUFS)
349 goto err;
350 else if (rc)
351 netdev_err(bp->dev,
352 "Failed to retrieve coredump for seg = %d\n",
353 seg_record->segment_id);
354
355 next_seg:
356 end = jiffies;
357 duration = jiffies_to_msecs(end - start);
358 bnxt_fill_coredump_seg_hdr(bp, &seg_hdr, seg_record, seg_len,
359 rc, duration, 0);
360
361 if (buf) {
362 /* Write segment header into the buffer */
363 memcpy(buf + offset, &seg_hdr, seg_hdr_len);
364 offset += seg_hdr_len + seg_len;
365 }
366
367 *dump_len += seg_len;
368 seg_record =
369 (struct coredump_segment_record *)((u8 *)seg_record +
370 seg_record_len);
371 }
372
373 err:
374 if (buf)
375 bnxt_fill_coredump_record(bp, buf + offset, start_time,
376 start_utc, coredump.total_segs + 1,
377 rc);
378 kfree(coredump.data);
379 *dump_len += sizeof(struct bnxt_coredump_record);
380 if (rc == -ENOBUFS)
381 netdev_err(bp->dev, "Firmware returned large coredump buffer\n");
382 return rc;
383 }
384
bnxt_get_coredump(struct bnxt * bp,u16 dump_type,void * buf,u32 * dump_len)385 int bnxt_get_coredump(struct bnxt *bp, u16 dump_type, void *buf, u32 *dump_len)
386 {
387 if (dump_type == BNXT_DUMP_CRASH) {
388 #ifdef CONFIG_TEE_BNXT_FW
389 return tee_bnxt_copy_coredump(buf, 0, *dump_len);
390 #else
391 return -EOPNOTSUPP;
392 #endif
393 } else {
394 return __bnxt_get_coredump(bp, buf, dump_len);
395 }
396 }
397
bnxt_hwrm_get_dump_len(struct bnxt * bp,u16 dump_type,u32 * dump_len)398 static int bnxt_hwrm_get_dump_len(struct bnxt *bp, u16 dump_type, u32 *dump_len)
399 {
400 struct hwrm_dbg_qcfg_output *resp;
401 struct hwrm_dbg_qcfg_input *req;
402 int rc, hdr_len = 0;
403
404 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
405 return -EOPNOTSUPP;
406
407 if (dump_type == BNXT_DUMP_CRASH &&
408 !(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_SOC_DDR))
409 return -EOPNOTSUPP;
410
411 rc = hwrm_req_init(bp, req, HWRM_DBG_QCFG);
412 if (rc)
413 return rc;
414
415 req->fid = cpu_to_le16(0xffff);
416 if (dump_type == BNXT_DUMP_CRASH)
417 req->flags = cpu_to_le16(DBG_QCFG_REQ_FLAGS_CRASHDUMP_SIZE_FOR_DEST_DEST_SOC_DDR);
418
419 resp = hwrm_req_hold(bp, req);
420 rc = hwrm_req_send(bp, req);
421 if (rc)
422 goto get_dump_len_exit;
423
424 if (dump_type == BNXT_DUMP_CRASH) {
425 *dump_len = le32_to_cpu(resp->crashdump_size);
426 } else {
427 /* Driver adds coredump header and "HWRM_VER_GET response"
428 * segment additionally to coredump.
429 */
430 hdr_len = sizeof(struct bnxt_coredump_segment_hdr) +
431 sizeof(struct hwrm_ver_get_output) +
432 sizeof(struct bnxt_coredump_record);
433 *dump_len = le32_to_cpu(resp->coredump_size) + hdr_len;
434 }
435 if (*dump_len <= hdr_len)
436 rc = -EINVAL;
437
438 get_dump_len_exit:
439 hwrm_req_drop(bp, req);
440 return rc;
441 }
442
bnxt_get_coredump_length(struct bnxt * bp,u16 dump_type)443 u32 bnxt_get_coredump_length(struct bnxt *bp, u16 dump_type)
444 {
445 u32 len = 0;
446
447 if (bnxt_hwrm_get_dump_len(bp, dump_type, &len)) {
448 if (dump_type == BNXT_DUMP_CRASH)
449 len = BNXT_CRASH_DUMP_LEN;
450 else
451 __bnxt_get_coredump(bp, NULL, &len);
452 }
453 return len;
454 }
455