rpadlpar_core.c 11 KB

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