Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PowerNV OPAL Dump Interface
0004  *
0005  * Copyright 2013,2014 IBM Corp.
0006  */
0007 
0008 #include <linux/kobject.h>
0009 #include <linux/mm.h>
0010 #include <linux/slab.h>
0011 #include <linux/vmalloc.h>
0012 #include <linux/pagemap.h>
0013 #include <linux/delay.h>
0014 #include <linux/interrupt.h>
0015 
0016 #include <asm/opal.h>
0017 
0018 #define DUMP_TYPE_FSP   0x01
0019 
0020 struct dump_obj {
0021     struct kobject  kobj;
0022     struct bin_attribute dump_attr;
0023     uint32_t    id;  /* becomes object name */
0024     uint32_t    type;
0025     uint32_t    size;
0026     char        *buffer;
0027 };
0028 #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
0029 
0030 struct dump_attribute {
0031     struct attribute attr;
0032     ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
0033             char *buf);
0034     ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
0035              const char *buf, size_t count);
0036 };
0037 #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
0038 
0039 static ssize_t dump_id_show(struct dump_obj *dump_obj,
0040                 struct dump_attribute *attr,
0041                 char *buf)
0042 {
0043     return sprintf(buf, "0x%x\n", dump_obj->id);
0044 }
0045 
0046 static const char* dump_type_to_string(uint32_t type)
0047 {
0048     switch (type) {
0049     case 0x01: return "SP Dump";
0050     case 0x02: return "System/Platform Dump";
0051     case 0x03: return "SMA Dump";
0052     default: return "unknown";
0053     }
0054 }
0055 
0056 static ssize_t dump_type_show(struct dump_obj *dump_obj,
0057                   struct dump_attribute *attr,
0058                   char *buf)
0059 {
0060 
0061     return sprintf(buf, "0x%x %s\n", dump_obj->type,
0062                dump_type_to_string(dump_obj->type));
0063 }
0064 
0065 static ssize_t dump_ack_show(struct dump_obj *dump_obj,
0066                  struct dump_attribute *attr,
0067                  char *buf)
0068 {
0069     return sprintf(buf, "ack - acknowledge dump\n");
0070 }
0071 
0072 /*
0073  * Send acknowledgement to OPAL
0074  */
0075 static int64_t dump_send_ack(uint32_t dump_id)
0076 {
0077     int rc;
0078 
0079     rc = opal_dump_ack(dump_id);
0080     if (rc)
0081         pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
0082             __func__, dump_id, rc);
0083     return rc;
0084 }
0085 
0086 static ssize_t dump_ack_store(struct dump_obj *dump_obj,
0087                   struct dump_attribute *attr,
0088                   const char *buf,
0089                   size_t count)
0090 {
0091     /*
0092      * Try to self remove this attribute. If we are successful,
0093      * delete the kobject itself.
0094      */
0095     if (sysfs_remove_file_self(&dump_obj->kobj, &attr->attr)) {
0096         dump_send_ack(dump_obj->id);
0097         kobject_put(&dump_obj->kobj);
0098     }
0099     return count;
0100 }
0101 
0102 /* Attributes of a dump
0103  * The binary attribute of the dump itself is dynamic
0104  * due to the dynamic size of the dump
0105  */
0106 static struct dump_attribute id_attribute =
0107     __ATTR(id, 0444, dump_id_show, NULL);
0108 static struct dump_attribute type_attribute =
0109     __ATTR(type, 0444, dump_type_show, NULL);
0110 static struct dump_attribute ack_attribute =
0111     __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
0112 
0113 static ssize_t init_dump_show(struct dump_obj *dump_obj,
0114                   struct dump_attribute *attr,
0115                   char *buf)
0116 {
0117     return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
0118 }
0119 
0120 static int64_t dump_fips_init(uint8_t type)
0121 {
0122     int rc;
0123 
0124     rc = opal_dump_init(type);
0125     if (rc)
0126         pr_warn("%s: Failed to initiate FSP dump (%d)\n",
0127             __func__, rc);
0128     return rc;
0129 }
0130 
0131 static ssize_t init_dump_store(struct dump_obj *dump_obj,
0132                    struct dump_attribute *attr,
0133                    const char *buf,
0134                    size_t count)
0135 {
0136     int rc;
0137 
0138     rc = dump_fips_init(DUMP_TYPE_FSP);
0139     if (rc == OPAL_SUCCESS)
0140         pr_info("%s: Initiated FSP dump\n", __func__);
0141 
0142     return count;
0143 }
0144 
0145 static struct dump_attribute initiate_attribute =
0146     __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
0147 
0148 static struct attribute *initiate_attrs[] = {
0149     &initiate_attribute.attr,
0150     NULL,
0151 };
0152 
0153 static const struct attribute_group initiate_attr_group = {
0154     .attrs = initiate_attrs,
0155 };
0156 
0157 static struct kset *dump_kset;
0158 
0159 static ssize_t dump_attr_show(struct kobject *kobj,
0160                   struct attribute *attr,
0161                   char *buf)
0162 {
0163     struct dump_attribute *attribute;
0164     struct dump_obj *dump;
0165 
0166     attribute = to_dump_attr(attr);
0167     dump = to_dump_obj(kobj);
0168 
0169     if (!attribute->show)
0170         return -EIO;
0171 
0172     return attribute->show(dump, attribute, buf);
0173 }
0174 
0175 static ssize_t dump_attr_store(struct kobject *kobj,
0176                    struct attribute *attr,
0177                    const char *buf, size_t len)
0178 {
0179     struct dump_attribute *attribute;
0180     struct dump_obj *dump;
0181 
0182     attribute = to_dump_attr(attr);
0183     dump = to_dump_obj(kobj);
0184 
0185     if (!attribute->store)
0186         return -EIO;
0187 
0188     return attribute->store(dump, attribute, buf, len);
0189 }
0190 
0191 static const struct sysfs_ops dump_sysfs_ops = {
0192     .show = dump_attr_show,
0193     .store = dump_attr_store,
0194 };
0195 
0196 static void dump_release(struct kobject *kobj)
0197 {
0198     struct dump_obj *dump;
0199 
0200     dump = to_dump_obj(kobj);
0201     vfree(dump->buffer);
0202     kfree(dump);
0203 }
0204 
0205 static struct attribute *dump_default_attrs[] = {
0206     &id_attribute.attr,
0207     &type_attribute.attr,
0208     &ack_attribute.attr,
0209     NULL,
0210 };
0211 ATTRIBUTE_GROUPS(dump_default);
0212 
0213 static struct kobj_type dump_ktype = {
0214     .sysfs_ops = &dump_sysfs_ops,
0215     .release = &dump_release,
0216     .default_groups = dump_default_groups,
0217 };
0218 
0219 static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
0220 {
0221     __be32 id, size, type;
0222     int rc;
0223 
0224     type = cpu_to_be32(0xffffffff);
0225 
0226     rc = opal_dump_info2(&id, &size, &type);
0227     if (rc == OPAL_PARAMETER)
0228         rc = opal_dump_info(&id, &size);
0229 
0230     if (rc) {
0231         pr_warn("%s: Failed to get dump info (%d)\n",
0232             __func__, rc);
0233         return rc;
0234     }
0235 
0236     *dump_id = be32_to_cpu(id);
0237     *dump_size = be32_to_cpu(size);
0238     *dump_type = be32_to_cpu(type);
0239 
0240     return rc;
0241 }
0242 
0243 static int64_t dump_read_data(struct dump_obj *dump)
0244 {
0245     struct opal_sg_list *list;
0246     uint64_t addr;
0247     int64_t rc;
0248 
0249     /* Allocate memory */
0250     dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
0251     if (!dump->buffer) {
0252         pr_err("%s : Failed to allocate memory\n", __func__);
0253         rc = -ENOMEM;
0254         goto out;
0255     }
0256 
0257     /* Generate SG list */
0258     list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
0259     if (!list) {
0260         rc = -ENOMEM;
0261         goto out;
0262     }
0263 
0264     /* First entry address */
0265     addr = __pa(list);
0266 
0267     /* Fetch data */
0268     rc = OPAL_BUSY_EVENT;
0269     while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
0270         rc = opal_dump_read(dump->id, addr);
0271         if (rc == OPAL_BUSY_EVENT) {
0272             opal_poll_events(NULL);
0273             msleep(20);
0274         }
0275     }
0276 
0277     if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
0278         pr_warn("%s: Extract dump failed for ID 0x%x\n",
0279             __func__, dump->id);
0280 
0281     /* Free SG list */
0282     opal_free_sg_list(list);
0283 
0284 out:
0285     return rc;
0286 }
0287 
0288 static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
0289                   struct bin_attribute *bin_attr,
0290                   char *buffer, loff_t pos, size_t count)
0291 {
0292     ssize_t rc;
0293 
0294     struct dump_obj *dump = to_dump_obj(kobj);
0295 
0296     if (!dump->buffer) {
0297         rc = dump_read_data(dump);
0298 
0299         if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
0300             vfree(dump->buffer);
0301             dump->buffer = NULL;
0302 
0303             return -EIO;
0304         }
0305         if (rc == OPAL_PARTIAL) {
0306             /* On a partial read, we just return EIO
0307              * and rely on userspace to ask us to try
0308              * again.
0309              */
0310             pr_info("%s: Platform dump partially read. ID = 0x%x\n",
0311                 __func__, dump->id);
0312             return -EIO;
0313         }
0314     }
0315 
0316     memcpy(buffer, dump->buffer + pos, count);
0317 
0318     /* You may think we could free the dump buffer now and retrieve
0319      * it again later if needed, but due to current firmware limitation,
0320      * that's not the case. So, once read into userspace once,
0321      * we keep the dump around until it's acknowledged by userspace.
0322      */
0323 
0324     return count;
0325 }
0326 
0327 static void create_dump_obj(uint32_t id, size_t size, uint32_t type)
0328 {
0329     struct dump_obj *dump;
0330     int rc;
0331 
0332     dump = kzalloc(sizeof(*dump), GFP_KERNEL);
0333     if (!dump)
0334         return;
0335 
0336     dump->kobj.kset = dump_kset;
0337 
0338     kobject_init(&dump->kobj, &dump_ktype);
0339 
0340     sysfs_bin_attr_init(&dump->dump_attr);
0341 
0342     dump->dump_attr.attr.name = "dump";
0343     dump->dump_attr.attr.mode = 0400;
0344     dump->dump_attr.size = size;
0345     dump->dump_attr.read = dump_attr_read;
0346 
0347     dump->id = id;
0348     dump->size = size;
0349     dump->type = type;
0350 
0351     rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
0352     if (rc) {
0353         kobject_put(&dump->kobj);
0354         return;
0355     }
0356 
0357     /*
0358      * As soon as the sysfs file for this dump is created/activated there is
0359      * a chance the opal_errd daemon (or any userspace) might read and
0360      * acknowledge the dump before kobject_uevent() is called. If that
0361      * happens then there is a potential race between
0362      * dump_ack_store->kobject_put() and kobject_uevent() which leads to a
0363      * use-after-free of a kernfs object resulting in a kernel crash.
0364      *
0365      * To avoid that, we need to take a reference on behalf of the bin file,
0366      * so that our reference remains valid while we call kobject_uevent().
0367      * We then drop our reference before exiting the function, leaving the
0368      * bin file to drop the last reference (if it hasn't already).
0369      */
0370 
0371     /* Take a reference for the bin file */
0372     kobject_get(&dump->kobj);
0373     rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
0374     if (rc == 0) {
0375         kobject_uevent(&dump->kobj, KOBJ_ADD);
0376 
0377         pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
0378             __func__, dump->id, dump->size);
0379     } else {
0380         /* Drop reference count taken for bin file */
0381         kobject_put(&dump->kobj);
0382     }
0383 
0384     /* Drop our reference */
0385     kobject_put(&dump->kobj);
0386     return;
0387 }
0388 
0389 static irqreturn_t process_dump(int irq, void *data)
0390 {
0391     int rc;
0392     uint32_t dump_id, dump_size, dump_type;
0393     char name[22];
0394     struct kobject *kobj;
0395 
0396     rc = dump_read_info(&dump_id, &dump_size, &dump_type);
0397     if (rc != OPAL_SUCCESS)
0398         return IRQ_HANDLED;
0399 
0400     sprintf(name, "0x%x-0x%x", dump_type, dump_id);
0401 
0402     /* we may get notified twice, let's handle
0403      * that gracefully and not create two conflicting
0404      * entries.
0405      */
0406     kobj = kset_find_obj(dump_kset, name);
0407     if (kobj) {
0408         /* Drop reference added by kset_find_obj() */
0409         kobject_put(kobj);
0410         return IRQ_HANDLED;
0411     }
0412 
0413     create_dump_obj(dump_id, dump_size, dump_type);
0414 
0415     return IRQ_HANDLED;
0416 }
0417 
0418 void __init opal_platform_dump_init(void)
0419 {
0420     int rc;
0421     int dump_irq;
0422 
0423     /* Dump not supported by firmware */
0424     if (!opal_check_token(OPAL_DUMP_READ))
0425         return;
0426 
0427     dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
0428     if (!dump_kset) {
0429         pr_warn("%s: Failed to create dump kset\n", __func__);
0430         return;
0431     }
0432 
0433     rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
0434     if (rc) {
0435         pr_warn("%s: Failed to create initiate dump attr group\n",
0436             __func__);
0437         kobject_put(&dump_kset->kobj);
0438         return;
0439     }
0440 
0441     dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
0442     if (!dump_irq) {
0443         pr_err("%s: Can't register OPAL event irq (%d)\n",
0444                __func__, dump_irq);
0445         return;
0446     }
0447 
0448     rc = request_threaded_irq(dump_irq, NULL, process_dump,
0449                 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
0450                 "opal-dump", NULL);
0451     if (rc) {
0452         pr_err("%s: Can't request OPAL event irq (%d)\n",
0453                __func__, rc);
0454         return;
0455     }
0456 
0457     if (opal_check_token(OPAL_DUMP_RESEND))
0458         opal_dump_resend_notification();
0459 }