rpadlpar_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/pci.h>
  20. #include <linux/string.h>
  21. #include <asm/pci-bridge.h>
  22. #include <linux/mutex.h>
  23. #include <asm/rtas.h>
  24. #include <asm/vio.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 = NULL;
  37. char *name;
  38. int rc;
  39. if (!parent)
  40. return NULL;
  41. while ((dn = of_get_next_child(parent, dn))) {
  42. rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
  43. if ((rc == 0) && (!strcmp(drc_name, name)))
  44. break;
  45. }
  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 = NULL;
  53. char *name;
  54. char *type;
  55. int rc;
  56. while ((np = of_find_node_by_name(np, "pci"))) {
  57. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  58. if (rc == 0)
  59. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  60. break;
  61. }
  62. return np;
  63. }
  64. static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
  65. {
  66. struct device_node *dn;
  67. dn = find_php_slot_pci_node(drc_name, "SLOT");
  68. if (dn) {
  69. *node_type = NODE_TYPE_SLOT;
  70. return dn;
  71. }
  72. dn = find_php_slot_pci_node(drc_name, "PHB");
  73. if (dn) {
  74. *node_type = NODE_TYPE_PHB;
  75. return dn;
  76. }
  77. dn = find_vio_slot_node(drc_name);
  78. if (dn) {
  79. *node_type = NODE_TYPE_VIO;
  80. return dn;
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * find_php_slot - return hotplug slot structure for device node
  86. * @dn: target &device_node
  87. *
  88. * This routine will return the hotplug slot structure
  89. * for a given device node. Note that built-in PCI slots
  90. * may be dlpar-able, but not hot-pluggable, so this routine
  91. * will return NULL for built-in PCI slots.
  92. */
  93. static struct slot *find_php_slot(struct device_node *dn)
  94. {
  95. struct list_head *tmp, *n;
  96. struct slot *slot;
  97. list_for_each_safe(tmp, n, &rpaphp_slot_head) {
  98. slot = list_entry(tmp, struct slot, rpaphp_slot_list);
  99. if (slot->dn == dn)
  100. return slot;
  101. }
  102. return NULL;
  103. }
  104. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  105. struct device_node *dev_dn)
  106. {
  107. struct pci_dev *tmp = NULL;
  108. struct device_node *child_dn;
  109. list_for_each_entry(tmp, &parent->devices, bus_list) {
  110. child_dn = pci_device_to_OF_node(tmp);
  111. if (child_dn == dev_dn)
  112. return tmp;
  113. }
  114. return NULL;
  115. }
  116. static void dlpar_pci_add_bus(struct device_node *dn)
  117. {
  118. struct pci_dn *pdn = PCI_DN(dn);
  119. struct pci_controller *phb = pdn->phb;
  120. struct pci_dev *dev = NULL;
  121. eeh_add_device_tree_early(dn);
  122. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  123. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  124. if (!dev) {
  125. printk(KERN_ERR "%s: failed to create pci dev for %s\n",
  126. __func__, dn->full_name);
  127. return;
  128. }
  129. /* Scan below the new bridge */
  130. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  131. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  132. of_scan_pci_bridge(dn, dev);
  133. /* Map IO space for child bus, which may or may not succeed */
  134. pcibios_map_io_space(dev->subordinate);
  135. /* Finish adding it : resource allocation, adding devices, etc...
  136. * Note that we need to perform the finish pass on the -parent-
  137. * bus of the EADS bridge so the bridge device itself gets
  138. * properly added
  139. */
  140. pcibios_finish_adding_to_bus(phb->bus);
  141. }
  142. static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  143. {
  144. struct pci_dev *dev;
  145. struct pci_controller *phb;
  146. if (pcibios_find_pci_bus(dn))
  147. return -EINVAL;
  148. /* Add pci bus */
  149. dlpar_pci_add_bus(dn);
  150. /* Confirm new bridge dev was created */
  151. phb = PCI_DN(dn)->phb;
  152. dev = dlpar_find_new_dev(phb->bus, dn);
  153. if (!dev) {
  154. printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
  155. drc_name);
  156. return -EIO;
  157. }
  158. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  159. printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
  160. __func__, dev->hdr_type, drc_name);
  161. return -EIO;
  162. }
  163. /* Add hotplug slot */
  164. if (rpaphp_add_slot(dn)) {
  165. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  166. __func__, drc_name);
  167. return -EIO;
  168. }
  169. return 0;
  170. }
  171. static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
  172. {
  173. struct slot *slot;
  174. struct pci_dn *pdn;
  175. int rc = 0;
  176. if (!pcibios_find_pci_bus(dn))
  177. return -EINVAL;
  178. /* If pci slot is hotplugable, use hotplug to remove it */
  179. slot = find_php_slot(dn);
  180. if (slot && rpaphp_deregister_slot(slot)) {
  181. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  182. __func__, drc_name);
  183. return -EIO;
  184. }
  185. pdn = dn->data;
  186. BUG_ON(!pdn || !pdn->phb);
  187. rc = remove_phb_dynamic(pdn->phb);
  188. if (rc < 0)
  189. return rc;
  190. pdn->phb = NULL;
  191. return 0;
  192. }
  193. static int dlpar_add_phb(char *drc_name, struct device_node *dn)
  194. {
  195. struct pci_controller *phb;
  196. if (PCI_DN(dn) && PCI_DN(dn)->phb) {
  197. /* PHB already exists */
  198. return -EINVAL;
  199. }
  200. phb = init_phb_dynamic(dn);
  201. if (!phb)
  202. return -EIO;
  203. if (rpaphp_add_slot(dn)) {
  204. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  205. __func__, drc_name);
  206. return -EIO;
  207. }
  208. return 0;
  209. }
  210. static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
  211. {
  212. if (vio_find_node(dn))
  213. return -EINVAL;
  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. return 0;
  281. }
  282. /**
  283. * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
  284. * @drc_name: drc-name of newly added slot
  285. * @dn: &device_node
  286. *
  287. * Remove the kernel and hotplug representations of a PCI I/O Slot.
  288. * Return Codes:
  289. * 0 Success
  290. * -ENODEV Not a valid drc_name
  291. * -EIO Internal PCI Error
  292. */
  293. int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
  294. {
  295. struct pci_bus *bus;
  296. struct slot *slot;
  297. bus = pcibios_find_pci_bus(dn);
  298. if (!bus)
  299. return -EINVAL;
  300. pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
  301. bus->self ? pci_name(bus->self) : "<!PHB!>");
  302. slot = find_php_slot(dn);
  303. if (slot) {
  304. pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
  305. pci_domain_nr(bus), bus->number);
  306. if (rpaphp_deregister_slot(slot)) {
  307. printk(KERN_ERR
  308. "%s: unable to remove hotplug slot %s\n",
  309. __func__, drc_name);
  310. return -EIO;
  311. }
  312. }
  313. /* Remove all devices below slot */
  314. pcibios_remove_pci_devices(bus);
  315. /* Unmap PCI IO space */
  316. if (pcibios_unmap_io_space(bus)) {
  317. printk(KERN_ERR "%s: failed to unmap bus range\n",
  318. __func__);
  319. return -ERANGE;
  320. }
  321. /* Remove the EADS bridge device itself */
  322. BUG_ON(!bus->self);
  323. pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
  324. eeh_remove_bus_device(bus->self);
  325. pci_remove_bus_device(bus->self);
  326. return 0;
  327. }
  328. /**
  329. * dlpar_remove_slot - DLPAR remove an I/O Slot
  330. * @drc_name: drc-name of newly added slot
  331. *
  332. * Remove the kernel and hotplug representations of an I/O Slot.
  333. * Return Codes:
  334. * 0 Success
  335. * -ENODEV Not a valid drc_name
  336. * -EINVAL Slot already removed
  337. * -ERESTARTSYS Signalled before obtaining lock
  338. * -EIO Internal Error
  339. */
  340. int dlpar_remove_slot(char *drc_name)
  341. {
  342. struct device_node *dn;
  343. int node_type;
  344. int rc = 0;
  345. if (mutex_lock_interruptible(&rpadlpar_mutex))
  346. return -ERESTARTSYS;
  347. dn = find_dlpar_node(drc_name, &node_type);
  348. if (!dn) {
  349. rc = -ENODEV;
  350. goto exit;
  351. }
  352. switch (node_type) {
  353. case NODE_TYPE_VIO:
  354. rc = dlpar_remove_vio_slot(drc_name, dn);
  355. break;
  356. case NODE_TYPE_PHB:
  357. rc = dlpar_remove_phb(drc_name, dn);
  358. break;
  359. case NODE_TYPE_SLOT:
  360. rc = dlpar_remove_pci_slot(drc_name, dn);
  361. break;
  362. }
  363. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  364. exit:
  365. mutex_unlock(&rpadlpar_mutex);
  366. return rc;
  367. }
  368. static inline int is_dlpar_capable(void)
  369. {
  370. int rc = rtas_token("ibm,configure-connector");
  371. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  372. }
  373. int __init rpadlpar_io_init(void)
  374. {
  375. int rc = 0;
  376. if (!is_dlpar_capable()) {
  377. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  378. __func__);
  379. return -EPERM;
  380. }
  381. rc = dlpar_sysfs_init();
  382. return rc;
  383. }
  384. void rpadlpar_io_exit(void)
  385. {
  386. dlpar_sysfs_exit();
  387. return;
  388. }
  389. module_init(rpadlpar_io_init);
  390. module_exit(rpadlpar_io_exit);
  391. MODULE_LICENSE("GPL");