dlpar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. static struct workqueue_struct *pseries_hp_wq;
  26. struct pseries_hp_work {
  27. struct work_struct work;
  28. struct pseries_hp_errorlog *errlog;
  29. struct completion *hp_completion;
  30. int *rc;
  31. };
  32. struct cc_workarea {
  33. __be32 drc_index;
  34. __be32 zero;
  35. __be32 name_offset;
  36. __be32 prop_length;
  37. __be32 prop_offset;
  38. };
  39. void dlpar_free_cc_property(struct property *prop)
  40. {
  41. kfree(prop->name);
  42. kfree(prop->value);
  43. kfree(prop);
  44. }
  45. static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
  46. {
  47. struct property *prop;
  48. char *name;
  49. char *value;
  50. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  51. if (!prop)
  52. return NULL;
  53. name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
  54. prop->name = kstrdup(name, GFP_KERNEL);
  55. prop->length = be32_to_cpu(ccwa->prop_length);
  56. value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
  57. prop->value = kmemdup(value, prop->length, GFP_KERNEL);
  58. if (!prop->value) {
  59. dlpar_free_cc_property(prop);
  60. return NULL;
  61. }
  62. return prop;
  63. }
  64. static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
  65. const char *path)
  66. {
  67. struct device_node *dn;
  68. char *name;
  69. /* If parent node path is "/" advance path to NULL terminator to
  70. * prevent double leading slashs in full_name.
  71. */
  72. if (!path[1])
  73. path++;
  74. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  75. if (!dn)
  76. return NULL;
  77. name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
  78. dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
  79. if (!dn->full_name) {
  80. kfree(dn);
  81. return NULL;
  82. }
  83. of_node_set_flag(dn, OF_DYNAMIC);
  84. of_node_init(dn);
  85. return dn;
  86. }
  87. static void dlpar_free_one_cc_node(struct device_node *dn)
  88. {
  89. struct property *prop;
  90. while (dn->properties) {
  91. prop = dn->properties;
  92. dn->properties = prop->next;
  93. dlpar_free_cc_property(prop);
  94. }
  95. kfree(dn->full_name);
  96. kfree(dn);
  97. }
  98. void dlpar_free_cc_nodes(struct device_node *dn)
  99. {
  100. if (dn->child)
  101. dlpar_free_cc_nodes(dn->child);
  102. if (dn->sibling)
  103. dlpar_free_cc_nodes(dn->sibling);
  104. dlpar_free_one_cc_node(dn);
  105. }
  106. #define COMPLETE 0
  107. #define NEXT_SIBLING 1
  108. #define NEXT_CHILD 2
  109. #define NEXT_PROPERTY 3
  110. #define PREV_PARENT 4
  111. #define MORE_MEMORY 5
  112. #define CALL_AGAIN -2
  113. #define ERR_CFG_USE -9003
  114. struct device_node *dlpar_configure_connector(__be32 drc_index,
  115. struct device_node *parent)
  116. {
  117. struct device_node *dn;
  118. struct device_node *first_dn = NULL;
  119. struct device_node *last_dn = NULL;
  120. struct property *property;
  121. struct property *last_property = NULL;
  122. struct cc_workarea *ccwa;
  123. char *data_buf;
  124. const char *parent_path = parent->full_name;
  125. int cc_token;
  126. int rc = -1;
  127. cc_token = rtas_token("ibm,configure-connector");
  128. if (cc_token == RTAS_UNKNOWN_SERVICE)
  129. return NULL;
  130. data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  131. if (!data_buf)
  132. return NULL;
  133. ccwa = (struct cc_workarea *)&data_buf[0];
  134. ccwa->drc_index = drc_index;
  135. ccwa->zero = 0;
  136. do {
  137. /* Since we release the rtas_data_buf lock between configure
  138. * connector calls we want to re-populate the rtas_data_buffer
  139. * with the contents of the previous call.
  140. */
  141. spin_lock(&rtas_data_buf_lock);
  142. memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
  143. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  144. memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  145. spin_unlock(&rtas_data_buf_lock);
  146. switch (rc) {
  147. case COMPLETE:
  148. break;
  149. case NEXT_SIBLING:
  150. dn = dlpar_parse_cc_node(ccwa, parent_path);
  151. if (!dn)
  152. goto cc_error;
  153. dn->parent = last_dn->parent;
  154. last_dn->sibling = dn;
  155. last_dn = dn;
  156. break;
  157. case NEXT_CHILD:
  158. if (first_dn)
  159. parent_path = last_dn->full_name;
  160. dn = dlpar_parse_cc_node(ccwa, parent_path);
  161. if (!dn)
  162. goto cc_error;
  163. if (!first_dn) {
  164. dn->parent = parent;
  165. first_dn = dn;
  166. } else {
  167. dn->parent = last_dn;
  168. if (last_dn)
  169. last_dn->child = dn;
  170. }
  171. last_dn = dn;
  172. break;
  173. case NEXT_PROPERTY:
  174. property = dlpar_parse_cc_property(ccwa);
  175. if (!property)
  176. goto cc_error;
  177. if (!last_dn->properties)
  178. last_dn->properties = property;
  179. else
  180. last_property->next = property;
  181. last_property = property;
  182. break;
  183. case PREV_PARENT:
  184. last_dn = last_dn->parent;
  185. parent_path = last_dn->parent->full_name;
  186. break;
  187. case CALL_AGAIN:
  188. break;
  189. case MORE_MEMORY:
  190. case ERR_CFG_USE:
  191. default:
  192. printk(KERN_ERR "Unexpected Error (%d) "
  193. "returned from configure-connector\n", rc);
  194. goto cc_error;
  195. }
  196. } while (rc);
  197. cc_error:
  198. kfree(data_buf);
  199. if (rc) {
  200. if (first_dn)
  201. dlpar_free_cc_nodes(first_dn);
  202. return NULL;
  203. }
  204. return first_dn;
  205. }
  206. int dlpar_attach_node(struct device_node *dn)
  207. {
  208. int rc;
  209. dn->parent = pseries_of_derive_parent(dn->full_name);
  210. if (IS_ERR(dn->parent))
  211. return PTR_ERR(dn->parent);
  212. rc = of_attach_node(dn);
  213. if (rc) {
  214. printk(KERN_ERR "Failed to add device node %s\n",
  215. dn->full_name);
  216. return rc;
  217. }
  218. of_node_put(dn->parent);
  219. return 0;
  220. }
  221. int dlpar_detach_node(struct device_node *dn)
  222. {
  223. struct device_node *child;
  224. int rc;
  225. child = of_get_next_child(dn, NULL);
  226. while (child) {
  227. dlpar_detach_node(child);
  228. child = of_get_next_child(dn, child);
  229. }
  230. rc = of_detach_node(dn);
  231. if (rc)
  232. return rc;
  233. return 0;
  234. }
  235. #define DR_ENTITY_SENSE 9003
  236. #define DR_ENTITY_PRESENT 1
  237. #define DR_ENTITY_UNUSABLE 2
  238. #define ALLOCATION_STATE 9003
  239. #define ALLOC_UNUSABLE 0
  240. #define ALLOC_USABLE 1
  241. #define ISOLATION_STATE 9001
  242. #define ISOLATE 0
  243. #define UNISOLATE 1
  244. int dlpar_acquire_drc(u32 drc_index)
  245. {
  246. int dr_status, rc;
  247. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  248. DR_ENTITY_SENSE, drc_index);
  249. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  250. return -1;
  251. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  252. if (rc)
  253. return rc;
  254. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  255. if (rc) {
  256. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  257. return rc;
  258. }
  259. return 0;
  260. }
  261. int dlpar_release_drc(u32 drc_index)
  262. {
  263. int dr_status, rc;
  264. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  265. DR_ENTITY_SENSE, drc_index);
  266. if (rc || dr_status != DR_ENTITY_PRESENT)
  267. return -1;
  268. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  269. if (rc)
  270. return rc;
  271. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  272. if (rc) {
  273. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  274. return rc;
  275. }
  276. return 0;
  277. }
  278. static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  279. {
  280. int rc;
  281. /* pseries error logs are in BE format, convert to cpu type */
  282. switch (hp_elog->id_type) {
  283. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  284. hp_elog->_drc_u.drc_count =
  285. be32_to_cpu(hp_elog->_drc_u.drc_count);
  286. break;
  287. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  288. hp_elog->_drc_u.drc_index =
  289. be32_to_cpu(hp_elog->_drc_u.drc_index);
  290. }
  291. switch (hp_elog->resource) {
  292. case PSERIES_HP_ELOG_RESOURCE_MEM:
  293. rc = dlpar_memory(hp_elog);
  294. break;
  295. case PSERIES_HP_ELOG_RESOURCE_CPU:
  296. rc = dlpar_cpu(hp_elog);
  297. break;
  298. default:
  299. pr_warn_ratelimited("Invalid resource (%d) specified\n",
  300. hp_elog->resource);
  301. rc = -EINVAL;
  302. }
  303. return rc;
  304. }
  305. static void pseries_hp_work_fn(struct work_struct *work)
  306. {
  307. struct pseries_hp_work *hp_work =
  308. container_of(work, struct pseries_hp_work, work);
  309. if (hp_work->rc)
  310. *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
  311. else
  312. handle_dlpar_errorlog(hp_work->errlog);
  313. if (hp_work->hp_completion)
  314. complete(hp_work->hp_completion);
  315. kfree(hp_work->errlog);
  316. kfree((void *)work);
  317. }
  318. void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
  319. struct completion *hotplug_done, int *rc)
  320. {
  321. struct pseries_hp_work *work;
  322. struct pseries_hp_errorlog *hp_errlog_copy;
  323. hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
  324. GFP_KERNEL);
  325. memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
  326. work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
  327. if (work) {
  328. INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
  329. work->errlog = hp_errlog_copy;
  330. work->hp_completion = hotplug_done;
  331. work->rc = rc;
  332. queue_work(pseries_hp_wq, (struct work_struct *)work);
  333. } else {
  334. *rc = -ENOMEM;
  335. kfree(hp_errlog_copy);
  336. complete(hotplug_done);
  337. }
  338. }
  339. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  340. const char *buf, size_t count)
  341. {
  342. struct pseries_hp_errorlog *hp_elog;
  343. struct completion hotplug_done;
  344. const char *arg;
  345. int rc;
  346. hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
  347. if (!hp_elog) {
  348. rc = -ENOMEM;
  349. goto dlpar_store_out;
  350. }
  351. /* Parse out the request from the user, this will be in the form
  352. * <resource> <action> <id_type> <id>
  353. */
  354. arg = buf;
  355. if (!strncmp(arg, "memory", 6)) {
  356. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  357. arg += strlen("memory ");
  358. } else if (!strncmp(arg, "cpu", 3)) {
  359. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
  360. arg += strlen("cpu ");
  361. } else {
  362. pr_err("Invalid resource specified: \"%s\"\n", buf);
  363. rc = -EINVAL;
  364. goto dlpar_store_out;
  365. }
  366. if (!strncmp(arg, "add", 3)) {
  367. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  368. arg += strlen("add ");
  369. } else if (!strncmp(arg, "remove", 6)) {
  370. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  371. arg += strlen("remove ");
  372. } else {
  373. pr_err("Invalid action specified: \"%s\"\n", buf);
  374. rc = -EINVAL;
  375. goto dlpar_store_out;
  376. }
  377. if (!strncmp(arg, "index", 5)) {
  378. u32 index;
  379. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  380. arg += strlen("index ");
  381. if (kstrtou32(arg, 0, &index)) {
  382. rc = -EINVAL;
  383. pr_err("Invalid drc_index specified: \"%s\"\n", buf);
  384. goto dlpar_store_out;
  385. }
  386. hp_elog->_drc_u.drc_index = cpu_to_be32(index);
  387. } else if (!strncmp(arg, "count", 5)) {
  388. u32 count;
  389. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  390. arg += strlen("count ");
  391. if (kstrtou32(arg, 0, &count)) {
  392. rc = -EINVAL;
  393. pr_err("Invalid count specified: \"%s\"\n", buf);
  394. goto dlpar_store_out;
  395. }
  396. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  397. } else {
  398. pr_err("Invalid id_type specified: \"%s\"\n", buf);
  399. rc = -EINVAL;
  400. goto dlpar_store_out;
  401. }
  402. init_completion(&hotplug_done);
  403. queue_hotplug_event(hp_elog, &hotplug_done, &rc);
  404. wait_for_completion(&hotplug_done);
  405. dlpar_store_out:
  406. kfree(hp_elog);
  407. return rc ? rc : count;
  408. }
  409. static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
  410. static int __init pseries_dlpar_init(void)
  411. {
  412. pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
  413. WQ_UNBOUND, 1);
  414. return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  415. }
  416. machine_device_initcall(pseries, pseries_dlpar_init);