rpaphp_core.c 12 KB

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