dlpar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 <linux/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. {
  66. struct device_node *dn;
  67. const char *name;
  68. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  69. if (!dn)
  70. return NULL;
  71. name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
  72. dn->full_name = kstrdup(name, GFP_KERNEL);
  73. if (!dn->full_name) {
  74. kfree(dn);
  75. return NULL;
  76. }
  77. of_node_set_flag(dn, OF_DYNAMIC);
  78. of_node_init(dn);
  79. return dn;
  80. }
  81. static void dlpar_free_one_cc_node(struct device_node *dn)
  82. {
  83. struct property *prop;
  84. while (dn->properties) {
  85. prop = dn->properties;
  86. dn->properties = prop->next;
  87. dlpar_free_cc_property(prop);
  88. }
  89. kfree(dn->full_name);
  90. kfree(dn);
  91. }
  92. void dlpar_free_cc_nodes(struct device_node *dn)
  93. {
  94. if (dn->child)
  95. dlpar_free_cc_nodes(dn->child);
  96. if (dn->sibling)
  97. dlpar_free_cc_nodes(dn->sibling);
  98. dlpar_free_one_cc_node(dn);
  99. }
  100. #define COMPLETE 0
  101. #define NEXT_SIBLING 1
  102. #define NEXT_CHILD 2
  103. #define NEXT_PROPERTY 3
  104. #define PREV_PARENT 4
  105. #define MORE_MEMORY 5
  106. #define CALL_AGAIN -2
  107. #define ERR_CFG_USE -9003
  108. struct device_node *dlpar_configure_connector(__be32 drc_index,
  109. struct device_node *parent)
  110. {
  111. struct device_node *dn;
  112. struct device_node *first_dn = NULL;
  113. struct device_node *last_dn = NULL;
  114. struct property *property;
  115. struct property *last_property = NULL;
  116. struct cc_workarea *ccwa;
  117. char *data_buf;
  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);
  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. dn = dlpar_parse_cc_node(ccwa);
  152. if (!dn)
  153. goto cc_error;
  154. if (!first_dn) {
  155. dn->parent = parent;
  156. first_dn = dn;
  157. } else {
  158. dn->parent = last_dn;
  159. if (last_dn)
  160. last_dn->child = dn;
  161. }
  162. last_dn = dn;
  163. break;
  164. case NEXT_PROPERTY:
  165. property = dlpar_parse_cc_property(ccwa);
  166. if (!property)
  167. goto cc_error;
  168. if (!last_dn->properties)
  169. last_dn->properties = property;
  170. else
  171. last_property->next = property;
  172. last_property = property;
  173. break;
  174. case PREV_PARENT:
  175. last_dn = last_dn->parent;
  176. break;
  177. case CALL_AGAIN:
  178. break;
  179. case MORE_MEMORY:
  180. case ERR_CFG_USE:
  181. default:
  182. printk(KERN_ERR "Unexpected Error (%d) "
  183. "returned from configure-connector\n", rc);
  184. goto cc_error;
  185. }
  186. } while (rc);
  187. cc_error:
  188. kfree(data_buf);
  189. if (rc) {
  190. if (first_dn)
  191. dlpar_free_cc_nodes(first_dn);
  192. return NULL;
  193. }
  194. return first_dn;
  195. }
  196. int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
  197. {
  198. int rc;
  199. dn->parent = parent;
  200. rc = of_attach_node(dn);
  201. if (rc) {
  202. printk(KERN_ERR "Failed to add device node %pOF\n", dn);
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. int dlpar_detach_node(struct device_node *dn)
  208. {
  209. struct device_node *child;
  210. int rc;
  211. child = of_get_next_child(dn, NULL);
  212. while (child) {
  213. dlpar_detach_node(child);
  214. child = of_get_next_child(dn, child);
  215. }
  216. rc = of_detach_node(dn);
  217. if (rc)
  218. return rc;
  219. return 0;
  220. }
  221. #define DR_ENTITY_SENSE 9003
  222. #define DR_ENTITY_PRESENT 1
  223. #define DR_ENTITY_UNUSABLE 2
  224. #define ALLOCATION_STATE 9003
  225. #define ALLOC_UNUSABLE 0
  226. #define ALLOC_USABLE 1
  227. #define ISOLATION_STATE 9001
  228. #define ISOLATE 0
  229. #define UNISOLATE 1
  230. int dlpar_acquire_drc(u32 drc_index)
  231. {
  232. int dr_status, rc;
  233. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  234. DR_ENTITY_SENSE, drc_index);
  235. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  236. return -1;
  237. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  238. if (rc)
  239. return rc;
  240. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  241. if (rc) {
  242. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  243. return rc;
  244. }
  245. return 0;
  246. }
  247. int dlpar_release_drc(u32 drc_index)
  248. {
  249. int dr_status, rc;
  250. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  251. DR_ENTITY_SENSE, drc_index);
  252. if (rc || dr_status != DR_ENTITY_PRESENT)
  253. return -1;
  254. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  255. if (rc)
  256. return rc;
  257. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  258. if (rc) {
  259. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  260. return rc;
  261. }
  262. return 0;
  263. }
  264. static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  265. {
  266. int rc;
  267. /* pseries error logs are in BE format, convert to cpu type */
  268. switch (hp_elog->id_type) {
  269. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  270. hp_elog->_drc_u.drc_count =
  271. be32_to_cpu(hp_elog->_drc_u.drc_count);
  272. break;
  273. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  274. hp_elog->_drc_u.drc_index =
  275. be32_to_cpu(hp_elog->_drc_u.drc_index);
  276. break;
  277. case PSERIES_HP_ELOG_ID_DRC_IC:
  278. hp_elog->_drc_u.ic.count =
  279. be32_to_cpu(hp_elog->_drc_u.ic.count);
  280. hp_elog->_drc_u.ic.index =
  281. be32_to_cpu(hp_elog->_drc_u.ic.index);
  282. }
  283. switch (hp_elog->resource) {
  284. case PSERIES_HP_ELOG_RESOURCE_MEM:
  285. rc = dlpar_memory(hp_elog);
  286. break;
  287. case PSERIES_HP_ELOG_RESOURCE_CPU:
  288. rc = dlpar_cpu(hp_elog);
  289. break;
  290. default:
  291. pr_warn_ratelimited("Invalid resource (%d) specified\n",
  292. hp_elog->resource);
  293. rc = -EINVAL;
  294. }
  295. return rc;
  296. }
  297. static void pseries_hp_work_fn(struct work_struct *work)
  298. {
  299. struct pseries_hp_work *hp_work =
  300. container_of(work, struct pseries_hp_work, work);
  301. if (hp_work->rc)
  302. *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
  303. else
  304. handle_dlpar_errorlog(hp_work->errlog);
  305. if (hp_work->hp_completion)
  306. complete(hp_work->hp_completion);
  307. kfree(hp_work->errlog);
  308. kfree((void *)work);
  309. }
  310. void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
  311. struct completion *hotplug_done, int *rc)
  312. {
  313. struct pseries_hp_work *work;
  314. struct pseries_hp_errorlog *hp_errlog_copy;
  315. hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
  316. GFP_KERNEL);
  317. memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
  318. work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
  319. if (work) {
  320. INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
  321. work->errlog = hp_errlog_copy;
  322. work->hp_completion = hotplug_done;
  323. work->rc = rc;
  324. queue_work(pseries_hp_wq, (struct work_struct *)work);
  325. } else {
  326. *rc = -ENOMEM;
  327. kfree(hp_errlog_copy);
  328. complete(hotplug_done);
  329. }
  330. }
  331. static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
  332. {
  333. char *arg;
  334. arg = strsep(cmd, " ");
  335. if (!arg)
  336. return -EINVAL;
  337. if (sysfs_streq(arg, "memory")) {
  338. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  339. } else if (sysfs_streq(arg, "cpu")) {
  340. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
  341. } else {
  342. pr_err("Invalid resource specified.\n");
  343. return -EINVAL;
  344. }
  345. return 0;
  346. }
  347. static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
  348. {
  349. char *arg;
  350. arg = strsep(cmd, " ");
  351. if (!arg)
  352. return -EINVAL;
  353. if (sysfs_streq(arg, "add")) {
  354. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  355. } else if (sysfs_streq(arg, "remove")) {
  356. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  357. } else {
  358. pr_err("Invalid action specified.\n");
  359. return -EINVAL;
  360. }
  361. return 0;
  362. }
  363. static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
  364. {
  365. char *arg;
  366. u32 count, index;
  367. arg = strsep(cmd, " ");
  368. if (!arg)
  369. return -EINVAL;
  370. if (sysfs_streq(arg, "indexed-count")) {
  371. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
  372. arg = strsep(cmd, " ");
  373. if (!arg) {
  374. pr_err("No DRC count specified.\n");
  375. return -EINVAL;
  376. }
  377. if (kstrtou32(arg, 0, &count)) {
  378. pr_err("Invalid DRC count specified.\n");
  379. return -EINVAL;
  380. }
  381. arg = strsep(cmd, " ");
  382. if (!arg) {
  383. pr_err("No DRC Index specified.\n");
  384. return -EINVAL;
  385. }
  386. if (kstrtou32(arg, 0, &index)) {
  387. pr_err("Invalid DRC Index specified.\n");
  388. return -EINVAL;
  389. }
  390. hp_elog->_drc_u.ic.count = cpu_to_be32(count);
  391. hp_elog->_drc_u.ic.index = cpu_to_be32(index);
  392. } else if (sysfs_streq(arg, "index")) {
  393. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  394. arg = strsep(cmd, " ");
  395. if (!arg) {
  396. pr_err("No DRC Index specified.\n");
  397. return -EINVAL;
  398. }
  399. if (kstrtou32(arg, 0, &index)) {
  400. pr_err("Invalid DRC Index specified.\n");
  401. return -EINVAL;
  402. }
  403. hp_elog->_drc_u.drc_index = cpu_to_be32(index);
  404. } else if (sysfs_streq(arg, "count")) {
  405. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  406. arg = strsep(cmd, " ");
  407. if (!arg) {
  408. pr_err("No DRC count specified.\n");
  409. return -EINVAL;
  410. }
  411. if (kstrtou32(arg, 0, &count)) {
  412. pr_err("Invalid DRC count specified.\n");
  413. return -EINVAL;
  414. }
  415. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  416. } else {
  417. pr_err("Invalid id_type specified.\n");
  418. return -EINVAL;
  419. }
  420. return 0;
  421. }
  422. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  423. const char *buf, size_t count)
  424. {
  425. struct pseries_hp_errorlog *hp_elog;
  426. struct completion hotplug_done;
  427. char *argbuf;
  428. char *args;
  429. int rc;
  430. args = argbuf = kstrdup(buf, GFP_KERNEL);
  431. hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
  432. if (!hp_elog || !argbuf) {
  433. pr_info("Could not allocate resources for DLPAR operation\n");
  434. kfree(argbuf);
  435. kfree(hp_elog);
  436. return -ENOMEM;
  437. }
  438. /*
  439. * Parse out the request from the user, this will be in the form:
  440. * <resource> <action> <id_type> <id>
  441. */
  442. rc = dlpar_parse_resource(&args, hp_elog);
  443. if (rc)
  444. goto dlpar_store_out;
  445. rc = dlpar_parse_action(&args, hp_elog);
  446. if (rc)
  447. goto dlpar_store_out;
  448. rc = dlpar_parse_id_type(&args, hp_elog);
  449. if (rc)
  450. goto dlpar_store_out;
  451. init_completion(&hotplug_done);
  452. queue_hotplug_event(hp_elog, &hotplug_done, &rc);
  453. wait_for_completion(&hotplug_done);
  454. dlpar_store_out:
  455. kfree(argbuf);
  456. kfree(hp_elog);
  457. if (rc)
  458. pr_err("Could not handle DLPAR request \"%s\"\n", buf);
  459. return rc ? rc : count;
  460. }
  461. static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
  462. char *buf)
  463. {
  464. return sprintf(buf, "%s\n", "memory,cpu");
  465. }
  466. static CLASS_ATTR_RW(dlpar);
  467. int __init dlpar_workqueue_init(void)
  468. {
  469. if (pseries_hp_wq)
  470. return 0;
  471. pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
  472. WQ_UNBOUND, 1);
  473. return pseries_hp_wq ? 0 : -ENOMEM;
  474. }
  475. static int __init dlpar_sysfs_init(void)
  476. {
  477. int rc;
  478. rc = dlpar_workqueue_init();
  479. if (rc)
  480. return rc;
  481. return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  482. }
  483. machine_device_initcall(pseries, dlpar_sysfs_init);