rpaphp_core.c 11 KB

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