rpaphp_core.c 11 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 int **drc_indexes,
  138. const int **drc_names, const int **drc_types,
  139. const int **drc_power_domains)
  140. {
  141. const int *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 int *indexes, *names;
  172. const int *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 ((unsigned int) 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, fndit;
  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. for (j = 0; j < entries; j++) {
  208. of_read_drc_info_cell(&info, &value, &drc);
  209. /* Should now know end of current entry */
  210. if (my_index > drc.last_drc_index)
  211. continue;
  212. fndit = 1;
  213. break;
  214. }
  215. /* Found it */
  216. if (fndit)
  217. sprintf(cell_drc_name, "%s%d", drc.drc_name_prefix,
  218. my_index);
  219. if (((drc_name == NULL) ||
  220. (drc_name && !strcmp(drc_name, cell_drc_name))) &&
  221. ((drc_type == NULL) ||
  222. (drc_type && !strcmp(drc_type, drc.drc_type))))
  223. return 0;
  224. return -EINVAL;
  225. }
  226. int rpaphp_check_drc_props(struct device_node *dn, char *drc_name,
  227. char *drc_type)
  228. {
  229. const unsigned int *my_index;
  230. my_index = of_get_property(dn, "ibm,my-drc-index", NULL);
  231. if (!my_index) {
  232. /* Node isn't DLPAR/hotplug capable */
  233. return -EINVAL;
  234. }
  235. if (firmware_has_feature(FW_FEATURE_DRC_INFO))
  236. return rpaphp_check_drc_props_v2(dn, drc_name, drc_type,
  237. *my_index);
  238. else
  239. return rpaphp_check_drc_props_v1(dn, drc_name, drc_type,
  240. *my_index);
  241. }
  242. EXPORT_SYMBOL_GPL(rpaphp_check_drc_props);
  243. static int is_php_type(char *drc_type)
  244. {
  245. unsigned long value;
  246. char *endptr;
  247. /* PCI Hotplug nodes have an integer for drc_type */
  248. value = simple_strtoul(drc_type, &endptr, 10);
  249. if (endptr == drc_type)
  250. return 0;
  251. return 1;
  252. }
  253. /**
  254. * is_php_dn() - return 1 if this is a hotpluggable pci slot, else 0
  255. * @dn: target &device_node
  256. * @indexes: passed to get_children_props()
  257. * @names: passed to get_children_props()
  258. * @types: returned from get_children_props()
  259. * @power_domains:
  260. *
  261. * This routine will return true only if the device node is
  262. * a hotpluggable slot. This routine will return false
  263. * for built-in pci slots (even when the built-in slots are
  264. * dlparable.)
  265. */
  266. static int is_php_dn(struct device_node *dn, const int **indexes,
  267. const int **names, const int **types, const int **power_domains)
  268. {
  269. const int *drc_types;
  270. int rc;
  271. rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
  272. if (rc < 0)
  273. return 0;
  274. if (!is_php_type((char *) &drc_types[1]))
  275. return 0;
  276. *types = drc_types;
  277. return 1;
  278. }
  279. /**
  280. * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
  281. * @dn: device node of slot
  282. *
  283. * This subroutine will register a hotpluggable slot with the
  284. * PCI hotplug infrastructure. This routine is typically called
  285. * during boot time, if the hotplug slots are present at boot time,
  286. * or is called later, by the dlpar add code, if the slot is
  287. * being dynamically added during runtime.
  288. *
  289. * If the device node points at an embedded (built-in) slot, this
  290. * routine will just return without doing anything, since embedded
  291. * slots cannot be hotplugged.
  292. *
  293. * To remove a slot, it suffices to call rpaphp_deregister_slot().
  294. */
  295. int rpaphp_add_slot(struct device_node *dn)
  296. {
  297. struct slot *slot;
  298. int retval = 0;
  299. int i;
  300. const int *indexes, *names, *types, *power_domains;
  301. char *name, *type;
  302. if (!dn->name || strcmp(dn->name, "pci"))
  303. return 0;
  304. /* If this is not a hotplug slot, return without doing anything. */
  305. if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
  306. return 0;
  307. dbg("Entry %s: dn=%pOF\n", __func__, dn);
  308. /* register PCI devices */
  309. name = (char *) &names[1];
  310. type = (char *) &types[1];
  311. for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
  312. int index;
  313. index = be32_to_cpu(indexes[i + 1]);
  314. slot = alloc_slot_struct(dn, index, name,
  315. be32_to_cpu(power_domains[i + 1]));
  316. if (!slot)
  317. return -ENOMEM;
  318. slot->type = simple_strtoul(type, NULL, 10);
  319. dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
  320. index, name, type);
  321. retval = rpaphp_enable_slot(slot);
  322. if (!retval)
  323. retval = rpaphp_register_slot(slot);
  324. if (retval)
  325. dealloc_slot_struct(slot);
  326. name += strlen(name) + 1;
  327. type += strlen(type) + 1;
  328. }
  329. dbg("%s - Exit: rc[%d]\n", __func__, retval);
  330. /* XXX FIXME: reports a failure only if last entry in loop failed */
  331. return retval;
  332. }
  333. EXPORT_SYMBOL_GPL(rpaphp_add_slot);
  334. static void __exit cleanup_slots(void)
  335. {
  336. struct slot *slot, *next;
  337. /*
  338. * Unregister all of our slots with the pci_hotplug subsystem,
  339. * and free up all memory that we had allocated.
  340. */
  341. list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
  342. rpaphp_slot_list) {
  343. list_del(&slot->rpaphp_slot_list);
  344. pci_hp_deregister(slot->hotplug_slot);
  345. dealloc_slot_struct(slot);
  346. }
  347. return;
  348. }
  349. static int __init rpaphp_init(void)
  350. {
  351. struct device_node *dn;
  352. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  353. for_each_node_by_name(dn, "pci")
  354. rpaphp_add_slot(dn);
  355. return 0;
  356. }
  357. static void __exit rpaphp_exit(void)
  358. {
  359. cleanup_slots();
  360. }
  361. static int enable_slot(struct hotplug_slot *hotplug_slot)
  362. {
  363. struct slot *slot = (struct slot *)hotplug_slot->private;
  364. int state;
  365. int retval;
  366. if (slot->state == CONFIGURED)
  367. return 0;
  368. retval = rpaphp_get_sensor_state(slot, &state);
  369. if (retval)
  370. return retval;
  371. if (state == PRESENT) {
  372. pci_lock_rescan_remove();
  373. pci_hp_add_devices(slot->bus);
  374. pci_unlock_rescan_remove();
  375. slot->state = CONFIGURED;
  376. } else if (state == EMPTY) {
  377. slot->state = EMPTY;
  378. } else {
  379. err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
  380. slot->state = NOT_VALID;
  381. return -EINVAL;
  382. }
  383. slot->bus->max_bus_speed = get_max_bus_speed(slot);
  384. return 0;
  385. }
  386. static int disable_slot(struct hotplug_slot *hotplug_slot)
  387. {
  388. struct slot *slot = (struct slot *)hotplug_slot->private;
  389. if (slot->state == NOT_CONFIGURED)
  390. return -EINVAL;
  391. pci_lock_rescan_remove();
  392. pci_hp_remove_devices(slot->bus);
  393. pci_unlock_rescan_remove();
  394. vm_unmap_aliases();
  395. slot->state = NOT_CONFIGURED;
  396. return 0;
  397. }
  398. struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
  399. .enable_slot = enable_slot,
  400. .disable_slot = disable_slot,
  401. .set_attention_status = set_attention_status,
  402. .get_power_status = get_power_status,
  403. .get_attention_status = get_attention_status,
  404. .get_adapter_status = get_adapter_status,
  405. };
  406. module_init(rpaphp_init);
  407. module_exit(rpaphp_exit);