rpaphp_core.c 11 KB

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