dlpar.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "of_helpers.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. int dlpar_attach_node(struct device_node *dn)
  200. {
  201. int rc;
  202. dn->parent = pseries_of_derive_parent(dn->full_name);
  203. if (IS_ERR(dn->parent))
  204. return PTR_ERR(dn->parent);
  205. rc = of_attach_node(dn);
  206. if (rc) {
  207. printk(KERN_ERR "Failed to add device node %s\n",
  208. dn->full_name);
  209. return rc;
  210. }
  211. of_node_put(dn->parent);
  212. return 0;
  213. }
  214. int dlpar_detach_node(struct device_node *dn)
  215. {
  216. struct device_node *child;
  217. int rc;
  218. child = of_get_next_child(dn, NULL);
  219. while (child) {
  220. dlpar_detach_node(child);
  221. child = of_get_next_child(dn, child);
  222. }
  223. rc = of_detach_node(dn);
  224. if (rc)
  225. return rc;
  226. of_node_put(dn); /* Must decrement the refcount */
  227. return 0;
  228. }
  229. #define DR_ENTITY_SENSE 9003
  230. #define DR_ENTITY_PRESENT 1
  231. #define DR_ENTITY_UNUSABLE 2
  232. #define ALLOCATION_STATE 9003
  233. #define ALLOC_UNUSABLE 0
  234. #define ALLOC_USABLE 1
  235. #define ISOLATION_STATE 9001
  236. #define ISOLATE 0
  237. #define UNISOLATE 1
  238. int dlpar_acquire_drc(u32 drc_index)
  239. {
  240. int dr_status, rc;
  241. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  242. DR_ENTITY_SENSE, drc_index);
  243. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  244. return -1;
  245. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  246. if (rc)
  247. return rc;
  248. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  249. if (rc) {
  250. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  251. return rc;
  252. }
  253. return 0;
  254. }
  255. int dlpar_release_drc(u32 drc_index)
  256. {
  257. int dr_status, rc;
  258. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  259. DR_ENTITY_SENSE, drc_index);
  260. if (rc || dr_status != DR_ENTITY_PRESENT)
  261. return -1;
  262. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  263. if (rc)
  264. return rc;
  265. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  266. if (rc) {
  267. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  268. return rc;
  269. }
  270. return 0;
  271. }
  272. static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  273. {
  274. int rc;
  275. /* pseries error logs are in BE format, convert to cpu type */
  276. switch (hp_elog->id_type) {
  277. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  278. hp_elog->_drc_u.drc_count =
  279. be32_to_cpu(hp_elog->_drc_u.drc_count);
  280. break;
  281. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  282. hp_elog->_drc_u.drc_index =
  283. be32_to_cpu(hp_elog->_drc_u.drc_index);
  284. }
  285. switch (hp_elog->resource) {
  286. case PSERIES_HP_ELOG_RESOURCE_MEM:
  287. rc = dlpar_memory(hp_elog);
  288. break;
  289. case PSERIES_HP_ELOG_RESOURCE_CPU:
  290. rc = dlpar_cpu(hp_elog);
  291. break;
  292. default:
  293. pr_warn_ratelimited("Invalid resource (%d) specified\n",
  294. hp_elog->resource);
  295. rc = -EINVAL;
  296. }
  297. return rc;
  298. }
  299. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  300. const char *buf, size_t count)
  301. {
  302. struct pseries_hp_errorlog *hp_elog;
  303. const char *arg;
  304. int rc;
  305. hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
  306. if (!hp_elog) {
  307. rc = -ENOMEM;
  308. goto dlpar_store_out;
  309. }
  310. /* Parse out the request from the user, this will be in the form
  311. * <resource> <action> <id_type> <id>
  312. */
  313. arg = buf;
  314. if (!strncmp(arg, "memory", 6)) {
  315. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  316. arg += strlen("memory ");
  317. } else if (!strncmp(arg, "cpu", 3)) {
  318. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
  319. arg += strlen("cpu ");
  320. } else {
  321. pr_err("Invalid resource specified: \"%s\"\n", buf);
  322. rc = -EINVAL;
  323. goto dlpar_store_out;
  324. }
  325. if (!strncmp(arg, "add", 3)) {
  326. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  327. arg += strlen("add ");
  328. } else if (!strncmp(arg, "remove", 6)) {
  329. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  330. arg += strlen("remove ");
  331. } else {
  332. pr_err("Invalid action specified: \"%s\"\n", buf);
  333. rc = -EINVAL;
  334. goto dlpar_store_out;
  335. }
  336. if (!strncmp(arg, "index", 5)) {
  337. u32 index;
  338. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  339. arg += strlen("index ");
  340. if (kstrtou32(arg, 0, &index)) {
  341. rc = -EINVAL;
  342. pr_err("Invalid drc_index specified: \"%s\"\n", buf);
  343. goto dlpar_store_out;
  344. }
  345. hp_elog->_drc_u.drc_index = cpu_to_be32(index);
  346. } else if (!strncmp(arg, "count", 5)) {
  347. u32 count;
  348. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  349. arg += strlen("count ");
  350. if (kstrtou32(arg, 0, &count)) {
  351. rc = -EINVAL;
  352. pr_err("Invalid count specified: \"%s\"\n", buf);
  353. goto dlpar_store_out;
  354. }
  355. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  356. } else {
  357. pr_err("Invalid id_type specified: \"%s\"\n", buf);
  358. rc = -EINVAL;
  359. goto dlpar_store_out;
  360. }
  361. rc = handle_dlpar_errorlog(hp_elog);
  362. dlpar_store_out:
  363. kfree(hp_elog);
  364. return rc ? rc : count;
  365. }
  366. static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
  367. static int __init pseries_dlpar_init(void)
  368. {
  369. return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  370. }
  371. machine_device_initcall(pseries, pseries_dlpar_init);