dlpar.c 13 KB

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