Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * SCLP Store Data support and sysfs interface
0004  *
0005  * Copyright IBM Corp. 2017
0006  */
0007 
0008 #define KMSG_COMPONENT "sclp_sd"
0009 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0010 
0011 #include <linux/completion.h>
0012 #include <linux/kobject.h>
0013 #include <linux/list.h>
0014 #include <linux/printk.h>
0015 #include <linux/slab.h>
0016 #include <linux/vmalloc.h>
0017 #include <linux/async.h>
0018 #include <linux/export.h>
0019 #include <linux/mutex.h>
0020 
0021 #include <asm/pgalloc.h>
0022 
0023 #include "sclp.h"
0024 
0025 #define SD_EQ_STORE_DATA    0
0026 #define SD_EQ_HALT      1
0027 #define SD_EQ_SIZE      2
0028 
0029 #define SD_DI_CONFIG        3
0030 
0031 struct sclp_sd_evbuf {
0032     struct evbuf_header hdr;
0033     u8 eq;
0034     u8 di;
0035     u8 rflags;
0036     u64 :56;
0037     u32 id;
0038     u16 :16;
0039     u8 fmt;
0040     u8 status;
0041     u64 sat;
0042     u64 sa;
0043     u32 esize;
0044     u32 dsize;
0045 } __packed;
0046 
0047 struct sclp_sd_sccb {
0048     struct sccb_header hdr;
0049     struct sclp_sd_evbuf evbuf;
0050 } __packed __aligned(PAGE_SIZE);
0051 
0052 /**
0053  * struct sclp_sd_data - Result of a Store Data request
0054  * @esize_bytes: Resulting esize in bytes
0055  * @dsize_bytes: Resulting dsize in bytes
0056  * @data: Pointer to data - must be released using vfree()
0057  */
0058 struct sclp_sd_data {
0059     size_t esize_bytes;
0060     size_t dsize_bytes;
0061     void *data;
0062 };
0063 
0064 /**
0065  * struct sclp_sd_listener - Listener for asynchronous Store Data response
0066  * @list: For enqueueing this struct
0067  * @id: Event ID of response to listen for
0068  * @completion: Can be used to wait for response
0069  * @evbuf: Contains the resulting Store Data response after completion
0070  */
0071 struct sclp_sd_listener {
0072     struct list_head list;
0073     u32 id;
0074     struct completion completion;
0075     struct sclp_sd_evbuf evbuf;
0076 };
0077 
0078 /**
0079  * struct sclp_sd_file - Sysfs representation of a Store Data entity
0080  * @kobj: Kobject
0081  * @data_attr: Attribute for accessing data contents
0082  * @data_mutex: Mutex to serialize access and updates to @data
0083  * @data: Data associated with this entity
0084  * @di: DI value associated with this entity
0085  */
0086 struct sclp_sd_file {
0087     struct kobject kobj;
0088     struct bin_attribute data_attr;
0089     struct mutex data_mutex;
0090     struct sclp_sd_data data;
0091     u8 di;
0092 };
0093 #define to_sd_file(x) container_of(x, struct sclp_sd_file, kobj)
0094 
0095 static struct kset *sclp_sd_kset;
0096 static struct sclp_sd_file *config_file;
0097 
0098 static LIST_HEAD(sclp_sd_queue);
0099 static DEFINE_SPINLOCK(sclp_sd_queue_lock);
0100 
0101 /**
0102  * sclp_sd_listener_add() - Add listener for Store Data responses
0103  * @listener: Listener to add
0104  */
0105 static void sclp_sd_listener_add(struct sclp_sd_listener *listener)
0106 {
0107     spin_lock_irq(&sclp_sd_queue_lock);
0108     list_add_tail(&listener->list, &sclp_sd_queue);
0109     spin_unlock_irq(&sclp_sd_queue_lock);
0110 }
0111 
0112 /**
0113  * sclp_sd_listener_remove() - Remove listener for Store Data responses
0114  * @listener: Listener to remove
0115  */
0116 static void sclp_sd_listener_remove(struct sclp_sd_listener *listener)
0117 {
0118     spin_lock_irq(&sclp_sd_queue_lock);
0119     list_del(&listener->list);
0120     spin_unlock_irq(&sclp_sd_queue_lock);
0121 }
0122 
0123 /**
0124  * sclp_sd_listener_init() - Initialize a Store Data response listener
0125  * @listener: Response listener to initialize
0126  * @id: Event ID to listen for
0127  *
0128  * Initialize a listener for asynchronous Store Data responses. This listener
0129  * can afterwards be used to wait for a specific response and to retrieve
0130  * the associated response data.
0131  */
0132 static void sclp_sd_listener_init(struct sclp_sd_listener *listener, u32 id)
0133 {
0134     memset(listener, 0, sizeof(*listener));
0135     listener->id = id;
0136     init_completion(&listener->completion);
0137 }
0138 
0139 /**
0140  * sclp_sd_receiver() - Receiver for Store Data events
0141  * @evbuf_hdr: Header of received events
0142  *
0143  * Process Store Data events and complete listeners with matching event IDs.
0144  */
0145 static void sclp_sd_receiver(struct evbuf_header *evbuf_hdr)
0146 {
0147     struct sclp_sd_evbuf *evbuf = (struct sclp_sd_evbuf *) evbuf_hdr;
0148     struct sclp_sd_listener *listener;
0149     int found = 0;
0150 
0151     pr_debug("received event (id=0x%08x)\n", evbuf->id);
0152     spin_lock(&sclp_sd_queue_lock);
0153     list_for_each_entry(listener, &sclp_sd_queue, list) {
0154         if (listener->id != evbuf->id)
0155             continue;
0156 
0157         listener->evbuf = *evbuf;
0158         complete(&listener->completion);
0159         found = 1;
0160         break;
0161     }
0162     spin_unlock(&sclp_sd_queue_lock);
0163 
0164     if (!found)
0165         pr_debug("unsolicited event (id=0x%08x)\n", evbuf->id);
0166 }
0167 
0168 static struct sclp_register sclp_sd_register = {
0169     .send_mask = EVTYP_STORE_DATA_MASK,
0170     .receive_mask = EVTYP_STORE_DATA_MASK,
0171     .receiver_fn = sclp_sd_receiver,
0172 };
0173 
0174 /**
0175  * sclp_sd_sync() - Perform Store Data request synchronously
0176  * @page: Address of work page - must be below 2GB
0177  * @eq: Input EQ value
0178  * @di: Input DI value
0179  * @sat: Input SAT value
0180  * @sa: Input SA value used to specify the address of the target buffer
0181  * @dsize_ptr: Optional pointer to input and output DSIZE value
0182  * @esize_ptr: Optional pointer to output ESIZE value
0183  *
0184  * Perform Store Data request with specified parameters and wait for completion.
0185  *
0186  * Return %0 on success and store resulting DSIZE and ESIZE values in
0187  * @dsize_ptr and @esize_ptr (if provided). Return non-zero on error.
0188  */
0189 static int sclp_sd_sync(unsigned long page, u8 eq, u8 di, u64 sat, u64 sa,
0190             u32 *dsize_ptr, u32 *esize_ptr)
0191 {
0192     struct sclp_sd_sccb *sccb = (void *) page;
0193     struct sclp_sd_listener listener;
0194     struct sclp_sd_evbuf *evbuf;
0195     int rc;
0196 
0197     sclp_sd_listener_init(&listener, __pa(sccb));
0198     sclp_sd_listener_add(&listener);
0199 
0200     /* Prepare SCCB */
0201     memset(sccb, 0, PAGE_SIZE);
0202     sccb->hdr.length = sizeof(sccb->hdr) + sizeof(sccb->evbuf);
0203     evbuf = &sccb->evbuf;
0204     evbuf->hdr.length = sizeof(*evbuf);
0205     evbuf->hdr.type = EVTYP_STORE_DATA;
0206     evbuf->eq = eq;
0207     evbuf->di = di;
0208     evbuf->id = listener.id;
0209     evbuf->fmt = 1;
0210     evbuf->sat = sat;
0211     evbuf->sa = sa;
0212     if (dsize_ptr)
0213         evbuf->dsize = *dsize_ptr;
0214 
0215     /* Perform command */
0216     pr_debug("request (eq=%d, di=%d, id=0x%08x)\n", eq, di, listener.id);
0217     rc = sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
0218     pr_debug("request done (rc=%d)\n", rc);
0219     if (rc)
0220         goto out;
0221 
0222     /* Evaluate response */
0223     if (sccb->hdr.response_code == 0x73f0) {
0224         pr_debug("event not supported\n");
0225         rc = -EIO;
0226         goto out_remove;
0227     }
0228     if (sccb->hdr.response_code != 0x0020 || !(evbuf->hdr.flags & 0x80)) {
0229         rc = -EIO;
0230         goto out;
0231     }
0232     if (!(evbuf->rflags & 0x80)) {
0233         rc = wait_for_completion_interruptible(&listener.completion);
0234         if (rc)
0235             goto out;
0236         evbuf = &listener.evbuf;
0237     }
0238     switch (evbuf->status) {
0239     case 0:
0240         if (dsize_ptr)
0241             *dsize_ptr = evbuf->dsize;
0242         if (esize_ptr)
0243             *esize_ptr = evbuf->esize;
0244         pr_debug("success (dsize=%u, esize=%u)\n", evbuf->dsize,
0245              evbuf->esize);
0246         break;
0247     case 3:
0248         rc = -ENOENT;
0249         break;
0250     default:
0251         rc = -EIO;
0252         break;
0253 
0254     }
0255 
0256 out:
0257     if (rc && rc != -ENOENT) {
0258         /* Provide some information about what went wrong */
0259         pr_warn("Store Data request failed (eq=%d, di=%d, "
0260             "response=0x%04x, flags=0x%02x, status=%d, rc=%d)\n",
0261             eq, di, sccb->hdr.response_code, evbuf->hdr.flags,
0262             evbuf->status, rc);
0263     }
0264 
0265 out_remove:
0266     sclp_sd_listener_remove(&listener);
0267 
0268     return rc;
0269 }
0270 
0271 /**
0272  * sclp_sd_store_data() - Obtain data for specified Store Data entity
0273  * @result: Resulting data
0274  * @di: DI value associated with this entity
0275  *
0276  * Perform a series of Store Data requests to obtain the size and contents of
0277  * the specified Store Data entity.
0278  *
0279  * Return:
0280  *   %0:       Success - result is stored in @result. @result->data must be
0281  *         released using vfree() after use.
0282  *   %-ENOENT: No data available for this entity
0283  *   %<0:      Other error
0284  */
0285 static int sclp_sd_store_data(struct sclp_sd_data *result, u8 di)
0286 {
0287     u32 dsize = 0, esize = 0;
0288     unsigned long page, asce = 0;
0289     void *data = NULL;
0290     int rc;
0291 
0292     page = __get_free_page(GFP_KERNEL | GFP_DMA);
0293     if (!page)
0294         return -ENOMEM;
0295 
0296     /* Get size */
0297     rc = sclp_sd_sync(page, SD_EQ_SIZE, di, 0, 0, &dsize, &esize);
0298     if (rc)
0299         goto out;
0300     if (dsize == 0)
0301         goto out_result;
0302 
0303     /* Allocate memory */
0304     data = vzalloc(array_size((size_t)dsize, PAGE_SIZE));
0305     if (!data) {
0306         rc = -ENOMEM;
0307         goto out;
0308     }
0309 
0310     /* Get translation table for buffer */
0311     asce = base_asce_alloc((unsigned long) data, dsize);
0312     if (!asce) {
0313         vfree(data);
0314         rc = -ENOMEM;
0315         goto out;
0316     }
0317 
0318     /* Get data */
0319     rc = sclp_sd_sync(page, SD_EQ_STORE_DATA, di, asce, (u64) data, &dsize,
0320               &esize);
0321     if (rc) {
0322         /* Cancel running request if interrupted */
0323         if (rc == -ERESTARTSYS)
0324             sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL);
0325         vfree(data);
0326         goto out;
0327     }
0328 
0329 out_result:
0330     result->esize_bytes = (size_t) esize * PAGE_SIZE;
0331     result->dsize_bytes = (size_t) dsize * PAGE_SIZE;
0332     result->data = data;
0333 
0334 out:
0335     base_asce_free(asce);
0336     free_page(page);
0337 
0338     return rc;
0339 }
0340 
0341 /**
0342  * sclp_sd_data_reset() - Reset Store Data result buffer
0343  * @data: Data buffer to reset
0344  *
0345  * Reset @data to initial state and release associated memory.
0346  */
0347 static void sclp_sd_data_reset(struct sclp_sd_data *data)
0348 {
0349     vfree(data->data);
0350     data->data = NULL;
0351     data->dsize_bytes = 0;
0352     data->esize_bytes = 0;
0353 }
0354 
0355 /**
0356  * sclp_sd_file_release() - Release function for sclp_sd_file object
0357  * @kobj: Kobject embedded in sclp_sd_file object
0358  */
0359 static void sclp_sd_file_release(struct kobject *kobj)
0360 {
0361     struct sclp_sd_file *sd_file = to_sd_file(kobj);
0362 
0363     sclp_sd_data_reset(&sd_file->data);
0364     kfree(sd_file);
0365 }
0366 
0367 /**
0368  * sclp_sd_file_update() - Update contents of sclp_sd_file object
0369  * @sd_file: Object to update
0370  *
0371  * Obtain the current version of data associated with the Store Data entity
0372  * @sd_file.
0373  *
0374  * On success, return %0 and generate a KOBJ_CHANGE event to indicate that the
0375  * data may have changed. Return non-zero otherwise.
0376  */
0377 static int sclp_sd_file_update(struct sclp_sd_file *sd_file)
0378 {
0379     const char *name = kobject_name(&sd_file->kobj);
0380     struct sclp_sd_data data;
0381     int rc;
0382 
0383     rc = sclp_sd_store_data(&data, sd_file->di);
0384     if (rc) {
0385         if (rc == -ENOENT) {
0386             pr_info("No data is available for the %s data entity\n",
0387                  name);
0388         }
0389         return rc;
0390     }
0391 
0392     mutex_lock(&sd_file->data_mutex);
0393     sclp_sd_data_reset(&sd_file->data);
0394     sd_file->data = data;
0395     mutex_unlock(&sd_file->data_mutex);
0396 
0397     pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes,
0398         name);
0399     kobject_uevent(&sd_file->kobj, KOBJ_CHANGE);
0400 
0401     return 0;
0402 }
0403 
0404 /**
0405  * sclp_sd_file_update_async() - Wrapper for asynchronous update call
0406  * @data: Object to update
0407  * @cookie: Unused
0408  */
0409 static void sclp_sd_file_update_async(void *data, async_cookie_t cookie)
0410 {
0411     struct sclp_sd_file *sd_file = data;
0412 
0413     sclp_sd_file_update(sd_file);
0414 }
0415 
0416 /**
0417  * reload_store() - Store function for "reload" sysfs attribute
0418  * @kobj: Kobject of sclp_sd_file object
0419  * @attr: Reload attribute
0420  * @buf: Data written to sysfs attribute
0421  * @count: Count of bytes written
0422  *
0423  * Initiate a reload of the data associated with an sclp_sd_file object.
0424  */
0425 static ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr,
0426                 const char *buf, size_t count)
0427 {
0428     struct sclp_sd_file *sd_file = to_sd_file(kobj);
0429 
0430     sclp_sd_file_update(sd_file);
0431 
0432     return count;
0433 }
0434 
0435 static struct kobj_attribute reload_attr = __ATTR_WO(reload);
0436 
0437 static struct attribute *sclp_sd_file_default_attrs[] = {
0438     &reload_attr.attr,
0439     NULL,
0440 };
0441 ATTRIBUTE_GROUPS(sclp_sd_file_default);
0442 
0443 static struct kobj_type sclp_sd_file_ktype = {
0444     .sysfs_ops = &kobj_sysfs_ops,
0445     .release = sclp_sd_file_release,
0446     .default_groups = sclp_sd_file_default_groups,
0447 };
0448 
0449 /**
0450  * data_read() - Read function for "data" sysfs attribute
0451  * @file: Open file pointer
0452  * @kobj: Kobject of sclp_sd_file object
0453  * @attr: Data attribute
0454  * @buffer: Target buffer
0455  * @off: Requested file offset
0456  * @size: Requested number of bytes
0457  *
0458  * Store the requested portion of the Store Data entity contents into the
0459  * specified buffer. Return the number of bytes stored on success, or %0
0460  * on EOF.
0461  */
0462 static ssize_t data_read(struct file *file, struct kobject *kobj,
0463              struct bin_attribute *attr, char *buffer,
0464              loff_t off, size_t size)
0465 {
0466     struct sclp_sd_file *sd_file = to_sd_file(kobj);
0467     size_t data_size;
0468     char *data;
0469 
0470     mutex_lock(&sd_file->data_mutex);
0471 
0472     data = sd_file->data.data;
0473     data_size = sd_file->data.dsize_bytes;
0474     if (!data || off >= data_size) {
0475         size = 0;
0476     } else {
0477         if (off + size > data_size)
0478             size = data_size - off;
0479         memcpy(buffer, data + off, size);
0480     }
0481 
0482     mutex_unlock(&sd_file->data_mutex);
0483 
0484     return size;
0485 }
0486 
0487 /**
0488  * sclp_sd_file_create() - Add a sysfs file representing a Store Data entity
0489  * @name: Name of file
0490  * @di: DI value associated with this entity
0491  *
0492  * Create a sysfs directory with the given @name located under
0493  *
0494  *   /sys/firmware/sclp_sd/
0495  *
0496  * The files in this directory can be used to access the contents of the Store
0497  * Data entity associated with @DI.
0498  *
0499  * Return pointer to resulting sclp_sd_file object on success, %NULL otherwise.
0500  * The object must be freed by calling kobject_put() on the embedded kobject
0501  * pointer after use.
0502  */
0503 static __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di)
0504 {
0505     struct sclp_sd_file *sd_file;
0506     int rc;
0507 
0508     sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL);
0509     if (!sd_file)
0510         return NULL;
0511     sd_file->di = di;
0512     mutex_init(&sd_file->data_mutex);
0513 
0514     /* Create kobject located under /sys/firmware/sclp_sd/ */
0515     sd_file->kobj.kset = sclp_sd_kset;
0516     rc = kobject_init_and_add(&sd_file->kobj, &sclp_sd_file_ktype, NULL,
0517                   "%s", name);
0518     if (rc) {
0519         kobject_put(&sd_file->kobj);
0520         return NULL;
0521     }
0522 
0523     sysfs_bin_attr_init(&sd_file->data_attr);
0524     sd_file->data_attr.attr.name = "data";
0525     sd_file->data_attr.attr.mode = 0444;
0526     sd_file->data_attr.read = data_read;
0527 
0528     rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr);
0529     if (rc) {
0530         kobject_put(&sd_file->kobj);
0531         return NULL;
0532     }
0533 
0534     /*
0535      * For completeness only - users interested in entity data should listen
0536      * for KOBJ_CHANGE instead.
0537      */
0538     kobject_uevent(&sd_file->kobj, KOBJ_ADD);
0539 
0540     /* Don't let a slow Store Data request delay further initialization */
0541     async_schedule(sclp_sd_file_update_async, sd_file);
0542 
0543     return sd_file;
0544 }
0545 
0546 /**
0547  * sclp_sd_init() - Initialize sclp_sd support and register sysfs files
0548  */
0549 static __init int sclp_sd_init(void)
0550 {
0551     int rc;
0552 
0553     rc = sclp_register(&sclp_sd_register);
0554     if (rc)
0555         return rc;
0556 
0557     /* Create kset named "sclp_sd" located under /sys/firmware/ */
0558     rc = -ENOMEM;
0559     sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj);
0560     if (!sclp_sd_kset)
0561         goto err_kset;
0562 
0563     rc = -EINVAL;
0564     config_file = sclp_sd_file_create("config", SD_DI_CONFIG);
0565     if (!config_file)
0566         goto err_config;
0567 
0568     return 0;
0569 
0570 err_config:
0571     kset_unregister(sclp_sd_kset);
0572 err_kset:
0573     sclp_unregister(&sclp_sd_register);
0574 
0575     return rc;
0576 }
0577 device_initcall(sclp_sd_init);