rpaphp_core.c 11 KB

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