xref: /linux/drivers/s390/char/sclp_sd.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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  */
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  */
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  */
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  */
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  */
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  */
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 			sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL);
325 		vfree(data);
326 		goto out;
327 	}
328 
329 out_result:
330 	result->esize_bytes = (size_t) esize * PAGE_SIZE;
331 	result->dsize_bytes = (size_t) dsize * PAGE_SIZE;
332 	result->data = data;
333 
334 out:
335 	base_asce_free(asce);
336 	free_page(page);
337 
338 	return rc;
339 }
340 
341 /**
342  * sclp_sd_data_reset() - Reset Store Data result buffer
343  * @data: Data buffer to reset
344  *
345  * Reset @data to initial state and release associated memory.
346  */
347 static void sclp_sd_data_reset(struct sclp_sd_data *data)
348 {
349 	vfree(data->data);
350 	data->data = NULL;
351 	data->dsize_bytes = 0;
352 	data->esize_bytes = 0;
353 }
354 
355 /**
356  * sclp_sd_file_release() - Release function for sclp_sd_file object
357  * @kobj: Kobject embedded in sclp_sd_file object
358  */
359 static void sclp_sd_file_release(struct kobject *kobj)
360 {
361 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
362 
363 	sclp_sd_data_reset(&sd_file->data);
364 	kfree(sd_file);
365 }
366 
367 /**
368  * sclp_sd_file_update() - Update contents of sclp_sd_file object
369  * @sd_file: Object to update
370  *
371  * Obtain the current version of data associated with the Store Data entity
372  * @sd_file.
373  *
374  * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the
375  * data may have changed. Return non-zero otherwise.
376  */
377 static int sclp_sd_file_update(struct sclp_sd_file *sd_file)
378 {
379 	const char *name = kobject_name(&sd_file->kobj);
380 	struct sclp_sd_data data;
381 	int rc;
382 
383 	rc = sclp_sd_store_data(&data, sd_file->di);
384 	if (rc) {
385 		if (rc == -ENOENT) {
386 			pr_info("No data is available for the %s data entity\n",
387 				 name);
388 		}
389 		return rc;
390 	}
391 
392 	mutex_lock(&sd_file->data_mutex);
393 	sclp_sd_data_reset(&sd_file->data);
394 	sd_file->data = data;
395 	mutex_unlock(&sd_file->data_mutex);
396 
397 	pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes,
398 		name);
399 	kobject_uevent(&sd_file->kobj, KOBJ_CHANGE);
400 
401 	return 0;
402 }
403 
404 /**
405  * sclp_sd_file_update_async() - Wrapper for asynchronous update call
406  * @data: Object to update
407  * @cookie: Unused
408  */
409 static void sclp_sd_file_update_async(void *data, async_cookie_t cookie)
410 {
411 	struct sclp_sd_file *sd_file = data;
412 
413 	sclp_sd_file_update(sd_file);
414 }
415 
416 /**
417  * reload_store() - Store function for "reload" sysfs attribute
418  * @kobj: Kobject of sclp_sd_file object
419  * @attr: Reload attribute
420  * @buf: Data written to sysfs attribute
421  * @count: Count of bytes written
422  *
423  * Initiate a reload of the data associated with an sclp_sd_file object.
424  */
425 static ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr,
426 			    const char *buf, size_t count)
427 {
428 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
429 
430 	sclp_sd_file_update(sd_file);
431 
432 	return count;
433 }
434 
435 static struct kobj_attribute reload_attr = __ATTR_WO(reload);
436 
437 static struct attribute *sclp_sd_file_default_attrs[] = {
438 	&reload_attr.attr,
439 	NULL,
440 };
441 ATTRIBUTE_GROUPS(sclp_sd_file_default);
442 
443 static struct kobj_type sclp_sd_file_ktype = {
444 	.sysfs_ops = &kobj_sysfs_ops,
445 	.release = sclp_sd_file_release,
446 	.default_groups = sclp_sd_file_default_groups,
447 };
448 
449 /**
450  * data_read() - Read function for "data" sysfs attribute
451  * @file: Open file pointer
452  * @kobj: Kobject of sclp_sd_file object
453  * @attr: Data attribute
454  * @buffer: Target buffer
455  * @off: Requested file offset
456  * @size: Requested number of bytes
457  *
458  * Store the requested portion of the Store Data entity contents into the
459  * specified buffer. Return the number of bytes stored on success, or %0
460  * on EOF.
461  */
462 static ssize_t data_read(struct file *file, struct kobject *kobj,
463 			 struct bin_attribute *attr, char *buffer,
464 			 loff_t off, size_t size)
465 {
466 	struct sclp_sd_file *sd_file = to_sd_file(kobj);
467 	size_t data_size;
468 	char *data;
469 
470 	mutex_lock(&sd_file->data_mutex);
471 
472 	data = sd_file->data.data;
473 	data_size = sd_file->data.dsize_bytes;
474 	if (!data || off >= data_size) {
475 		size = 0;
476 	} else {
477 		if (off + size > data_size)
478 			size = data_size - off;
479 		memcpy(buffer, data + off, size);
480 	}
481 
482 	mutex_unlock(&sd_file->data_mutex);
483 
484 	return size;
485 }
486 
487 /**
488  * sclp_sd_file_create() - Add a sysfs file representing a Store Data entity
489  * @name: Name of file
490  * @di: DI value associated with this entity
491  *
492  * Create a sysfs directory with the given @name located under
493  *
494  *   /sys/firmware/sclp_sd/
495  *
496  * The files in this directory can be used to access the contents of the Store
497  * Data entity associated with @DI.
498  *
499  * Return pointer to resulting sclp_sd_file object on success, %NULL otherwise.
500  * The object must be freed by calling kobject_put() on the embedded kobject
501  * pointer after use.
502  */
503 static __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di)
504 {
505 	struct sclp_sd_file *sd_file;
506 	int rc;
507 
508 	sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL);
509 	if (!sd_file)
510 		return NULL;
511 	sd_file->di = di;
512 	mutex_init(&sd_file->data_mutex);
513 
514 	/* Create kobject located under /sys/firmware/sclp_sd/ */
515 	sd_file->kobj.kset = sclp_sd_kset;
516 	rc = kobject_init_and_add(&sd_file->kobj, &sclp_sd_file_ktype, NULL,
517 				  "%s", name);
518 	if (rc) {
519 		kobject_put(&sd_file->kobj);
520 		return NULL;
521 	}
522 
523 	sysfs_bin_attr_init(&sd_file->data_attr);
524 	sd_file->data_attr.attr.name = "data";
525 	sd_file->data_attr.attr.mode = 0444;
526 	sd_file->data_attr.read = data_read;
527 
528 	rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr);
529 	if (rc) {
530 		kobject_put(&sd_file->kobj);
531 		return NULL;
532 	}
533 
534 	/*
535 	 * For completeness only - users interested in entity data should listen
536 	 * for KOBJ_CHANGE instead.
537 	 */
538 	kobject_uevent(&sd_file->kobj, KOBJ_ADD);
539 
540 	/* Don't let a slow Store Data request delay further initialization */
541 	async_schedule(sclp_sd_file_update_async, sd_file);
542 
543 	return sd_file;
544 }
545 
546 /**
547  * sclp_sd_init() - Initialize sclp_sd support and register sysfs files
548  */
549 static __init int sclp_sd_init(void)
550 {
551 	int rc;
552 
553 	rc = sclp_register(&sclp_sd_register);
554 	if (rc)
555 		return rc;
556 
557 	/* Create kset named "sclp_sd" located under /sys/firmware/ */
558 	rc = -ENOMEM;
559 	sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj);
560 	if (!sclp_sd_kset)
561 		goto err_kset;
562 
563 	rc = -EINVAL;
564 	config_file = sclp_sd_file_create("config", SD_DI_CONFIG);
565 	if (!config_file)
566 		goto err_config;
567 
568 	return 0;
569 
570 err_config:
571 	kset_unregister(sclp_sd_kset);
572 err_kset:
573 	sclp_unregister(&sclp_sd_register);
574 
575 	return rc;
576 }
577 device_initcall(sclp_sd_init);
578