xref: /openbmc/linux/drivers/s390/char/sclp_sd.c (revision 1e8b7fb4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SCLP Store Data support and sysfs interface
4  *
5  * Copyright IBM Corp. 2017
6  */
7 
8 #define KMSG_COMPONENT "sclp_sd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
11 #include <linux/completion.h>
12 #include <linux/kobject.h>
13 #include <linux/list.h>
14 #include <linux/printk.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <linux/async.h>
18 #include <linux/export.h>
19 #include <linux/mutex.h>
20 
21 #include <asm/pgalloc.h>
22 
23 #include "sclp.h"
24 
25 #define SD_EQ_STORE_DATA	0
26 #define SD_EQ_HALT		1
27 #define SD_EQ_SIZE		2
28 
29 #define SD_DI_CONFIG		3
30 
31 struct sclp_sd_evbuf {
32 	struct evbuf_header hdr;
33 	u8 eq;
34 	u8 di;
35 	u8 rflags;
36 	u64 :56;
37 	u32 id;
38 	u16 :16;
39 	u8 fmt;
40 	u8 status;
41 	u64 sat;
42 	u64 sa;
43 	u32 esize;
44 	u32 dsize;
45 } __packed;
46 
47 struct sclp_sd_sccb {
48 	struct sccb_header hdr;
49 	struct sclp_sd_evbuf evbuf;
50 } __packed __aligned(PAGE_SIZE);
51 
52 /**
53  * struct sclp_sd_data - Result of a Store Data request
54  * @esize_bytes: Resulting esize in bytes
55  * @dsize_bytes: Resulting dsize in bytes
56  * @data: Pointer to data - must be released using vfree()
57  */
58 struct sclp_sd_data {
59 	size_t esize_bytes;
60 	size_t dsize_bytes;
61 	void *data;
62 };
63 
64 /**
65  * struct sclp_sd_listener - Listener for asynchronous Store Data response
66  * @list: For enqueueing this struct
67  * @id: Event ID of response to listen for
68  * @completion: Can be used to wait for response
69  * @evbuf: Contains the resulting Store Data response after completion
70  */
71 struct sclp_sd_listener {
72 	struct list_head list;
73 	u32 id;
74 	struct completion completion;
75 	struct sclp_sd_evbuf evbuf;
76 };
77 
78 /**
79  * struct sclp_sd_file - Sysfs representation of a Store Data entity
80  * @kobj: Kobject
81  * @data_attr: Attribute for accessing data contents
82  * @data_mutex: Mutex to serialize access and updates to @data
83  * @data: Data associated with this entity
84  * @di: DI value associated with this entity
85  */
86 struct sclp_sd_file {
87 	struct kobject kobj;
88 	struct bin_attribute data_attr;
89 	struct mutex data_mutex;
90 	struct sclp_sd_data data;
91 	u8 di;
92 };
93 #define to_sd_file(x) container_of(x, struct sclp_sd_file, kobj)
94 
95 static struct kset *sclp_sd_kset;
96 static struct sclp_sd_file *config_file;
97 
98 static LIST_HEAD(sclp_sd_queue);
99 static DEFINE_SPINLOCK(sclp_sd_queue_lock);
100 
101 /**
102  * sclp_sd_listener_add() - Add listener for Store Data responses
103  * @listener: Listener to add
104  */
sclp_sd_listener_add(struct sclp_sd_listener * listener)105 static void sclp_sd_listener_add(struct sclp_sd_listener *listener)
106 {
107 	spin_lock_irq(&sclp_sd_queue_lock);
108 	list_add_tail(&listener->list, &sclp_sd_queue);
109 	spin_unlock_irq(&sclp_sd_queue_lock);
110 }
111 
112 /**
113  * sclp_sd_listener_remove() - Remove listener for Store Data responses
114  * @listener: Listener to remove
115  */
sclp_sd_listener_remove(struct sclp_sd_listener * listener)116 static void sclp_sd_listener_remove(struct sclp_sd_listener *listener)
117 {
118 	spin_lock_irq(&sclp_sd_queue_lock);
119 	list_del(&listener->list);
120 	spin_unlock_irq(&sclp_sd_queue_lock);
121 }
122 
123 /**
124  * sclp_sd_listener_init() - Initialize a Store Data response listener
125  * @listener: Response listener to initialize
126  * @id: Event ID to listen for
127  *
128  * Initialize a listener for asynchronous Store Data responses. This listener
129  * can afterwards be used to wait for a specific response and to retrieve
130  * the associated response data.
131  */
sclp_sd_listener_init(struct sclp_sd_listener * listener,u32 id)132 static void sclp_sd_listener_init(struct sclp_sd_listener *listener, u32 id)
133 {
134 	memset(listener, 0, sizeof(*listener));
135 	listener->id = id;
136 	init_completion(&listener->completion);
137 }
138 
139 /**
140  * sclp_sd_receiver() - Receiver for Store Data events
141  * @evbuf_hdr: Header of received events
142  *
143  * Process Store Data events and complete listeners with matching event IDs.
144  */
sclp_sd_receiver(struct evbuf_header * evbuf_hdr)145 static void sclp_sd_receiver(struct evbuf_header *evbuf_hdr)
146 {
147 	struct sclp_sd_evbuf *evbuf = (struct sclp_sd_evbuf *) evbuf_hdr;
148 	struct sclp_sd_listener *listener;
149 	int found = 0;
150 
151 	pr_debug("received event (id=0x%08x)\n", evbuf->id);
152 	spin_lock(&sclp_sd_queue_lock);
153 	list_for_each_entry(listener, &sclp_sd_queue, list) {
154 		if (listener->id != evbuf->id)
155 			continue;
156 
157 		listener->evbuf = *evbuf;
158 		complete(&listener->completion);
159 		found = 1;
160 		break;
161 	}
162 	spin_unlock(&sclp_sd_queue_lock);
163 
164 	if (!found)
165 		pr_debug("unsolicited event (id=0x%08x)\n", evbuf->id);
166 }
167 
168 static struct sclp_register sclp_sd_register = {
169 	.send_mask = EVTYP_STORE_DATA_MASK,
170 	.receive_mask = EVTYP_STORE_DATA_MASK,
171 	.receiver_fn = sclp_sd_receiver,
172 };
173 
174 /**
175  * sclp_sd_sync() - Perform Store Data request synchronously
176  * @page: Address of work page - must be below 2GB
177  * @eq: Input EQ value
178  * @di: Input DI value
179  * @sat: Input SAT value
180  * @sa: Input SA value used to specify the address of the target buffer
181  * @dsize_ptr: Optional pointer to input and output DSIZE value
182  * @esize_ptr: Optional pointer to output ESIZE value
183  *
184  * Perform Store Data request with specified parameters and wait for completion.
185  *
186  * Return %0 on success and store resulting DSIZE and ESIZE values in
187  * @dsize_ptr and @esize_ptr (if provided). Return non-zero on error.
188  */
sclp_sd_sync(unsigned long page,u8 eq,u8 di,u64 sat,u64 sa,u32 * dsize_ptr,u32 * esize_ptr)189 static int sclp_sd_sync(unsigned long page, u8 eq, u8 di, u64 sat, u64 sa,
190 			u32 *dsize_ptr, u32 *esize_ptr)
191 {
192 	struct sclp_sd_sccb *sccb = (void *) page;
193 	struct sclp_sd_listener listener;
194 	struct sclp_sd_evbuf *evbuf;
195 	int rc;
196 
197 	sclp_sd_listener_init(&listener, __pa(sccb));
198 	sclp_sd_listener_add(&listener);
199 
200 	/* Prepare SCCB */
201 	memset(sccb, 0, PAGE_SIZE);
202 	sccb->hdr.length = sizeof(sccb->hdr) + sizeof(sccb->evbuf);
203 	evbuf = &sccb->evbuf;
204 	evbuf->hdr.length = sizeof(*evbuf);
205 	evbuf->hdr.type = EVTYP_STORE_DATA;
206 	evbuf->eq = eq;
207 	evbuf->di = di;
208 	evbuf->id = listener.id;
209 	evbuf->fmt = 1;
210 	evbuf->sat = sat;
211 	evbuf->sa = sa;
212 	if (dsize_ptr)
213 		evbuf->dsize = *dsize_ptr;
214 
215 	/* Perform command */
216 	pr_debug("request (eq=%d, di=%d, id=0x%08x)\n", eq, di, listener.id);
217 	rc = sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
218 	pr_debug("request done (rc=%d)\n", rc);
219 	if (rc)
220 		goto out;
221 
222 	/* Evaluate response */
223 	if (sccb->hdr.response_code == 0x73f0) {
224 		pr_debug("event not supported\n");
225 		rc = -EIO;
226 		goto out_remove;
227 	}
228 	if (sccb->hdr.response_code != 0x0020 || !(evbuf->hdr.flags & 0x80)) {
229 		rc = -EIO;
230 		goto out;
231 	}
232 	if (!(evbuf->rflags & 0x80)) {
233 		rc = wait_for_completion_interruptible(&listener.completion);
234 		if (rc)
235 			goto out;
236 		evbuf = &listener.evbuf;
237 	}
238 	switch (evbuf->status) {
239 	case 0:
240 		if (dsize_ptr)
241 			*dsize_ptr = evbuf->dsize;
242 		if (esize_ptr)
243 			*esize_ptr = evbuf->esize;
244 		pr_debug("success (dsize=%u, esize=%u)\n", evbuf->dsize,
245 			 evbuf->esize);
246 		break;
247 	case 3:
248 		rc = -ENOENT;
249 		break;
250 	default:
251 		rc = -EIO;
252 		break;
253 
254 	}
255 
256 out:
257 	if (rc && rc != -ENOENT) {
258 		/* Provide some information about what went wrong */
259 		pr_warn("Store Data request failed (eq=%d, di=%d, "
260 			"response=0x%04x, flags=0x%02x, status=%d, rc=%d)\n",
261 			eq, di, sccb->hdr.response_code, evbuf->hdr.flags,
262 			evbuf->status, rc);
263 	}
264 
265 out_remove:
266 	sclp_sd_listener_remove(&listener);
267 
268 	return rc;
269 }
270 
271 /**
272  * sclp_sd_store_data() - Obtain data for specified Store Data entity
273  * @result: Resulting data
274  * @di: DI value associated with this entity
275  *
276  * Perform a series of Store Data requests to obtain the size and contents of
277  * the specified Store Data entity.
278  *
279  * Return:
280  *   %0:       Success - result is stored in @result. @result->data must be
281  *	       released using vfree() after use.
282  *   %-ENOENT: No data available for this entity
283  *   %<0:      Other error
284  */
sclp_sd_store_data(struct sclp_sd_data * result,u8 di)285 static int sclp_sd_store_data(struct sclp_sd_data *result, u8 di)
286 {
287 	u32 dsize = 0, esize = 0;
288 	unsigned long page, asce = 0;
289 	void *data = NULL;
290 	int rc;
291 
292 	page = __get_free_page(GFP_KERNEL | GFP_DMA);
293 	if (!page)
294 		return -ENOMEM;
295 
296 	/* Get size */
297 	rc = sclp_sd_sync(page, SD_EQ_SIZE, di, 0, 0, &dsize, &esize);
298 	if (rc)
299 		goto out;
300 	if (dsize == 0)
301 		goto out_result;
302 
303 	/* Allocate memory */
304 	data = vzalloc(array_size((size_t)dsize, PAGE_SIZE));
305 	if (!data) {
306 		rc = -ENOMEM;
307 		goto out;
308 	}
309 
310 	/* Get translation table for buffer */
311 	asce = base_asce_alloc((unsigned long) data, dsize);
312 	if (!asce) {
313 		vfree(data);
314 		rc = -ENOMEM;
315 		goto out;
316 	}
317 
318 	/* Get data */
319 	rc = sclp_sd_sync(page, SD_EQ_STORE_DATA, di, asce, (u64) data, &dsize,
320 			  &esize);
321 	if (rc) {
322 		/* Cancel running request if interrupted */
323 		if (rc == -ERESTARTSYS) {
324 			if (sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL)) {
325 				pr_warn("Could not stop Store Data request - leaking at least %zu bytes\n",
326 					(size_t)dsize * PAGE_SIZE);
327 				data = NULL;
328 				asce = 0;
329 			}
330 		}
331 		vfree(data);
332 		goto out;
333 	}
334 
335 out_result:
336 	result->esize_bytes = (size_t) esize * PAGE_SIZE;
337 	result->dsize_bytes = (size_t) dsize * PAGE_SIZE;
338 	result->data = data;
339 
340 out:
341 	base_asce_free(asce);
342 	free_page(page);
343 
344 	return rc;
345 }
346 
347 /**
348  * sclp_sd_data_reset() - Reset Store Data result buffer
349  * @data: Data buffer to reset
350  *
351  * Reset @data to initial state and release associated memory.
352  */
sclp_sd_data_reset(struct sclp_sd_data * data)353 static void sclp_sd_data_reset(struct sclp_sd_data *data)
354 {
355 	vfree(data->data);
356 	data->data = NULL;
357 	data->dsize_bytes = 0;
358 	data->esize_bytes = 0;
359 }
360 
361 /**
362  * sclp_sd_file_release() - Release function for sclp_sd_file object
363  * @kobj: Kobject embedded in sclp_sd_file object
364  */
sclp_sd_file_release(struct kobject * kobj)365 static void sclp_sd_file_release(struct kobject *kobj)
366 {
367 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
368 
369 	sclp_sd_data_reset(&sd_file->data);
370 	kfree(sd_file);
371 }
372 
373 /**
374  * sclp_sd_file_update() - Update contents of sclp_sd_file object
375  * @sd_file: Object to update
376  *
377  * Obtain the current version of data associated with the Store Data entity
378  * @sd_file.
379  *
380  * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the
381  * data may have changed. Return non-zero otherwise.
382  */
sclp_sd_file_update(struct sclp_sd_file * sd_file)383 static int sclp_sd_file_update(struct sclp_sd_file *sd_file)
384 {
385 	const char *name = kobject_name(&sd_file->kobj);
386 	struct sclp_sd_data data;
387 	int rc;
388 
389 	rc = sclp_sd_store_data(&data, sd_file->di);
390 	if (rc) {
391 		if (rc == -ENOENT) {
392 			pr_info("No data is available for the %s data entity\n",
393 				 name);
394 		}
395 		return rc;
396 	}
397 
398 	mutex_lock(&sd_file->data_mutex);
399 	sclp_sd_data_reset(&sd_file->data);
400 	sd_file->data = data;
401 	mutex_unlock(&sd_file->data_mutex);
402 
403 	pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes,
404 		name);
405 	kobject_uevent(&sd_file->kobj, KOBJ_CHANGE);
406 
407 	return 0;
408 }
409 
410 /**
411  * sclp_sd_file_update_async() - Wrapper for asynchronous update call
412  * @data: Object to update
413  * @cookie: Unused
414  */
sclp_sd_file_update_async(void * data,async_cookie_t cookie)415 static void sclp_sd_file_update_async(void *data, async_cookie_t cookie)
416 {
417 	struct sclp_sd_file *sd_file = data;
418 
419 	sclp_sd_file_update(sd_file);
420 }
421 
422 /**
423  * reload_store() - Store function for "reload" sysfs attribute
424  * @kobj: Kobject of sclp_sd_file object
425  * @attr: Reload attribute
426  * @buf: Data written to sysfs attribute
427  * @count: Count of bytes written
428  *
429  * Initiate a reload of the data associated with an sclp_sd_file object.
430  */
reload_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)431 static ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr,
432 			    const char *buf, size_t count)
433 {
434 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
435 
436 	sclp_sd_file_update(sd_file);
437 
438 	return count;
439 }
440 
441 static struct kobj_attribute reload_attr = __ATTR_WO(reload);
442 
443 static struct attribute *sclp_sd_file_default_attrs[] = {
444 	&reload_attr.attr,
445 	NULL,
446 };
447 ATTRIBUTE_GROUPS(sclp_sd_file_default);
448 
449 static struct kobj_type sclp_sd_file_ktype = {
450 	.sysfs_ops = &kobj_sysfs_ops,
451 	.release = sclp_sd_file_release,
452 	.default_groups = sclp_sd_file_default_groups,
453 };
454 
455 /**
456  * data_read() - Read function for "data" sysfs attribute
457  * @file: Open file pointer
458  * @kobj: Kobject of sclp_sd_file object
459  * @attr: Data attribute
460  * @buffer: Target buffer
461  * @off: Requested file offset
462  * @size: Requested number of bytes
463  *
464  * Store the requested portion of the Store Data entity contents into the
465  * specified buffer. Return the number of bytes stored on success, or %0
466  * on EOF.
467  */
data_read(struct file * file,struct kobject * kobj,struct bin_attribute * attr,char * buffer,loff_t off,size_t size)468 static ssize_t data_read(struct file *file, struct kobject *kobj,
469 			 struct bin_attribute *attr, char *buffer,
470 			 loff_t off, size_t size)
471 {
472 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
473 	size_t data_size;
474 	char *data;
475 
476 	mutex_lock(&sd_file->data_mutex);
477 
478 	data = sd_file->data.data;
479 	data_size = sd_file->data.dsize_bytes;
480 	if (!data || off >= data_size) {
481 		size = 0;
482 	} else {
483 		if (off + size > data_size)
484 			size = data_size - off;
485 		memcpy(buffer, data + off, size);
486 	}
487 
488 	mutex_unlock(&sd_file->data_mutex);
489 
490 	return size;
491 }
492 
493 /**
494  * sclp_sd_file_create() - Add a sysfs file representing a Store Data entity
495  * @name: Name of file
496  * @di: DI value associated with this entity
497  *
498  * Create a sysfs directory with the given @name located under
499  *
500  *   /sys/firmware/sclp_sd/
501  *
502  * The files in this directory can be used to access the contents of the Store
503  * Data entity associated with @DI.
504  *
505  * Return pointer to resulting sclp_sd_file object on success, %NULL otherwise.
506  * The object must be freed by calling kobject_put() on the embedded kobject
507  * pointer after use.
508  */
sclp_sd_file_create(const char * name,u8 di)509 static __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di)
510 {
511 	struct sclp_sd_file *sd_file;
512 	int rc;
513 
514 	sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL);
515 	if (!sd_file)
516 		return NULL;
517 	sd_file->di = di;
518 	mutex_init(&sd_file->data_mutex);
519 
520 	/* Create kobject located under /sys/firmware/sclp_sd/ */
521 	sd_file->kobj.kset = sclp_sd_kset;
522 	rc = kobject_init_and_add(&sd_file->kobj, &sclp_sd_file_ktype, NULL,
523 				  "%s", name);
524 	if (rc) {
525 		kobject_put(&sd_file->kobj);
526 		return NULL;
527 	}
528 
529 	sysfs_bin_attr_init(&sd_file->data_attr);
530 	sd_file->data_attr.attr.name = "data";
531 	sd_file->data_attr.attr.mode = 0444;
532 	sd_file->data_attr.read = data_read;
533 
534 	rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr);
535 	if (rc) {
536 		kobject_put(&sd_file->kobj);
537 		return NULL;
538 	}
539 
540 	/*
541 	 * For completeness only - users interested in entity data should listen
542 	 * for KOBJ_CHANGE instead.
543 	 */
544 	kobject_uevent(&sd_file->kobj, KOBJ_ADD);
545 
546 	/* Don't let a slow Store Data request delay further initialization */
547 	async_schedule(sclp_sd_file_update_async, sd_file);
548 
549 	return sd_file;
550 }
551 
552 /**
553  * sclp_sd_init() - Initialize sclp_sd support and register sysfs files
554  */
sclp_sd_init(void)555 static __init int sclp_sd_init(void)
556 {
557 	int rc;
558 
559 	rc = sclp_register(&sclp_sd_register);
560 	if (rc)
561 		return rc;
562 
563 	/* Create kset named "sclp_sd" located under /sys/firmware/ */
564 	rc = -ENOMEM;
565 	sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj);
566 	if (!sclp_sd_kset)
567 		goto err_kset;
568 
569 	rc = -EINVAL;
570 	config_file = sclp_sd_file_create("config", SD_DI_CONFIG);
571 	if (!config_file)
572 		goto err_config;
573 
574 	return 0;
575 
576 err_config:
577 	kset_unregister(sclp_sd_kset);
578 err_kset:
579 	sclp_unregister(&sclp_sd_register);
580 
581 	return rc;
582 }
583 device_initcall(sclp_sd_init);
584