rpaphp_core.c 11 KB

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