rpadlpar_core.c 10 KB

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