rpaphp_core.c 13 KB

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