dlpar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. 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. of_node_put(dn); /* Must decrement the refcount */
  234. return 0;
  235. }
  236. #define DR_ENTITY_SENSE 9003
  237. #define DR_ENTITY_PRESENT 1
  238. #define DR_ENTITY_UNUSABLE 2
  239. #define ALLOCATION_STATE 9003
  240. #define ALLOC_UNUSABLE 0
  241. #define ALLOC_USABLE 1
  242. #define ISOLATION_STATE 9001
  243. #define ISOLATE 0
  244. #define UNISOLATE 1
  245. int dlpar_acquire_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_UNUSABLE)
  251. return -1;
  252. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  253. if (rc)
  254. return rc;
  255. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  256. if (rc) {
  257. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  258. return rc;
  259. }
  260. return 0;
  261. }
  262. int dlpar_release_drc(u32 drc_index)
  263. {
  264. int dr_status, rc;
  265. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  266. DR_ENTITY_SENSE, drc_index);
  267. if (rc || dr_status != DR_ENTITY_PRESENT)
  268. return -1;
  269. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  270. if (rc)
  271. return rc;
  272. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  273. if (rc) {
  274. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  275. return rc;
  276. }
  277. return 0;
  278. }
  279. static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  280. {
  281. int rc;
  282. /* pseries error logs are in BE format, convert to cpu type */
  283. switch (hp_elog->id_type) {
  284. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  285. hp_elog->_drc_u.drc_count =
  286. be32_to_cpu(hp_elog->_drc_u.drc_count);
  287. break;
  288. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  289. hp_elog->_drc_u.drc_index =
  290. be32_to_cpu(hp_elog->_drc_u.drc_index);
  291. }
  292. switch (hp_elog->resource) {
  293. case PSERIES_HP_ELOG_RESOURCE_MEM:
  294. rc = dlpar_memory(hp_elog);
  295. break;
  296. case PSERIES_HP_ELOG_RESOURCE_CPU:
  297. rc = dlpar_cpu(hp_elog);
  298. break;
  299. default:
  300. pr_warn_ratelimited("Invalid resource (%d) specified\n",
  301. hp_elog->resource);
  302. rc = -EINVAL;
  303. }
  304. return rc;
  305. }
  306. static void pseries_hp_work_fn(struct work_struct *work)
  307. {
  308. struct pseries_hp_work *hp_work =
  309. container_of(work, struct pseries_hp_work, work);
  310. if (hp_work->rc)
  311. *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
  312. else
  313. handle_dlpar_errorlog(hp_work->errlog);
  314. if (hp_work->hp_completion)
  315. complete(hp_work->hp_completion);
  316. kfree(hp_work->errlog);
  317. kfree((void *)work);
  318. }
  319. void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
  320. struct completion *hotplug_done, int *rc)
  321. {
  322. struct pseries_hp_work *work;
  323. struct pseries_hp_errorlog *hp_errlog_copy;
  324. hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
  325. GFP_KERNEL);
  326. memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
  327. work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
  328. if (work) {
  329. INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
  330. work->errlog = hp_errlog_copy;
  331. work->hp_completion = hotplug_done;
  332. work->rc = rc;
  333. queue_work(pseries_hp_wq, (struct work_struct *)work);
  334. } else {
  335. *rc = -ENOMEM;
  336. kfree(hp_errlog_copy);
  337. complete(hotplug_done);
  338. }
  339. }
  340. static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
  341. {
  342. char *arg;
  343. arg = strsep(cmd, " ");
  344. if (!arg)
  345. return -EINVAL;
  346. if (sysfs_streq(arg, "memory")) {
  347. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  348. } else if (sysfs_streq(arg, "cpu")) {
  349. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
  350. } else {
  351. pr_err("Invalid resource specified.\n");
  352. return -EINVAL;
  353. }
  354. return 0;
  355. }
  356. static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
  357. {
  358. char *arg;
  359. arg = strsep(cmd, " ");
  360. if (!arg)
  361. return -EINVAL;
  362. if (sysfs_streq(arg, "add")) {
  363. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  364. } else if (sysfs_streq(arg, "remove")) {
  365. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  366. } else {
  367. pr_err("Invalid action specified.\n");
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
  373. {
  374. char *arg;
  375. u32 count, index;
  376. arg = strsep(cmd, " ");
  377. if (!arg)
  378. return -EINVAL;
  379. if (sysfs_streq(arg, "index")) {
  380. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  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.drc_index = cpu_to_be32(index);
  391. } else if (sysfs_streq(arg, "count")) {
  392. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  393. arg = strsep(cmd, " ");
  394. if (!arg) {
  395. pr_err("No DRC count specified.\n");
  396. return -EINVAL;
  397. }
  398. if (kstrtou32(arg, 0, &count)) {
  399. pr_err("Invalid DRC count specified.\n");
  400. return -EINVAL;
  401. }
  402. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  403. } else {
  404. pr_err("Invalid id_type specified.\n");
  405. return -EINVAL;
  406. }
  407. return 0;
  408. }
  409. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  410. const char *buf, size_t count)
  411. {
  412. struct pseries_hp_errorlog *hp_elog;
  413. struct completion hotplug_done;
  414. char *argbuf;
  415. char *args;
  416. int rc;
  417. args = argbuf = kstrdup(buf, GFP_KERNEL);
  418. hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
  419. if (!hp_elog || !argbuf) {
  420. pr_info("Could not allocate resources for DLPAR operation\n");
  421. kfree(argbuf);
  422. kfree(hp_elog);
  423. return -ENOMEM;
  424. }
  425. /*
  426. * Parse out the request from the user, this will be in the form:
  427. * <resource> <action> <id_type> <id>
  428. */
  429. rc = dlpar_parse_resource(&args, hp_elog);
  430. if (rc)
  431. goto dlpar_store_out;
  432. rc = dlpar_parse_action(&args, hp_elog);
  433. if (rc)
  434. goto dlpar_store_out;
  435. rc = dlpar_parse_id_type(&args, hp_elog);
  436. if (rc)
  437. goto dlpar_store_out;
  438. init_completion(&hotplug_done);
  439. queue_hotplug_event(hp_elog, &hotplug_done, &rc);
  440. wait_for_completion(&hotplug_done);
  441. dlpar_store_out:
  442. kfree(argbuf);
  443. kfree(hp_elog);
  444. if (rc)
  445. pr_err("Could not handle DLPAR request \"%s\"\n", buf);
  446. return rc ? rc : count;
  447. }
  448. static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
  449. static int __init pseries_dlpar_init(void)
  450. {
  451. pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
  452. WQ_UNBOUND, 1);
  453. return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  454. }
  455. machine_device_initcall(pseries, pseries_dlpar_init);