dlpar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for dynamic reconfiguration for PCI, Memory, and CPU
  4. * Hotplug and Dynamic Logical Partitioning on RPA platforms.
  5. *
  6. * Copyright (C) 2009 Nathan Fontenot
  7. * Copyright (C) 2009 IBM Corporation
  8. */
  9. #define pr_fmt(fmt) "dlpar: " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/notifier.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/cpu.h>
  14. #include <linux/slab.h>
  15. #include <linux/of.h>
  16. #include "of_helpers.h"
  17. #include "pseries.h"
  18. #include <asm/machdep.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/rtas.h>
  21. static struct workqueue_struct *pseries_hp_wq;
  22. struct pseries_hp_work {
  23. struct work_struct work;
  24. struct pseries_hp_errorlog *errlog;
  25. };
  26. struct cc_workarea {
  27. __be32 drc_index;
  28. __be32 zero;
  29. __be32 name_offset;
  30. __be32 prop_length;
  31. __be32 prop_offset;
  32. };
  33. void dlpar_free_cc_property(struct property *prop)
  34. {
  35. kfree(prop->name);
  36. kfree(prop->value);
  37. kfree(prop);
  38. }
  39. static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
  40. {
  41. struct property *prop;
  42. char *name;
  43. char *value;
  44. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  45. if (!prop)
  46. return NULL;
  47. name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
  48. prop->name = kstrdup(name, GFP_KERNEL);
  49. if (!prop->name) {
  50. dlpar_free_cc_property(prop);
  51. return NULL;
  52. }
  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 ERR_CFG_USE -9003
  105. struct device_node *dlpar_configure_connector(__be32 drc_index,
  106. struct device_node *parent)
  107. {
  108. struct device_node *dn;
  109. struct device_node *first_dn = NULL;
  110. struct device_node *last_dn = NULL;
  111. struct property *property;
  112. struct property *last_property = NULL;
  113. struct cc_workarea *ccwa;
  114. char *data_buf;
  115. int cc_token;
  116. int rc = -1;
  117. cc_token = rtas_token("ibm,configure-connector");
  118. if (cc_token == RTAS_UNKNOWN_SERVICE)
  119. return NULL;
  120. data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  121. if (!data_buf)
  122. return NULL;
  123. ccwa = (struct cc_workarea *)&data_buf[0];
  124. ccwa->drc_index = drc_index;
  125. ccwa->zero = 0;
  126. do {
  127. /* Since we release the rtas_data_buf lock between configure
  128. * connector calls we want to re-populate the rtas_data_buffer
  129. * with the contents of the previous call.
  130. */
  131. spin_lock(&rtas_data_buf_lock);
  132. memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
  133. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  134. memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  135. spin_unlock(&rtas_data_buf_lock);
  136. if (rtas_busy_delay(rc))
  137. continue;
  138. switch (rc) {
  139. case COMPLETE:
  140. break;
  141. case NEXT_SIBLING:
  142. dn = dlpar_parse_cc_node(ccwa);
  143. if (!dn)
  144. goto cc_error;
  145. dn->parent = last_dn->parent;
  146. last_dn->sibling = dn;
  147. last_dn = dn;
  148. break;
  149. case NEXT_CHILD:
  150. dn = dlpar_parse_cc_node(ccwa);
  151. if (!dn)
  152. goto cc_error;
  153. if (!first_dn) {
  154. dn->parent = parent;
  155. first_dn = dn;
  156. } else {
  157. dn->parent = last_dn;
  158. if (last_dn)
  159. last_dn->child = dn;
  160. }
  161. last_dn = dn;
  162. break;
  163. case NEXT_PROPERTY:
  164. property = dlpar_parse_cc_property(ccwa);
  165. if (!property)
  166. goto cc_error;
  167. if (!last_dn->properties)
  168. last_dn->properties = property;
  169. else
  170. last_property->next = property;
  171. last_property = property;
  172. break;
  173. case PREV_PARENT:
  174. last_dn = last_dn->parent;
  175. break;
  176. case MORE_MEMORY:
  177. case ERR_CFG_USE:
  178. default:
  179. printk(KERN_ERR "Unexpected Error (%d) "
  180. "returned from configure-connector\n", rc);
  181. goto cc_error;
  182. }
  183. } while (rc);
  184. cc_error:
  185. kfree(data_buf);
  186. if (rc) {
  187. if (first_dn)
  188. dlpar_free_cc_nodes(first_dn);
  189. return NULL;
  190. }
  191. return first_dn;
  192. }
  193. int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
  194. {
  195. int rc;
  196. dn->parent = parent;
  197. rc = of_attach_node(dn);
  198. if (rc) {
  199. printk(KERN_ERR "Failed to add device node %pOF\n", dn);
  200. return rc;
  201. }
  202. return 0;
  203. }
  204. int dlpar_detach_node(struct device_node *dn)
  205. {
  206. struct device_node *child;
  207. int rc;
  208. child = of_get_next_child(dn, NULL);
  209. while (child) {
  210. dlpar_detach_node(child);
  211. child = of_get_next_child(dn, child);
  212. }
  213. rc = of_detach_node(dn);
  214. if (rc)
  215. return rc;
  216. of_node_put(dn);
  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_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
  232. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  233. return -1;
  234. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  235. if (rc)
  236. return rc;
  237. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  238. if (rc) {
  239. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  240. return rc;
  241. }
  242. return 0;
  243. }
  244. int dlpar_release_drc(u32 drc_index)
  245. {
  246. int dr_status, rc;
  247. rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
  248. if (rc || dr_status != DR_ENTITY_PRESENT)
  249. return -1;
  250. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  251. if (rc)
  252. return rc;
  253. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  254. if (rc) {
  255. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  256. return rc;
  257. }
  258. return 0;
  259. }
  260. int dlpar_unisolate_drc(u32 drc_index)
  261. {
  262. int dr_status, rc;
  263. rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
  264. if (rc || dr_status != DR_ENTITY_PRESENT)
  265. return -1;
  266. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  267. return 0;
  268. }
  269. int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
  270. {
  271. int rc;
  272. /* pseries error logs are in BE format, convert to cpu type */
  273. switch (hp_elog->id_type) {
  274. case PSERIES_HP_ELOG_ID_DRC_COUNT:
  275. hp_elog->_drc_u.drc_count =
  276. be32_to_cpu(hp_elog->_drc_u.drc_count);
  277. break;
  278. case PSERIES_HP_ELOG_ID_DRC_INDEX:
  279. hp_elog->_drc_u.drc_index =
  280. be32_to_cpu(hp_elog->_drc_u.drc_index);
  281. break;
  282. case PSERIES_HP_ELOG_ID_DRC_IC:
  283. hp_elog->_drc_u.ic.count =
  284. be32_to_cpu(hp_elog->_drc_u.ic.count);
  285. hp_elog->_drc_u.ic.index =
  286. be32_to_cpu(hp_elog->_drc_u.ic.index);
  287. }
  288. switch (hp_elog->resource) {
  289. case PSERIES_HP_ELOG_RESOURCE_MEM:
  290. rc = dlpar_memory(hp_elog);
  291. break;
  292. case PSERIES_HP_ELOG_RESOURCE_CPU:
  293. rc = dlpar_cpu(hp_elog);
  294. break;
  295. case PSERIES_HP_ELOG_RESOURCE_PMEM:
  296. rc = dlpar_hp_pmem(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. handle_dlpar_errorlog(hp_work->errlog);
  310. kfree(hp_work->errlog);
  311. kfree(work);
  312. }
  313. void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
  314. {
  315. struct pseries_hp_work *work;
  316. struct pseries_hp_errorlog *hp_errlog_copy;
  317. hp_errlog_copy = kmemdup(hp_errlog, sizeof(*hp_errlog), GFP_ATOMIC);
  318. if (!hp_errlog_copy)
  319. return;
  320. work = kmalloc(sizeof(struct pseries_hp_work), GFP_ATOMIC);
  321. if (work) {
  322. INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
  323. work->errlog = hp_errlog_copy;
  324. queue_work(pseries_hp_wq, (struct work_struct *)work);
  325. } else {
  326. kfree(hp_errlog_copy);
  327. }
  328. }
  329. static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
  330. {
  331. char *arg;
  332. arg = strsep(cmd, " ");
  333. if (!arg)
  334. return -EINVAL;
  335. if (sysfs_streq(arg, "memory")) {
  336. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  337. } else if (sysfs_streq(arg, "cpu")) {
  338. hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
  339. } else {
  340. pr_err("Invalid resource specified.\n");
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
  346. {
  347. char *arg;
  348. arg = strsep(cmd, " ");
  349. if (!arg)
  350. return -EINVAL;
  351. if (sysfs_streq(arg, "add")) {
  352. hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
  353. } else if (sysfs_streq(arg, "remove")) {
  354. hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
  355. } else {
  356. pr_err("Invalid action specified.\n");
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
  362. {
  363. char *arg;
  364. u32 count, index;
  365. arg = strsep(cmd, " ");
  366. if (!arg)
  367. return -EINVAL;
  368. if (sysfs_streq(arg, "indexed-count")) {
  369. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
  370. arg = strsep(cmd, " ");
  371. if (!arg) {
  372. pr_err("No DRC count specified.\n");
  373. return -EINVAL;
  374. }
  375. if (kstrtou32(arg, 0, &count)) {
  376. pr_err("Invalid DRC count specified.\n");
  377. return -EINVAL;
  378. }
  379. arg = strsep(cmd, " ");
  380. if (!arg) {
  381. pr_err("No DRC Index specified.\n");
  382. return -EINVAL;
  383. }
  384. if (kstrtou32(arg, 0, &index)) {
  385. pr_err("Invalid DRC Index specified.\n");
  386. return -EINVAL;
  387. }
  388. hp_elog->_drc_u.ic.count = cpu_to_be32(count);
  389. hp_elog->_drc_u.ic.index = cpu_to_be32(index);
  390. } else if (sysfs_streq(arg, "index")) {
  391. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  392. arg = strsep(cmd, " ");
  393. if (!arg) {
  394. pr_err("No DRC Index specified.\n");
  395. return -EINVAL;
  396. }
  397. if (kstrtou32(arg, 0, &index)) {
  398. pr_err("Invalid DRC Index specified.\n");
  399. return -EINVAL;
  400. }
  401. hp_elog->_drc_u.drc_index = cpu_to_be32(index);
  402. } else if (sysfs_streq(arg, "count")) {
  403. hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
  404. arg = strsep(cmd, " ");
  405. if (!arg) {
  406. pr_err("No DRC count specified.\n");
  407. return -EINVAL;
  408. }
  409. if (kstrtou32(arg, 0, &count)) {
  410. pr_err("Invalid DRC count specified.\n");
  411. return -EINVAL;
  412. }
  413. hp_elog->_drc_u.drc_count = cpu_to_be32(count);
  414. } else {
  415. pr_err("Invalid id_type specified.\n");
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
  421. const char *buf, size_t count)
  422. {
  423. struct pseries_hp_errorlog hp_elog;
  424. char *argbuf;
  425. char *args;
  426. int rc;
  427. args = argbuf = kstrdup(buf, GFP_KERNEL);
  428. if (!argbuf)
  429. return -ENOMEM;
  430. /*
  431. * Parse out the request from the user, this will be in the form:
  432. * <resource> <action> <id_type> <id>
  433. */
  434. rc = dlpar_parse_resource(&args, &hp_elog);
  435. if (rc)
  436. goto dlpar_store_out;
  437. rc = dlpar_parse_action(&args, &hp_elog);
  438. if (rc)
  439. goto dlpar_store_out;
  440. rc = dlpar_parse_id_type(&args, &hp_elog);
  441. if (rc)
  442. goto dlpar_store_out;
  443. rc = handle_dlpar_errorlog(&hp_elog);
  444. dlpar_store_out:
  445. kfree(argbuf);
  446. if (rc)
  447. pr_err("Could not handle DLPAR request \"%s\"\n", buf);
  448. return rc ? rc : count;
  449. }
  450. static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
  451. char *buf)
  452. {
  453. return sprintf(buf, "%s\n", "memory,cpu");
  454. }
  455. static CLASS_ATTR_RW(dlpar);
  456. int __init dlpar_workqueue_init(void)
  457. {
  458. if (pseries_hp_wq)
  459. return 0;
  460. pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
  461. WQ_UNBOUND, 1);
  462. return pseries_hp_wq ? 0 : -ENOMEM;
  463. }
  464. static int __init dlpar_sysfs_init(void)
  465. {
  466. int rc;
  467. rc = dlpar_workqueue_init();
  468. if (rc)
  469. return rc;
  470. return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
  471. }
  472. machine_device_initcall(pseries, dlpar_sysfs_init);