1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Error log support on PowerNV.
4  *
5  * Copyright 2013,2014 IBM Corp.
6  */
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/of.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/fs.h>
14 #include <linux/vmalloc.h>
15 #include <linux/fcntl.h>
16 #include <linux/kobject.h>
17 #include <linux/uaccess.h>
18 #include <asm/opal.h>
19 
20 struct elog_obj {
21 	struct kobject kobj;
22 	struct bin_attribute raw_attr;
23 	uint64_t id;
24 	uint64_t type;
25 	size_t size;
26 	char *buffer;
27 };
28 #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
29 
30 struct elog_attribute {
31 	struct attribute attr;
32 	ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
33 			char *buf);
34 	ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
35 			 const char *buf, size_t count);
36 };
37 #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
38 
39 static ssize_t elog_id_show(struct elog_obj *elog_obj,
40 			    struct elog_attribute *attr,
41 			    char *buf)
42 {
43 	return sprintf(buf, "0x%llx\n", elog_obj->id);
44 }
45 
46 static const char *elog_type_to_string(uint64_t type)
47 {
48 	switch (type) {
49 	case 0: return "PEL";
50 	default: return "unknown";
51 	}
52 }
53 
54 static ssize_t elog_type_show(struct elog_obj *elog_obj,
55 			      struct elog_attribute *attr,
56 			      char *buf)
57 {
58 	return sprintf(buf, "0x%llx %s\n",
59 		       elog_obj->type,
60 		       elog_type_to_string(elog_obj->type));
61 }
62 
63 static ssize_t elog_ack_show(struct elog_obj *elog_obj,
64 			     struct elog_attribute *attr,
65 			     char *buf)
66 {
67 	return sprintf(buf, "ack - acknowledge log message\n");
68 }
69 
70 static ssize_t elog_ack_store(struct elog_obj *elog_obj,
71 			      struct elog_attribute *attr,
72 			      const char *buf,
73 			      size_t count)
74 {
75 	opal_send_ack_elog(elog_obj->id);
76 	sysfs_remove_file_self(&elog_obj->kobj, &attr->attr);
77 	kobject_put(&elog_obj->kobj);
78 	return count;
79 }
80 
81 static struct elog_attribute id_attribute =
82 	__ATTR(id, 0444, elog_id_show, NULL);
83 static struct elog_attribute type_attribute =
84 	__ATTR(type, 0444, elog_type_show, NULL);
85 static struct elog_attribute ack_attribute =
86 	__ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
87 
88 static struct kset *elog_kset;
89 
90 static ssize_t elog_attr_show(struct kobject *kobj,
91 			      struct attribute *attr,
92 			      char *buf)
93 {
94 	struct elog_attribute *attribute;
95 	struct elog_obj *elog;
96 
97 	attribute = to_elog_attr(attr);
98 	elog = to_elog_obj(kobj);
99 
100 	if (!attribute->show)
101 		return -EIO;
102 
103 	return attribute->show(elog, attribute, buf);
104 }
105 
106 static ssize_t elog_attr_store(struct kobject *kobj,
107 			       struct attribute *attr,
108 			       const char *buf, size_t len)
109 {
110 	struct elog_attribute *attribute;
111 	struct elog_obj *elog;
112 
113 	attribute = to_elog_attr(attr);
114 	elog = to_elog_obj(kobj);
115 
116 	if (!attribute->store)
117 		return -EIO;
118 
119 	return attribute->store(elog, attribute, buf, len);
120 }
121 
122 static const struct sysfs_ops elog_sysfs_ops = {
123 	.show = elog_attr_show,
124 	.store = elog_attr_store,
125 };
126 
127 static void elog_release(struct kobject *kobj)
128 {
129 	struct elog_obj *elog;
130 
131 	elog = to_elog_obj(kobj);
132 	kfree(elog->buffer);
133 	kfree(elog);
134 }
135 
136 static struct attribute *elog_default_attrs[] = {
137 	&id_attribute.attr,
138 	&type_attribute.attr,
139 	&ack_attribute.attr,
140 	NULL,
141 };
142 
143 static struct kobj_type elog_ktype = {
144 	.sysfs_ops = &elog_sysfs_ops,
145 	.release = &elog_release,
146 	.default_attrs = elog_default_attrs,
147 };
148 
149 /* Maximum size of a single log on FSP is 16KB */
150 #define OPAL_MAX_ERRLOG_SIZE	16384
151 
152 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
153 			     struct bin_attribute *bin_attr,
154 			     char *buffer, loff_t pos, size_t count)
155 {
156 	int opal_rc;
157 
158 	struct elog_obj *elog = to_elog_obj(kobj);
159 
160 	/* We may have had an error reading before, so let's retry */
161 	if (!elog->buffer) {
162 		elog->buffer = kzalloc(elog->size, GFP_KERNEL);
163 		if (!elog->buffer)
164 			return -EIO;
165 
166 		opal_rc = opal_read_elog(__pa(elog->buffer),
167 					 elog->size, elog->id);
168 		if (opal_rc != OPAL_SUCCESS) {
169 			pr_err("ELOG: log read failed for log-id=%llx\n",
170 			       elog->id);
171 			kfree(elog->buffer);
172 			elog->buffer = NULL;
173 			return -EIO;
174 		}
175 	}
176 
177 	memcpy(buffer, elog->buffer + pos, count);
178 
179 	return count;
180 }
181 
182 static struct elog_obj *create_elog_obj(uint64_t id, size_t size, uint64_t type)
183 {
184 	struct elog_obj *elog;
185 	int rc;
186 
187 	elog = kzalloc(sizeof(*elog), GFP_KERNEL);
188 	if (!elog)
189 		return NULL;
190 
191 	elog->kobj.kset = elog_kset;
192 
193 	kobject_init(&elog->kobj, &elog_ktype);
194 
195 	sysfs_bin_attr_init(&elog->raw_attr);
196 
197 	elog->raw_attr.attr.name = "raw";
198 	elog->raw_attr.attr.mode = 0400;
199 	elog->raw_attr.size = size;
200 	elog->raw_attr.read = raw_attr_read;
201 
202 	elog->id = id;
203 	elog->size = size;
204 	elog->type = type;
205 
206 	elog->buffer = kzalloc(elog->size, GFP_KERNEL);
207 
208 	if (elog->buffer) {
209 		rc = opal_read_elog(__pa(elog->buffer),
210 					 elog->size, elog->id);
211 		if (rc != OPAL_SUCCESS) {
212 			pr_err("ELOG: log read failed for log-id=%llx\n",
213 			       elog->id);
214 			kfree(elog->buffer);
215 			elog->buffer = NULL;
216 		}
217 	}
218 
219 	rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
220 	if (rc) {
221 		kobject_put(&elog->kobj);
222 		return NULL;
223 	}
224 
225 	rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
226 	if (rc) {
227 		kobject_put(&elog->kobj);
228 		return NULL;
229 	}
230 
231 	kobject_uevent(&elog->kobj, KOBJ_ADD);
232 
233 	return elog;
234 }
235 
236 static irqreturn_t elog_event(int irq, void *data)
237 {
238 	__be64 size;
239 	__be64 id;
240 	__be64 type;
241 	uint64_t elog_size;
242 	uint64_t log_id;
243 	uint64_t elog_type;
244 	int rc;
245 	char name[2+16+1];
246 	struct kobject *kobj;
247 
248 	rc = opal_get_elog_size(&id, &size, &type);
249 	if (rc != OPAL_SUCCESS) {
250 		pr_err("ELOG: OPAL log info read failed\n");
251 		return IRQ_HANDLED;
252 	}
253 
254 	elog_size = be64_to_cpu(size);
255 	log_id = be64_to_cpu(id);
256 	elog_type = be64_to_cpu(type);
257 
258 	WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
259 
260 	if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
261 		elog_size  =  OPAL_MAX_ERRLOG_SIZE;
262 
263 	sprintf(name, "0x%llx", log_id);
264 
265 	/* we may get notified twice, let's handle
266 	 * that gracefully and not create two conflicting
267 	 * entries.
268 	 */
269 	kobj = kset_find_obj(elog_kset, name);
270 	if (kobj) {
271 		/* Drop reference added by kset_find_obj() */
272 		kobject_put(kobj);
273 		return IRQ_HANDLED;
274 	}
275 
276 	create_elog_obj(log_id, elog_size, elog_type);
277 
278 	return IRQ_HANDLED;
279 }
280 
281 int __init opal_elog_init(void)
282 {
283 	int rc = 0, irq;
284 
285 	/* ELOG not supported by firmware */
286 	if (!opal_check_token(OPAL_ELOG_READ))
287 		return -1;
288 
289 	elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
290 	if (!elog_kset) {
291 		pr_warn("%s: failed to create elog kset\n", __func__);
292 		return -1;
293 	}
294 
295 	irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
296 	if (!irq) {
297 		pr_err("%s: Can't register OPAL event irq (%d)\n",
298 		       __func__, irq);
299 		return irq;
300 	}
301 
302 	rc = request_threaded_irq(irq, NULL, elog_event,
303 			IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
304 	if (rc) {
305 		pr_err("%s: Can't request OPAL event irq (%d)\n",
306 		       __func__, rc);
307 		return rc;
308 	}
309 
310 	/* We are now ready to pull error logs from opal. */
311 	if (opal_check_token(OPAL_ELOG_RESEND))
312 		opal_resend_pending_logs();
313 
314 	return 0;
315 }
316