rpadlpar_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Interface for Dynamic Logical Partitioning of I/O Slots on
  3. * RPA-compliant PPC64 platform.
  4. *
  5. * John Rose <johnrose@austin.ibm.com>
  6. * Linda Xie <lxie@us.ibm.com>
  7. *
  8. * October 2003
  9. *
  10. * Copyright (C) 2003 IBM.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <linux/vmalloc.h>
  23. #include <asm/pci-bridge.h>
  24. #include <linux/mutex.h>
  25. #include <asm/rtas.h>
  26. #include <asm/vio.h>
  27. #include "../pci.h"
  28. #include "rpaphp.h"
  29. #include "rpadlpar.h"
  30. static DEFINE_MUTEX(rpadlpar_mutex);
  31. #define DLPAR_MODULE_NAME "rpadlpar_io"
  32. #define NODE_TYPE_VIO 1
  33. #define NODE_TYPE_SLOT 2
  34. #define NODE_TYPE_PHB 3
  35. static struct device_node *find_vio_slot_node(char *drc_name)
  36. {
  37. struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
  38. struct device_node *dn = NULL;
  39. char *name;
  40. int rc;
  41. if (!parent)
  42. return NULL;
  43. while ((dn = of_get_next_child(parent, dn))) {
  44. rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
  45. if ((rc == 0) && (!strcmp(drc_name, name)))
  46. break;
  47. }
  48. return dn;
  49. }
  50. /* Find dlpar-capable pci node that contains the specified name and type */
  51. static struct device_node *find_php_slot_pci_node(char *drc_name,
  52. char *drc_type)
  53. {
  54. struct device_node *np = NULL;
  55. char *name;
  56. char *type;
  57. int rc;
  58. while ((np = of_find_node_by_name(np, "pci"))) {
  59. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  60. if (rc == 0)
  61. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  62. break;
  63. }
  64. return np;
  65. }
  66. static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
  67. {
  68. struct device_node *dn;
  69. dn = find_php_slot_pci_node(drc_name, "SLOT");
  70. if (dn) {
  71. *node_type = NODE_TYPE_SLOT;
  72. return dn;
  73. }
  74. dn = find_php_slot_pci_node(drc_name, "PHB");
  75. if (dn) {
  76. *node_type = NODE_TYPE_PHB;
  77. return dn;
  78. }
  79. dn = find_vio_slot_node(drc_name);
  80. if (dn) {
  81. *node_type = NODE_TYPE_VIO;
  82. return dn;
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * find_php_slot - return hotplug slot structure for device node
  88. * @dn: target &device_node
  89. *
  90. * This routine will return the hotplug slot structure
  91. * for a given device node. Note that built-in PCI slots
  92. * may be dlpar-able, but not hot-pluggable, so this routine
  93. * will return NULL for built-in PCI slots.
  94. */
  95. static struct slot *find_php_slot(struct device_node *dn)
  96. {
  97. struct slot *slot, *next;
  98. list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
  99. rpaphp_slot_list) {
  100. if (slot->dn == dn)
  101. return slot;
  102. }
  103. return NULL;
  104. }
  105. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  106. struct device_node *dev_dn)
  107. {
  108. struct pci_dev *tmp = NULL;
  109. struct device_node *child_dn;
  110. list_for_each_entry(tmp, &parent->devices, bus_list) {
  111. child_dn = pci_device_to_OF_node(tmp);
  112. if (child_dn == dev_dn)
  113. return tmp;
  114. }
  115. return NULL;
  116. }
  117. static void dlpar_pci_add_bus(struct device_node *dn)
  118. {
  119. struct pci_dn *pdn = PCI_DN(dn);
  120. struct pci_controller *phb = pdn->phb;
  121. struct pci_dev *dev = NULL;
  122. eeh_add_device_tree_early(pdn);
  123. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  124. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  125. if (!dev) {
  126. printk(KERN_ERR "%s: failed to create pci dev for %s\n",
  127. __func__, dn->full_name);
  128. return;
  129. }
  130. /* Scan below the new bridge */
  131. if (pci_is_bridge(dev))
  132. of_scan_pci_bridge(dev);
  133. /* Map IO space for child bus, which may or may not succeed */
  134. pcibios_map_io_space(dev->subordinate);
  135. /* Finish adding it : resource allocation, adding devices, etc...
  136. * Note that we need to perform the finish pass on the -parent-
  137. * bus of the EADS bridge so the bridge device itself gets
  138. * properly added
  139. */
  140. pcibios_finish_adding_to_bus(phb->bus);
  141. }
  142. static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  143. {
  144. struct pci_dev *dev;
  145. struct pci_controller *phb;
  146. if (pci_find_bus_by_node(dn))
  147. return -EINVAL;
  148. /* Add pci bus */
  149. dlpar_pci_add_bus(dn);
  150. /* Confirm new bridge dev was created */
  151. phb = PCI_DN(dn)->phb;
  152. dev = dlpar_find_new_dev(phb->bus, dn);
  153. if (!dev) {
  154. printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
  155. drc_name);
  156. return -EIO;
  157. }
  158. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  159. printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
  160. __func__, dev->hdr_type, drc_name);
  161. return -EIO;
  162. }
  163. /* Add hotplug slot */
  164. if (rpaphp_add_slot(dn)) {
  165. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  166. __func__, drc_name);
  167. return -EIO;
  168. }
  169. return 0;
  170. }
  171. static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
  172. {
  173. struct slot *slot;
  174. struct pci_dn *pdn;
  175. int rc = 0;
  176. if (!pci_find_bus_by_node(dn))
  177. return -EINVAL;
  178. /* If pci slot is hotpluggable, use hotplug to remove it */
  179. slot = find_php_slot(dn);
  180. if (slot && rpaphp_deregister_slot(slot)) {
  181. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  182. __func__, drc_name);
  183. return -EIO;
  184. }
  185. pdn = dn->data;
  186. BUG_ON(!pdn || !pdn->phb);
  187. rc = remove_phb_dynamic(pdn->phb);
  188. if (rc < 0)
  189. return rc;
  190. pdn->phb = NULL;
  191. return 0;
  192. }
  193. static int dlpar_add_phb(char *drc_name, struct device_node *dn)
  194. {
  195. struct pci_controller *phb;
  196. if (PCI_DN(dn) && PCI_DN(dn)->phb) {
  197. /* PHB already exists */
  198. return -EINVAL;
  199. }
  200. phb = init_phb_dynamic(dn);
  201. if (!phb)
  202. return -EIO;
  203. if (rpaphp_add_slot(dn)) {
  204. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  205. __func__, drc_name);
  206. return -EIO;
  207. }
  208. return 0;
  209. }
  210. static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
  211. {
  212. struct vio_dev *vio_dev;
  213. vio_dev = vio_find_node(dn);
  214. if (vio_dev) {
  215. put_device(&vio_dev->dev);
  216. return -EINVAL;
  217. }
  218. if (!vio_register_device_node(dn)) {
  219. printk(KERN_ERR
  220. "%s: failed to register vio node %s\n",
  221. __func__, drc_name);
  222. return -EIO;
  223. }
  224. return 0;
  225. }
  226. /**
  227. * dlpar_add_slot - DLPAR add an I/O Slot
  228. * @drc_name: drc-name of newly added slot
  229. *
  230. * Make the hotplug module and the kernel aware of a newly added I/O Slot.
  231. * Return Codes:
  232. * 0 Success
  233. * -ENODEV Not a valid drc_name
  234. * -EINVAL Slot already added
  235. * -ERESTARTSYS Signalled before obtaining lock
  236. * -EIO Internal PCI Error
  237. */
  238. int dlpar_add_slot(char *drc_name)
  239. {
  240. struct device_node *dn = NULL;
  241. int node_type;
  242. int rc = -EIO;
  243. if (mutex_lock_interruptible(&rpadlpar_mutex))
  244. return -ERESTARTSYS;
  245. /* Find newly added node */
  246. dn = find_dlpar_node(drc_name, &node_type);
  247. if (!dn) {
  248. rc = -ENODEV;
  249. goto exit;
  250. }
  251. switch (node_type) {
  252. case NODE_TYPE_VIO:
  253. rc = dlpar_add_vio_slot(drc_name, dn);
  254. break;
  255. case NODE_TYPE_SLOT:
  256. rc = dlpar_add_pci_slot(drc_name, dn);
  257. break;
  258. case NODE_TYPE_PHB:
  259. rc = dlpar_add_phb(drc_name, dn);
  260. break;
  261. }
  262. printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
  263. exit:
  264. mutex_unlock(&rpadlpar_mutex);
  265. return rc;
  266. }
  267. /**
  268. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  269. * @drc_name: drc-name of newly added slot
  270. * @dn: &device_node
  271. *
  272. * Remove the kernel and hotplug representations of an I/O Slot.
  273. * Return Codes:
  274. * 0 Success
  275. * -EINVAL Vio dev doesn't exist
  276. */
  277. static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
  278. {
  279. struct vio_dev *vio_dev;
  280. vio_dev = vio_find_node(dn);
  281. if (!vio_dev)
  282. return -EINVAL;
  283. vio_unregister_device(vio_dev);
  284. put_device(&vio_dev->dev);
  285. return 0;
  286. }
  287. /**
  288. * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
  289. * @drc_name: drc-name of newly added slot
  290. * @dn: &device_node
  291. *
  292. * Remove the kernel and hotplug representations of a PCI I/O Slot.
  293. * Return Codes:
  294. * 0 Success
  295. * -ENODEV Not a valid drc_name
  296. * -EIO Internal PCI Error
  297. */
  298. int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
  299. {
  300. struct pci_bus *bus;
  301. struct slot *slot;
  302. int ret = 0;
  303. pci_lock_rescan_remove();
  304. bus = pci_find_bus_by_node(dn);
  305. if (!bus) {
  306. ret = -EINVAL;
  307. goto out;
  308. }
  309. pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
  310. bus->self ? pci_name(bus->self) : "<!PHB!>");
  311. slot = find_php_slot(dn);
  312. if (slot) {
  313. pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
  314. pci_domain_nr(bus), bus->number);
  315. if (rpaphp_deregister_slot(slot)) {
  316. printk(KERN_ERR
  317. "%s: unable to remove hotplug slot %s\n",
  318. __func__, drc_name);
  319. ret = -EIO;
  320. goto out;
  321. }
  322. }
  323. /* Remove all devices below slot */
  324. pci_hp_remove_devices(bus);
  325. /* Unmap PCI IO space */
  326. if (pcibios_unmap_io_space(bus)) {
  327. printk(KERN_ERR "%s: failed to unmap bus range\n",
  328. __func__);
  329. ret = -ERANGE;
  330. goto out;
  331. }
  332. /* Remove the EADS bridge device itself */
  333. BUG_ON(!bus->self);
  334. pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
  335. pci_stop_and_remove_bus_device(bus->self);
  336. out:
  337. pci_unlock_rescan_remove();
  338. return ret;
  339. }
  340. /**
  341. * dlpar_remove_slot - DLPAR remove an I/O Slot
  342. * @drc_name: drc-name of newly added slot
  343. *
  344. * Remove the kernel and hotplug representations of an I/O Slot.
  345. * Return Codes:
  346. * 0 Success
  347. * -ENODEV Not a valid drc_name
  348. * -EINVAL Slot already removed
  349. * -ERESTARTSYS Signalled before obtaining lock
  350. * -EIO Internal Error
  351. */
  352. int dlpar_remove_slot(char *drc_name)
  353. {
  354. struct device_node *dn;
  355. int node_type;
  356. int rc = 0;
  357. if (mutex_lock_interruptible(&rpadlpar_mutex))
  358. return -ERESTARTSYS;
  359. dn = find_dlpar_node(drc_name, &node_type);
  360. if (!dn) {
  361. rc = -ENODEV;
  362. goto exit;
  363. }
  364. switch (node_type) {
  365. case NODE_TYPE_VIO:
  366. rc = dlpar_remove_vio_slot(drc_name, dn);
  367. break;
  368. case NODE_TYPE_PHB:
  369. rc = dlpar_remove_phb(drc_name, dn);
  370. break;
  371. case NODE_TYPE_SLOT:
  372. rc = dlpar_remove_pci_slot(drc_name, dn);
  373. break;
  374. }
  375. vm_unmap_aliases();
  376. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  377. exit:
  378. mutex_unlock(&rpadlpar_mutex);
  379. return rc;
  380. }
  381. static inline int is_dlpar_capable(void)
  382. {
  383. int rc = rtas_token("ibm,configure-connector");
  384. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  385. }
  386. int __init rpadlpar_io_init(void)
  387. {
  388. int rc = 0;
  389. if (!is_dlpar_capable()) {
  390. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  391. __func__);
  392. return -EPERM;
  393. }
  394. rc = dlpar_sysfs_init();
  395. return rc;
  396. }
  397. void rpadlpar_io_exit(void)
  398. {
  399. dlpar_sysfs_exit();
  400. return;
  401. }
  402. module_init(rpadlpar_io_init);
  403. module_exit(rpadlpar_io_exit);
  404. MODULE_LICENSE("GPL");