rpadlpar_core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. of_node_put(parent);
  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. int rc;
  53. while ((np = of_find_node_by_name(np, "pci"))) {
  54. rc = rpaphp_check_drc_props(np, drc_name, drc_type);
  55. if (rc == 0)
  56. break;
  57. }
  58. return np;
  59. }
  60. /* Returns a device_node with its reference count incremented */
  61. static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
  62. {
  63. struct device_node *dn;
  64. dn = find_php_slot_pci_node(drc_name, "SLOT");
  65. if (dn) {
  66. *node_type = NODE_TYPE_SLOT;
  67. return dn;
  68. }
  69. dn = find_php_slot_pci_node(drc_name, "PHB");
  70. if (dn) {
  71. *node_type = NODE_TYPE_PHB;
  72. return dn;
  73. }
  74. dn = find_vio_slot_node(drc_name);
  75. if (dn) {
  76. *node_type = NODE_TYPE_VIO;
  77. return dn;
  78. }
  79. return NULL;
  80. }
  81. /**
  82. * find_php_slot - return hotplug slot structure for device node
  83. * @dn: target &device_node
  84. *
  85. * This routine will return the hotplug slot structure
  86. * for a given device node. Note that built-in PCI slots
  87. * may be dlpar-able, but not hot-pluggable, so this routine
  88. * will return NULL for built-in PCI slots.
  89. */
  90. static struct slot *find_php_slot(struct device_node *dn)
  91. {
  92. struct slot *slot, *next;
  93. list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
  94. rpaphp_slot_list) {
  95. if (slot->dn == dn)
  96. return slot;
  97. }
  98. return NULL;
  99. }
  100. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  101. struct device_node *dev_dn)
  102. {
  103. struct pci_dev *tmp = NULL;
  104. struct device_node *child_dn;
  105. list_for_each_entry(tmp, &parent->devices, bus_list) {
  106. child_dn = pci_device_to_OF_node(tmp);
  107. if (child_dn == dev_dn)
  108. return tmp;
  109. }
  110. return NULL;
  111. }
  112. static void dlpar_pci_add_bus(struct device_node *dn)
  113. {
  114. struct pci_dn *pdn = PCI_DN(dn);
  115. struct pci_controller *phb = pdn->phb;
  116. struct pci_dev *dev = NULL;
  117. eeh_add_device_tree_early(pdn);
  118. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  119. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  120. if (!dev) {
  121. printk(KERN_ERR "%s: failed to create pci dev for %pOF\n",
  122. __func__, dn);
  123. return;
  124. }
  125. /* Scan below the new bridge */
  126. if (pci_is_bridge(dev))
  127. of_scan_pci_bridge(dev);
  128. /* Map IO space for child bus, which may or may not succeed */
  129. pcibios_map_io_space(dev->subordinate);
  130. /* Finish adding it : resource allocation, adding devices, etc...
  131. * Note that we need to perform the finish pass on the -parent-
  132. * bus of the EADS bridge so the bridge device itself gets
  133. * properly added
  134. */
  135. pcibios_finish_adding_to_bus(phb->bus);
  136. }
  137. static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  138. {
  139. struct pci_dev *dev;
  140. struct pci_controller *phb;
  141. if (pci_find_bus_by_node(dn))
  142. return -EINVAL;
  143. /* Add pci bus */
  144. dlpar_pci_add_bus(dn);
  145. /* Confirm new bridge dev was created */
  146. phb = PCI_DN(dn)->phb;
  147. dev = dlpar_find_new_dev(phb->bus, dn);
  148. if (!dev) {
  149. printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
  150. drc_name);
  151. return -EIO;
  152. }
  153. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  154. printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
  155. __func__, dev->hdr_type, drc_name);
  156. return -EIO;
  157. }
  158. /* Add hotplug slot */
  159. if (rpaphp_add_slot(dn)) {
  160. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  161. __func__, drc_name);
  162. return -EIO;
  163. }
  164. return 0;
  165. }
  166. static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
  167. {
  168. struct slot *slot;
  169. struct pci_dn *pdn;
  170. int rc = 0;
  171. if (!pci_find_bus_by_node(dn))
  172. return -EINVAL;
  173. /* If pci slot is hotpluggable, use hotplug to remove it */
  174. slot = find_php_slot(dn);
  175. if (slot && rpaphp_deregister_slot(slot)) {
  176. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  177. __func__, drc_name);
  178. return -EIO;
  179. }
  180. pdn = dn->data;
  181. BUG_ON(!pdn || !pdn->phb);
  182. rc = remove_phb_dynamic(pdn->phb);
  183. if (rc < 0)
  184. return rc;
  185. pdn->phb = NULL;
  186. return 0;
  187. }
  188. static int dlpar_add_phb(char *drc_name, struct device_node *dn)
  189. {
  190. struct pci_controller *phb;
  191. if (PCI_DN(dn) && PCI_DN(dn)->phb) {
  192. /* PHB already exists */
  193. return -EINVAL;
  194. }
  195. phb = init_phb_dynamic(dn);
  196. if (!phb)
  197. return -EIO;
  198. if (rpaphp_add_slot(dn)) {
  199. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  200. __func__, drc_name);
  201. return -EIO;
  202. }
  203. return 0;
  204. }
  205. static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
  206. {
  207. struct vio_dev *vio_dev;
  208. vio_dev = vio_find_node(dn);
  209. if (vio_dev) {
  210. put_device(&vio_dev->dev);
  211. return -EINVAL;
  212. }
  213. if (!vio_register_device_node(dn)) {
  214. printk(KERN_ERR
  215. "%s: failed to register vio node %s\n",
  216. __func__, drc_name);
  217. return -EIO;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * dlpar_add_slot - DLPAR add an I/O Slot
  223. * @drc_name: drc-name of newly added slot
  224. *
  225. * Make the hotplug module and the kernel aware of a newly added I/O Slot.
  226. * Return Codes:
  227. * 0 Success
  228. * -ENODEV Not a valid drc_name
  229. * -EINVAL Slot already added
  230. * -ERESTARTSYS Signalled before obtaining lock
  231. * -EIO Internal PCI Error
  232. */
  233. int dlpar_add_slot(char *drc_name)
  234. {
  235. struct device_node *dn = NULL;
  236. int node_type;
  237. int rc = -EIO;
  238. if (mutex_lock_interruptible(&rpadlpar_mutex))
  239. return -ERESTARTSYS;
  240. /* Find newly added node */
  241. dn = find_dlpar_node(drc_name, &node_type);
  242. if (!dn) {
  243. rc = -ENODEV;
  244. goto exit;
  245. }
  246. switch (node_type) {
  247. case NODE_TYPE_VIO:
  248. rc = dlpar_add_vio_slot(drc_name, dn);
  249. break;
  250. case NODE_TYPE_SLOT:
  251. rc = dlpar_add_pci_slot(drc_name, dn);
  252. break;
  253. case NODE_TYPE_PHB:
  254. rc = dlpar_add_phb(drc_name, dn);
  255. break;
  256. }
  257. of_node_put(dn);
  258. printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
  259. exit:
  260. mutex_unlock(&rpadlpar_mutex);
  261. return rc;
  262. }
  263. /**
  264. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  265. * @drc_name: drc-name of newly added slot
  266. * @dn: &device_node
  267. *
  268. * Remove the kernel and hotplug representations of an I/O Slot.
  269. * Return Codes:
  270. * 0 Success
  271. * -EINVAL Vio dev doesn't exist
  272. */
  273. static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
  274. {
  275. struct vio_dev *vio_dev;
  276. vio_dev = vio_find_node(dn);
  277. if (!vio_dev)
  278. return -EINVAL;
  279. vio_unregister_device(vio_dev);
  280. put_device(&vio_dev->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. int ret = 0;
  299. pci_lock_rescan_remove();
  300. bus = pci_find_bus_by_node(dn);
  301. if (!bus) {
  302. ret = -EINVAL;
  303. goto out;
  304. }
  305. pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
  306. bus->self ? pci_name(bus->self) : "<!PHB!>");
  307. slot = find_php_slot(dn);
  308. if (slot) {
  309. pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
  310. pci_domain_nr(bus), bus->number);
  311. if (rpaphp_deregister_slot(slot)) {
  312. printk(KERN_ERR
  313. "%s: unable to remove hotplug slot %s\n",
  314. __func__, drc_name);
  315. ret = -EIO;
  316. goto out;
  317. }
  318. }
  319. /* Remove all devices below slot */
  320. pci_hp_remove_devices(bus);
  321. /* Unmap PCI IO space */
  322. if (pcibios_unmap_io_space(bus)) {
  323. printk(KERN_ERR "%s: failed to unmap bus range\n",
  324. __func__);
  325. ret = -ERANGE;
  326. goto out;
  327. }
  328. /* Remove the EADS bridge device itself */
  329. BUG_ON(!bus->self);
  330. pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
  331. pci_stop_and_remove_bus_device(bus->self);
  332. out:
  333. pci_unlock_rescan_remove();
  334. return ret;
  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. of_node_put(dn);
  372. vm_unmap_aliases();
  373. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  374. exit:
  375. mutex_unlock(&rpadlpar_mutex);
  376. return rc;
  377. }
  378. static inline int is_dlpar_capable(void)
  379. {
  380. int rc = rtas_token("ibm,configure-connector");
  381. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  382. }
  383. int __init rpadlpar_io_init(void)
  384. {
  385. if (!is_dlpar_capable()) {
  386. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  387. __func__);
  388. return -EPERM;
  389. }
  390. return dlpar_sysfs_init();
  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");