rpadlpar_core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <asm/semaphore.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 DECLARE_MUTEX(rpadlpar_sem);
  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_type(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. static struct slot *find_slot(struct device_node *dn)
  84. {
  85. struct list_head *tmp, *n;
  86. struct slot *slot;
  87. list_for_each_safe(tmp, n, &rpaphp_slot_head) {
  88. slot = list_entry(tmp, struct slot, rpaphp_slot_list);
  89. if (slot->dn == dn)
  90. return slot;
  91. }
  92. return NULL;
  93. }
  94. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  95. struct device_node *dev_dn)
  96. {
  97. struct pci_dev *tmp = NULL;
  98. struct device_node *child_dn;
  99. list_for_each_entry(tmp, &parent->devices, bus_list) {
  100. child_dn = pci_device_to_OF_node(tmp);
  101. if (child_dn == dev_dn)
  102. return tmp;
  103. }
  104. return NULL;
  105. }
  106. static void dlpar_pci_add_bus(struct device_node *dn)
  107. {
  108. struct pci_dn *pdn = PCI_DN(dn);
  109. struct pci_controller *phb = pdn->phb;
  110. struct pci_dev *dev = NULL;
  111. eeh_add_device_tree_early(dn);
  112. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  113. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  114. if (!dev) {
  115. printk(KERN_ERR "%s: failed to create pci dev for %s\n",
  116. __FUNCTION__, dn->full_name);
  117. return;
  118. }
  119. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  120. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  121. of_scan_pci_bridge(dn, dev);
  122. pcibios_fixup_new_pci_devices(dev->subordinate,0);
  123. /* Claim new bus resources */
  124. pcibios_claim_one_bus(dev->bus);
  125. /* ioremap() for child bus, which may or may not succeed */
  126. remap_bus_range(dev->subordinate);
  127. /* Add new devices to global lists. Register in proc, sysfs. */
  128. pci_bus_add_devices(phb->bus);
  129. }
  130. static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  131. {
  132. struct pci_dev *dev;
  133. struct pci_controller *phb;
  134. if (pcibios_find_pci_bus(dn))
  135. return -EINVAL;
  136. /* Add pci bus */
  137. dlpar_pci_add_bus(dn);
  138. /* Confirm new bridge dev was created */
  139. phb = PCI_DN(dn)->phb;
  140. dev = dlpar_find_new_dev(phb->bus, dn);
  141. if (!dev) {
  142. printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
  143. drc_name);
  144. return -EIO;
  145. }
  146. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  147. printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
  148. __FUNCTION__, dev->hdr_type, drc_name);
  149. return -EIO;
  150. }
  151. /* Add hotplug slot */
  152. if (rpaphp_add_slot(dn)) {
  153. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  154. __FUNCTION__, drc_name);
  155. return -EIO;
  156. }
  157. return 0;
  158. }
  159. static int dlpar_remove_root_bus(struct pci_controller *phb)
  160. {
  161. struct pci_bus *phb_bus;
  162. int rc;
  163. phb_bus = phb->bus;
  164. if (!(list_empty(&phb_bus->children) &&
  165. list_empty(&phb_bus->devices))) {
  166. return -EBUSY;
  167. }
  168. rc = pcibios_remove_root_bus(phb);
  169. if (rc)
  170. return -EIO;
  171. device_unregister(phb_bus->bridge);
  172. pci_remove_bus(phb_bus);
  173. return 0;
  174. }
  175. static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
  176. {
  177. struct slot *slot;
  178. struct pci_dn *pdn;
  179. int rc = 0;
  180. if (!pcibios_find_pci_bus(dn))
  181. return -EINVAL;
  182. slot = find_slot(dn);
  183. if (slot) {
  184. /* Remove hotplug slot */
  185. if (rpaphp_deregister_slot(slot)) {
  186. printk(KERN_ERR
  187. "%s: unable to remove hotplug slot %s\n",
  188. __FUNCTION__, drc_name);
  189. return -EIO;
  190. }
  191. }
  192. pdn = dn->data;
  193. BUG_ON(!pdn || !pdn->phb);
  194. rc = dlpar_remove_root_bus(pdn->phb);
  195. if (rc < 0)
  196. return rc;
  197. pdn->phb = NULL;
  198. return 0;
  199. }
  200. static int dlpar_add_phb(char *drc_name, struct device_node *dn)
  201. {
  202. struct pci_controller *phb;
  203. if (PCI_DN(dn) && PCI_DN(dn)->phb) {
  204. /* PHB already exists */
  205. return -EINVAL;
  206. }
  207. phb = init_phb_dynamic(dn);
  208. if (!phb)
  209. return -EIO;
  210. if (rpaphp_add_slot(dn)) {
  211. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  212. __FUNCTION__, drc_name);
  213. return -EIO;
  214. }
  215. return 0;
  216. }
  217. static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
  218. {
  219. if (vio_find_node(dn))
  220. return -EINVAL;
  221. if (!vio_register_device_node(dn)) {
  222. printk(KERN_ERR
  223. "%s: failed to register vio node %s\n",
  224. __FUNCTION__, drc_name);
  225. return -EIO;
  226. }
  227. return 0;
  228. }
  229. /**
  230. * dlpar_add_slot - DLPAR add an I/O Slot
  231. * @drc_name: drc-name of newly added slot
  232. *
  233. * Make the hotplug module and the kernel aware
  234. * of a newly added I/O Slot.
  235. * Return Codes -
  236. * 0 Success
  237. * -ENODEV Not a valid drc_name
  238. * -EINVAL Slot already added
  239. * -ERESTARTSYS Signalled before obtaining lock
  240. * -EIO Internal PCI Error
  241. */
  242. int dlpar_add_slot(char *drc_name)
  243. {
  244. struct device_node *dn = NULL;
  245. int node_type;
  246. int rc = -EIO;
  247. if (down_interruptible(&rpadlpar_sem))
  248. return -ERESTARTSYS;
  249. /* Find newly added node */
  250. dn = find_dlpar_node(drc_name, &node_type);
  251. if (!dn) {
  252. rc = -ENODEV;
  253. goto exit;
  254. }
  255. switch (node_type) {
  256. case NODE_TYPE_VIO:
  257. rc = dlpar_add_vio_slot(drc_name, dn);
  258. break;
  259. case NODE_TYPE_SLOT:
  260. rc = dlpar_add_pci_slot(drc_name, dn);
  261. break;
  262. case NODE_TYPE_PHB:
  263. rc = dlpar_add_phb(drc_name, dn);
  264. break;
  265. }
  266. printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
  267. exit:
  268. up(&rpadlpar_sem);
  269. return rc;
  270. }
  271. /**
  272. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  273. * @drc_name: drc-name of newly added slot
  274. *
  275. * Remove the kernel and hotplug representations
  276. * of an I/O Slot.
  277. * Return Codes:
  278. * 0 Success
  279. * -EINVAL Vio dev doesn't exist
  280. */
  281. static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
  282. {
  283. struct vio_dev *vio_dev;
  284. vio_dev = vio_find_node(dn);
  285. if (!vio_dev)
  286. return -EINVAL;
  287. vio_unregister_device(vio_dev);
  288. return 0;
  289. }
  290. /**
  291. * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
  292. * @drc_name: drc-name of newly added slot
  293. *
  294. * Remove the kernel and hotplug representations
  295. * of a PCI I/O Slot.
  296. * Return Codes:
  297. * 0 Success
  298. * -ENODEV Not a valid drc_name
  299. * -EIO Internal PCI Error
  300. */
  301. int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
  302. {
  303. struct pci_bus *bus;
  304. struct slot *slot;
  305. bus = pcibios_find_pci_bus(dn);
  306. if (!bus)
  307. return -EINVAL;
  308. slot = find_slot(dn);
  309. if (slot) {
  310. /* Remove hotplug slot */
  311. if (rpaphp_deregister_slot(slot)) {
  312. printk(KERN_ERR
  313. "%s: unable to remove hotplug slot %s\n",
  314. __FUNCTION__, drc_name);
  315. return -EIO;
  316. }
  317. } else {
  318. struct pci_dev *dev, *tmp;
  319. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  320. eeh_remove_bus_device(dev);
  321. pci_remove_bus_device(dev);
  322. }
  323. }
  324. if (unmap_bus_range(bus)) {
  325. printk(KERN_ERR "%s: failed to unmap bus range\n",
  326. __FUNCTION__);
  327. return -ERANGE;
  328. }
  329. BUG_ON(!bus->self);
  330. pci_remove_bus_device(bus->self);
  331. return 0;
  332. }
  333. /**
  334. * dlpar_remove_slot - DLPAR remove an I/O Slot
  335. * @drc_name: drc-name of newly added slot
  336. *
  337. * Remove the kernel and hotplug representations
  338. * of an I/O Slot.
  339. * Return Codes:
  340. * 0 Success
  341. * -ENODEV Not a valid drc_name
  342. * -EINVAL Slot already removed
  343. * -ERESTARTSYS Signalled before obtaining lock
  344. * -EIO Internal Error
  345. */
  346. int dlpar_remove_slot(char *drc_name)
  347. {
  348. struct device_node *dn;
  349. int node_type;
  350. int rc = 0;
  351. if (down_interruptible(&rpadlpar_sem))
  352. return -ERESTARTSYS;
  353. dn = find_dlpar_node(drc_name, &node_type);
  354. if (!dn) {
  355. rc = -ENODEV;
  356. goto exit;
  357. }
  358. switch (node_type) {
  359. case NODE_TYPE_VIO:
  360. rc = dlpar_remove_vio_slot(drc_name, dn);
  361. break;
  362. case NODE_TYPE_PHB:
  363. rc = dlpar_remove_phb(drc_name, dn);
  364. break;
  365. case NODE_TYPE_SLOT:
  366. rc = dlpar_remove_pci_slot(drc_name, dn);
  367. break;
  368. }
  369. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  370. exit:
  371. up(&rpadlpar_sem);
  372. return rc;
  373. }
  374. static inline int is_dlpar_capable(void)
  375. {
  376. int rc = rtas_token("ibm,configure-connector");
  377. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  378. }
  379. int __init rpadlpar_io_init(void)
  380. {
  381. int rc = 0;
  382. if (!is_dlpar_capable()) {
  383. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  384. __FUNCTION__);
  385. return -EPERM;
  386. }
  387. rc = dlpar_sysfs_init();
  388. return rc;
  389. }
  390. void rpadlpar_io_exit(void)
  391. {
  392. dlpar_sysfs_exit();
  393. return;
  394. }
  395. module_init(rpadlpar_io_init);
  396. module_exit(rpadlpar_io_exit);
  397. MODULE_LICENSE("GPL");