rpadlpar_core.c 10.0 KB

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