1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* QLogic qed NIC Driver
3 * Copyright (c) 2015-2016 QLogic Corporation
6 #include <linux/crc32.h>
8 #include "qed_dev_api.h"
11 #include "qed_selftest.h"
13 int qed_selftest_memory(struct qed_dev *cdev)
17 for_each_hwfn(cdev, i) {
18 rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
26 int qed_selftest_interrupt(struct qed_dev *cdev)
30 for_each_hwfn(cdev, i) {
31 rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
39 int qed_selftest_register(struct qed_dev *cdev)
41 struct qed_hwfn *p_hwfn;
42 struct qed_ptt *p_ptt;
45 /* although performed by MCP, this test is per engine */
46 for_each_hwfn(cdev, i) {
47 p_hwfn = &cdev->hwfns[i];
48 p_ptt = qed_ptt_acquire(p_hwfn);
50 DP_ERR(p_hwfn, "failed to acquire ptt\n");
53 rc = qed_mcp_bist_register_test(p_hwfn, p_ptt);
54 qed_ptt_release(p_hwfn, p_ptt);
62 int qed_selftest_clock(struct qed_dev *cdev)
64 struct qed_hwfn *p_hwfn;
65 struct qed_ptt *p_ptt;
68 /* although performed by MCP, this test is per engine */
69 for_each_hwfn(cdev, i) {
70 p_hwfn = &cdev->hwfns[i];
71 p_ptt = qed_ptt_acquire(p_hwfn);
73 DP_ERR(p_hwfn, "failed to acquire ptt\n");
76 rc = qed_mcp_bist_clock_test(p_hwfn, p_ptt);
77 qed_ptt_release(p_hwfn, p_ptt);
85 int qed_selftest_nvram(struct qed_dev *cdev)
87 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
88 struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
89 u32 num_images, i, j, nvm_crc, calc_crc;
90 struct bist_nvm_image_att image_att;
96 DP_ERR(p_hwfn, "failed to acquire ptt\n");
100 /* Acquire from MFW the amount of available images */
101 rc = qed_mcp_bist_nvm_get_num_images(p_hwfn, p_ptt, &num_images);
102 if (rc || !num_images) {
103 DP_ERR(p_hwfn, "Failed getting number of images\n");
108 /* Iterate over images and validate CRC */
109 for (i = 0; i < num_images; i++) {
110 /* This mailbox returns information about the image required for
113 rc = qed_mcp_bist_nvm_get_image_att(p_hwfn, p_ptt,
117 "Failed getting image index %d attributes\n",
122 /* After MFW crash dump is collected - the image's CRC stops
125 if (image_att.image_type == NVM_TYPE_MDUMP)
128 DP_VERBOSE(p_hwfn, QED_MSG_SP, "image index %d, size %x\n",
131 /* Allocate a buffer for holding the nvram image */
132 buf = kzalloc(image_att.len, GFP_KERNEL);
138 /* Read image into buffer */
139 rc = qed_mcp_nvm_read(p_hwfn->cdev, image_att.nvm_start_addr,
143 "Failed reading image index %d from nvm.\n", i);
147 /* Convert the buffer into big-endian format (excluding the
148 * closing 4 bytes of CRC).
150 for (j = 0; j < image_att.len - 4; j += 4) {
151 val = cpu_to_be32(*(u32 *)&buf[j]);
152 *(u32 *)&buf[j] = (__force u32)val;
155 /* Calc CRC for the "actual" image buffer, i.e. not including
156 * the last 4 CRC bytes.
158 nvm_crc = *(u32 *)(buf + image_att.len - 4);
159 calc_crc = crc32(0xffffffff, buf, image_att.len - 4);
160 calc_crc = (__force u32)~cpu_to_be32(calc_crc);
161 DP_VERBOSE(p_hwfn, QED_MSG_SP,
162 "nvm crc 0x%x, calc_crc 0x%x\n", nvm_crc, calc_crc);
164 if (calc_crc != nvm_crc) {
169 /* Done with this image; Free to prevent double release
170 * on subsequent failure.
176 qed_ptt_release(p_hwfn, p_ptt);
182 qed_ptt_release(p_hwfn, p_ptt);