rpadlpar_core.c 9.8 KB

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