rpadlpar_core.c 11 KB

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