rpaphp_core.c 11 KB

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