rpaphp_core.c 11 KB

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