net: qed: convert to SPDX License Identifiers
[linux-2.6-microblaze.git] / drivers / net / ethernet / qlogic / qed / qed_selftest.c
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  */
5
6 #include <linux/crc32.h>
7 #include "qed.h"
8 #include "qed_dev_api.h"
9 #include "qed_mcp.h"
10 #include "qed_sp.h"
11 #include "qed_selftest.h"
12
13 int qed_selftest_memory(struct qed_dev *cdev)
14 {
15         int rc = 0, i;
16
17         for_each_hwfn(cdev, i) {
18                 rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
19                 if (rc)
20                         return rc;
21         }
22
23         return rc;
24 }
25
26 int qed_selftest_interrupt(struct qed_dev *cdev)
27 {
28         int rc = 0, i;
29
30         for_each_hwfn(cdev, i) {
31                 rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
32                 if (rc)
33                         return rc;
34         }
35
36         return rc;
37 }
38
39 int qed_selftest_register(struct qed_dev *cdev)
40 {
41         struct qed_hwfn *p_hwfn;
42         struct qed_ptt *p_ptt;
43         int rc = 0, i;
44
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);
49                 if (!p_ptt) {
50                         DP_ERR(p_hwfn, "failed to acquire ptt\n");
51                         return -EBUSY;
52                 }
53                 rc = qed_mcp_bist_register_test(p_hwfn, p_ptt);
54                 qed_ptt_release(p_hwfn, p_ptt);
55                 if (rc)
56                         break;
57         }
58
59         return rc;
60 }
61
62 int qed_selftest_clock(struct qed_dev *cdev)
63 {
64         struct qed_hwfn *p_hwfn;
65         struct qed_ptt *p_ptt;
66         int rc = 0, i;
67
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);
72                 if (!p_ptt) {
73                         DP_ERR(p_hwfn, "failed to acquire ptt\n");
74                         return -EBUSY;
75                 }
76                 rc = qed_mcp_bist_clock_test(p_hwfn, p_ptt);
77                 qed_ptt_release(p_hwfn, p_ptt);
78                 if (rc)
79                         break;
80         }
81
82         return rc;
83 }
84
85 int qed_selftest_nvram(struct qed_dev *cdev)
86 {
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;
91         u8 *buf = NULL;
92         __be32 val;
93         int rc;
94
95         if (!p_ptt) {
96                 DP_ERR(p_hwfn, "failed to acquire ptt\n");
97                 return -EBUSY;
98         }
99
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");
104                 rc = -EINVAL;
105                 goto err0;
106         }
107
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
111                  * reading it.
112                  */
113                 rc = qed_mcp_bist_nvm_get_image_att(p_hwfn, p_ptt,
114                                                     &image_att, i);
115                 if (rc) {
116                         DP_ERR(p_hwfn,
117                                "Failed getting image index %d attributes\n",
118                                i);
119                         goto err0;
120                 }
121
122                 /* After MFW crash dump is collected - the image's CRC stops
123                  * being valid.
124                  */
125                 if (image_att.image_type == NVM_TYPE_MDUMP)
126                         continue;
127
128                 DP_VERBOSE(p_hwfn, QED_MSG_SP, "image index %d, size %x\n",
129                            i, image_att.len);
130
131                 /* Allocate a buffer for holding the nvram image */
132                 buf = kzalloc(image_att.len, GFP_KERNEL);
133                 if (!buf) {
134                         rc = -ENOMEM;
135                         goto err0;
136                 }
137
138                 /* Read image into buffer */
139                 rc = qed_mcp_nvm_read(p_hwfn->cdev, image_att.nvm_start_addr,
140                                       buf, image_att.len);
141                 if (rc) {
142                         DP_ERR(p_hwfn,
143                                "Failed reading image index %d from nvm.\n", i);
144                         goto err1;
145                 }
146
147                 /* Convert the buffer into big-endian format (excluding the
148                  * closing 4 bytes of CRC).
149                  */
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;
153                 }
154
155                 /* Calc CRC for the "actual" image buffer, i.e. not including
156                  * the last 4 CRC bytes.
157                  */
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);
163
164                 if (calc_crc != nvm_crc) {
165                         rc = -EINVAL;
166                         goto err1;
167                 }
168
169                 /* Done with this image; Free to prevent double release
170                  * on subsequent failure.
171                  */
172                 kfree(buf);
173                 buf = NULL;
174         }
175
176         qed_ptt_release(p_hwfn, p_ptt);
177         return 0;
178
179 err1:
180         kfree(buf);
181 err0:
182         qed_ptt_release(p_hwfn, p_ptt);
183         return rc;
184 }