PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/pci/hotplug/rpaphp_core.c

https://github.com/kvaneesh/linux
C | 539 lines | 368 code | 92 blank | 79 comment | 70 complexity | af5a2b1c3f9559ebcd9ff1a7269bef5a MD5 | raw file
  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 = to_slot(hotplug_slot);
  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. slot->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 = to_slot(hotplug_slot);
  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 = to_slot(hotplug_slot);
  86. *value = slot->attention_status;
  87. return 0;
  88. }
  89. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  90. {
  91. struct slot *slot = to_slot(hotplug_slot);
  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 its my-drc-index property. Next,
  164. * obtain the DRC info from its 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. char *endptr;
  247. /* PCI Hotplug nodes have an integer for drc_type */
  248. 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 __be32 **indexes,
  267. const __be32 **names, const __be32 **types,
  268. const __be32 **power_domains)
  269. {
  270. const __be32 *drc_types;
  271. int rc;
  272. rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
  273. if (rc < 0)
  274. return 0;
  275. if (!is_php_type((char *) &drc_types[1]))
  276. return 0;
  277. *types = drc_types;
  278. return 1;
  279. }
  280. static int rpaphp_drc_info_add_slot(struct device_node *dn)
  281. {
  282. struct slot *slot;
  283. struct property *info;
  284. struct of_drc_info drc;
  285. char drc_name[MAX_DRC_NAME_LEN];
  286. const __be32 *cur;
  287. u32 count;
  288. int retval = 0;
  289. info = of_find_property(dn, "ibm,drc-info", NULL);
  290. if (!info)
  291. return 0;
  292. cur = of_prop_next_u32(info, NULL, &count);
  293. if (cur)
  294. cur++;
  295. else
  296. return 0;
  297. of_read_drc_info_cell(&info, &cur, &drc);
  298. if (!is_php_type(drc.drc_type))
  299. return 0;
  300. sprintf(drc_name, "%s%d", drc.drc_name_prefix, drc.drc_name_suffix_start);
  301. slot = alloc_slot_struct(dn, drc.drc_index_start, drc_name, drc.drc_power_domain);
  302. if (!slot)
  303. return -ENOMEM;
  304. slot->type = simple_strtoul(drc.drc_type, NULL, 10);
  305. retval = rpaphp_enable_slot(slot);
  306. if (!retval)
  307. retval = rpaphp_register_slot(slot);
  308. if (retval)
  309. dealloc_slot_struct(slot);
  310. return retval;
  311. }
  312. static int rpaphp_drc_add_slot(struct device_node *dn)
  313. {
  314. struct slot *slot;
  315. int retval = 0;
  316. int i;
  317. const __be32 *indexes, *names, *types, *power_domains;
  318. char *name, *type;
  319. /* If this is not a hotplug slot, return without doing anything. */
  320. if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
  321. return 0;
  322. dbg("Entry %s: dn=%pOF\n", __func__, dn);
  323. /* register PCI devices */
  324. name = (char *) &names[1];
  325. type = (char *) &types[1];
  326. for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
  327. int index;
  328. index = be32_to_cpu(indexes[i + 1]);
  329. slot = alloc_slot_struct(dn, index, name,
  330. be32_to_cpu(power_domains[i + 1]));
  331. if (!slot)
  332. return -ENOMEM;
  333. slot->type = simple_strtoul(type, NULL, 10);
  334. dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
  335. index, name, type);
  336. retval = rpaphp_enable_slot(slot);
  337. if (!retval)
  338. retval = rpaphp_register_slot(slot);
  339. if (retval)
  340. dealloc_slot_struct(slot);
  341. name += strlen(name) + 1;
  342. type += strlen(type) + 1;
  343. }
  344. dbg("%s - Exit: rc[%d]\n", __func__, retval);
  345. /* XXX FIXME: reports a failure only if last entry in loop failed */
  346. return retval;
  347. }
  348. /**
  349. * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
  350. * @dn: device node of slot
  351. *
  352. * This subroutine will register a hotpluggable slot with the
  353. * PCI hotplug infrastructure. This routine is typically called
  354. * during boot time, if the hotplug slots are present at boot time,
  355. * or is called later, by the dlpar add code, if the slot is
  356. * being dynamically added during runtime.
  357. *
  358. * If the device node points at an embedded (built-in) slot, this
  359. * routine will just return without doing anything, since embedded
  360. * slots cannot be hotplugged.
  361. *
  362. * To remove a slot, it suffices to call rpaphp_deregister_slot().
  363. */
  364. int rpaphp_add_slot(struct device_node *dn)
  365. {
  366. if (!of_node_name_eq(dn, "pci"))
  367. return 0;
  368. if (of_find_property(dn, "ibm,drc-info", NULL))
  369. return rpaphp_drc_info_add_slot(dn);
  370. else
  371. return rpaphp_drc_add_slot(dn);
  372. }
  373. EXPORT_SYMBOL_GPL(rpaphp_add_slot);
  374. static void __exit cleanup_slots(void)
  375. {
  376. struct slot *slot, *next;
  377. /*
  378. * Unregister all of our slots with the pci_hotplug subsystem,
  379. * and free up all memory that we had allocated.
  380. */
  381. list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
  382. rpaphp_slot_list) {
  383. list_del(&slot->rpaphp_slot_list);
  384. pci_hp_deregister(&slot->hotplug_slot);
  385. dealloc_slot_struct(slot);
  386. }
  387. }
  388. static int __init rpaphp_init(void)
  389. {
  390. struct device_node *dn;
  391. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  392. for_each_node_by_name(dn, "pci")
  393. rpaphp_add_slot(dn);
  394. return 0;
  395. }
  396. static void __exit rpaphp_exit(void)
  397. {
  398. cleanup_slots();
  399. }
  400. static int enable_slot(struct hotplug_slot *hotplug_slot)
  401. {
  402. struct slot *slot = to_slot(hotplug_slot);
  403. int state;
  404. int retval;
  405. if (slot->state == CONFIGURED)
  406. return 0;
  407. retval = rpaphp_get_sensor_state(slot, &state);
  408. if (retval)
  409. return retval;
  410. if (state == PRESENT) {
  411. pseries_eeh_init_edev_recursive(PCI_DN(slot->dn));
  412. pci_lock_rescan_remove();
  413. pci_hp_add_devices(slot->bus);
  414. pci_unlock_rescan_remove();
  415. slot->state = CONFIGURED;
  416. } else if (state == EMPTY) {
  417. slot->state = EMPTY;
  418. } else {
  419. err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
  420. slot->state = NOT_VALID;
  421. return -EINVAL;
  422. }
  423. slot->bus->max_bus_speed = get_max_bus_speed(slot);
  424. return 0;
  425. }
  426. static int disable_slot(struct hotplug_slot *hotplug_slot)
  427. {
  428. struct slot *slot = to_slot(hotplug_slot);
  429. if (slot->state == NOT_CONFIGURED)
  430. return -EINVAL;
  431. pci_lock_rescan_remove();
  432. pci_hp_remove_devices(slot->bus);
  433. pci_unlock_rescan_remove();
  434. vm_unmap_aliases();
  435. slot->state = NOT_CONFIGURED;
  436. return 0;
  437. }
  438. const struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
  439. .enable_slot = enable_slot,
  440. .disable_slot = disable_slot,
  441. .set_attention_status = set_attention_status,
  442. .get_power_status = get_power_status,
  443. .get_attention_status = get_attention_status,
  444. .get_adapter_status = get_adapter_status,
  445. };
  446. module_init(rpaphp_init);
  447. module_exit(rpaphp_exit);