18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SCLP Store Data support and sysfs interface 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2017 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define KMSG_COMPONENT "sclp_sd" 98c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/completion.h> 128c2ecf20Sopenharmony_ci#include <linux/kobject.h> 138c2ecf20Sopenharmony_ci#include <linux/list.h> 148c2ecf20Sopenharmony_ci#include <linux/printk.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 178c2ecf20Sopenharmony_ci#include <linux/async.h> 188c2ecf20Sopenharmony_ci#include <linux/export.h> 198c2ecf20Sopenharmony_ci#include <linux/mutex.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <asm/pgalloc.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include "sclp.h" 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define SD_EQ_STORE_DATA 0 268c2ecf20Sopenharmony_ci#define SD_EQ_HALT 1 278c2ecf20Sopenharmony_ci#define SD_EQ_SIZE 2 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define SD_DI_CONFIG 3 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct sclp_sd_evbuf { 328c2ecf20Sopenharmony_ci struct evbuf_header hdr; 338c2ecf20Sopenharmony_ci u8 eq; 348c2ecf20Sopenharmony_ci u8 di; 358c2ecf20Sopenharmony_ci u8 rflags; 368c2ecf20Sopenharmony_ci u64 :56; 378c2ecf20Sopenharmony_ci u32 id; 388c2ecf20Sopenharmony_ci u16 :16; 398c2ecf20Sopenharmony_ci u8 fmt; 408c2ecf20Sopenharmony_ci u8 status; 418c2ecf20Sopenharmony_ci u64 sat; 428c2ecf20Sopenharmony_ci u64 sa; 438c2ecf20Sopenharmony_ci u32 esize; 448c2ecf20Sopenharmony_ci u32 dsize; 458c2ecf20Sopenharmony_ci} __packed; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistruct sclp_sd_sccb { 488c2ecf20Sopenharmony_ci struct sccb_header hdr; 498c2ecf20Sopenharmony_ci struct sclp_sd_evbuf evbuf; 508c2ecf20Sopenharmony_ci} __packed __aligned(PAGE_SIZE); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/** 538c2ecf20Sopenharmony_ci * struct sclp_sd_data - Result of a Store Data request 548c2ecf20Sopenharmony_ci * @esize_bytes: Resulting esize in bytes 558c2ecf20Sopenharmony_ci * @dsize_bytes: Resulting dsize in bytes 568c2ecf20Sopenharmony_ci * @data: Pointer to data - must be released using vfree() 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_cistruct sclp_sd_data { 598c2ecf20Sopenharmony_ci size_t esize_bytes; 608c2ecf20Sopenharmony_ci size_t dsize_bytes; 618c2ecf20Sopenharmony_ci void *data; 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/** 658c2ecf20Sopenharmony_ci * struct sclp_sd_listener - Listener for asynchronous Store Data response 668c2ecf20Sopenharmony_ci * @list: For enqueueing this struct 678c2ecf20Sopenharmony_ci * @id: Event ID of response to listen for 688c2ecf20Sopenharmony_ci * @completion: Can be used to wait for response 698c2ecf20Sopenharmony_ci * @evbuf: Contains the resulting Store Data response after completion 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_cistruct sclp_sd_listener { 728c2ecf20Sopenharmony_ci struct list_head list; 738c2ecf20Sopenharmony_ci u32 id; 748c2ecf20Sopenharmony_ci struct completion completion; 758c2ecf20Sopenharmony_ci struct sclp_sd_evbuf evbuf; 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/** 798c2ecf20Sopenharmony_ci * struct sclp_sd_file - Sysfs representation of a Store Data entity 808c2ecf20Sopenharmony_ci * @kobj: Kobject 818c2ecf20Sopenharmony_ci * @data_attr: Attribute for accessing data contents 828c2ecf20Sopenharmony_ci * @data_mutex: Mutex to serialize access and updates to @data 838c2ecf20Sopenharmony_ci * @data: Data associated with this entity 848c2ecf20Sopenharmony_ci * @di: DI value associated with this entity 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_cistruct sclp_sd_file { 878c2ecf20Sopenharmony_ci struct kobject kobj; 888c2ecf20Sopenharmony_ci struct bin_attribute data_attr; 898c2ecf20Sopenharmony_ci struct mutex data_mutex; 908c2ecf20Sopenharmony_ci struct sclp_sd_data data; 918c2ecf20Sopenharmony_ci u8 di; 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ci#define to_sd_file(x) container_of(x, struct sclp_sd_file, kobj) 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic struct kset *sclp_sd_kset; 968c2ecf20Sopenharmony_cistatic struct sclp_sd_file *config_file; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic LIST_HEAD(sclp_sd_queue); 998c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(sclp_sd_queue_lock); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/** 1028c2ecf20Sopenharmony_ci * sclp_sd_listener_add() - Add listener for Store Data responses 1038c2ecf20Sopenharmony_ci * @listener: Listener to add 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_cistatic void sclp_sd_listener_add(struct sclp_sd_listener *listener) 1068c2ecf20Sopenharmony_ci{ 1078c2ecf20Sopenharmony_ci spin_lock_irq(&sclp_sd_queue_lock); 1088c2ecf20Sopenharmony_ci list_add_tail(&listener->list, &sclp_sd_queue); 1098c2ecf20Sopenharmony_ci spin_unlock_irq(&sclp_sd_queue_lock); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/** 1138c2ecf20Sopenharmony_ci * sclp_sd_listener_remove() - Remove listener for Store Data responses 1148c2ecf20Sopenharmony_ci * @listener: Listener to remove 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_cistatic void sclp_sd_listener_remove(struct sclp_sd_listener *listener) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci spin_lock_irq(&sclp_sd_queue_lock); 1198c2ecf20Sopenharmony_ci list_del(&listener->list); 1208c2ecf20Sopenharmony_ci spin_unlock_irq(&sclp_sd_queue_lock); 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/** 1248c2ecf20Sopenharmony_ci * sclp_sd_listener_init() - Initialize a Store Data response listener 1258c2ecf20Sopenharmony_ci * @id: Event ID to listen for 1268c2ecf20Sopenharmony_ci * 1278c2ecf20Sopenharmony_ci * Initialize a listener for asynchronous Store Data responses. This listener 1288c2ecf20Sopenharmony_ci * can afterwards be used to wait for a specific response and to retrieve 1298c2ecf20Sopenharmony_ci * the associated response data. 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_cistatic void sclp_sd_listener_init(struct sclp_sd_listener *listener, u32 id) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci memset(listener, 0, sizeof(*listener)); 1348c2ecf20Sopenharmony_ci listener->id = id; 1358c2ecf20Sopenharmony_ci init_completion(&listener->completion); 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci/** 1398c2ecf20Sopenharmony_ci * sclp_sd_receiver() - Receiver for Store Data events 1408c2ecf20Sopenharmony_ci * @evbuf_hdr: Header of received events 1418c2ecf20Sopenharmony_ci * 1428c2ecf20Sopenharmony_ci * Process Store Data events and complete listeners with matching event IDs. 1438c2ecf20Sopenharmony_ci */ 1448c2ecf20Sopenharmony_cistatic void sclp_sd_receiver(struct evbuf_header *evbuf_hdr) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct sclp_sd_evbuf *evbuf = (struct sclp_sd_evbuf *) evbuf_hdr; 1478c2ecf20Sopenharmony_ci struct sclp_sd_listener *listener; 1488c2ecf20Sopenharmony_ci int found = 0; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci pr_debug("received event (id=0x%08x)\n", evbuf->id); 1518c2ecf20Sopenharmony_ci spin_lock(&sclp_sd_queue_lock); 1528c2ecf20Sopenharmony_ci list_for_each_entry(listener, &sclp_sd_queue, list) { 1538c2ecf20Sopenharmony_ci if (listener->id != evbuf->id) 1548c2ecf20Sopenharmony_ci continue; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci listener->evbuf = *evbuf; 1578c2ecf20Sopenharmony_ci complete(&listener->completion); 1588c2ecf20Sopenharmony_ci found = 1; 1598c2ecf20Sopenharmony_ci break; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci spin_unlock(&sclp_sd_queue_lock); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (!found) 1648c2ecf20Sopenharmony_ci pr_debug("unsolicited event (id=0x%08x)\n", evbuf->id); 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic struct sclp_register sclp_sd_register = { 1688c2ecf20Sopenharmony_ci .send_mask = EVTYP_STORE_DATA_MASK, 1698c2ecf20Sopenharmony_ci .receive_mask = EVTYP_STORE_DATA_MASK, 1708c2ecf20Sopenharmony_ci .receiver_fn = sclp_sd_receiver, 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** 1748c2ecf20Sopenharmony_ci * sclp_sd_sync() - Perform Store Data request synchronously 1758c2ecf20Sopenharmony_ci * @page: Address of work page - must be below 2GB 1768c2ecf20Sopenharmony_ci * @eq: Input EQ value 1778c2ecf20Sopenharmony_ci * @di: Input DI value 1788c2ecf20Sopenharmony_ci * @sat: Input SAT value 1798c2ecf20Sopenharmony_ci * @sa: Input SA value used to specify the address of the target buffer 1808c2ecf20Sopenharmony_ci * @dsize_ptr: Optional pointer to input and output DSIZE value 1818c2ecf20Sopenharmony_ci * @esize_ptr: Optional pointer to output ESIZE value 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci * Perform Store Data request with specified parameters and wait for completion. 1848c2ecf20Sopenharmony_ci * 1858c2ecf20Sopenharmony_ci * Return %0 on success and store resulting DSIZE and ESIZE values in 1868c2ecf20Sopenharmony_ci * @dsize_ptr and @esize_ptr (if provided). Return non-zero on error. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_cistatic int sclp_sd_sync(unsigned long page, u8 eq, u8 di, u64 sat, u64 sa, 1898c2ecf20Sopenharmony_ci u32 *dsize_ptr, u32 *esize_ptr) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci struct sclp_sd_sccb *sccb = (void *) page; 1928c2ecf20Sopenharmony_ci struct sclp_sd_listener listener; 1938c2ecf20Sopenharmony_ci struct sclp_sd_evbuf *evbuf; 1948c2ecf20Sopenharmony_ci int rc; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci sclp_sd_listener_init(&listener, (u32) (addr_t) sccb); 1978c2ecf20Sopenharmony_ci sclp_sd_listener_add(&listener); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* Prepare SCCB */ 2008c2ecf20Sopenharmony_ci memset(sccb, 0, PAGE_SIZE); 2018c2ecf20Sopenharmony_ci sccb->hdr.length = sizeof(sccb->hdr) + sizeof(sccb->evbuf); 2028c2ecf20Sopenharmony_ci evbuf = &sccb->evbuf; 2038c2ecf20Sopenharmony_ci evbuf->hdr.length = sizeof(*evbuf); 2048c2ecf20Sopenharmony_ci evbuf->hdr.type = EVTYP_STORE_DATA; 2058c2ecf20Sopenharmony_ci evbuf->eq = eq; 2068c2ecf20Sopenharmony_ci evbuf->di = di; 2078c2ecf20Sopenharmony_ci evbuf->id = listener.id; 2088c2ecf20Sopenharmony_ci evbuf->fmt = 1; 2098c2ecf20Sopenharmony_ci evbuf->sat = sat; 2108c2ecf20Sopenharmony_ci evbuf->sa = sa; 2118c2ecf20Sopenharmony_ci if (dsize_ptr) 2128c2ecf20Sopenharmony_ci evbuf->dsize = *dsize_ptr; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* Perform command */ 2158c2ecf20Sopenharmony_ci pr_debug("request (eq=%d, di=%d, id=0x%08x)\n", eq, di, listener.id); 2168c2ecf20Sopenharmony_ci rc = sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA, sccb); 2178c2ecf20Sopenharmony_ci pr_debug("request done (rc=%d)\n", rc); 2188c2ecf20Sopenharmony_ci if (rc) 2198c2ecf20Sopenharmony_ci goto out; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* Evaluate response */ 2228c2ecf20Sopenharmony_ci if (sccb->hdr.response_code == 0x73f0) { 2238c2ecf20Sopenharmony_ci pr_debug("event not supported\n"); 2248c2ecf20Sopenharmony_ci rc = -EIO; 2258c2ecf20Sopenharmony_ci goto out_remove; 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci if (sccb->hdr.response_code != 0x0020 || !(evbuf->hdr.flags & 0x80)) { 2288c2ecf20Sopenharmony_ci rc = -EIO; 2298c2ecf20Sopenharmony_ci goto out; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci if (!(evbuf->rflags & 0x80)) { 2328c2ecf20Sopenharmony_ci rc = wait_for_completion_interruptible(&listener.completion); 2338c2ecf20Sopenharmony_ci if (rc) 2348c2ecf20Sopenharmony_ci goto out; 2358c2ecf20Sopenharmony_ci evbuf = &listener.evbuf; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci switch (evbuf->status) { 2388c2ecf20Sopenharmony_ci case 0: 2398c2ecf20Sopenharmony_ci if (dsize_ptr) 2408c2ecf20Sopenharmony_ci *dsize_ptr = evbuf->dsize; 2418c2ecf20Sopenharmony_ci if (esize_ptr) 2428c2ecf20Sopenharmony_ci *esize_ptr = evbuf->esize; 2438c2ecf20Sopenharmony_ci pr_debug("success (dsize=%u, esize=%u)\n", evbuf->dsize, 2448c2ecf20Sopenharmony_ci evbuf->esize); 2458c2ecf20Sopenharmony_ci break; 2468c2ecf20Sopenharmony_ci case 3: 2478c2ecf20Sopenharmony_ci rc = -ENOENT; 2488c2ecf20Sopenharmony_ci break; 2498c2ecf20Sopenharmony_ci default: 2508c2ecf20Sopenharmony_ci rc = -EIO; 2518c2ecf20Sopenharmony_ci break; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ciout: 2568c2ecf20Sopenharmony_ci if (rc && rc != -ENOENT) { 2578c2ecf20Sopenharmony_ci /* Provide some information about what went wrong */ 2588c2ecf20Sopenharmony_ci pr_warn("Store Data request failed (eq=%d, di=%d, " 2598c2ecf20Sopenharmony_ci "response=0x%04x, flags=0x%02x, status=%d, rc=%d)\n", 2608c2ecf20Sopenharmony_ci eq, di, sccb->hdr.response_code, evbuf->hdr.flags, 2618c2ecf20Sopenharmony_ci evbuf->status, rc); 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ciout_remove: 2658c2ecf20Sopenharmony_ci sclp_sd_listener_remove(&listener); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci return rc; 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci/** 2718c2ecf20Sopenharmony_ci * sclp_sd_store_data() - Obtain data for specified Store Data entity 2728c2ecf20Sopenharmony_ci * @result: Resulting data 2738c2ecf20Sopenharmony_ci * @di: DI value associated with this entity 2748c2ecf20Sopenharmony_ci * 2758c2ecf20Sopenharmony_ci * Perform a series of Store Data requests to obtain the size and contents of 2768c2ecf20Sopenharmony_ci * the specified Store Data entity. 2778c2ecf20Sopenharmony_ci * 2788c2ecf20Sopenharmony_ci * Return: 2798c2ecf20Sopenharmony_ci * %0: Success - result is stored in @result. @result->data must be 2808c2ecf20Sopenharmony_ci * released using vfree() after use. 2818c2ecf20Sopenharmony_ci * %-ENOENT: No data available for this entity 2828c2ecf20Sopenharmony_ci * %<0: Other error 2838c2ecf20Sopenharmony_ci */ 2848c2ecf20Sopenharmony_cistatic int sclp_sd_store_data(struct sclp_sd_data *result, u8 di) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci u32 dsize = 0, esize = 0; 2878c2ecf20Sopenharmony_ci unsigned long page, asce = 0; 2888c2ecf20Sopenharmony_ci void *data = NULL; 2898c2ecf20Sopenharmony_ci int rc; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci page = __get_free_page(GFP_KERNEL | GFP_DMA); 2928c2ecf20Sopenharmony_ci if (!page) 2938c2ecf20Sopenharmony_ci return -ENOMEM; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci /* Get size */ 2968c2ecf20Sopenharmony_ci rc = sclp_sd_sync(page, SD_EQ_SIZE, di, 0, 0, &dsize, &esize); 2978c2ecf20Sopenharmony_ci if (rc) 2988c2ecf20Sopenharmony_ci goto out; 2998c2ecf20Sopenharmony_ci if (dsize == 0) 3008c2ecf20Sopenharmony_ci goto out_result; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci /* Allocate memory */ 3038c2ecf20Sopenharmony_ci data = vzalloc(array_size((size_t)dsize, PAGE_SIZE)); 3048c2ecf20Sopenharmony_ci if (!data) { 3058c2ecf20Sopenharmony_ci rc = -ENOMEM; 3068c2ecf20Sopenharmony_ci goto out; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci /* Get translation table for buffer */ 3108c2ecf20Sopenharmony_ci asce = base_asce_alloc((unsigned long) data, dsize); 3118c2ecf20Sopenharmony_ci if (!asce) { 3128c2ecf20Sopenharmony_ci vfree(data); 3138c2ecf20Sopenharmony_ci rc = -ENOMEM; 3148c2ecf20Sopenharmony_ci goto out; 3158c2ecf20Sopenharmony_ci } 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci /* Get data */ 3188c2ecf20Sopenharmony_ci rc = sclp_sd_sync(page, SD_EQ_STORE_DATA, di, asce, (u64) data, &dsize, 3198c2ecf20Sopenharmony_ci &esize); 3208c2ecf20Sopenharmony_ci if (rc) { 3218c2ecf20Sopenharmony_ci /* Cancel running request if interrupted */ 3228c2ecf20Sopenharmony_ci if (rc == -ERESTARTSYS) { 3238c2ecf20Sopenharmony_ci if (sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL)) { 3248c2ecf20Sopenharmony_ci pr_warn("Could not stop Store Data request - leaking at least %zu bytes\n", 3258c2ecf20Sopenharmony_ci (size_t)dsize * PAGE_SIZE); 3268c2ecf20Sopenharmony_ci data = NULL; 3278c2ecf20Sopenharmony_ci asce = 0; 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci vfree(data); 3318c2ecf20Sopenharmony_ci goto out; 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ciout_result: 3358c2ecf20Sopenharmony_ci result->esize_bytes = (size_t) esize * PAGE_SIZE; 3368c2ecf20Sopenharmony_ci result->dsize_bytes = (size_t) dsize * PAGE_SIZE; 3378c2ecf20Sopenharmony_ci result->data = data; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ciout: 3408c2ecf20Sopenharmony_ci base_asce_free(asce); 3418c2ecf20Sopenharmony_ci free_page(page); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci return rc; 3448c2ecf20Sopenharmony_ci} 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci/** 3478c2ecf20Sopenharmony_ci * sclp_sd_data_reset() - Reset Store Data result buffer 3488c2ecf20Sopenharmony_ci * @data: Data buffer to reset 3498c2ecf20Sopenharmony_ci * 3508c2ecf20Sopenharmony_ci * Reset @data to initial state and release associated memory. 3518c2ecf20Sopenharmony_ci */ 3528c2ecf20Sopenharmony_cistatic void sclp_sd_data_reset(struct sclp_sd_data *data) 3538c2ecf20Sopenharmony_ci{ 3548c2ecf20Sopenharmony_ci vfree(data->data); 3558c2ecf20Sopenharmony_ci data->data = NULL; 3568c2ecf20Sopenharmony_ci data->dsize_bytes = 0; 3578c2ecf20Sopenharmony_ci data->esize_bytes = 0; 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci/** 3618c2ecf20Sopenharmony_ci * sclp_sd_file_release() - Release function for sclp_sd_file object 3628c2ecf20Sopenharmony_ci * @kobj: Kobject embedded in sclp_sd_file object 3638c2ecf20Sopenharmony_ci */ 3648c2ecf20Sopenharmony_cistatic void sclp_sd_file_release(struct kobject *kobj) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci struct sclp_sd_file *sd_file = to_sd_file(kobj); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci sclp_sd_data_reset(&sd_file->data); 3698c2ecf20Sopenharmony_ci kfree(sd_file); 3708c2ecf20Sopenharmony_ci} 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci/** 3738c2ecf20Sopenharmony_ci * sclp_sd_file_update() - Update contents of sclp_sd_file object 3748c2ecf20Sopenharmony_ci * @sd_file: Object to update 3758c2ecf20Sopenharmony_ci * 3768c2ecf20Sopenharmony_ci * Obtain the current version of data associated with the Store Data entity 3778c2ecf20Sopenharmony_ci * @sd_file. 3788c2ecf20Sopenharmony_ci * 3798c2ecf20Sopenharmony_ci * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the 3808c2ecf20Sopenharmony_ci * data may have changed. Return non-zero otherwise. 3818c2ecf20Sopenharmony_ci */ 3828c2ecf20Sopenharmony_cistatic int sclp_sd_file_update(struct sclp_sd_file *sd_file) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci const char *name = kobject_name(&sd_file->kobj); 3858c2ecf20Sopenharmony_ci struct sclp_sd_data data; 3868c2ecf20Sopenharmony_ci int rc; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci rc = sclp_sd_store_data(&data, sd_file->di); 3898c2ecf20Sopenharmony_ci if (rc) { 3908c2ecf20Sopenharmony_ci if (rc == -ENOENT) { 3918c2ecf20Sopenharmony_ci pr_info("No data is available for the %s data entity\n", 3928c2ecf20Sopenharmony_ci name); 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci return rc; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci mutex_lock(&sd_file->data_mutex); 3988c2ecf20Sopenharmony_ci sclp_sd_data_reset(&sd_file->data); 3998c2ecf20Sopenharmony_ci sd_file->data = data; 4008c2ecf20Sopenharmony_ci mutex_unlock(&sd_file->data_mutex); 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes, 4038c2ecf20Sopenharmony_ci name); 4048c2ecf20Sopenharmony_ci kobject_uevent(&sd_file->kobj, KOBJ_CHANGE); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci return 0; 4078c2ecf20Sopenharmony_ci} 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci/** 4108c2ecf20Sopenharmony_ci * sclp_sd_file_update_async() - Wrapper for asynchronous update call 4118c2ecf20Sopenharmony_ci * @data: Object to update 4128c2ecf20Sopenharmony_ci */ 4138c2ecf20Sopenharmony_cistatic void sclp_sd_file_update_async(void *data, async_cookie_t cookie) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci struct sclp_sd_file *sd_file = data; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci sclp_sd_file_update(sd_file); 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci/** 4218c2ecf20Sopenharmony_ci * reload_store() - Store function for "reload" sysfs attribute 4228c2ecf20Sopenharmony_ci * @kobj: Kobject of sclp_sd_file object 4238c2ecf20Sopenharmony_ci * 4248c2ecf20Sopenharmony_ci * Initiate a reload of the data associated with an sclp_sd_file object. 4258c2ecf20Sopenharmony_ci */ 4268c2ecf20Sopenharmony_cistatic ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr, 4278c2ecf20Sopenharmony_ci const char *buf, size_t count) 4288c2ecf20Sopenharmony_ci{ 4298c2ecf20Sopenharmony_ci struct sclp_sd_file *sd_file = to_sd_file(kobj); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci sclp_sd_file_update(sd_file); 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci return count; 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_cistatic struct kobj_attribute reload_attr = __ATTR_WO(reload); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_cistatic struct attribute *sclp_sd_file_default_attrs[] = { 4398c2ecf20Sopenharmony_ci &reload_attr.attr, 4408c2ecf20Sopenharmony_ci NULL, 4418c2ecf20Sopenharmony_ci}; 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_cistatic struct kobj_type sclp_sd_file_ktype = { 4448c2ecf20Sopenharmony_ci .sysfs_ops = &kobj_sysfs_ops, 4458c2ecf20Sopenharmony_ci .release = sclp_sd_file_release, 4468c2ecf20Sopenharmony_ci .default_attrs = sclp_sd_file_default_attrs, 4478c2ecf20Sopenharmony_ci}; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci/** 4508c2ecf20Sopenharmony_ci * data_read() - Read function for "read" sysfs attribute 4518c2ecf20Sopenharmony_ci * @kobj: Kobject of sclp_sd_file object 4528c2ecf20Sopenharmony_ci * @buffer: Target buffer 4538c2ecf20Sopenharmony_ci * @off: Requested file offset 4548c2ecf20Sopenharmony_ci * @size: Requested number of bytes 4558c2ecf20Sopenharmony_ci * 4568c2ecf20Sopenharmony_ci * Store the requested portion of the Store Data entity contents into the 4578c2ecf20Sopenharmony_ci * specified buffer. Return the number of bytes stored on success, or %0 4588c2ecf20Sopenharmony_ci * on EOF. 4598c2ecf20Sopenharmony_ci */ 4608c2ecf20Sopenharmony_cistatic ssize_t data_read(struct file *file, struct kobject *kobj, 4618c2ecf20Sopenharmony_ci struct bin_attribute *attr, char *buffer, 4628c2ecf20Sopenharmony_ci loff_t off, size_t size) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci struct sclp_sd_file *sd_file = to_sd_file(kobj); 4658c2ecf20Sopenharmony_ci size_t data_size; 4668c2ecf20Sopenharmony_ci char *data; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci mutex_lock(&sd_file->data_mutex); 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci data = sd_file->data.data; 4718c2ecf20Sopenharmony_ci data_size = sd_file->data.dsize_bytes; 4728c2ecf20Sopenharmony_ci if (!data || off >= data_size) { 4738c2ecf20Sopenharmony_ci size = 0; 4748c2ecf20Sopenharmony_ci } else { 4758c2ecf20Sopenharmony_ci if (off + size > data_size) 4768c2ecf20Sopenharmony_ci size = data_size - off; 4778c2ecf20Sopenharmony_ci memcpy(buffer, data + off, size); 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci mutex_unlock(&sd_file->data_mutex); 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci return size; 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci/** 4868c2ecf20Sopenharmony_ci * sclp_sd_file_create() - Add a sysfs file representing a Store Data entity 4878c2ecf20Sopenharmony_ci * @name: Name of file 4888c2ecf20Sopenharmony_ci * @di: DI value associated with this entity 4898c2ecf20Sopenharmony_ci * 4908c2ecf20Sopenharmony_ci * Create a sysfs directory with the given @name located under 4918c2ecf20Sopenharmony_ci * 4928c2ecf20Sopenharmony_ci * /sys/firmware/sclp_sd/ 4938c2ecf20Sopenharmony_ci * 4948c2ecf20Sopenharmony_ci * The files in this directory can be used to access the contents of the Store 4958c2ecf20Sopenharmony_ci * Data entity associated with @DI. 4968c2ecf20Sopenharmony_ci * 4978c2ecf20Sopenharmony_ci * Return pointer to resulting sclp_sd_file object on success, %NULL otherwise. 4988c2ecf20Sopenharmony_ci * The object must be freed by calling kobject_put() on the embedded kobject 4998c2ecf20Sopenharmony_ci * pointer after use. 5008c2ecf20Sopenharmony_ci */ 5018c2ecf20Sopenharmony_cistatic __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci struct sclp_sd_file *sd_file; 5048c2ecf20Sopenharmony_ci int rc; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL); 5078c2ecf20Sopenharmony_ci if (!sd_file) 5088c2ecf20Sopenharmony_ci return NULL; 5098c2ecf20Sopenharmony_ci sd_file->di = di; 5108c2ecf20Sopenharmony_ci mutex_init(&sd_file->data_mutex); 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci /* Create kobject located under /sys/firmware/sclp_sd/ */ 5138c2ecf20Sopenharmony_ci sd_file->kobj.kset = sclp_sd_kset; 5148c2ecf20Sopenharmony_ci rc = kobject_init_and_add(&sd_file->kobj, &sclp_sd_file_ktype, NULL, 5158c2ecf20Sopenharmony_ci "%s", name); 5168c2ecf20Sopenharmony_ci if (rc) { 5178c2ecf20Sopenharmony_ci kobject_put(&sd_file->kobj); 5188c2ecf20Sopenharmony_ci return NULL; 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci sysfs_bin_attr_init(&sd_file->data_attr); 5228c2ecf20Sopenharmony_ci sd_file->data_attr.attr.name = "data"; 5238c2ecf20Sopenharmony_ci sd_file->data_attr.attr.mode = 0444; 5248c2ecf20Sopenharmony_ci sd_file->data_attr.read = data_read; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr); 5278c2ecf20Sopenharmony_ci if (rc) { 5288c2ecf20Sopenharmony_ci kobject_put(&sd_file->kobj); 5298c2ecf20Sopenharmony_ci return NULL; 5308c2ecf20Sopenharmony_ci } 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci /* 5338c2ecf20Sopenharmony_ci * For completeness only - users interested in entity data should listen 5348c2ecf20Sopenharmony_ci * for KOBJ_CHANGE instead. 5358c2ecf20Sopenharmony_ci */ 5368c2ecf20Sopenharmony_ci kobject_uevent(&sd_file->kobj, KOBJ_ADD); 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci /* Don't let a slow Store Data request delay further initialization */ 5398c2ecf20Sopenharmony_ci async_schedule(sclp_sd_file_update_async, sd_file); 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci return sd_file; 5428c2ecf20Sopenharmony_ci} 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci/** 5458c2ecf20Sopenharmony_ci * sclp_sd_init() - Initialize sclp_sd support and register sysfs files 5468c2ecf20Sopenharmony_ci */ 5478c2ecf20Sopenharmony_cistatic __init int sclp_sd_init(void) 5488c2ecf20Sopenharmony_ci{ 5498c2ecf20Sopenharmony_ci int rc; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci rc = sclp_register(&sclp_sd_register); 5528c2ecf20Sopenharmony_ci if (rc) 5538c2ecf20Sopenharmony_ci return rc; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci /* Create kset named "sclp_sd" located under /sys/firmware/ */ 5568c2ecf20Sopenharmony_ci rc = -ENOMEM; 5578c2ecf20Sopenharmony_ci sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj); 5588c2ecf20Sopenharmony_ci if (!sclp_sd_kset) 5598c2ecf20Sopenharmony_ci goto err_kset; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci rc = -EINVAL; 5628c2ecf20Sopenharmony_ci config_file = sclp_sd_file_create("config", SD_DI_CONFIG); 5638c2ecf20Sopenharmony_ci if (!config_file) 5648c2ecf20Sopenharmony_ci goto err_config; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci return 0; 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_cierr_config: 5698c2ecf20Sopenharmony_ci kset_unregister(sclp_sd_kset); 5708c2ecf20Sopenharmony_cierr_kset: 5718c2ecf20Sopenharmony_ci sclp_unregister(&sclp_sd_register); 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci return rc; 5748c2ecf20Sopenharmony_ci} 5758c2ecf20Sopenharmony_cidevice_initcall(sclp_sd_init); 576