rpadlpar_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/string.h>
  20. #include <asm/pci-bridge.h>
  21. #include <asm/semaphore.h>
  22. #include <asm/rtas.h>
  23. #include <asm/vio.h>
  24. #include "../pci.h"
  25. #include "rpaphp.h"
  26. #include "rpadlpar.h"
  27. static DECLARE_MUTEX(rpadlpar_sem);
  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. char *name;
  37. int rc;
  38. if (!parent)
  39. return NULL;
  40. while ((dn = of_get_next_child(parent, dn))) {
  41. rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
  42. if ((rc == 0) && (!strcmp(drc_name, name)))
  43. break;
  44. }
  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. char *name;
  53. char *type;
  54. int rc;
  55. while ((np = of_find_node_by_type(np, "pci"))) {
  56. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  57. if (rc == 0)
  58. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  59. break;
  60. }
  61. return np;
  62. }
  63. static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
  64. {
  65. struct device_node *dn;
  66. dn = find_php_slot_pci_node(drc_name, "SLOT");
  67. if (dn) {
  68. *node_type = NODE_TYPE_SLOT;
  69. return dn;
  70. }
  71. dn = find_php_slot_pci_node(drc_name, "PHB");
  72. if (dn) {
  73. *node_type = NODE_TYPE_PHB;
  74. return dn;
  75. }
  76. dn = find_vio_slot_node(drc_name);
  77. if (dn) {
  78. *node_type = NODE_TYPE_VIO;
  79. return dn;
  80. }
  81. return NULL;
  82. }
  83. static struct slot *find_slot(struct device_node *dn)
  84. {
  85. struct list_head *tmp, *n;
  86. struct slot *slot;
  87. list_for_each_safe(tmp, n, &rpaphp_slot_head) {
  88. slot = list_entry(tmp, struct slot, rpaphp_slot_list);
  89. if (slot->dn == dn)
  90. return slot;
  91. }
  92. return NULL;
  93. }
  94. static void rpadlpar_claim_one_bus(struct pci_bus *b)
  95. {
  96. struct list_head *ld;
  97. struct pci_bus *child_bus;
  98. for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
  99. struct pci_dev *dev = pci_dev_b(ld);
  100. int i;
  101. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  102. struct resource *r = &dev->resource[i];
  103. if (r->parent || !r->start || !r->flags)
  104. continue;
  105. rpaphp_claim_resource(dev, i);
  106. }
  107. }
  108. list_for_each_entry(child_bus, &b->children, node)
  109. rpadlpar_claim_one_bus(child_bus);
  110. }
  111. static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
  112. struct device_node *dev_dn)
  113. {
  114. struct pci_dev *tmp = NULL;
  115. struct device_node *child_dn;
  116. list_for_each_entry(tmp, &parent->devices, bus_list) {
  117. child_dn = pci_device_to_OF_node(tmp);
  118. if (child_dn == dev_dn)
  119. return tmp;
  120. }
  121. return NULL;
  122. }
  123. static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
  124. {
  125. struct pci_dn *pdn = dn->data;
  126. struct pci_controller *phb = pdn->phb;
  127. struct pci_dev *dev = NULL;
  128. rpaphp_eeh_init_nodes(dn);
  129. /* Add EADS device to PHB bus, adding new entry to bus->devices */
  130. dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
  131. if (!dev) {
  132. printk(KERN_ERR "%s: failed to create pci dev for %s\n",
  133. __FUNCTION__, dn->full_name);
  134. return NULL;
  135. }
  136. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  137. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  138. of_scan_pci_bridge(dn, dev);
  139. rpaphp_init_new_devs(dev->subordinate);
  140. /* Claim new bus resources */
  141. rpadlpar_claim_one_bus(dev->bus);
  142. /* ioremap() for child bus, which may or may not succeed */
  143. (void) remap_bus_range(dev->bus);
  144. /* Add new devices to global lists. Register in proc, sysfs. */
  145. pci_bus_add_devices(phb->bus);
  146. /* Confirm new bridge dev was created */
  147. dev = dlpar_find_new_dev(phb->bus, dn);
  148. if (dev) {
  149. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  150. printk(KERN_ERR "%s: unexpected header type %d\n",
  151. __FUNCTION__, dev->hdr_type);
  152. return NULL;
  153. }
  154. }
  155. return dev;
  156. }
  157. static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  158. {
  159. struct pci_dev *dev;
  160. if (rpaphp_find_pci_bus(dn))
  161. return -EINVAL;
  162. /* Add pci bus */
  163. dev = dlpar_pci_add_bus(dn);
  164. if (!dev) {
  165. printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
  166. drc_name);
  167. return -EIO;
  168. }
  169. /* Add hotplug slot */
  170. if (rpaphp_add_slot(dn)) {
  171. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  172. __FUNCTION__, drc_name);
  173. return -EIO;
  174. }
  175. return 0;
  176. }
  177. static int dlpar_remove_root_bus(struct pci_controller *phb)
  178. {
  179. struct pci_bus *phb_bus;
  180. int rc;
  181. phb_bus = phb->bus;
  182. if (!(list_empty(&phb_bus->children) &&
  183. list_empty(&phb_bus->devices))) {
  184. return -EBUSY;
  185. }
  186. rc = pcibios_remove_root_bus(phb);
  187. if (rc)
  188. return -EIO;
  189. device_unregister(phb_bus->bridge);
  190. pci_remove_bus(phb_bus);
  191. return 0;
  192. }
  193. static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
  194. {
  195. struct slot *slot;
  196. struct pci_dn *pdn;
  197. int rc = 0;
  198. if (!rpaphp_find_pci_bus(dn))
  199. return -EINVAL;
  200. slot = find_slot(dn);
  201. if (slot) {
  202. /* Remove hotplug slot */
  203. if (rpaphp_remove_slot(slot)) {
  204. printk(KERN_ERR
  205. "%s: unable to remove hotplug slot %s\n",
  206. __FUNCTION__, drc_name);
  207. return -EIO;
  208. }
  209. }
  210. pdn = dn->data;
  211. BUG_ON(!pdn || !pdn->phb);
  212. rc = dlpar_remove_root_bus(pdn->phb);
  213. if (rc < 0)
  214. return rc;
  215. pdn->phb = NULL;
  216. return 0;
  217. }
  218. static int dlpar_add_phb(char *drc_name, struct device_node *dn)
  219. {
  220. struct pci_controller *phb;
  221. if (PCI_DN(dn) && PCI_DN(dn)->phb) {
  222. /* PHB already exists */
  223. return -EINVAL;
  224. }
  225. phb = init_phb_dynamic(dn);
  226. if (!phb)
  227. return -EIO;
  228. if (rpaphp_add_slot(dn)) {
  229. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  230. __FUNCTION__, drc_name);
  231. return -EIO;
  232. }
  233. return 0;
  234. }
  235. static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
  236. {
  237. if (vio_find_node(dn))
  238. return -EINVAL;
  239. if (!vio_register_device_node(dn)) {
  240. printk(KERN_ERR
  241. "%s: failed to register vio node %s\n",
  242. __FUNCTION__, drc_name);
  243. return -EIO;
  244. }
  245. return 0;
  246. }
  247. /**
  248. * dlpar_add_slot - DLPAR add an I/O Slot
  249. * @drc_name: drc-name of newly added slot
  250. *
  251. * Make the hotplug module and the kernel aware
  252. * of a newly added I/O Slot.
  253. * Return Codes -
  254. * 0 Success
  255. * -ENODEV Not a valid drc_name
  256. * -EINVAL Slot already added
  257. * -ERESTARTSYS Signalled before obtaining lock
  258. * -EIO Internal PCI Error
  259. */
  260. int dlpar_add_slot(char *drc_name)
  261. {
  262. struct device_node *dn = NULL;
  263. int node_type;
  264. int rc = -EIO;
  265. if (down_interruptible(&rpadlpar_sem))
  266. return -ERESTARTSYS;
  267. /* Find newly added node */
  268. dn = find_dlpar_node(drc_name, &node_type);
  269. if (!dn) {
  270. rc = -ENODEV;
  271. goto exit;
  272. }
  273. switch (node_type) {
  274. case NODE_TYPE_VIO:
  275. rc = dlpar_add_vio_slot(drc_name, dn);
  276. break;
  277. case NODE_TYPE_SLOT:
  278. rc = dlpar_add_pci_slot(drc_name, dn);
  279. break;
  280. case NODE_TYPE_PHB:
  281. rc = dlpar_add_phb(drc_name, dn);
  282. break;
  283. }
  284. printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
  285. exit:
  286. up(&rpadlpar_sem);
  287. return rc;
  288. }
  289. /**
  290. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  291. * @drc_name: drc-name of newly added slot
  292. *
  293. * Remove the kernel and hotplug representations
  294. * of an I/O Slot.
  295. * Return Codes:
  296. * 0 Success
  297. * -EINVAL Vio dev doesn't exist
  298. */
  299. static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
  300. {
  301. struct vio_dev *vio_dev;
  302. vio_dev = vio_find_node(dn);
  303. if (!vio_dev)
  304. return -EINVAL;
  305. vio_unregister_device(vio_dev);
  306. return 0;
  307. }
  308. /**
  309. * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
  310. * @drc_name: drc-name of newly added slot
  311. *
  312. * Remove the kernel and hotplug representations
  313. * of a PCI I/O Slot.
  314. * Return Codes:
  315. * 0 Success
  316. * -ENODEV Not a valid drc_name
  317. * -EIO Internal PCI Error
  318. */
  319. int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
  320. {
  321. struct pci_bus *bus;
  322. struct slot *slot;
  323. bus = rpaphp_find_pci_bus(dn);
  324. if (!bus)
  325. return -EINVAL;
  326. slot = find_slot(dn);
  327. if (slot) {
  328. /* Remove hotplug slot */
  329. if (rpaphp_remove_slot(slot)) {
  330. printk(KERN_ERR
  331. "%s: unable to remove hotplug slot %s\n",
  332. __FUNCTION__, drc_name);
  333. return -EIO;
  334. }
  335. } else {
  336. rpaphp_unconfig_pci_adapter(bus);
  337. }
  338. if (unmap_bus_range(bus)) {
  339. printk(KERN_ERR "%s: failed to unmap bus range\n",
  340. __FUNCTION__);
  341. return -ERANGE;
  342. }
  343. BUG_ON(!bus->self);
  344. pci_remove_bus_device(bus->self);
  345. return 0;
  346. }
  347. /**
  348. * dlpar_remove_slot - DLPAR remove an I/O Slot
  349. * @drc_name: drc-name of newly added slot
  350. *
  351. * Remove the kernel and hotplug representations
  352. * of an I/O Slot.
  353. * Return Codes:
  354. * 0 Success
  355. * -ENODEV Not a valid drc_name
  356. * -EINVAL Slot already removed
  357. * -ERESTARTSYS Signalled before obtaining lock
  358. * -EIO Internal Error
  359. */
  360. int dlpar_remove_slot(char *drc_name)
  361. {
  362. struct device_node *dn;
  363. int node_type;
  364. int rc = 0;
  365. if (down_interruptible(&rpadlpar_sem))
  366. return -ERESTARTSYS;
  367. dn = find_dlpar_node(drc_name, &node_type);
  368. if (!dn) {
  369. rc = -ENODEV;
  370. goto exit;
  371. }
  372. switch (node_type) {
  373. case NODE_TYPE_VIO:
  374. rc = dlpar_remove_vio_slot(drc_name, dn);
  375. break;
  376. case NODE_TYPE_PHB:
  377. rc = dlpar_remove_phb(drc_name, dn);
  378. break;
  379. case NODE_TYPE_SLOT:
  380. rc = dlpar_remove_pci_slot(drc_name, dn);
  381. break;
  382. }
  383. printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
  384. exit:
  385. up(&rpadlpar_sem);
  386. return rc;
  387. }
  388. static inline int is_dlpar_capable(void)
  389. {
  390. int rc = rtas_token("ibm,configure-connector");
  391. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  392. }
  393. int __init rpadlpar_io_init(void)
  394. {
  395. int rc = 0;
  396. if (!is_dlpar_capable()) {
  397. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  398. __FUNCTION__);
  399. return -EPERM;
  400. }
  401. rc = dlpar_sysfs_init();
  402. return rc;
  403. }
  404. void rpadlpar_io_exit(void)
  405. {
  406. dlpar_sysfs_exit();
  407. return;
  408. }
  409. module_init(rpadlpar_io_init);
  410. module_exit(rpadlpar_io_exit);
  411. MODULE_LICENSE("GPL");