dlpar.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Support for dynamic reconfiguration for PCI, Memory, and CPU
  3. * Hotplug and Dynamic Logical Partitioning on RPA platforms.
  4. *
  5. * Copyright (C) 2009 Nathan Fontenot
  6. * Copyright (C) 2009 IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) "dlpar: " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/notifier.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/cpu.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include "offline_states.h"
  20. #include "pseries.h"
  21. #include <asm/prom.h>
  22. #include <asm/machdep.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/rtas.h>
  25. struct cc_workarea {
  26. __be32 drc_index;
  27. __be32 zero;
  28. __be32 name_offset;
  29. __be32 prop_length;
  30. __be32 prop_offset;
  31. };
  32. void dlpar_free_cc_property(struct property *prop)
  33. {
  34. kfree(prop->name);
  35. kfree(prop->value);
  36. kfree(prop);
  37. }
  38. static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
  39. {
  40. struct property *prop;
  41. char *name;
  42. char *value;
  43. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  44. if (!prop)
  45. return NULL;
  46. name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
  47. prop->name = kstrdup(name, GFP_KERNEL);
  48. prop->length = be32_to_cpu(ccwa->prop_length);
  49. value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
  50. prop->value = kmemdup(value, prop->length, GFP_KERNEL);
  51. if (!prop->value) {
  52. dlpar_free_cc_property(prop);
  53. return NULL;
  54. }
  55. return prop;
  56. }
  57. static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
  58. const char *path)
  59. {
  60. struct device_node *dn;
  61. char *name;
  62. /* If parent node path is "/" advance path to NULL terminator to
  63. * prevent double leading slashs in full_name.
  64. */
  65. if (!path[1])
  66. path++;
  67. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  68. if (!dn)
  69. return NULL;
  70. name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
  71. dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
  72. if (!dn->full_name) {
  73. kfree(dn);
  74. return NULL;
  75. }
  76. of_node_set_flag(dn, OF_DYNAMIC);
  77. of_node_init(dn);
  78. return dn;
  79. }
  80. static void dlpar_free_one_cc_node(struct device_node *dn)
  81. {
  82. struct property *prop;
  83. while (dn->properties) {
  84. prop = dn->properties;
  85. dn->properties = prop->next;
  86. dlpar_free_cc_property(prop);
  87. }
  88. kfree(dn->full_name);
  89. kfree(dn);
  90. }
  91. void dlpar_free_cc_nodes(struct device_node *dn)
  92. {
  93. if (dn->child)
  94. dlpar_free_cc_nodes(dn->child);
  95. if (dn->sibling)
  96. dlpar_free_cc_nodes(dn->sibling);
  97. dlpar_free_one_cc_node(dn);
  98. }
  99. #define COMPLETE 0
  100. #define NEXT_SIBLING 1
  101. #define NEXT_CHILD 2
  102. #define NEXT_PROPERTY 3
  103. #define PREV_PARENT 4
  104. #define MORE_MEMORY 5
  105. #define CALL_AGAIN -2
  106. #define ERR_CFG_USE -9003
  107. struct device_node *dlpar_configure_connector(__be32 drc_index,
  108. struct device_node *parent)
  109. {
  110. struct device_node *dn;
  111. struct device_node *first_dn = NULL;
  112. struct device_node *last_dn = NULL;
  113. struct property *property;
  114. struct property *last_property = NULL;
  115. struct cc_workarea *ccwa;
  116. char *data_buf;
  117. const char *parent_path = parent->full_name;
  118. int cc_token;
  119. int rc = -1;
  120. cc_token = rtas_token("ibm,configure-connector");
  121. if (cc_token == RTAS_UNKNOWN_SERVICE)
  122. return NULL;
  123. data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  124. if (!data_buf)
  125. return NULL;
  126. ccwa = (struct cc_workarea *)&data_buf[0];
  127. ccwa->drc_index = drc_index;
  128. ccwa->zero = 0;
  129. do {
  130. /* Since we release the rtas_data_buf lock between configure
  131. * connector calls we want to re-populate the rtas_data_buffer
  132. * with the contents of the previous call.
  133. */
  134. spin_lock(&rtas_data_buf_lock);
  135. memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
  136. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  137. memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  138. spin_unlock(&rtas_data_buf_lock);
  139. switch (rc) {
  140. case COMPLETE:
  141. break;
  142. case NEXT_SIBLING:
  143. dn = dlpar_parse_cc_node(ccwa, parent_path);
  144. if (!dn)
  145. goto cc_error;
  146. dn->parent = last_dn->parent;
  147. last_dn->sibling = dn;
  148. last_dn = dn;
  149. break;
  150. case NEXT_CHILD:
  151. if (first_dn)
  152. parent_path = last_dn->full_name;
  153. dn = dlpar_parse_cc_node(ccwa, parent_path);
  154. if (!dn)
  155. goto cc_error;
  156. if (!first_dn) {
  157. dn->parent = parent;
  158. first_dn = dn;
  159. } else {
  160. dn->parent = last_dn;
  161. if (last_dn)
  162. last_dn->child = dn;
  163. }
  164. last_dn = dn;
  165. break;
  166. case NEXT_PROPERTY:
  167. property = dlpar_parse_cc_property(ccwa);
  168. if (!property)
  169. goto cc_error;
  170. if (!last_dn->properties)
  171. last_dn->properties = property;
  172. else
  173. last_property->next = property;
  174. last_property = property;
  175. break;
  176. case PREV_PARENT:
  177. last_dn = last_dn->parent;
  178. parent_path = last_dn->parent->full_name;
  179. break;
  180. case CALL_AGAIN:
  181. break;
  182. case MORE_MEMORY:
  183. case ERR_CFG_USE:
  184. default:
  185. printk(KERN_ERR "Unexpected Error (%d) "
  186. "returned from configure-connector\n", rc);
  187. goto cc_error;
  188. }
  189. } while (rc);
  190. cc_error:
  191. kfree(data_buf);
  192. if (rc) {
  193. if (first_dn)
  194. dlpar_free_cc_nodes(first_dn);
  195. return NULL;
  196. }
  197. return first_dn;
  198. }
  199. static struct device_node *derive_parent(const char *path)
  200. {
  201. struct device_node *parent;
  202. char *last_slash;
  203. last_slash = strrchr(path, '/');
  204. if (last_slash == path) {
  205. parent = of_find_node_by_path("/");
  206. } else {
  207. char *parent_path;
  208. int parent_path_len = last_slash - path + 1;
  209. parent_path = kmalloc(parent_path_len, GFP_KERNEL);
  210. if (!parent_path)
  211. return NULL;
  212. strlcpy(parent_path, path, parent_path_len);
  213. parent = of_find_node_by_path(parent_path);
  214. kfree(parent_path);
  215. }
  216. return parent;
  217. }
  218. int dlpar_attach_node(struct device_node *dn)
  219. {
  220. int rc;
  221. dn->parent = derive_parent(dn->full_name);
  222. if (!dn->parent)
  223. return -ENOMEM;
  224. rc = of_attach_node(dn);
  225. if (rc) {
  226. printk(KERN_ERR "Failed to add device node %s\n",
  227. dn->full_name);
  228. return rc;
  229. }
  230. of_node_put(dn->parent);
  231. return 0;
  232. }
  233. int dlpar_detach_node(struct device_node *dn)
  234. {
  235. struct device_node *child;
  236. int rc;
  237. child = of_get_next_child(dn, NULL);
  238. while (child) {
  239. dlpar_detach_node(child);
  240. child = of_get_next_child(dn, child);
  241. }
  242. rc = of_detach_node(dn);
  243. if (rc)
  244. return rc;
  245. of_node_put(dn); /* Must decrement the refcount */
  246. return 0;
  247. }
  248. #define DR_ENTITY_SENSE 9003
  249. #define DR_ENTITY_PRESENT 1
  250. #define DR_ENTITY_UNUSABLE 2
  251. #define ALLOCATION_STATE 9003
  252. #define ALLOC_UNUSABLE 0
  253. #define ALLOC_USABLE 1
  254. #define ISOLATION_STATE 9001
  255. #define ISOLATE 0
  256. #define UNISOLATE 1
  257. int dlpar_acquire_drc(u32 drc_index)
  258. {
  259. int dr_status, rc;
  260. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  261. DR_ENTITY_SENSE, drc_index);
  262. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  263. return -1;
  264. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  265. if (rc)
  266. return rc;
  267. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  268. if (rc) {
  269. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  270. return rc;
  271. }
  272. return 0;
  273. }
  274. int dlpar_release_drc(u32 drc_index)
  275. {
  276. int dr_status, rc;
  277. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  278. DR_ENTITY_SENSE, drc_index);
  279. if (rc || dr_status != DR_ENTITY_PRESENT)
  280. return -1;
  281. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  282. if (rc)
  283. return rc;
  284. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  285. if (rc) {
  286. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  287. return rc;
  288. }
  289. return 0;
  290. }
  291. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  292. static int dlpar_online_cpu(struct device_node *dn)
  293. {
  294. int rc = 0;
  295. unsigned int cpu;
  296. int len, nthreads, i;
  297. const __be32 *intserv;
  298. u32 thread;
  299. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  300. if (!intserv)
  301. return -EINVAL;
  302. nthreads = len / sizeof(u32);
  303. cpu_maps_update_begin();
  304. for (i = 0; i < nthreads; i++) {
  305. thread = be32_to_cpu(intserv[i]);
  306. for_each_present_cpu(cpu) {
  307. if (get_hard_smp_processor_id(cpu) != thread)
  308. continue;
  309. BUG_ON(get_cpu_current_state(cpu)
  310. != CPU_STATE_OFFLINE);
  311. cpu_maps_update_done();
  312. rc = device_online(get_cpu_device(cpu));
  313. if (rc)
  314. goto out;
  315. cpu_maps_update_begin();
  316. break;
  317. }
  318. if (cpu == num_possible_cpus())
  319. printk(KERN_WARNING "Could not find cpu to online "
  320. "with physical id 0x%x\n", thread);
  321. }
  322. cpu_maps_update_done();
  323. out:
  324. return rc;
  325. }
  326. static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
  327. {
  328. struct device_node *dn, *parent;
  329. u32 drc_index;
  330. int rc;
  331. rc = kstrtou32(buf, 0, &drc_index);
  332. if (rc)
  333. return -EINVAL;
  334. rc = dlpar_acquire_drc(drc_index);
  335. if (rc)
  336. return -EINVAL;
  337. parent = of_find_node_by_path("/cpus");
  338. if (!parent)
  339. return -ENODEV;
  340. dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
  341. of_node_put(parent);
  342. if (!dn) {
  343. dlpar_release_drc(drc_index);
  344. return -EINVAL;
  345. }
  346. rc = dlpar_attach_node(dn);
  347. if (rc) {
  348. dlpar_release_drc(drc_index);
  349. dlpar_free_cc_nodes(dn);
  350. return rc;
  351. }
  352. rc = dlpar_online_cpu(dn);
  353. if (rc)
  354. return rc;
  355. return count;
  356. }
  357. static int dlpar_offline_cpu(struct device_node *dn)
  358. {
  359. int rc = 0;
  360. unsigned int cpu;
  361. int len, nthreads, i;
  362. const __be32 *intserv;
  363. u32 thread;
  364. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  365. if (!intserv)
  366. return -EINVAL;
  367. nthreads = len / sizeof(u32);
  368. cpu_maps_update_begin();
  369. for (i = 0; i < nthreads; i++) {
  370. thread = be32_to_cpu(intserv[i]);
  371. for_each_present_cpu(cpu) {
  372. if (get_hard_smp_processor_id(cpu) != thread)
  373. continue;
  374. if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
  375. break;
  376. if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
  377. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  378. cpu_maps_update_done();
  379. rc = device_offline(get_cpu_device(cpu));
  380. if (rc)
  381. goto out;
  382. cpu_maps_update_begin();
  383. break;
  384. }
  385. /*
  386. * The cpu is in CPU_STATE_INACTIVE.
  387. * Upgrade it's state to CPU_STATE_OFFLINE.
  388. */
  389. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  390. BUG_ON(plpar_hcall_norets(H_PROD, thread)
  391. != H_SUCCESS);
  392. __cpu_die(cpu);
  393. break;
  394. }
  395. if (cpu == num_possible_cpus())
  396. printk(KERN_WARNING "Could not find cpu to offline "
  397. "with physical id 0x%x\n", thread);
  398. }
  399. cpu_maps_update_done();
  400. out:
  401. return rc;
  402. }
  403. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  404. {
  405. struct device_node *dn;
  406. u32 drc_index;
  407. int rc;
  408. dn = of_find_node_by_path(buf);
  409. if (!dn)
  410. return -EINVAL;
  411. rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
  412. if (rc) {
  413. of_node_put(dn);
  414. return -EINVAL;
  415. }
  416. rc = dlpar_offline_cpu(dn);
  417. if (rc) {
  418. of_node_put(dn);
  419. return -EINVAL;
  420. }
  421. rc = dlpar_release_drc(drc_index);
  422. if (rc) {
  423. of_node_put(dn);
  424. return rc;
  425. }
  426. rc = dlpar_detach_node(dn);
  427. if (rc) {
  428. dlpar_acquire_drc(drc_index);
  429. return rc;
  430. }
  431. of_node_put(dn);
  432. return count;
  433. }
  434. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  435. static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  436. {
  437. int rc;
  438. /* pseries error logs are in BE format, convert to cpu type */
  439. switch (hp_elog->id_type) {
  440. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  441. hp_elog->_drc_u.drc_count =
  442. be32_to_cpu(hp_elog->_drc_u.drc_count);
  443. break;
  444. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  445. hp_elog->_drc_u.drc_index =
  446. be32_to_cpu(hp_elog->_drc_u.drc_index);
  447. }
  448. switch (hp_elog->resource) {
  449. case PSERIES_HP_ELOG_RESOURCE_MEM:
  450. rc = dlpar_memory(hp_elog);
  451. break;
  452. default:
  453. pr_warn_ratelimited("Invalid resource (%d) specified\n",
  454. hp_elog->resource);
  455. rc = -EINVAL;
  456. }
  457. return rc;
  458. }
  459. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  460. const char *buf, size_t count)
  461. {
  462. struct pseries_hp_errorlog *hp_elog;
  463. const char *arg;
  464. int rc;
  465. hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
  466. if (!hp_elog) {
  467. rc = -ENOMEM;
  468. goto dlpar_store_out;
  469. }
  470. /* Parse out the request from the user, this will be in the form
  471. * <resource> <action> <id_type> <id>
  472. */
  473. arg = buf;
  474. if (!strncmp(arg, "memory", 6)) {
  475. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  476. arg += strlen("memory ");
  477. } else {
  478. pr_err("Invalid resource specified: \"%s\"\n", buf);
  479. rc = -EINVAL;
  480. goto dlpar_store_out;
  481. }
  482. if (!strncmp(arg, "add", 3)) {
  483. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  484. arg += strlen("add ");
  485. } else if (!strncmp(arg, "remove", 6)) {
  486. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  487. arg += strlen("remove ");
  488. } else {
  489. pr_err("Invalid action specified: \"%s\"\n", buf);
  490. rc = -EINVAL;
  491. goto dlpar_store_out;
  492. }
  493. if (!strncmp(arg, "index", 5)) {
  494. u32 index;
  495. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  496. arg += strlen("index ");
  497. if (kstrtou32(arg, 0, &index)) {
  498. rc = -EINVAL;
  499. pr_err("Invalid drc_index specified: \"%s\"\n", buf);
  500. goto dlpar_store_out;
  501. }
  502. hp_elog->_drc_u.drc_index = cpu_to_be32(index);
  503. } else if (!strncmp(arg, "count", 5)) {
  504. u32 count;
  505. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  506. arg += strlen("count ");
  507. if (kstrtou32(arg, 0, &count)) {
  508. rc = -EINVAL;
  509. pr_err("Invalid count specified: \"%s\"\n", buf);
  510. goto dlpar_store_out;
  511. }
  512. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  513. } else {
  514. pr_err("Invalid id_type specified: \"%s\"\n", buf);
  515. rc = -EINVAL;
  516. goto dlpar_store_out;
  517. }
  518. rc = handle_dlpar_errorlog(hp_elog);
  519. dlpar_store_out:
  520. kfree(hp_elog);
  521. return rc ? rc : count;
  522. }
  523. static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
  524. static int __init pseries_dlpar_init(void)
  525. {
  526. int rc;
  527. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  528. ppc_md.cpu_probe = dlpar_cpu_probe;
  529. ppc_md.cpu_release = dlpar_cpu_release;
  530. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  531. rc = sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  532. return rc;
  533. }
  534. machine_device_initcall(pseries, pseries_dlpar_init);