summaryrefslogtreecommitdiffstats
path: root/drivers/edac/ecs.c
blob: 1d51838a60c11117a939586316f05e494bd849a8 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * The generic ECS driver is designed to support control of on-die error
 * check scrub (e.g., DDR5 ECS). The common sysfs ECS interface abstracts
 * the control of various ECS functionalities into a unified set of functions.
 *
 * Copyright (c) 2024-2025 HiSilicon Limited.
 */

#include <linux/edac.h>

#define EDAC_ECS_FRU_NAME "ecs_fru"

enum edac_ecs_attributes {
	ECS_LOG_ENTRY_TYPE,
	ECS_MODE,
	ECS_RESET,
	ECS_THRESHOLD,
	ECS_MAX_ATTRS
};

struct edac_ecs_dev_attr {
	struct device_attribute dev_attr;
	int fru_id;
};

struct edac_ecs_fru_context {
	char name[EDAC_FEAT_NAME_LEN];
	struct edac_ecs_dev_attr dev_attr[ECS_MAX_ATTRS];
	struct attribute *ecs_attrs[ECS_MAX_ATTRS + 1];
	struct attribute_group group;
};

struct edac_ecs_context {
	u16 num_media_frus;
	struct edac_ecs_fru_context *fru_ctxs;
};

#define TO_ECS_DEV_ATTR(_dev_attr)	\
	container_of(_dev_attr, struct edac_ecs_dev_attr, dev_attr)

#define EDAC_ECS_ATTR_SHOW(attrib, cb, type, format)				\
static ssize_t attrib##_show(struct device *ras_feat_dev,			\
			     struct device_attribute *attr, char *buf)		\
{										\
	struct edac_ecs_dev_attr *dev_attr = TO_ECS_DEV_ATTR(attr);		\
	struct edac_dev_feat_ctx *ctx = dev_get_drvdata(ras_feat_dev);		\
	const struct edac_ecs_ops *ops = ctx->ecs.ecs_ops;			\
	type data;								\
	int ret;								\
										\
	ret = ops->cb(ras_feat_dev->parent, ctx->ecs.private,			\
		      dev_attr->fru_id, &data);					\
	if (ret)								\
		return ret;							\
										\
	return sysfs_emit(buf, format, data);					\
}

EDAC_ECS_ATTR_SHOW(log_entry_type, get_log_entry_type, u32, "%u\n")
EDAC_ECS_ATTR_SHOW(mode, get_mode, u32, "%u\n")
EDAC_ECS_ATTR_SHOW(threshold, get_threshold, u32, "%u\n")

#define EDAC_ECS_ATTR_STORE(attrib, cb, type, conv_func)			\
static ssize_t attrib##_store(struct device *ras_feat_dev,			\
			      struct device_attribute *attr,			\
			      const char *buf, size_t len)			\
{										\
	struct edac_ecs_dev_attr *dev_attr = TO_ECS_DEV_ATTR(attr);		\
	struct edac_dev_feat_ctx *ctx = dev_get_drvdata(ras_feat_dev);		\
	const struct edac_ecs_ops *ops = ctx->ecs.ecs_ops;			\
	type data;								\
	int ret;								\
										\
	ret = conv_func(buf, 0, &data);						\
	if (ret < 0)								\
		return ret;							\
										\
	ret = ops->cb(ras_feat_dev->parent, ctx->ecs.private,			\
		      dev_attr->fru_id, data);					\
	if (ret)								\
		return ret;							\
										\
	return len;								\
}

EDAC_ECS_ATTR_STORE(log_entry_type, set_log_entry_type, unsigned long, kstrtoul)
EDAC_ECS_ATTR_STORE(mode, set_mode, unsigned long, kstrtoul)
EDAC_ECS_ATTR_STORE(reset, reset, unsigned long, kstrtoul)
EDAC_ECS_ATTR_STORE(threshold, set_threshold, unsigned long, kstrtoul)

static umode_t ecs_attr_visible(struct kobject *kobj, struct attribute *a, int attr_id)
{
	struct device *ras_feat_dev = kobj_to_dev(kobj);
	struct edac_dev_feat_ctx *ctx = dev_get_drvdata(ras_feat_dev);
	const struct edac_ecs_ops *ops = ctx->ecs.ecs_ops;

	switch (attr_id) {
	case ECS_LOG_ENTRY_TYPE:
		if (ops->get_log_entry_type)  {
			if (ops->set_log_entry_type)
				return a->mode;
			else
				return 0444;
		}
		break;
	case ECS_MODE:
		if (ops->get_mode) {
			if (ops->set_mode)
				return a->mode;
			else
				return 0444;
		}
		break;
	case ECS_RESET:
		if (ops->reset)
			return a->mode;
		break;
	case ECS_THRESHOLD:
		if (ops->get_threshold) {
			if (ops->set_threshold)
				return a->mode;
			else
				return 0444;
		}
		break;
	default:
		break;
	}

	return 0;
}

#define EDAC_ECS_ATTR_RO(_name, _fru_id)       \
	((struct edac_ecs_dev_attr) { .dev_attr = __ATTR_RO(_name), \
				     .fru_id = _fru_id })

#define EDAC_ECS_ATTR_WO(_name, _fru_id)       \
	((struct edac_ecs_dev_attr) { .dev_attr = __ATTR_WO(_name), \
				     .fru_id = _fru_id })

#define EDAC_ECS_ATTR_RW(_name, _fru_id)       \
	((struct edac_ecs_dev_attr) { .dev_attr = __ATTR_RW(_name), \
				     .fru_id = _fru_id })

static int ecs_create_desc(struct device *ecs_dev, const struct attribute_group **attr_groups,
			   u16 num_media_frus)
{
	struct edac_ecs_context *ecs_ctx;
	u32 fru;

	ecs_ctx = devm_kzalloc(ecs_dev, sizeof(*ecs_ctx), GFP_KERNEL);
	if (!ecs_ctx)
		return -ENOMEM;

	ecs_ctx->num_media_frus = num_media_frus;
	ecs_ctx->fru_ctxs = devm_kcalloc(ecs_dev, num_media_frus,
					 sizeof(*ecs_ctx->fru_ctxs),
					 GFP_KERNEL);
	if (!ecs_ctx->fru_ctxs)
		return -ENOMEM;

	for (fru = 0; fru < num_media_frus; fru++) {
		struct edac_ecs_fru_context *fru_ctx = &ecs_ctx->fru_ctxs[fru];
		struct attribute_group *group = &fru_ctx->group;
		int i;

		fru_ctx->dev_attr[ECS_LOG_ENTRY_TYPE]	= EDAC_ECS_ATTR_RW(log_entry_type, fru);
		fru_ctx->dev_attr[ECS_MODE]		= EDAC_ECS_ATTR_RW(mode, fru);
		fru_ctx->dev_attr[ECS_RESET]		= EDAC_ECS_ATTR_WO(reset, fru);
		fru_ctx->dev_attr[ECS_THRESHOLD]	= EDAC_ECS_ATTR_RW(threshold, fru);

		for (i = 0; i < ECS_MAX_ATTRS; i++)
			fru_ctx->ecs_attrs[i] = &fru_ctx->dev_attr[i].dev_attr.attr;

		sprintf(fru_ctx->name, "%s%d", EDAC_ECS_FRU_NAME, fru);
		group->name = fru_ctx->name;
		group->attrs = fru_ctx->ecs_attrs;
		group->is_visible  = ecs_attr_visible;

		attr_groups[fru] = group;
	}

	return 0;
}

/**
 * edac_ecs_get_desc - get EDAC ECS descriptors
 * @ecs_dev: client device, supports ECS feature
 * @attr_groups: pointer to attribute group container
 * @num_media_frus: number of media FRUs in the device
 *
 * Return:
 *  * %0	- Success.
 *  * %-EINVAL	- Invalid parameters passed.
 *  * %-ENOMEM	- Dynamic memory allocation failed.
 */
int edac_ecs_get_desc(struct device *ecs_dev,
		      const struct attribute_group **attr_groups, u16 num_media_frus)
{
	if (!ecs_dev || !attr_groups || !num_media_frus)
		return -EINVAL;

	return ecs_create_desc(ecs_dev, attr_groups, num_media_frus);
}