rpadlpar_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <linux/vmalloc.h>
  23. #include <asm/pci-bridge.h>
  24. #include <linux/mutex.h>
  25. #include <asm/rtas.h>
  26. #include <asm/vio.h>
  27. #include "../pci.h"
  28. #include "rpaphp.h"
  29. #include "rpadlpar.h"
  30. static DEFINE_MUTEX(rpadlpar_mutex);
  31. #define DLPAR_MODULE_NAME "rpadlpar_io"
  32. #define NODE_TYPE_VIO 1
  33. #define NODE_TYPE_SLOT 2
  34. #define NODE_TYPE_PHB 3
  35. static struct device_node *find_vio_slot_node(char *drc_name)
  36. {
  37. struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
  38. struct device_node *dn = NULL;
  39. char *name;
  40. int rc;
  41. if (!parent)
  42. return NULL;
  43. while ((dn = of_get_next_child(parent, dn))) {
  44. rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
  45. if ((rc == 0) && (!strcmp(drc_name, name)))
  46. break;
  47. }
  48. return dn;
  49. }
  50. /* Find dlpar-capable pci node that contains the specified name and type */
  51. static struct device_node *find_php_slot_pci_node(char *drc_name,
  52. char *drc_type)
  53. {
  54. struct device_node *np = NULL;
  55. char *name;
  56. char *type;
  57. int rc;
  58. while ((np = of_find_node_by_name(np, "pci"))) {
  59. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  60. if (rc == 0)
  61. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  62. break;
  63. }
  64. return np;
  65. }
  66. static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
  67. {
  68. struct device_node *dn;
  69. dn = find_php_slot_pci_node(drc_name, "SLOT");
  70. if (dn) {
  71. *node_type = NODE_TYPE_SLOT;
  72. return dn;
  73. }
  74. dn = find_php_slot_pci_node(drc_name, "PHB");
  75. if (dn) {
  76. *node_type = NODE_TYPE_PHB;
  77. return dn;
  78. }
  79. dn = find_vio_slot_node(drc_name);
  80. if (dn) {
  81. *node_type = NODE_TYPE_VIO;
  82. return dn;
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * find_php_slot - return hotplug slot structure for device node
  88. * @dn: target &device_node
  89. *
  90. * This routine will return the hotplug slot structure
  91. * for a given device node. Note that built-in PCI slots
  92. * may be dlpar-able, but not hot-pluggable, so this routine
  93. * will return NULL for built-in PCI slots.
  94. */
  95. static struct slot *find_php_slot(struct device_node *dn)
  96. {
  97. struct list_head *tmp, *n;
  98. struct slot *slot;
  99. list_for_each_safe(tmp, n, &rpaphp_slot_head) {
  100. slot = list_entry(tmp, struct slot, rpaphp_slot_list);
  101. if (slot->dn == dn)
  102. return slot;
  103. }
  104. return NULL;
  105. }
  106. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  107. struct device_node *dev_dn)
  108. {
  109. struct pci_dev *tmp = NULL;
  110. struct device_node *child_dn;
  111. list_for_each_entry(tmp, &parent->devices, bus_list) {
  112. child_dn = pci_device_to_OF_node(tmp);
  113. if (child_dn == dev_dn)
  114. return tmp;
  115. }
  116. return NULL;
  117. }
  118. static void dlpar_pci_add_bus(struct device_node *dn)
  119. {
  120. struct pci_dn *pdn = PCI_DN(dn);
  121. struct pci_controller *phb = pdn->phb;
  122. struct pci_dev *dev = NULL;
  123. eeh_add_device_tree_early(pdn);
  124. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  125. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  126. if (!dev) {
  127. printk(KERN_ERR "%s: failed to create pci dev for %s\n",
  128. __func__, dn->full_name);
  129. return;
  130. }
  131. /* Scan below the new bridge */
  132. if (pci_is_bridge(dev))
  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 hotpluggable, 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. struct vio_dev *vio_dev;
  214. vio_dev = vio_find_node(dn);
  215. if (vio_dev) {
  216. put_device(&vio_dev->dev);
  217. return -EINVAL;
  218. }
  219. if (!vio_register_device_node(dn)) {
  220. printk(KERN_ERR
  221. "%s: failed to register vio node %s\n",
  222. __func__, drc_name);
  223. return -EIO;
  224. }
  225. return 0;
  226. }
  227. /**
  228. * dlpar_add_slot - DLPAR add an I/O Slot
  229. * @drc_name: drc-name of newly added slot
  230. *
  231. * Make the hotplug module and the kernel aware of a newly added I/O Slot.
  232. * Return Codes:
  233. * 0 Success
  234. * -ENODEV Not a valid drc_name
  235. * -EINVAL Slot already added
  236. * -ERESTARTSYS Signalled before obtaining lock
  237. * -EIO Internal PCI Error
  238. */
  239. int dlpar_add_slot(char *drc_name)
  240. {
  241. struct device_node *dn = NULL;
  242. int node_type;
  243. int rc = -EIO;
  244. if (mutex_lock_interruptible(&rpadlpar_mutex))
  245. return -ERESTARTSYS;
  246. /* Find newly added node */
  247. dn = find_dlpar_node(drc_name, &node_type);
  248. if (!dn) {
  249. rc = -ENODEV;
  250. goto exit;
  251. }
  252. switch (node_type) {
  253. case NODE_TYPE_VIO:
  254. rc = dlpar_add_vio_slot(drc_name, dn);
  255. break;
  256. case NODE_TYPE_SLOT:
  257. rc = dlpar_add_pci_slot(drc_name, dn);
  258. break;
  259. case NODE_TYPE_PHB:
  260. rc = dlpar_add_phb(drc_name, dn);
  261. break;
  262. }
  263. printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
  264. exit:
  265. mutex_unlock(&rpadlpar_mutex);
  266. return rc;
  267. }
  268. /**
  269. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  270. * @drc_name: drc-name of newly added slot
  271. * @dn: &device_node
  272. *
  273. * Remove the kernel and hotplug representations of an I/O Slot.
  274. * Return Codes:
  275. * 0 Success
  276. * -EINVAL Vio dev doesn't exist
  277. */
  278. static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
  279. {
  280. struct vio_dev *vio_dev;
  281. vio_dev = vio_find_node(dn);
  282. if (!vio_dev)
  283. return -EINVAL;
  284. vio_unregister_device(vio_dev);
  285. put_device(&vio_dev->dev);
  286. return 0;
  287. }
  288. /**
  289. * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
  290. * @drc_name: drc-name of newly added slot
  291. * @dn: &device_node
  292. *
  293. * Remove the kernel and hotplug representations of a PCI I/O Slot.
  294. * Return Codes:
  295. * 0 Success
  296. * -ENODEV Not a valid drc_name
  297. * -EIO Internal PCI Error
  298. */
  299. int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
  300. {
  301. struct pci_bus *bus;
  302. struct slot *slot;
  303. int ret = 0;
  304. pci_lock_rescan_remove();
  305. bus = pcibios_find_pci_bus(dn);
  306. if (!bus) {
  307. ret = -EINVAL;
  308. goto out;
  309. }
  310. pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
  311. bus->self ? pci_name(bus->self) : "<!PHB!>");
  312. slot = find_php_slot(dn);
  313. if (slot) {
  314. pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
  315. pci_domain_nr(bus), bus->number);
  316. if (rpaphp_deregister_slot(slot)) {
  317. printk(KERN_ERR
  318. "%s: unable to remove hotplug slot %s\n",
  319. __func__, drc_name);
  320. ret = -EIO;
  321. goto out;
  322. }
  323. }
  324. /* Remove all devices below slot */
  325. pcibios_remove_pci_devices(bus);
  326. /* Unmap PCI IO space */
  327. if (pcibios_unmap_io_space(bus)) {
  328. printk(KERN_ERR "%s: failed to unmap bus range\n",
  329. __func__);
  330. ret = -ERANGE;
  331. goto out;
  332. }
  333. /* Remove the EADS bridge device itself */
  334. BUG_ON(!bus->self);
  335. pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
  336. pci_stop_and_remove_bus_device(bus->self);
  337. out:
  338. pci_unlock_rescan_remove();
  339. return ret;
  340. }
  341. /**
  342. * dlpar_remove_slot - DLPAR remove an I/O Slot
  343. * @drc_name: drc-name of newly added slot
  344. *
  345. * Remove the kernel and hotplug representations of an I/O Slot.
  346. * Return Codes:
  347. * 0 Success
  348. * -ENODEV Not a valid drc_name
  349. * -EINVAL Slot already removed
  350. * -ERESTARTSYS Signalled before obtaining lock
  351. * -EIO Internal Error
  352. */
  353. int dlpar_remove_slot(char *drc_name)
  354. {
  355. struct device_node *dn;
  356. int node_type;
  357. int rc = 0;
  358. if (mutex_lock_interruptible(&rpadlpar_mutex))
  359. return -ERESTARTSYS;
  360. dn = find_dlpar_node(drc_name, &node_type);
  361. if (!dn) {
  362. rc = -ENODEV;
  363. goto exit;
  364. }
  365. switch (node_type) {
  366. case NODE_TYPE_VIO:
  367. rc = dlpar_remove_vio_slot(drc_name, dn);
  368. break;
  369. case NODE_TYPE_PHB:
  370. rc = dlpar_remove_phb(drc_name, dn);
  371. break;
  372. case NODE_TYPE_SLOT:
  373. rc = dlpar_remove_pci_slot(drc_name, dn);
  374. break;
  375. }
  376. vm_unmap_aliases();
  377. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  378. exit:
  379. mutex_unlock(&rpadlpar_mutex);
  380. return rc;
  381. }
  382. static inline int is_dlpar_capable(void)
  383. {
  384. int rc = rtas_token("ibm,configure-connector");
  385. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  386. }
  387. int __init rpadlpar_io_init(void)
  388. {
  389. int rc = 0;
  390. if (!is_dlpar_capable()) {
  391. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  392. __func__);
  393. return -EPERM;
  394. }
  395. rc = dlpar_sysfs_init();
  396. return rc;
  397. }
  398. void rpadlpar_io_exit(void)
  399. {
  400. dlpar_sysfs_exit();
  401. return;
  402. }
  403. module_init(rpadlpar_io_init);
  404. module_exit(rpadlpar_io_exit);
  405. MODULE_LICENSE("GPL");