rpadlpar_core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "../pci.h"
  24. #include "rpaphp.h"
  25. #include "rpadlpar.h"
  26. static DEFINE_MUTEX(rpadlpar_mutex);
  27. #define DLPAR_MODULE_NAME "rpadlpar_io"
  28. #define NODE_TYPE_VIO 1
  29. #define NODE_TYPE_SLOT 2
  30. #define NODE_TYPE_PHB 3
  31. static struct device_node *find_vio_slot_node(char *drc_name)
  32. {
  33. struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
  34. struct device_node *dn = NULL;
  35. char *name;
  36. int rc;
  37. if (!parent)
  38. return NULL;
  39. while ((dn = of_get_next_child(parent, dn))) {
  40. rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
  41. if ((rc == 0) && (!strcmp(drc_name, name)))
  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. char *name;
  52. char *type;
  53. int rc;
  54. while ((np = of_find_node_by_name(np, "pci"))) {
  55. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  56. if (rc == 0)
  57. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  58. break;
  59. }
  60. return np;
  61. }
  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. eeh_add_device_tree_early(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. 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. vm_unmap_aliases();
  372. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  373. exit:
  374. mutex_unlock(&rpadlpar_mutex);
  375. return rc;
  376. }
  377. static inline int is_dlpar_capable(void)
  378. {
  379. int rc = rtas_token("ibm,configure-connector");
  380. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  381. }
  382. int __init rpadlpar_io_init(void)
  383. {
  384. if (!is_dlpar_capable()) {
  385. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  386. __func__);
  387. return -EPERM;
  388. }
  389. return dlpar_sysfs_init();
  390. }
  391. void rpadlpar_io_exit(void)
  392. {
  393. dlpar_sysfs_exit();
  394. return;
  395. }
  396. module_init(rpadlpar_io_init);
  397. module_exit(rpadlpar_io_exit);
  398. MODULE_LICENSE("GPL");