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