rpaphp_core.c 11 KB

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