File: daxdev-errors.c

package info (click to toggle)
ndctl 82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,460 kB
  • sloc: ansic: 42,027; sh: 3,974; makefile: 28
file content (363 lines) | stat: -rw-r--r-- 8,205 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2015-2020 Intel Corporation. All rights reserved.
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <linux/fiemap.h>
#include <setjmp.h>
#include <limits.h>

#include <util/log.h>
#include <util/sysfs.h>
#include <daxctl/libdaxctl.h>
#include <ccan/array_size/array_size.h>
#include <ndctl/libndctl.h>
#include <ndctl/ndctl.h>

#define fail() fprintf(stderr, "%s: failed at: %d\n", __func__, __LINE__)

struct check_cmd {
	struct ndctl_cmd *cmd;
	struct ndctl_test *test;
};

static sigjmp_buf sj_env;
static int sig_count;

static const char *NFIT_PROVIDER0 = "nfit_test.0";
static struct check_cmd *check_cmds;

static void sigbus_hdl(int sig, siginfo_t *siginfo, void *ptr)
{
	fprintf(stderr, "** Received a SIGBUS **\n");
	sig_count++;
	siglongjmp(sj_env, 1);
}

static int check_ars_cap(struct ndctl_bus *bus, uint64_t start,
		size_t size, struct check_cmd *check)
{
	struct ndctl_cmd *cmd;
	int rc;

	if (check->cmd != NULL) {
		fprintf(stderr, "%s: expected a NULL command, by default\n",
				__func__);
		return -EINVAL;
	}

	cmd = ndctl_bus_cmd_new_ars_cap(bus, start, size);
	if (!cmd) {
		fprintf(stderr, "%s: bus: %s failed to create cmd\n",
				__func__, ndctl_bus_get_provider(bus));
		return -ENOTTY;
	}

	rc = ndctl_cmd_submit(cmd);
	if (rc < 0) {
		fprintf(stderr, "%s: bus: %s failed to submit cmd: %d\n",
				__func__, ndctl_bus_get_provider(bus), rc);
		ndctl_cmd_unref(cmd);
		return rc;
	}

	if (ndctl_cmd_ars_cap_get_size(cmd) < sizeof(struct nd_cmd_ars_status)){
		fprintf(stderr, "%s: bus: %s expected size >= %zd got: %d\n",
				__func__, ndctl_bus_get_provider(bus),
				sizeof(struct nd_cmd_ars_status),
				ndctl_cmd_ars_cap_get_size(cmd));
		ndctl_cmd_unref(cmd);
		return -ENXIO;
	}

	check->cmd = cmd;
	return 0;
}

static int check_ars_start(struct ndctl_bus *bus, struct check_cmd *check)
{
	struct ndctl_cmd *cmd_ars_cap = check_cmds[ND_CMD_ARS_CAP].cmd;
	struct ndctl_cmd *cmd;
	int rc;

	if (check->cmd != NULL) {
		fprintf(stderr, "%s: expected a NULL command, by default\n",
				__func__);
		return -ENXIO;
	}

	cmd = ndctl_bus_cmd_new_ars_start(cmd_ars_cap, ND_ARS_PERSISTENT);
	if (!cmd) {
		fprintf(stderr, "%s: bus: %s failed to create cmd\n",
				__func__, ndctl_bus_get_provider(bus));
		return -ENOTTY;
	}

	rc = ndctl_cmd_submit(cmd);
	if (rc < 0) {
		fprintf(stderr, "%s: bus: %s failed to submit cmd: %d\n",
				__func__, ndctl_bus_get_provider(bus), rc);
		ndctl_cmd_unref(cmd);
		return rc;
	}

	check->cmd = cmd;
	return 0;
}

static int check_ars_status(struct ndctl_bus *bus, struct check_cmd *check)
{
	struct ndctl_cmd *cmd_ars_cap = check_cmds[ND_CMD_ARS_CAP].cmd;
	struct ndctl_cmd *cmd;
	unsigned long tmo = 5;
	unsigned int i;
	int rc;

	if (check->cmd != NULL) {
		fprintf(stderr, "%s: expected a NULL command, by default\n",
				__func__);
		return -ENXIO;
	}

 retry:
	cmd = ndctl_bus_cmd_new_ars_status(cmd_ars_cap);
	if (!cmd) {
		fprintf(stderr, "%s: bus: %s failed to create cmd\n",
				__func__, ndctl_bus_get_provider(bus));
		return -ENOTTY;
	}

	rc = ndctl_cmd_submit(cmd);
	if (rc < 0) {
		fprintf(stderr, "%s: bus: %s failed to submit cmd: %d\n",
				__func__, ndctl_bus_get_provider(bus), rc);
		ndctl_cmd_unref(cmd);
		return rc;
	}

	if (!tmo) {
		fprintf(stderr, "%s: bus: %s ars timeout\n", __func__,
				ndctl_bus_get_provider(bus));
		return -EIO;
	}

	if (ndctl_cmd_ars_in_progress(cmd)) {
		tmo--;
		sleep(1);
		goto retry;
	}

	for (i = 0; i < ndctl_cmd_ars_num_records(cmd); i++) {
		fprintf(stderr, "%s: record[%d].addr: 0x%llx\n", __func__, i,
				ndctl_cmd_ars_get_record_addr(cmd, i));
		fprintf(stderr, "%s: record[%d].length: 0x%llx\n", __func__, i,
				ndctl_cmd_ars_get_record_len(cmd, i));
	}

	check->cmd = cmd;
	return 0;
}

static int check_clear_error(struct ndctl_bus *bus, struct check_cmd *check)
{
	struct ndctl_cmd *ars_cap = check_cmds[ND_CMD_ARS_CAP].cmd;
	struct ndctl_cmd *clear_err;
	unsigned long long cleared;
	struct ndctl_range range;
	int rc;

	if (check->cmd != NULL) {
		fprintf(stderr, "%s: expected a NULL command, by default\n",
				__func__);
		return -ENXIO;
	}

	if (ndctl_cmd_ars_cap_get_range(ars_cap, &range)) {
		fprintf(stderr, "failed to get ars_cap range\n");
		return -ENXIO;
	}

	fprintf(stderr, "%s: clearing at %#llx for %llu bytes\n",
			__func__, range.address, range.length);

	clear_err = ndctl_bus_cmd_new_clear_error(range.address,
					range.length, ars_cap);
	if (!clear_err) {
		fprintf(stderr, "%s: bus: %s failed to create cmd\n",
				__func__, ndctl_bus_get_provider(bus));
		return -ENOTTY;
	}

	rc = ndctl_cmd_submit(clear_err);
	if (rc < 0) {
		fprintf(stderr, "%s: bus: %s failed to submit cmd: %d\n",
				__func__, ndctl_bus_get_provider(bus), rc);
		ndctl_cmd_unref(clear_err);
		return rc;
	}

	cleared = ndctl_cmd_clear_error_get_cleared(clear_err);
	if (cleared != range.length) {
		fprintf(stderr, "%s: bus: %s expected to clear: %lld actual: %lld\
n",
				__func__, ndctl_bus_get_provider(bus),
				range.length, cleared);
		return -ENXIO;
	}

	check->cmd = clear_err;
	return 0;
}

static struct ndctl_dax * get_dax_region(struct ndctl_region *region)
{
	struct ndctl_dax *dax;

	ndctl_dax_foreach(region, dax)
		if (ndctl_dax_is_enabled(dax) &&
			ndctl_dax_is_configured(dax))
			return dax;

	return NULL;
}

static int test_daxdev_clear_error(const char *bus_name,
		const char *region_name)
{
	int rc = 0, i;
	struct ndctl_ctx *ctx;
	struct ndctl_bus *bus;
	struct ndctl_region *region;
	struct ndctl_dax *dax = NULL;
	uint64_t base, start, offset, blocks, size;
        struct check_cmd __bus_cmds[] = {
                [ND_CMD_ARS_CAP] = {},
                [ND_CMD_ARS_START] = {},
                [ND_CMD_ARS_STATUS] = {},
                [ND_CMD_CLEAR_ERROR] = {},
        };
	char path[256];
	char buf[SYSFS_ATTR_SIZE];
	struct log_ctx log_ctx;

	log_init(&log_ctx, "test/init", "NDCTL_DAXDEV_TEST");
	rc = ndctl_new(&ctx);
	if (rc)
		return rc;

	bus = ndctl_bus_get_by_provider(ctx, NFIT_PROVIDER0);
	if (!bus) {
		rc = -ENODEV;
		goto cleanup;
	}

	ndctl_region_foreach(bus, region) {
		if (strncmp(region_name,
				ndctl_region_get_devname(region), 256) == 0) {
			/* find the dax region */
			dax = get_dax_region(region);
			break;
		}
	}

	if (!dax) {
		rc = -ENODEV;
		goto cleanup;
	}

	/* get badblocks */
	if (snprintf(path, 256,
			"/sys/devices/platform/%s/%s/%s/badblocks",
			NFIT_PROVIDER0,
			bus_name,
			ndctl_region_get_devname(region)) >= 256) {
		fprintf(stderr, "%s: buffer too small!\n",
				ndctl_region_get_devname(region));
		rc = -ENXIO;
		goto cleanup;
	}

	if (__sysfs_read_attr(&log_ctx, path, buf) < 0) {
		rc = -ENXIO;
		goto cleanup;
	}

	/* retrieve badblocks from buf */
	rc = sscanf(buf, "%lu %lu", &offset, &blocks);
	if (rc == EOF) {
		rc = -errno;
		goto cleanup;
	}

	/* get resource base */
	base = ndctl_region_get_resource(region);
	if (base == ULLONG_MAX) {
		rc = -ERANGE;
		goto cleanup;
	}

	check_cmds = __bus_cmds;
	start = base + offset * 512;
	size = 512 * blocks;

	rc = check_ars_cap(bus, start, size, &check_cmds[ND_CMD_ARS_CAP]);
	if (rc < 0)
		goto cleanup;

	rc = check_ars_start(bus, &check_cmds[ND_CMD_ARS_START]);
	if (rc < 0)
		goto cleanup;

	rc = check_ars_status(bus, &check_cmds[ND_CMD_ARS_STATUS]);
	if (rc < 0)
		goto cleanup;

	rc = check_clear_error(bus, &check_cmds[ND_CMD_CLEAR_ERROR]);
	if (rc < 0)
		goto cleanup;

	for (i = 1; i < (int)ARRAY_SIZE(__bus_cmds); i++) {
		if (__bus_cmds[i].cmd) {
			ndctl_cmd_unref(__bus_cmds[i].cmd);
			__bus_cmds[i].cmd = NULL;
                }
	}

cleanup:
	ndctl_unref(ctx);
	return rc;
}


int main(int argc, char *argv[])
{
	int rc;
	struct sigaction act;

	if (argc < 1 || argc > 4)
		return -EINVAL;

	memset(&act, 0, sizeof(act));
	act.sa_sigaction = sigbus_hdl;
	act.sa_flags = SA_SIGINFO;

	if (sigaction(SIGBUS, &act, 0)) {
		fail();
		return 1;
	}

	rc = test_daxdev_clear_error(argv[1], argv[2]);

	return rc;
}