dlpar.c 12 KB

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