rpaphp_core.c 11 KB

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