1 // SPDX-License-Identifier: GPL-2.0
3 * SCLP Store Data support and sysfs interface
5 * Copyright IBM Corp. 2017
8 #define KMSG_COMPONENT "sclp_sd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
21 #include <asm/pgalloc.h>
25 #define SD_EQ_STORE_DATA 0
29 #define SD_DI_CONFIG 3
31 struct sclp_sd_evbuf {
32 struct evbuf_header hdr;
48 struct sccb_header hdr;
49 struct sclp_sd_evbuf evbuf;
50 } __packed __aligned(PAGE_SIZE);
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()
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
71 struct sclp_sd_listener {
72 struct list_head list;
74 struct completion completion;
75 struct sclp_sd_evbuf evbuf;
79 * struct sclp_sd_file - Sysfs representation of a Store Data entity
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
88 struct bin_attribute data_attr;
89 struct mutex data_mutex;
90 struct sclp_sd_data data;
93 #define to_sd_file(x) container_of(x, struct sclp_sd_file, kobj)
95 static struct kset *sclp_sd_kset;
96 static struct sclp_sd_file *config_file;
98 static LIST_HEAD(sclp_sd_queue);
99 static DEFINE_SPINLOCK(sclp_sd_queue_lock);
102 * sclp_sd_listener_add() - Add listener for Store Data responses
103 * @listener: Listener to add
105 static void sclp_sd_listener_add(struct sclp_sd_listener *listener)
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);
113 * sclp_sd_listener_remove() - Remove listener for Store Data responses
114 * @listener: Listener to remove
116 static void sclp_sd_listener_remove(struct sclp_sd_listener *listener)
118 spin_lock_irq(&sclp_sd_queue_lock);
119 list_del(&listener->list);
120 spin_unlock_irq(&sclp_sd_queue_lock);
124 * sclp_sd_listener_init() - Initialize a Store Data response listener
125 * @listener: Response listener to initialize
126 * @id: Event ID to listen for
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.
132 static void sclp_sd_listener_init(struct sclp_sd_listener *listener, u32 id)
134 memset(listener, 0, sizeof(*listener));
136 init_completion(&listener->completion);
140 * sclp_sd_receiver() - Receiver for Store Data events
141 * @evbuf_hdr: Header of received events
143 * Process Store Data events and complete listeners with matching event IDs.
145 static void sclp_sd_receiver(struct evbuf_header *evbuf_hdr)
147 struct sclp_sd_evbuf *evbuf = (struct sclp_sd_evbuf *) evbuf_hdr;
148 struct sclp_sd_listener *listener;
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)
157 listener->evbuf = *evbuf;
158 complete(&listener->completion);
162 spin_unlock(&sclp_sd_queue_lock);
165 pr_debug("unsolicited event (id=0x%08x)\n", evbuf->id);
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,
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
184 * Perform Store Data request with specified parameters and wait for completion.
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.
189 static int sclp_sd_sync(unsigned long page, u8 eq, u8 di, u64 sat, u64 sa,
190 u32 *dsize_ptr, u32 *esize_ptr)
192 struct sclp_sd_sccb *sccb = (void *) page;
193 struct sclp_sd_listener listener;
194 struct sclp_sd_evbuf *evbuf;
197 sclp_sd_listener_init(&listener, __pa(sccb));
198 sclp_sd_listener_add(&listener);
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;
208 evbuf->id = listener.id;
213 evbuf->dsize = *dsize_ptr;
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);
222 /* Evaluate response */
223 if (sccb->hdr.response_code == 0x73f0) {
224 pr_debug("event not supported\n");
228 if (sccb->hdr.response_code != 0x0020 || !(evbuf->hdr.flags & 0x80)) {
232 if (!(evbuf->rflags & 0x80)) {
233 rc = wait_for_completion_interruptible(&listener.completion);
236 evbuf = &listener.evbuf;
238 switch (evbuf->status) {
241 *dsize_ptr = evbuf->dsize;
243 *esize_ptr = evbuf->esize;
244 pr_debug("success (dsize=%u, esize=%u)\n", evbuf->dsize,
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,
266 sclp_sd_listener_remove(&listener);
272 * sclp_sd_store_data() - Obtain data for specified Store Data entity
273 * @result: Resulting data
274 * @di: DI value associated with this entity
276 * Perform a series of Store Data requests to obtain the size and contents of
277 * the specified Store Data entity.
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
285 static int sclp_sd_store_data(struct sclp_sd_data *result, u8 di)
287 u32 dsize = 0, esize = 0;
288 unsigned long page, asce = 0;
292 page = __get_free_page(GFP_KERNEL | GFP_DMA);
297 rc = sclp_sd_sync(page, SD_EQ_SIZE, di, 0, 0, &dsize, &esize);
303 /* Allocate memory */
304 data = vzalloc(array_size((size_t)dsize, PAGE_SIZE));
310 /* Get translation table for buffer */
311 asce = base_asce_alloc((unsigned long) data, dsize);
319 rc = sclp_sd_sync(page, SD_EQ_STORE_DATA, di, asce, (u64) data, &dsize,
322 /* Cancel running request if interrupted */
323 if (rc == -ERESTARTSYS)
324 sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL);
330 result->esize_bytes = (size_t) esize * PAGE_SIZE;
331 result->dsize_bytes = (size_t) dsize * PAGE_SIZE;
335 base_asce_free(asce);
342 * sclp_sd_data_reset() - Reset Store Data result buffer
343 * @data: Data buffer to reset
345 * Reset @data to initial state and release associated memory.
347 static void sclp_sd_data_reset(struct sclp_sd_data *data)
351 data->dsize_bytes = 0;
352 data->esize_bytes = 0;
356 * sclp_sd_file_release() - Release function for sclp_sd_file object
357 * @kobj: Kobject embedded in sclp_sd_file object
359 static void sclp_sd_file_release(struct kobject *kobj)
361 struct sclp_sd_file *sd_file = to_sd_file(kobj);
363 sclp_sd_data_reset(&sd_file->data);
368 * sclp_sd_file_update() - Update contents of sclp_sd_file object
369 * @sd_file: Object to update
371 * Obtain the current version of data associated with the Store Data entity
374 * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the
375 * data may have changed. Return non-zero otherwise.
377 static int sclp_sd_file_update(struct sclp_sd_file *sd_file)
379 const char *name = kobject_name(&sd_file->kobj);
380 struct sclp_sd_data data;
383 rc = sclp_sd_store_data(&data, sd_file->di);
386 pr_info("No data is available for the %s data entity\n",
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);
397 pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes,
399 kobject_uevent(&sd_file->kobj, KOBJ_CHANGE);
405 * sclp_sd_file_update_async() - Wrapper for asynchronous update call
406 * @data: Object to update
409 static void sclp_sd_file_update_async(void *data, async_cookie_t cookie)
411 struct sclp_sd_file *sd_file = data;
413 sclp_sd_file_update(sd_file);
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
423 * Initiate a reload of the data associated with an sclp_sd_file object.
425 static ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr,
426 const char *buf, size_t count)
428 struct sclp_sd_file *sd_file = to_sd_file(kobj);
430 sclp_sd_file_update(sd_file);
435 static struct kobj_attribute reload_attr = __ATTR_WO(reload);
437 static struct attribute *sclp_sd_file_default_attrs[] = {
441 ATTRIBUTE_GROUPS(sclp_sd_file_default);
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,
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
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
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)
466 struct sclp_sd_file *sd_file = to_sd_file(kobj);
470 mutex_lock(&sd_file->data_mutex);
472 data = sd_file->data.data;
473 data_size = sd_file->data.dsize_bytes;
474 if (!data || off >= data_size) {
477 if (off + size > data_size)
478 size = data_size - off;
479 memcpy(buffer, data + off, size);
482 mutex_unlock(&sd_file->data_mutex);
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
492 * Create a sysfs directory with the given @name located under
494 * /sys/firmware/sclp_sd/
496 * The files in this directory can be used to access the contents of the Store
497 * Data entity associated with @DI.
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
503 static __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di)
505 struct sclp_sd_file *sd_file;
508 sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL);
512 mutex_init(&sd_file->data_mutex);
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,
519 kobject_put(&sd_file->kobj);
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;
528 rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr);
530 kobject_put(&sd_file->kobj);
535 * For completeness only - users interested in entity data should listen
536 * for KOBJ_CHANGE instead.
538 kobject_uevent(&sd_file->kobj, KOBJ_ADD);
540 /* Don't let a slow Store Data request delay further initialization */
541 async_schedule(sclp_sd_file_update_async, sd_file);
547 * sclp_sd_init() - Initialize sclp_sd support and register sysfs files
549 static __init int sclp_sd_init(void)
553 rc = sclp_register(&sclp_sd_register);
557 /* Create kset named "sclp_sd" located under /sys/firmware/ */
559 sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj);
564 config_file = sclp_sd_file_create("config", SD_DI_CONFIG);
571 kset_unregister(sclp_sd_kset);
573 sclp_unregister(&sclp_sd_register);
577 device_initcall(sclp_sd_init);