rpaphp_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
  4. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  5. *
  6. * All rights reserved.
  7. *
  8. * Send feedback to <lxie@us.ibm.com>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_hotplug.h>
  16. #include <linux/smp.h>
  17. #include <linux/init.h>
  18. #include <linux/vmalloc.h>
  19. #include <asm/eeh.h> /* for eeh_add_device() */
  20. #include <asm/rtas.h> /* rtas_call */
  21. #include <asm/pci-bridge.h> /* for pci_controller */
  22. #include "../pci.h" /* for pci_add_new_bus */
  23. /* and pci_do_scan_bus */
  24. #include "rpaphp.h"
  25. bool rpaphp_debug;
  26. LIST_HEAD(rpaphp_slot_head);
  27. EXPORT_SYMBOL_GPL(rpaphp_slot_head);
  28. #define DRIVER_VERSION "0.1"
  29. #define DRIVER_AUTHOR "Linda Xie <lxie@us.ibm.com>"
  30. #define DRIVER_DESC "RPA HOT Plug PCI Controller Driver"
  31. #define MAX_LOC_CODE 128
  32. MODULE_AUTHOR(DRIVER_AUTHOR);
  33. MODULE_DESCRIPTION(DRIVER_DESC);
  34. MODULE_LICENSE("GPL");
  35. module_param_named(debug, rpaphp_debug, bool, 0644);
  36. /**
  37. * set_attention_status - set attention LED
  38. * @hotplug_slot: target &hotplug_slot
  39. * @value: LED control value
  40. *
  41. * echo 0 > attention -- set LED OFF
  42. * echo 1 > attention -- set LED ON
  43. * echo 2 > attention -- set LED ID(identify, light is blinking)
  44. */
  45. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
  46. {
  47. int rc;
  48. struct slot *slot = (struct slot *)hotplug_slot->private;
  49. switch (value) {
  50. case 0:
  51. case 1:
  52. case 2:
  53. break;
  54. default:
  55. value = 1;
  56. break;
  57. }
  58. rc = rtas_set_indicator(DR_INDICATOR, slot->index, value);
  59. if (!rc)
  60. hotplug_slot->info->attention_status = value;
  61. return rc;
  62. }
  63. /**
  64. * get_power_status - get power status of a slot
  65. * @hotplug_slot: slot to get status
  66. * @value: pointer to store status
  67. */
  68. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  69. {
  70. int retval, level;
  71. struct slot *slot = (struct slot *)hotplug_slot->private;
  72. retval = rtas_get_power_level(slot->power_domain, &level);
  73. if (!retval)
  74. *value = level;
  75. return retval;
  76. }
  77. /**
  78. * get_attention_status - get attention LED status
  79. * @hotplug_slot: slot to get status
  80. * @value: pointer to store status
  81. */
  82. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  83. {
  84. struct slot *slot = (struct slot *)hotplug_slot->private;
  85. *value = slot->hotplug_slot->info->attention_status;
  86. return 0;
  87. }
  88. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  89. {
  90. struct slot *slot = (struct slot *)hotplug_slot->private;
  91. int rc, state;
  92. rc = rpaphp_get_sensor_state(slot, &state);
  93. *value = NOT_VALID;
  94. if (rc)
  95. return rc;
  96. if (state == EMPTY)
  97. *value = EMPTY;
  98. else if (state == PRESENT)
  99. *value = slot->state;
  100. return 0;
  101. }
  102. static enum pci_bus_speed get_max_bus_speed(struct slot *slot)
  103. {
  104. enum pci_bus_speed speed;
  105. switch (slot->type) {
  106. case 1:
  107. case 2:
  108. case 3:
  109. case 4:
  110. case 5:
  111. case 6:
  112. speed = PCI_SPEED_33MHz; /* speed for case 1-6 */
  113. break;
  114. case 7:
  115. case 8:
  116. speed = PCI_SPEED_66MHz;
  117. break;
  118. case 11:
  119. case 14:
  120. speed = PCI_SPEED_66MHz_PCIX;
  121. break;
  122. case 12:
  123. case 15:
  124. speed = PCI_SPEED_100MHz_PCIX;
  125. break;
  126. case 13:
  127. case 16:
  128. speed = PCI_SPEED_133MHz_PCIX;
  129. break;
  130. default:
  131. speed = PCI_SPEED_UNKNOWN;
  132. break;
  133. }
  134. return speed;
  135. }
  136. static int get_children_props(struct device_node *dn, const int **drc_indexes,
  137. const int **drc_names, const int **drc_types,
  138. const int **drc_power_domains)
  139. {
  140. const int *indexes, *names, *types, *domains;
  141. indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
  142. names = of_get_property(dn, "ibm,drc-names", NULL);
  143. types = of_get_property(dn, "ibm,drc-types", NULL);
  144. domains = of_get_property(dn, "ibm,drc-power-domains", NULL);
  145. if (!indexes || !names || !types || !domains) {
  146. /* Slot does not have dynamically-removable children */
  147. return -EINVAL;
  148. }
  149. if (drc_indexes)
  150. *drc_indexes = indexes;
  151. if (drc_names)
  152. /* &drc_names[1] contains NULL terminated slot names */
  153. *drc_names = names;
  154. if (drc_types)
  155. /* &drc_types[1] contains NULL terminated slot types */
  156. *drc_types = types;
  157. if (drc_power_domains)
  158. *drc_power_domains = domains;
  159. return 0;
  160. }
  161. /* To get the DRC props describing the current node, first obtain it's
  162. * my-drc-index property. Next obtain the DRC list from it's parent. Use
  163. * the my-drc-index for correlation, and obtain the requested properties.
  164. */
  165. int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
  166. char **drc_name, char **drc_type, int *drc_power_domain)
  167. {
  168. const int *indexes, *names;
  169. const int *types, *domains;
  170. const unsigned int *my_index;
  171. char *name_tmp, *type_tmp;
  172. int i, rc;
  173. my_index = of_get_property(dn, "ibm,my-drc-index", NULL);
  174. if (!my_index) {
  175. /* Node isn't DLPAR/hotplug capable */
  176. return -EINVAL;
  177. }
  178. rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
  179. if (rc < 0) {
  180. return -EINVAL;
  181. }
  182. name_tmp = (char *) &names[1];
  183. type_tmp = (char *) &types[1];
  184. /* Iterate through parent properties, looking for my-drc-index */
  185. for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
  186. if ((unsigned int) indexes[i + 1] == *my_index) {
  187. if (drc_name)
  188. *drc_name = name_tmp;
  189. if (drc_type)
  190. *drc_type = type_tmp;
  191. if (drc_index)
  192. *drc_index = be32_to_cpu(*my_index);
  193. if (drc_power_domain)
  194. *drc_power_domain = be32_to_cpu(domains[i+1]);
  195. return 0;
  196. }
  197. name_tmp += (strlen(name_tmp) + 1);
  198. type_tmp += (strlen(type_tmp) + 1);
  199. }
  200. return -EINVAL;
  201. }
  202. EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);
  203. static int is_php_type(char *drc_type)
  204. {
  205. unsigned long value;
  206. char *endptr;
  207. /* PCI Hotplug nodes have an integer for drc_type */
  208. value = simple_strtoul(drc_type, &endptr, 10);
  209. if (endptr == drc_type)
  210. return 0;
  211. return 1;
  212. }
  213. /**
  214. * is_php_dn() - return 1 if this is a hotpluggable pci slot, else 0
  215. * @dn: target &device_node
  216. * @indexes: passed to get_children_props()
  217. * @names: passed to get_children_props()
  218. * @types: returned from get_children_props()
  219. * @power_domains:
  220. *
  221. * This routine will return true only if the device node is
  222. * a hotpluggable slot. This routine will return false
  223. * for built-in pci slots (even when the built-in slots are
  224. * dlparable.)
  225. */
  226. static int is_php_dn(struct device_node *dn, const int **indexes,
  227. const int **names, const int **types, const int **power_domains)
  228. {
  229. const int *drc_types;
  230. int rc;
  231. rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
  232. if (rc < 0)
  233. return 0;
  234. if (!is_php_type((char *) &drc_types[1]))
  235. return 0;
  236. *types = drc_types;
  237. return 1;
  238. }
  239. /**
  240. * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
  241. * @dn: device node of slot
  242. *
  243. * This subroutine will register a hotpluggable slot with the
  244. * PCI hotplug infrastructure. This routine is typically called
  245. * during boot time, if the hotplug slots are present at boot time,
  246. * or is called later, by the dlpar add code, if the slot is
  247. * being dynamically added during runtime.
  248. *
  249. * If the device node points at an embedded (built-in) slot, this
  250. * routine will just return without doing anything, since embedded
  251. * slots cannot be hotplugged.
  252. *
  253. * To remove a slot, it suffices to call rpaphp_deregister_slot().
  254. */
  255. int rpaphp_add_slot(struct device_node *dn)
  256. {
  257. struct slot *slot;
  258. int retval = 0;
  259. int i;
  260. const int *indexes, *names, *types, *power_domains;
  261. char *name, *type;
  262. if (!dn->name || strcmp(dn->name, "pci"))
  263. return 0;
  264. /* If this is not a hotplug slot, return without doing anything. */
  265. if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
  266. return 0;
  267. dbg("Entry %s: dn=%pOF\n", __func__, dn);
  268. /* register PCI devices */
  269. name = (char *) &names[1];
  270. type = (char *) &types[1];
  271. for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
  272. int index;
  273. index = be32_to_cpu(indexes[i + 1]);
  274. slot = alloc_slot_struct(dn, index, name,
  275. be32_to_cpu(power_domains[i + 1]));
  276. if (!slot)
  277. return -ENOMEM;
  278. slot->type = simple_strtoul(type, NULL, 10);
  279. dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
  280. index, name, type);
  281. retval = rpaphp_enable_slot(slot);
  282. if (!retval)
  283. retval = rpaphp_register_slot(slot);
  284. if (retval)
  285. dealloc_slot_struct(slot);
  286. name += strlen(name) + 1;
  287. type += strlen(type) + 1;
  288. }
  289. dbg("%s - Exit: rc[%d]\n", __func__, retval);
  290. /* XXX FIXME: reports a failure only if last entry in loop failed */
  291. return retval;
  292. }
  293. EXPORT_SYMBOL_GPL(rpaphp_add_slot);
  294. static void __exit cleanup_slots(void)
  295. {
  296. struct slot *slot, *next;
  297. /*
  298. * Unregister all of our slots with the pci_hotplug subsystem,
  299. * and free up all memory that we had allocated.
  300. * memory will be freed in release_slot callback.
  301. */
  302. list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
  303. rpaphp_slot_list) {
  304. list_del(&slot->rpaphp_slot_list);
  305. pci_hp_deregister(slot->hotplug_slot);
  306. }
  307. return;
  308. }
  309. static int __init rpaphp_init(void)
  310. {
  311. struct device_node *dn;
  312. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  313. for_each_node_by_name(dn, "pci")
  314. rpaphp_add_slot(dn);
  315. return 0;
  316. }
  317. static void __exit rpaphp_exit(void)
  318. {
  319. cleanup_slots();
  320. }
  321. static int enable_slot(struct hotplug_slot *hotplug_slot)
  322. {
  323. struct slot *slot = (struct slot *)hotplug_slot->private;
  324. int state;
  325. int retval;
  326. if (slot->state == CONFIGURED)
  327. return 0;
  328. retval = rpaphp_get_sensor_state(slot, &state);
  329. if (retval)
  330. return retval;
  331. if (state == PRESENT) {
  332. pci_lock_rescan_remove();
  333. pci_hp_add_devices(slot->bus);
  334. pci_unlock_rescan_remove();
  335. slot->state = CONFIGURED;
  336. } else if (state == EMPTY) {
  337. slot->state = EMPTY;
  338. } else {
  339. err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
  340. slot->state = NOT_VALID;
  341. return -EINVAL;
  342. }
  343. slot->bus->max_bus_speed = get_max_bus_speed(slot);
  344. return 0;
  345. }
  346. static int disable_slot(struct hotplug_slot *hotplug_slot)
  347. {
  348. struct slot *slot = (struct slot *)hotplug_slot->private;
  349. if (slot->state == NOT_CONFIGURED)
  350. return -EINVAL;
  351. pci_lock_rescan_remove();
  352. pci_hp_remove_devices(slot->bus);
  353. pci_unlock_rescan_remove();
  354. vm_unmap_aliases();
  355. slot->state = NOT_CONFIGURED;
  356. return 0;
  357. }
  358. struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
  359. .enable_slot = enable_slot,
  360. .disable_slot = disable_slot,
  361. .set_attention_status = set_attention_status,
  362. .get_power_status = get_power_status,
  363. .get_attention_status = get_attention_status,
  364. .get_adapter_status = get_adapter_status,
  365. };
  366. module_init(rpaphp_init);
  367. module_exit(rpaphp_exit);