rpadlpar_core.c 10 KB

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