rpadlpar_core.c 11 KB

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