1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* QLogic qed NIC Driver
3 * Copyright (c) 2015-2016 QLogic Corporation
4 * Copyright (c) 2019-2020 Marvell International Ltd.
7 #include <linux/crc32.h>
9 #include "qed_dev_api.h"
12 #include "qed_selftest.h"
14 int qed_selftest_memory(struct qed_dev
*cdev
)
18 for_each_hwfn(cdev
, i
) {
19 rc
= qed_sp_heartbeat_ramrod(&cdev
->hwfns
[i
]);
27 int qed_selftest_interrupt(struct qed_dev
*cdev
)
31 for_each_hwfn(cdev
, i
) {
32 rc
= qed_sp_heartbeat_ramrod(&cdev
->hwfns
[i
]);
40 int qed_selftest_register(struct qed_dev
*cdev
)
42 struct qed_hwfn
*p_hwfn
;
43 struct qed_ptt
*p_ptt
;
46 /* although performed by MCP, this test is per engine */
47 for_each_hwfn(cdev
, i
) {
48 p_hwfn
= &cdev
->hwfns
[i
];
49 p_ptt
= qed_ptt_acquire(p_hwfn
);
51 DP_ERR(p_hwfn
, "failed to acquire ptt\n");
54 rc
= qed_mcp_bist_register_test(p_hwfn
, p_ptt
);
55 qed_ptt_release(p_hwfn
, p_ptt
);
63 int qed_selftest_clock(struct qed_dev
*cdev
)
65 struct qed_hwfn
*p_hwfn
;
66 struct qed_ptt
*p_ptt
;
69 /* although performed by MCP, this test is per engine */
70 for_each_hwfn(cdev
, i
) {
71 p_hwfn
= &cdev
->hwfns
[i
];
72 p_ptt
= qed_ptt_acquire(p_hwfn
);
74 DP_ERR(p_hwfn
, "failed to acquire ptt\n");
77 rc
= qed_mcp_bist_clock_test(p_hwfn
, p_ptt
);
78 qed_ptt_release(p_hwfn
, p_ptt
);
86 int qed_selftest_nvram(struct qed_dev
*cdev
)
88 struct qed_hwfn
*p_hwfn
= QED_LEADING_HWFN(cdev
);
89 struct qed_ptt
*p_ptt
= qed_ptt_acquire(p_hwfn
);
90 u32 num_images
, i
, j
, nvm_crc
, calc_crc
;
91 struct bist_nvm_image_att image_att
;
97 DP_ERR(p_hwfn
, "failed to acquire ptt\n");
101 /* Acquire from MFW the amount of available images */
102 rc
= qed_mcp_bist_nvm_get_num_images(p_hwfn
, p_ptt
, &num_images
);
103 if (rc
|| !num_images
) {
104 DP_ERR(p_hwfn
, "Failed getting number of images\n");
109 /* Iterate over images and validate CRC */
110 for (i
= 0; i
< num_images
; i
++) {
111 /* This mailbox returns information about the image required for
114 rc
= qed_mcp_bist_nvm_get_image_att(p_hwfn
, p_ptt
,
118 "Failed getting image index %d attributes\n",
123 /* After MFW crash dump is collected - the image's CRC stops
126 if (image_att
.image_type
== NVM_TYPE_MDUMP
)
129 DP_VERBOSE(p_hwfn
, QED_MSG_SP
, "image index %d, size %x\n",
132 /* Allocate a buffer for holding the nvram image */
133 buf
= kzalloc(image_att
.len
, GFP_KERNEL
);
139 /* Read image into buffer */
140 rc
= qed_mcp_nvm_read(p_hwfn
->cdev
, image_att
.nvm_start_addr
,
144 "Failed reading image index %d from nvm.\n", i
);
148 /* Convert the buffer into big-endian format (excluding the
149 * closing 4 bytes of CRC).
151 for (j
= 0; j
< image_att
.len
- 4; j
+= 4) {
152 val
= cpu_to_be32(*(u32
*)&buf
[j
]);
153 *(u32
*)&buf
[j
] = (__force u32
)val
;
156 /* Calc CRC for the "actual" image buffer, i.e. not including
157 * the last 4 CRC bytes.
159 nvm_crc
= *(u32
*)(buf
+ image_att
.len
- 4);
160 calc_crc
= crc32(0xffffffff, buf
, image_att
.len
- 4);
161 calc_crc
= (__force u32
)~cpu_to_be32(calc_crc
);
162 DP_VERBOSE(p_hwfn
, QED_MSG_SP
,
163 "nvm crc 0x%x, calc_crc 0x%x\n", nvm_crc
, calc_crc
);
165 if (calc_crc
!= nvm_crc
) {
170 /* Done with this image; Free to prevent double release
171 * on subsequent failure.
177 qed_ptt_release(p_hwfn
, p_ptt
);
183 qed_ptt_release(p_hwfn
, p_ptt
);