Path: blob/master/arch/powerpc/platforms/pseries/reconfig.c
10818 views
/*1* pSeries_reconfig.c - support for dynamic reconfiguration (including PCI2* Hotplug and Dynamic Logical Partitioning on RPA platforms).3*4* Copyright (C) 2005 Nathan Lynch5* Copyright (C) 2005 IBM Corporation6*7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License version10* 2 as published by the Free Software Foundation.11*/1213#include <linux/kernel.h>14#include <linux/kref.h>15#include <linux/notifier.h>16#include <linux/proc_fs.h>17#include <linux/slab.h>1819#include <asm/prom.h>20#include <asm/machdep.h>21#include <asm/uaccess.h>22#include <asm/pSeries_reconfig.h>23#include <asm/mmu.h>24252627/*28* Routines for "runtime" addition and removal of device tree nodes.29*/30#ifdef CONFIG_PROC_DEVICETREE31/*32* Add a node to /proc/device-tree.33*/34static void add_node_proc_entries(struct device_node *np)35{36struct proc_dir_entry *ent;3738ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);39if (ent)40proc_device_tree_add_node(np, ent);41}4243static void remove_node_proc_entries(struct device_node *np)44{45struct property *pp = np->properties;46struct device_node *parent = np->parent;4748while (pp) {49remove_proc_entry(pp->name, np->pde);50pp = pp->next;51}52if (np->pde)53remove_proc_entry(np->pde->name, parent->pde);54}55#else /* !CONFIG_PROC_DEVICETREE */56static void add_node_proc_entries(struct device_node *np)57{58return;59}6061static void remove_node_proc_entries(struct device_node *np)62{63return;64}65#endif /* CONFIG_PROC_DEVICETREE */6667/**68* derive_parent - basically like dirname(1)69* @path: the full_name of a node to be added to the tree70*71* Returns the node which should be the parent of the node72* described by path. E.g., for path = "/foo/bar", returns73* the node with full_name = "/foo".74*/75static struct device_node *derive_parent(const char *path)76{77struct device_node *parent = NULL;78char *parent_path = "/";79size_t parent_path_len = strrchr(path, '/') - path + 1;8081/* reject if path is "/" */82if (!strcmp(path, "/"))83return ERR_PTR(-EINVAL);8485if (strrchr(path, '/') != path) {86parent_path = kmalloc(parent_path_len, GFP_KERNEL);87if (!parent_path)88return ERR_PTR(-ENOMEM);89strlcpy(parent_path, path, parent_path_len);90}91parent = of_find_node_by_path(parent_path);92if (!parent)93return ERR_PTR(-EINVAL);94if (strcmp(parent_path, "/"))95kfree(parent_path);96return parent;97}9899BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);100101int pSeries_reconfig_notifier_register(struct notifier_block *nb)102{103return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);104}105106void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)107{108blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);109}110111static int pSeries_reconfig_add_node(const char *path, struct property *proplist)112{113struct device_node *np;114int err = -ENOMEM;115116np = kzalloc(sizeof(*np), GFP_KERNEL);117if (!np)118goto out_err;119120np->full_name = kstrdup(path, GFP_KERNEL);121if (!np->full_name)122goto out_err;123124np->properties = proplist;125of_node_set_flag(np, OF_DYNAMIC);126kref_init(&np->kref);127128np->parent = derive_parent(path);129if (IS_ERR(np->parent)) {130err = PTR_ERR(np->parent);131goto out_err;132}133134err = blocking_notifier_call_chain(&pSeries_reconfig_chain,135PSERIES_RECONFIG_ADD, np);136if (err == NOTIFY_BAD) {137printk(KERN_ERR "Failed to add device node %s\n", path);138err = -ENOMEM; /* For now, safe to assume kmalloc failure */139goto out_err;140}141142of_attach_node(np);143144add_node_proc_entries(np);145146of_node_put(np->parent);147148return 0;149150out_err:151if (np) {152of_node_put(np->parent);153kfree(np->full_name);154kfree(np);155}156return err;157}158159static int pSeries_reconfig_remove_node(struct device_node *np)160{161struct device_node *parent, *child;162163parent = of_get_parent(np);164if (!parent)165return -EINVAL;166167if ((child = of_get_next_child(np, NULL))) {168of_node_put(child);169of_node_put(parent);170return -EBUSY;171}172173remove_node_proc_entries(np);174175blocking_notifier_call_chain(&pSeries_reconfig_chain,176PSERIES_RECONFIG_REMOVE, np);177of_detach_node(np);178179of_node_put(parent);180of_node_put(np); /* Must decrement the refcount */181return 0;182}183184/*185* /proc/powerpc/ofdt - yucky binary interface for adding and removing186* OF device nodes. Should be deprecated as soon as we get an187* in-kernel wrapper for the RTAS ibm,configure-connector call.188*/189190static void release_prop_list(const struct property *prop)191{192struct property *next;193for (; prop; prop = next) {194next = prop->next;195kfree(prop->name);196kfree(prop->value);197kfree(prop);198}199200}201202/**203* parse_next_property - process the next property from raw input buffer204* @buf: input buffer, must be nul-terminated205* @end: end of the input buffer + 1, for validation206* @name: return value; set to property name in buf207* @length: return value; set to length of value208* @value: return value; set to the property value in buf209*210* Note that the caller must make copies of the name and value returned,211* this function does no allocation or copying of the data. Return value212* is set to the next name in buf, or NULL on error.213*/214static char * parse_next_property(char *buf, char *end, char **name, int *length,215unsigned char **value)216{217char *tmp;218219*name = buf;220221tmp = strchr(buf, ' ');222if (!tmp) {223printk(KERN_ERR "property parse failed in %s at line %d\n",224__func__, __LINE__);225return NULL;226}227*tmp = '\0';228229if (++tmp >= end) {230printk(KERN_ERR "property parse failed in %s at line %d\n",231__func__, __LINE__);232return NULL;233}234235/* now we're on the length */236*length = -1;237*length = simple_strtoul(tmp, &tmp, 10);238if (*length == -1) {239printk(KERN_ERR "property parse failed in %s at line %d\n",240__func__, __LINE__);241return NULL;242}243if (*tmp != ' ' || ++tmp >= end) {244printk(KERN_ERR "property parse failed in %s at line %d\n",245__func__, __LINE__);246return NULL;247}248249/* now we're on the value */250*value = tmp;251tmp += *length;252if (tmp > end) {253printk(KERN_ERR "property parse failed in %s at line %d\n",254__func__, __LINE__);255return NULL;256}257else if (tmp < end && *tmp != ' ' && *tmp != '\0') {258printk(KERN_ERR "property parse failed in %s at line %d\n",259__func__, __LINE__);260return NULL;261}262tmp++;263264/* and now we should be on the next name, or the end */265return tmp;266}267268static struct property *new_property(const char *name, const int length,269const unsigned char *value, struct property *last)270{271struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);272273if (!new)274return NULL;275276if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))277goto cleanup;278if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))279goto cleanup;280281strcpy(new->name, name);282memcpy(new->value, value, length);283*(((char *)new->value) + length) = 0;284new->length = length;285new->next = last;286return new;287288cleanup:289kfree(new->name);290kfree(new->value);291kfree(new);292return NULL;293}294295static int do_add_node(char *buf, size_t bufsize)296{297char *path, *end, *name;298struct device_node *np;299struct property *prop = NULL;300unsigned char* value;301int length, rv = 0;302303end = buf + bufsize;304path = buf;305buf = strchr(buf, ' ');306if (!buf)307return -EINVAL;308*buf = '\0';309buf++;310311if ((np = of_find_node_by_path(path))) {312of_node_put(np);313return -EINVAL;314}315316/* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */317while (buf < end &&318(buf = parse_next_property(buf, end, &name, &length, &value))) {319struct property *last = prop;320321prop = new_property(name, length, value, last);322if (!prop) {323rv = -ENOMEM;324prop = last;325goto out;326}327}328if (!buf) {329rv = -EINVAL;330goto out;331}332333rv = pSeries_reconfig_add_node(path, prop);334335out:336if (rv)337release_prop_list(prop);338return rv;339}340341static int do_remove_node(char *buf)342{343struct device_node *node;344int rv = -ENODEV;345346if ((node = of_find_node_by_path(buf)))347rv = pSeries_reconfig_remove_node(node);348349of_node_put(node);350return rv;351}352353static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)354{355char *handle_str;356phandle handle;357*npp = NULL;358359handle_str = buf;360361buf = strchr(buf, ' ');362if (!buf)363return NULL;364*buf = '\0';365buf++;366367handle = simple_strtoul(handle_str, NULL, 0);368369*npp = of_find_node_by_phandle(handle);370return buf;371}372373static int do_add_property(char *buf, size_t bufsize)374{375struct property *prop = NULL;376struct device_node *np;377unsigned char *value;378char *name, *end;379int length;380end = buf + bufsize;381buf = parse_node(buf, bufsize, &np);382383if (!np)384return -ENODEV;385386if (parse_next_property(buf, end, &name, &length, &value) == NULL)387return -EINVAL;388389prop = new_property(name, length, value, NULL);390if (!prop)391return -ENOMEM;392393prom_add_property(np, prop);394395return 0;396}397398static int do_remove_property(char *buf, size_t bufsize)399{400struct device_node *np;401char *tmp;402struct property *prop;403buf = parse_node(buf, bufsize, &np);404405if (!np)406return -ENODEV;407408tmp = strchr(buf,' ');409if (tmp)410*tmp = '\0';411412if (strlen(buf) == 0)413return -EINVAL;414415prop = of_find_property(np, buf, NULL);416417return prom_remove_property(np, prop);418}419420static int do_update_property(char *buf, size_t bufsize)421{422struct device_node *np;423unsigned char *value;424char *name, *end, *next_prop;425int rc, length;426struct property *newprop, *oldprop;427buf = parse_node(buf, bufsize, &np);428end = buf + bufsize;429430if (!np)431return -ENODEV;432433next_prop = parse_next_property(buf, end, &name, &length, &value);434if (!next_prop)435return -EINVAL;436437newprop = new_property(name, length, value, NULL);438if (!newprop)439return -ENOMEM;440441if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))442slb_set_size(*(int *)value);443444oldprop = of_find_property(np, name,NULL);445if (!oldprop) {446if (strlen(name))447return prom_add_property(np, newprop);448return -ENODEV;449}450451rc = prom_update_property(np, newprop, oldprop);452if (rc)453return rc;454455/* For memory under the ibm,dynamic-reconfiguration-memory node456* of the device tree, adding and removing memory is just an update457* to the ibm,dynamic-memory property instead of adding/removing a458* memory node in the device tree. For these cases we still need to459* involve the notifier chain.460*/461if (!strcmp(name, "ibm,dynamic-memory")) {462int action;463464next_prop = parse_next_property(next_prop, end, &name,465&length, &value);466if (!next_prop)467return -EINVAL;468469if (!strcmp(name, "add"))470action = PSERIES_DRCONF_MEM_ADD;471else472action = PSERIES_DRCONF_MEM_REMOVE;473474rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,475action, value);476if (rc == NOTIFY_BAD) {477rc = prom_update_property(np, oldprop, newprop);478return -ENOMEM;479}480}481482return 0;483}484485/**486* ofdt_write - perform operations on the Open Firmware device tree487*488* @file: not used489* @buf: command and arguments490* @count: size of the command buffer491* @off: not used492*493* Operations supported at this time are addition and removal of494* whole nodes along with their properties. Operations on individual495* properties are not implemented (yet).496*/497static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,498loff_t *off)499{500int rv = 0;501char *kbuf;502char *tmp;503504if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {505rv = -ENOMEM;506goto out;507}508if (copy_from_user(kbuf, buf, count)) {509rv = -EFAULT;510goto out;511}512513kbuf[count] = '\0';514515tmp = strchr(kbuf, ' ');516if (!tmp) {517rv = -EINVAL;518goto out;519}520*tmp = '\0';521tmp++;522523if (!strcmp(kbuf, "add_node"))524rv = do_add_node(tmp, count - (tmp - kbuf));525else if (!strcmp(kbuf, "remove_node"))526rv = do_remove_node(tmp);527else if (!strcmp(kbuf, "add_property"))528rv = do_add_property(tmp, count - (tmp - kbuf));529else if (!strcmp(kbuf, "remove_property"))530rv = do_remove_property(tmp, count - (tmp - kbuf));531else if (!strcmp(kbuf, "update_property"))532rv = do_update_property(tmp, count - (tmp - kbuf));533else534rv = -EINVAL;535out:536kfree(kbuf);537return rv ? rv : count;538}539540static const struct file_operations ofdt_fops = {541.write = ofdt_write,542.llseek = noop_llseek,543};544545/* create /proc/powerpc/ofdt write-only by root */546static int proc_ppc64_create_ofdt(void)547{548struct proc_dir_entry *ent;549550if (!machine_is(pseries))551return 0;552553ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);554if (ent)555ent->size = 0;556557return 0;558}559__initcall(proc_ppc64_create_ofdt);560561562