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 * @id: Event ID to listen for
127 * Initialize a listener for asynchronous Store Data responses. This listener
128 * can afterwards be used to wait for a specific response and to retrieve
129 * the associated response data.
131 static void sclp_sd_listener_init(struct sclp_sd_listener
*listener
, u32 id
)
133 memset(listener
, 0, sizeof(*listener
));
135 init_completion(&listener
->completion
);
139 * sclp_sd_receiver() - Receiver for Store Data events
140 * @evbuf_hdr: Header of received events
142 * Process Store Data events and complete listeners with matching event IDs.
144 static void sclp_sd_receiver(struct evbuf_header
*evbuf_hdr
)
146 struct sclp_sd_evbuf
*evbuf
= (struct sclp_sd_evbuf
*) evbuf_hdr
;
147 struct sclp_sd_listener
*listener
;
150 pr_debug("received event (id=0x%08x)\n", evbuf
->id
);
151 spin_lock(&sclp_sd_queue_lock
);
152 list_for_each_entry(listener
, &sclp_sd_queue
, list
) {
153 if (listener
->id
!= evbuf
->id
)
156 listener
->evbuf
= *evbuf
;
157 complete(&listener
->completion
);
161 spin_unlock(&sclp_sd_queue_lock
);
164 pr_debug("unsolicited event (id=0x%08x)\n", evbuf
->id
);
167 static struct sclp_register sclp_sd_register
= {
168 .send_mask
= EVTYP_STORE_DATA_MASK
,
169 .receive_mask
= EVTYP_STORE_DATA_MASK
,
170 .receiver_fn
= sclp_sd_receiver
,
174 * sclp_sd_sync() - Perform Store Data request synchronously
175 * @page: Address of work page - must be below 2GB
176 * @eq: Input EQ value
177 * @di: Input DI value
178 * @sat: Input SAT value
179 * @sa: Input SA value used to specify the address of the target buffer
180 * @dsize_ptr: Optional pointer to input and output DSIZE value
181 * @esize_ptr: Optional pointer to output ESIZE value
183 * Perform Store Data request with specified parameters and wait for completion.
185 * Return %0 on success and store resulting DSIZE and ESIZE values in
186 * @dsize_ptr and @esize_ptr (if provided). Return non-zero on error.
188 static int sclp_sd_sync(unsigned long page
, u8 eq
, u8 di
, u64 sat
, u64 sa
,
189 u32
*dsize_ptr
, u32
*esize_ptr
)
191 struct sclp_sd_sccb
*sccb
= (void *) page
;
192 struct sclp_sd_listener listener
;
193 struct sclp_sd_evbuf
*evbuf
;
196 sclp_sd_listener_init(&listener
, (u32
) (addr_t
) sccb
);
197 sclp_sd_listener_add(&listener
);
200 memset(sccb
, 0, PAGE_SIZE
);
201 sccb
->hdr
.length
= sizeof(sccb
->hdr
) + sizeof(sccb
->evbuf
);
202 evbuf
= &sccb
->evbuf
;
203 evbuf
->hdr
.length
= sizeof(*evbuf
);
204 evbuf
->hdr
.type
= EVTYP_STORE_DATA
;
207 evbuf
->id
= listener
.id
;
212 evbuf
->dsize
= *dsize_ptr
;
214 /* Perform command */
215 pr_debug("request (eq=%d, di=%d, id=0x%08x)\n", eq
, di
, listener
.id
);
216 rc
= sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA
, sccb
);
217 pr_debug("request done (rc=%d)\n", rc
);
221 /* Evaluate response */
222 if (sccb
->hdr
.response_code
== 0x73f0) {
223 pr_debug("event not supported\n");
227 if (sccb
->hdr
.response_code
!= 0x0020 || !(evbuf
->hdr
.flags
& 0x80)) {
231 if (!(evbuf
->rflags
& 0x80)) {
232 rc
= wait_for_completion_interruptible(&listener
.completion
);
235 evbuf
= &listener
.evbuf
;
237 switch (evbuf
->status
) {
240 *dsize_ptr
= evbuf
->dsize
;
242 *esize_ptr
= evbuf
->esize
;
243 pr_debug("success (dsize=%u, esize=%u)\n", evbuf
->dsize
,
256 if (rc
&& rc
!= -ENOENT
) {
257 /* Provide some information about what went wrong */
258 pr_warn("Store Data request failed (eq=%d, di=%d, "
259 "response=0x%04x, flags=0x%02x, status=%d, rc=%d)\n",
260 eq
, di
, sccb
->hdr
.response_code
, evbuf
->hdr
.flags
,
265 sclp_sd_listener_remove(&listener
);
271 * sclp_sd_store_data() - Obtain data for specified Store Data entity
272 * @result: Resulting data
273 * @di: DI value associated with this entity
275 * Perform a series of Store Data requests to obtain the size and contents of
276 * the specified Store Data entity.
279 * %0: Success - result is stored in @result. @result->data must be
280 * released using vfree() after use.
281 * %-ENOENT: No data available for this entity
284 static int sclp_sd_store_data(struct sclp_sd_data
*result
, u8 di
)
286 u32 dsize
= 0, esize
= 0;
287 unsigned long page
, asce
= 0;
291 page
= __get_free_page(GFP_KERNEL
| GFP_DMA
);
296 rc
= sclp_sd_sync(page
, SD_EQ_SIZE
, di
, 0, 0, &dsize
, &esize
);
302 /* Allocate memory */
303 data
= vzalloc((size_t) dsize
* PAGE_SIZE
);
309 /* Get translation table for buffer */
310 asce
= base_asce_alloc((unsigned long) data
, dsize
);
318 rc
= sclp_sd_sync(page
, SD_EQ_STORE_DATA
, di
, asce
, (u64
) data
, &dsize
,
321 /* Cancel running request if interrupted */
322 if (rc
== -ERESTARTSYS
)
323 sclp_sd_sync(page
, SD_EQ_HALT
, di
, 0, 0, NULL
, NULL
);
329 result
->esize_bytes
= (size_t) esize
* PAGE_SIZE
;
330 result
->dsize_bytes
= (size_t) dsize
* PAGE_SIZE
;
334 base_asce_free(asce
);
341 * sclp_sd_data_reset() - Reset Store Data result buffer
342 * @data: Data buffer to reset
344 * Reset @data to initial state and release associated memory.
346 static void sclp_sd_data_reset(struct sclp_sd_data
*data
)
350 data
->dsize_bytes
= 0;
351 data
->esize_bytes
= 0;
355 * sclp_sd_file_release() - Release function for sclp_sd_file object
356 * @kobj: Kobject embedded in sclp_sd_file object
358 static void sclp_sd_file_release(struct kobject
*kobj
)
360 struct sclp_sd_file
*sd_file
= to_sd_file(kobj
);
362 sclp_sd_data_reset(&sd_file
->data
);
367 * sclp_sd_file_update() - Update contents of sclp_sd_file object
368 * @sd_file: Object to update
370 * Obtain the current version of data associated with the Store Data entity
373 * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the
374 * data may have changed. Return non-zero otherwise.
376 static int sclp_sd_file_update(struct sclp_sd_file
*sd_file
)
378 const char *name
= kobject_name(&sd_file
->kobj
);
379 struct sclp_sd_data data
;
382 rc
= sclp_sd_store_data(&data
, sd_file
->di
);
385 pr_info("No data is available for the %s data entity\n",
391 mutex_lock(&sd_file
->data_mutex
);
392 sclp_sd_data_reset(&sd_file
->data
);
393 sd_file
->data
= data
;
394 mutex_unlock(&sd_file
->data_mutex
);
396 pr_info("A %zu-byte %s data entity was retrieved\n", data
.dsize_bytes
,
398 kobject_uevent(&sd_file
->kobj
, KOBJ_CHANGE
);
404 * sclp_sd_file_update_async() - Wrapper for asynchronous update call
405 * @data: Object to update
407 static void sclp_sd_file_update_async(void *data
, async_cookie_t cookie
)
409 struct sclp_sd_file
*sd_file
= data
;
411 sclp_sd_file_update(sd_file
);
415 * reload_store() - Store function for "reload" sysfs attribute
416 * @kobj: Kobject of sclp_sd_file object
418 * Initiate a reload of the data associated with an sclp_sd_file object.
420 static ssize_t
reload_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
421 const char *buf
, size_t count
)
423 struct sclp_sd_file
*sd_file
= to_sd_file(kobj
);
425 sclp_sd_file_update(sd_file
);
430 static struct kobj_attribute reload_attr
= __ATTR_WO(reload
);
432 static struct attribute
*sclp_sd_file_default_attrs
[] = {
437 static struct kobj_type sclp_sd_file_ktype
= {
438 .sysfs_ops
= &kobj_sysfs_ops
,
439 .release
= sclp_sd_file_release
,
440 .default_attrs
= sclp_sd_file_default_attrs
,
444 * data_read() - Read function for "read" sysfs attribute
445 * @kobj: Kobject of sclp_sd_file object
446 * @buffer: Target buffer
447 * @off: Requested file offset
448 * @size: Requested number of bytes
450 * Store the requested portion of the Store Data entity contents into the
451 * specified buffer. Return the number of bytes stored on success, or %0
454 static ssize_t
data_read(struct file
*file
, struct kobject
*kobj
,
455 struct bin_attribute
*attr
, char *buffer
,
456 loff_t off
, size_t size
)
458 struct sclp_sd_file
*sd_file
= to_sd_file(kobj
);
462 mutex_lock(&sd_file
->data_mutex
);
464 data
= sd_file
->data
.data
;
465 data_size
= sd_file
->data
.dsize_bytes
;
466 if (!data
|| off
>= data_size
) {
469 if (off
+ size
> data_size
)
470 size
= data_size
- off
;
471 memcpy(buffer
, data
+ off
, size
);
474 mutex_unlock(&sd_file
->data_mutex
);
480 * sclp_sd_file_create() - Add a sysfs file representing a Store Data entity
481 * @name: Name of file
482 * @di: DI value associated with this entity
484 * Create a sysfs directory with the given @name located under
486 * /sys/firmware/sclp_sd/
488 * The files in this directory can be used to access the contents of the Store
489 * Data entity associated with @DI.
491 * Return pointer to resulting sclp_sd_file object on success, %NULL otherwise.
492 * The object must be freed by calling kobject_put() on the embedded kobject
495 static __init
struct sclp_sd_file
*sclp_sd_file_create(const char *name
, u8 di
)
497 struct sclp_sd_file
*sd_file
;
500 sd_file
= kzalloc(sizeof(*sd_file
), GFP_KERNEL
);
504 mutex_init(&sd_file
->data_mutex
);
506 /* Create kobject located under /sys/firmware/sclp_sd/ */
507 sd_file
->kobj
.kset
= sclp_sd_kset
;
508 rc
= kobject_init_and_add(&sd_file
->kobj
, &sclp_sd_file_ktype
, NULL
,
511 kobject_put(&sd_file
->kobj
);
515 sysfs_bin_attr_init(&sd_file
->data_attr
);
516 sd_file
->data_attr
.attr
.name
= "data";
517 sd_file
->data_attr
.attr
.mode
= 0444;
518 sd_file
->data_attr
.read
= data_read
;
520 rc
= sysfs_create_bin_file(&sd_file
->kobj
, &sd_file
->data_attr
);
522 kobject_put(&sd_file
->kobj
);
527 * For completeness only - users interested in entity data should listen
528 * for KOBJ_CHANGE instead.
530 kobject_uevent(&sd_file
->kobj
, KOBJ_ADD
);
532 /* Don't let a slow Store Data request delay further initialization */
533 async_schedule(sclp_sd_file_update_async
, sd_file
);
539 * sclp_sd_init() - Initialize sclp_sd support and register sysfs files
541 static __init
int sclp_sd_init(void)
545 rc
= sclp_register(&sclp_sd_register
);
549 /* Create kset named "sclp_sd" located under /sys/firmware/ */
551 sclp_sd_kset
= kset_create_and_add("sclp_sd", NULL
, firmware_kobj
);
556 config_file
= sclp_sd_file_create("config", SD_DI_CONFIG
);
563 kset_unregister(sclp_sd_kset
);
565 sclp_unregister(&sclp_sd_register
);
569 device_initcall(sclp_sd_init
);