1 /* 2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI 3 * Hotplug and Dynamic Logical Partitioning on RPA platforms). 4 * 5 * Copyright (C) 2005 Nathan Lynch 6 * Copyright (C) 2005 IBM Corporation 7 * 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License version 11 * 2 as published by the Free Software Foundation. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/kref.h> 16 #include <linux/notifier.h> 17 #include <linux/proc_fs.h> 18 19 #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> 24 25 26 27 /* 28 * Routines for "runtime" addition and removal of device tree nodes. 29 */ 30 #ifdef CONFIG_PROC_DEVICETREE 31 /* 32 * Add a node to /proc/device-tree. 33 */ 34 static void add_node_proc_entries(struct device_node *np) 35 { 36 struct proc_dir_entry *ent; 37 38 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde); 39 if (ent) 40 proc_device_tree_add_node(np, ent); 41 } 42 43 static void remove_node_proc_entries(struct device_node *np) 44 { 45 struct property *pp = np->properties; 46 struct device_node *parent = np->parent; 47 48 while (pp) { 49 remove_proc_entry(pp->name, np->pde); 50 pp = pp->next; 51 } 52 if (np->pde) 53 remove_proc_entry(np->pde->name, parent->pde); 54 } 55 #else /* !CONFIG_PROC_DEVICETREE */ 56 static void add_node_proc_entries(struct device_node *np) 57 { 58 return; 59 } 60 61 static void remove_node_proc_entries(struct device_node *np) 62 { 63 return; 64 } 65 #endif /* CONFIG_PROC_DEVICETREE */ 66 67 /** 68 * derive_parent - basically like dirname(1) 69 * @path: the full_name of a node to be added to the tree 70 * 71 * Returns the node which should be the parent of the node 72 * described by path. E.g., for path = "/foo/bar", returns 73 * the node with full_name = "/foo". 74 */ 75 static struct device_node *derive_parent(const char *path) 76 { 77 struct device_node *parent = NULL; 78 char *parent_path = "/"; 79 size_t parent_path_len = strrchr(path, '/') - path + 1; 80 81 /* reject if path is "/" */ 82 if (!strcmp(path, "/")) 83 return ERR_PTR(-EINVAL); 84 85 if (strrchr(path, '/') != path) { 86 parent_path = kmalloc(parent_path_len, GFP_KERNEL); 87 if (!parent_path) 88 return ERR_PTR(-ENOMEM); 89 strlcpy(parent_path, path, parent_path_len); 90 } 91 parent = of_find_node_by_path(parent_path); 92 if (!parent) 93 return ERR_PTR(-EINVAL); 94 if (strcmp(parent_path, "/")) 95 kfree(parent_path); 96 return parent; 97 } 98 99 static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain); 100 101 int pSeries_reconfig_notifier_register(struct notifier_block *nb) 102 { 103 return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb); 104 } 105 106 void pSeries_reconfig_notifier_unregister(struct notifier_block *nb) 107 { 108 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb); 109 } 110 111 static int pSeries_reconfig_add_node(const char *path, struct property *proplist) 112 { 113 struct device_node *np; 114 int err = -ENOMEM; 115 116 np = kzalloc(sizeof(*np), GFP_KERNEL); 117 if (!np) 118 goto out_err; 119 120 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL); 121 if (!np->full_name) 122 goto out_err; 123 124 strcpy(np->full_name, path); 125 126 np->properties = proplist; 127 of_node_set_flag(np, OF_DYNAMIC); 128 kref_init(&np->kref); 129 130 np->parent = derive_parent(path); 131 if (IS_ERR(np->parent)) { 132 err = PTR_ERR(np->parent); 133 goto out_err; 134 } 135 136 err = blocking_notifier_call_chain(&pSeries_reconfig_chain, 137 PSERIES_RECONFIG_ADD, np); 138 if (err == NOTIFY_BAD) { 139 printk(KERN_ERR "Failed to add device node %s\n", path); 140 err = -ENOMEM; /* For now, safe to assume kmalloc failure */ 141 goto out_err; 142 } 143 144 of_attach_node(np); 145 146 add_node_proc_entries(np); 147 148 of_node_put(np->parent); 149 150 return 0; 151 152 out_err: 153 if (np) { 154 of_node_put(np->parent); 155 kfree(np->full_name); 156 kfree(np); 157 } 158 return err; 159 } 160 161 static int pSeries_reconfig_remove_node(struct device_node *np) 162 { 163 struct device_node *parent, *child; 164 165 parent = of_get_parent(np); 166 if (!parent) 167 return -EINVAL; 168 169 if ((child = of_get_next_child(np, NULL))) { 170 of_node_put(child); 171 of_node_put(parent); 172 return -EBUSY; 173 } 174 175 remove_node_proc_entries(np); 176 177 blocking_notifier_call_chain(&pSeries_reconfig_chain, 178 PSERIES_RECONFIG_REMOVE, np); 179 of_detach_node(np); 180 181 of_node_put(parent); 182 of_node_put(np); /* Must decrement the refcount */ 183 return 0; 184 } 185 186 /* 187 * /proc/powerpc/ofdt - yucky binary interface for adding and removing 188 * OF device nodes. Should be deprecated as soon as we get an 189 * in-kernel wrapper for the RTAS ibm,configure-connector call. 190 */ 191 192 static void release_prop_list(const struct property *prop) 193 { 194 struct property *next; 195 for (; prop; prop = next) { 196 next = prop->next; 197 kfree(prop->name); 198 kfree(prop->value); 199 kfree(prop); 200 } 201 202 } 203 204 /** 205 * parse_next_property - process the next property from raw input buffer 206 * @buf: input buffer, must be nul-terminated 207 * @end: end of the input buffer + 1, for validation 208 * @name: return value; set to property name in buf 209 * @length: return value; set to length of value 210 * @value: return value; set to the property value in buf 211 * 212 * Note that the caller must make copies of the name and value returned, 213 * this function does no allocation or copying of the data. Return value 214 * is set to the next name in buf, or NULL on error. 215 */ 216 static char * parse_next_property(char *buf, char *end, char **name, int *length, 217 unsigned char **value) 218 { 219 char *tmp; 220 221 *name = buf; 222 223 tmp = strchr(buf, ' '); 224 if (!tmp) { 225 printk(KERN_ERR "property parse failed in %s at line %d\n", 226 __func__, __LINE__); 227 return NULL; 228 } 229 *tmp = '\0'; 230 231 if (++tmp >= end) { 232 printk(KERN_ERR "property parse failed in %s at line %d\n", 233 __func__, __LINE__); 234 return NULL; 235 } 236 237 /* now we're on the length */ 238 *length = -1; 239 *length = simple_strtoul(tmp, &tmp, 10); 240 if (*length == -1) { 241 printk(KERN_ERR "property parse failed in %s at line %d\n", 242 __func__, __LINE__); 243 return NULL; 244 } 245 if (*tmp != ' ' || ++tmp >= end) { 246 printk(KERN_ERR "property parse failed in %s at line %d\n", 247 __func__, __LINE__); 248 return NULL; 249 } 250 251 /* now we're on the value */ 252 *value = tmp; 253 tmp += *length; 254 if (tmp > end) { 255 printk(KERN_ERR "property parse failed in %s at line %d\n", 256 __func__, __LINE__); 257 return NULL; 258 } 259 else if (tmp < end && *tmp != ' ' && *tmp != '\0') { 260 printk(KERN_ERR "property parse failed in %s at line %d\n", 261 __func__, __LINE__); 262 return NULL; 263 } 264 tmp++; 265 266 /* and now we should be on the next name, or the end */ 267 return tmp; 268 } 269 270 static struct property *new_property(const char *name, const int length, 271 const unsigned char *value, struct property *last) 272 { 273 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL); 274 275 if (!new) 276 return NULL; 277 278 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL))) 279 goto cleanup; 280 if (!(new->value = kmalloc(length + 1, GFP_KERNEL))) 281 goto cleanup; 282 283 strcpy(new->name, name); 284 memcpy(new->value, value, length); 285 *(((char *)new->value) + length) = 0; 286 new->length = length; 287 new->next = last; 288 return new; 289 290 cleanup: 291 kfree(new->name); 292 kfree(new->value); 293 kfree(new); 294 return NULL; 295 } 296 297 static int do_add_node(char *buf, size_t bufsize) 298 { 299 char *path, *end, *name; 300 struct device_node *np; 301 struct property *prop = NULL; 302 unsigned char* value; 303 int length, rv = 0; 304 305 end = buf + bufsize; 306 path = buf; 307 buf = strchr(buf, ' '); 308 if (!buf) 309 return -EINVAL; 310 *buf = '\0'; 311 buf++; 312 313 if ((np = of_find_node_by_path(path))) { 314 of_node_put(np); 315 return -EINVAL; 316 } 317 318 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */ 319 while (buf < end && 320 (buf = parse_next_property(buf, end, &name, &length, &value))) { 321 struct property *last = prop; 322 323 prop = new_property(name, length, value, last); 324 if (!prop) { 325 rv = -ENOMEM; 326 prop = last; 327 goto out; 328 } 329 } 330 if (!buf) { 331 rv = -EINVAL; 332 goto out; 333 } 334 335 rv = pSeries_reconfig_add_node(path, prop); 336 337 out: 338 if (rv) 339 release_prop_list(prop); 340 return rv; 341 } 342 343 static int do_remove_node(char *buf) 344 { 345 struct device_node *node; 346 int rv = -ENODEV; 347 348 if ((node = of_find_node_by_path(buf))) 349 rv = pSeries_reconfig_remove_node(node); 350 351 of_node_put(node); 352 return rv; 353 } 354 355 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp) 356 { 357 char *handle_str; 358 phandle handle; 359 *npp = NULL; 360 361 handle_str = buf; 362 363 buf = strchr(buf, ' '); 364 if (!buf) 365 return NULL; 366 *buf = '\0'; 367 buf++; 368 369 handle = simple_strtoul(handle_str, NULL, 0); 370 371 *npp = of_find_node_by_phandle(handle); 372 return buf; 373 } 374 375 static int do_add_property(char *buf, size_t bufsize) 376 { 377 struct property *prop = NULL; 378 struct device_node *np; 379 unsigned char *value; 380 char *name, *end; 381 int length; 382 end = buf + bufsize; 383 buf = parse_node(buf, bufsize, &np); 384 385 if (!np) 386 return -ENODEV; 387 388 if (parse_next_property(buf, end, &name, &length, &value) == NULL) 389 return -EINVAL; 390 391 prop = new_property(name, length, value, NULL); 392 if (!prop) 393 return -ENOMEM; 394 395 prom_add_property(np, prop); 396 397 return 0; 398 } 399 400 static int do_remove_property(char *buf, size_t bufsize) 401 { 402 struct device_node *np; 403 char *tmp; 404 struct property *prop; 405 buf = parse_node(buf, bufsize, &np); 406 407 if (!np) 408 return -ENODEV; 409 410 tmp = strchr(buf,' '); 411 if (tmp) 412 *tmp = '\0'; 413 414 if (strlen(buf) == 0) 415 return -EINVAL; 416 417 prop = of_find_property(np, buf, NULL); 418 419 return prom_remove_property(np, prop); 420 } 421 422 static int do_update_property(char *buf, size_t bufsize) 423 { 424 struct device_node *np; 425 unsigned char *value; 426 char *name, *end, *next_prop; 427 int rc, length; 428 struct property *newprop, *oldprop; 429 buf = parse_node(buf, bufsize, &np); 430 end = buf + bufsize; 431 432 if (!np) 433 return -ENODEV; 434 435 next_prop = parse_next_property(buf, end, &name, &length, &value); 436 if (!next_prop) 437 return -EINVAL; 438 439 newprop = new_property(name, length, value, NULL); 440 if (!newprop) 441 return -ENOMEM; 442 443 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size")) 444 slb_set_size(*(int *)value); 445 446 oldprop = of_find_property(np, name,NULL); 447 if (!oldprop) { 448 if (strlen(name)) 449 return prom_add_property(np, newprop); 450 return -ENODEV; 451 } 452 453 rc = prom_update_property(np, newprop, oldprop); 454 if (rc) 455 return rc; 456 457 /* For memory under the ibm,dynamic-reconfiguration-memory node 458 * of the device tree, adding and removing memory is just an update 459 * to the ibm,dynamic-memory property instead of adding/removing a 460 * memory node in the device tree. For these cases we still need to 461 * involve the notifier chain. 462 */ 463 if (!strcmp(name, "ibm,dynamic-memory")) { 464 int action; 465 466 next_prop = parse_next_property(next_prop, end, &name, 467 &length, &value); 468 if (!next_prop) 469 return -EINVAL; 470 471 if (!strcmp(name, "add")) 472 action = PSERIES_DRCONF_MEM_ADD; 473 else 474 action = PSERIES_DRCONF_MEM_REMOVE; 475 476 rc = blocking_notifier_call_chain(&pSeries_reconfig_chain, 477 action, value); 478 if (rc == NOTIFY_BAD) { 479 rc = prom_update_property(np, oldprop, newprop); 480 return -ENOMEM; 481 } 482 } 483 484 return 0; 485 } 486 487 /** 488 * ofdt_write - perform operations on the Open Firmware device tree 489 * 490 * @file: not used 491 * @buf: command and arguments 492 * @count: size of the command buffer 493 * @off: not used 494 * 495 * Operations supported at this time are addition and removal of 496 * whole nodes along with their properties. Operations on individual 497 * properties are not implemented (yet). 498 */ 499 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count, 500 loff_t *off) 501 { 502 int rv = 0; 503 char *kbuf; 504 char *tmp; 505 506 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) { 507 rv = -ENOMEM; 508 goto out; 509 } 510 if (copy_from_user(kbuf, buf, count)) { 511 rv = -EFAULT; 512 goto out; 513 } 514 515 kbuf[count] = '\0'; 516 517 tmp = strchr(kbuf, ' '); 518 if (!tmp) { 519 rv = -EINVAL; 520 goto out; 521 } 522 *tmp = '\0'; 523 tmp++; 524 525 if (!strcmp(kbuf, "add_node")) 526 rv = do_add_node(tmp, count - (tmp - kbuf)); 527 else if (!strcmp(kbuf, "remove_node")) 528 rv = do_remove_node(tmp); 529 else if (!strcmp(kbuf, "add_property")) 530 rv = do_add_property(tmp, count - (tmp - kbuf)); 531 else if (!strcmp(kbuf, "remove_property")) 532 rv = do_remove_property(tmp, count - (tmp - kbuf)); 533 else if (!strcmp(kbuf, "update_property")) 534 rv = do_update_property(tmp, count - (tmp - kbuf)); 535 else 536 rv = -EINVAL; 537 out: 538 kfree(kbuf); 539 return rv ? rv : count; 540 } 541 542 static const struct file_operations ofdt_fops = { 543 .write = ofdt_write 544 }; 545 546 /* create /proc/powerpc/ofdt write-only by root */ 547 static int proc_ppc64_create_ofdt(void) 548 { 549 struct proc_dir_entry *ent; 550 551 if (!machine_is(pseries)) 552 return 0; 553 554 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops); 555 if (ent) 556 ent->size = 0; 557 558 return 0; 559 } 560 __initcall(proc_ppc64_create_ofdt); 561