dlpar.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <linux/kernel.h>
  13. #include <linux/kref.h>
  14. #include <linux/notifier.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/cpu.h>
  18. #include <asm/prom.h>
  19. #include <asm/machdep.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/rtas.h>
  22. #include <asm/pSeries_reconfig.h>
  23. struct cc_workarea {
  24. u32 drc_index;
  25. u32 zero;
  26. u32 name_offset;
  27. u32 prop_length;
  28. u32 prop_offset;
  29. };
  30. static void dlpar_free_cc_property(struct property *prop)
  31. {
  32. kfree(prop->name);
  33. kfree(prop->value);
  34. kfree(prop);
  35. }
  36. static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
  37. {
  38. struct property *prop;
  39. char *name;
  40. char *value;
  41. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  42. if (!prop)
  43. return NULL;
  44. name = (char *)ccwa + ccwa->name_offset;
  45. prop->name = kstrdup(name, GFP_KERNEL);
  46. prop->length = ccwa->prop_length;
  47. value = (char *)ccwa + ccwa->prop_offset;
  48. prop->value = kzalloc(prop->length, GFP_KERNEL);
  49. if (!prop->value) {
  50. dlpar_free_cc_property(prop);
  51. return NULL;
  52. }
  53. memcpy(prop->value, value, prop->length);
  54. return prop;
  55. }
  56. static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
  57. {
  58. struct device_node *dn;
  59. char *name;
  60. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  61. if (!dn)
  62. return NULL;
  63. /* The configure connector reported name does not contain a
  64. * preceeding '/', so we allocate a buffer large enough to
  65. * prepend this to the full_name.
  66. */
  67. name = (char *)ccwa + ccwa->name_offset;
  68. dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL);
  69. if (!dn->full_name) {
  70. kfree(dn);
  71. return NULL;
  72. }
  73. sprintf(dn->full_name, "/%s", name);
  74. return dn;
  75. }
  76. static void dlpar_free_one_cc_node(struct device_node *dn)
  77. {
  78. struct property *prop;
  79. while (dn->properties) {
  80. prop = dn->properties;
  81. dn->properties = prop->next;
  82. dlpar_free_cc_property(prop);
  83. }
  84. kfree(dn->full_name);
  85. kfree(dn);
  86. }
  87. static void dlpar_free_cc_nodes(struct device_node *dn)
  88. {
  89. if (dn->child)
  90. dlpar_free_cc_nodes(dn->child);
  91. if (dn->sibling)
  92. dlpar_free_cc_nodes(dn->sibling);
  93. dlpar_free_one_cc_node(dn);
  94. }
  95. #define NEXT_SIBLING 1
  96. #define NEXT_CHILD 2
  97. #define NEXT_PROPERTY 3
  98. #define PREV_PARENT 4
  99. #define MORE_MEMORY 5
  100. #define CALL_AGAIN -2
  101. #define ERR_CFG_USE -9003
  102. struct device_node *dlpar_configure_connector(u32 drc_index)
  103. {
  104. struct device_node *dn;
  105. struct device_node *first_dn = NULL;
  106. struct device_node *last_dn = NULL;
  107. struct property *property;
  108. struct property *last_property = NULL;
  109. struct cc_workarea *ccwa;
  110. int cc_token;
  111. int rc;
  112. cc_token = rtas_token("ibm,configure-connector");
  113. if (cc_token == RTAS_UNKNOWN_SERVICE)
  114. return NULL;
  115. spin_lock(&rtas_data_buf_lock);
  116. ccwa = (struct cc_workarea *)&rtas_data_buf[0];
  117. ccwa->drc_index = drc_index;
  118. ccwa->zero = 0;
  119. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  120. while (rc) {
  121. switch (rc) {
  122. case NEXT_SIBLING:
  123. dn = dlpar_parse_cc_node(ccwa);
  124. if (!dn)
  125. goto cc_error;
  126. dn->parent = last_dn->parent;
  127. last_dn->sibling = dn;
  128. last_dn = dn;
  129. break;
  130. case NEXT_CHILD:
  131. dn = dlpar_parse_cc_node(ccwa);
  132. if (!dn)
  133. goto cc_error;
  134. if (!first_dn)
  135. first_dn = dn;
  136. else {
  137. dn->parent = last_dn;
  138. if (last_dn)
  139. last_dn->child = dn;
  140. }
  141. last_dn = dn;
  142. break;
  143. case NEXT_PROPERTY:
  144. property = dlpar_parse_cc_property(ccwa);
  145. if (!property)
  146. goto cc_error;
  147. if (!last_dn->properties)
  148. last_dn->properties = property;
  149. else
  150. last_property->next = property;
  151. last_property = property;
  152. break;
  153. case PREV_PARENT:
  154. last_dn = last_dn->parent;
  155. break;
  156. case CALL_AGAIN:
  157. break;
  158. case MORE_MEMORY:
  159. case ERR_CFG_USE:
  160. default:
  161. printk(KERN_ERR "Unexpected Error (%d) "
  162. "returned from configure-connector\n", rc);
  163. goto cc_error;
  164. }
  165. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  166. }
  167. spin_unlock(&rtas_data_buf_lock);
  168. return first_dn;
  169. cc_error:
  170. if (first_dn)
  171. dlpar_free_cc_nodes(first_dn);
  172. spin_unlock(&rtas_data_buf_lock);
  173. return NULL;
  174. }
  175. static struct device_node *derive_parent(const char *path)
  176. {
  177. struct device_node *parent;
  178. char *last_slash;
  179. last_slash = strrchr(path, '/');
  180. if (last_slash == path) {
  181. parent = of_find_node_by_path("/");
  182. } else {
  183. char *parent_path;
  184. int parent_path_len = last_slash - path + 1;
  185. parent_path = kmalloc(parent_path_len, GFP_KERNEL);
  186. if (!parent_path)
  187. return NULL;
  188. strlcpy(parent_path, path, parent_path_len);
  189. parent = of_find_node_by_path(parent_path);
  190. kfree(parent_path);
  191. }
  192. return parent;
  193. }
  194. int dlpar_attach_node(struct device_node *dn)
  195. {
  196. struct proc_dir_entry *ent;
  197. int rc;
  198. of_node_set_flag(dn, OF_DYNAMIC);
  199. kref_init(&dn->kref);
  200. dn->parent = derive_parent(dn->full_name);
  201. if (!dn->parent)
  202. return -ENOMEM;
  203. rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
  204. PSERIES_RECONFIG_ADD, dn);
  205. if (rc == NOTIFY_BAD) {
  206. printk(KERN_ERR "Failed to add device node %s\n",
  207. dn->full_name);
  208. return -ENOMEM; /* For now, safe to assume kmalloc failure */
  209. }
  210. of_attach_node(dn);
  211. #ifdef CONFIG_PROC_DEVICETREE
  212. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  213. if (ent)
  214. proc_device_tree_add_node(dn, ent);
  215. #endif
  216. of_node_put(dn->parent);
  217. return 0;
  218. }
  219. int dlpar_detach_node(struct device_node *dn)
  220. {
  221. struct device_node *parent = dn->parent;
  222. struct property *prop = dn->properties;
  223. #ifdef CONFIG_PROC_DEVICETREE
  224. while (prop) {
  225. remove_proc_entry(prop->name, dn->pde);
  226. prop = prop->next;
  227. }
  228. if (dn->pde)
  229. remove_proc_entry(dn->pde->name, parent->pde);
  230. #endif
  231. blocking_notifier_call_chain(&pSeries_reconfig_chain,
  232. PSERIES_RECONFIG_REMOVE, dn);
  233. of_detach_node(dn);
  234. of_node_put(dn); /* Must decrement the refcount */
  235. return 0;
  236. }
  237. #define DR_ENTITY_SENSE 9003
  238. #define DR_ENTITY_PRESENT 1
  239. #define DR_ENTITY_UNUSABLE 2
  240. #define ALLOCATION_STATE 9003
  241. #define ALLOC_UNUSABLE 0
  242. #define ALLOC_USABLE 1
  243. #define ISOLATION_STATE 9001
  244. #define ISOLATE 0
  245. #define UNISOLATE 1
  246. int dlpar_acquire_drc(u32 drc_index)
  247. {
  248. int dr_status, rc;
  249. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  250. DR_ENTITY_SENSE, drc_index);
  251. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  252. return -1;
  253. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  254. if (rc)
  255. return rc;
  256. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  257. if (rc) {
  258. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  259. return rc;
  260. }
  261. return 0;
  262. }
  263. int dlpar_release_drc(u32 drc_index)
  264. {
  265. int dr_status, rc;
  266. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  267. DR_ENTITY_SENSE, drc_index);
  268. if (rc || dr_status != DR_ENTITY_PRESENT)
  269. return -1;
  270. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  271. if (rc)
  272. return rc;
  273. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  274. if (rc) {
  275. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  276. return rc;
  277. }
  278. return 0;
  279. }