rpaphp_core.c 11 KB

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