rpadlpar_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 "../pci.h"
  23. #include "rpaphp.h"
  24. #include "rpadlpar.h"
  25. static DECLARE_MUTEX(rpadlpar_sem);
  26. #define NODE_TYPE_VIO 1
  27. #define NODE_TYPE_SLOT 2
  28. #define NODE_TYPE_PHB 3
  29. static struct device_node *find_php_slot_vio_node(char *drc_name)
  30. {
  31. struct device_node *child;
  32. struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
  33. char *loc_code;
  34. if (!parent)
  35. return NULL;
  36. for (child = of_get_next_child(parent, NULL);
  37. child; child = of_get_next_child(parent, child)) {
  38. loc_code = get_property(child, "ibm,loc-code", NULL);
  39. if (loc_code && !strncmp(loc_code, drc_name, strlen(drc_name)))
  40. return child;
  41. }
  42. return NULL;
  43. }
  44. /* Find dlpar-capable pci node that contains the specified name and type */
  45. static struct device_node *find_php_slot_pci_node(char *drc_name,
  46. char *drc_type)
  47. {
  48. struct device_node *np = NULL;
  49. char *name;
  50. char *type;
  51. int rc;
  52. while ((np = of_find_node_by_type(np, "pci"))) {
  53. rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
  54. if (rc == 0)
  55. if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
  56. break;
  57. }
  58. return np;
  59. }
  60. static struct device_node *find_newly_added_node(char *drc_name, int *node_type)
  61. {
  62. struct device_node *dn;
  63. dn = find_php_slot_pci_node(drc_name, "SLOT");
  64. if (dn) {
  65. *node_type = NODE_TYPE_SLOT;
  66. return dn;
  67. }
  68. dn = find_php_slot_pci_node(drc_name, "PHB");
  69. if (dn) {
  70. *node_type = NODE_TYPE_PHB;
  71. return dn;
  72. }
  73. dn = find_php_slot_vio_node(drc_name);
  74. if (dn) {
  75. *node_type = NODE_TYPE_VIO;
  76. return dn;
  77. }
  78. return NULL;
  79. }
  80. static struct slot *find_slot(char *drc_name)
  81. {
  82. struct list_head *tmp, *n;
  83. struct slot *slot;
  84. list_for_each_safe(tmp, n, &rpaphp_slot_head) {
  85. slot = list_entry(tmp, struct slot, rpaphp_slot_list);
  86. if (strcmp(slot->location, drc_name) == 0)
  87. return slot;
  88. }
  89. return NULL;
  90. }
  91. static void rpadlpar_claim_one_bus(struct pci_bus *b)
  92. {
  93. struct list_head *ld;
  94. struct pci_bus *child_bus;
  95. for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
  96. struct pci_dev *dev = pci_dev_b(ld);
  97. int i;
  98. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  99. struct resource *r = &dev->resource[i];
  100. if (r->parent || !r->start || !r->flags)
  101. continue;
  102. rpaphp_claim_resource(dev, i);
  103. }
  104. }
  105. list_for_each_entry(child_bus, &b->children, node)
  106. rpadlpar_claim_one_bus(child_bus);
  107. }
  108. static int pci_add_secondary_bus(struct device_node *dn,
  109. struct pci_dev *bridge_dev)
  110. {
  111. struct pci_controller *hose = dn->phb;
  112. struct pci_bus *child;
  113. u8 sec_busno;
  114. /* Get busno of downstream bus */
  115. pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
  116. /* Allocate and add to children of bridge_dev->bus */
  117. child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
  118. if (!child) {
  119. printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
  120. return -ENOMEM;
  121. }
  122. sprintf(child->name, "PCI Bus #%02x", child->number);
  123. /* Fixup subordinate bridge bases and resources */
  124. pcibios_fixup_bus(child);
  125. /* Claim new bus resources */
  126. rpadlpar_claim_one_bus(bridge_dev->bus);
  127. if (hose->last_busno < child->number)
  128. hose->last_busno = child->number;
  129. dn->bussubno = child->number;
  130. /* ioremap() for child bus, which may or may not succeed */
  131. remap_bus_range(child);
  132. return 0;
  133. }
  134. static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
  135. {
  136. struct pci_controller *hose = dn->phb;
  137. struct pci_dev *dev = NULL;
  138. /* Scan phb bus for EADS device, adding new one to bus->devices */
  139. if (!pci_scan_single_device(hose->bus, dn->devfn)) {
  140. printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
  141. return NULL;
  142. }
  143. /* Add new devices to global lists. Register in proc, sysfs. */
  144. pci_bus_add_devices(hose->bus);
  145. /* Confirm new bridge dev was created */
  146. dev = rpaphp_find_pci_dev(dn);
  147. if (!dev) {
  148. printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
  149. return NULL;
  150. }
  151. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  152. printk(KERN_ERR "%s: unexpected header type %d\n",
  153. __FUNCTION__, dev->hdr_type);
  154. return NULL;
  155. }
  156. if (pci_add_secondary_bus(dn, dev))
  157. return NULL;
  158. return dev;
  159. }
  160. static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
  161. {
  162. struct pci_bus *secondary_bus;
  163. if (!bridge_dev) {
  164. printk(KERN_ERR "%s: unexpected null device\n",
  165. __FUNCTION__);
  166. return -EINVAL;
  167. }
  168. secondary_bus = bridge_dev->subordinate;
  169. if (unmap_bus_range(secondary_bus)) {
  170. printk(KERN_ERR "%s: failed to unmap bus range\n",
  171. __FUNCTION__);
  172. return -ERANGE;
  173. }
  174. pci_remove_bus_device(bridge_dev);
  175. return 0;
  176. }
  177. static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
  178. {
  179. struct pci_dev *dev;
  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. return 0;
  188. }
  189. static int dlpar_remove_root_bus(struct pci_controller *phb)
  190. {
  191. struct pci_bus *phb_bus;
  192. int rc;
  193. phb_bus = phb->bus;
  194. if (!(list_empty(&phb_bus->children) &&
  195. list_empty(&phb_bus->devices))) {
  196. return -EBUSY;
  197. }
  198. rc = pcibios_remove_root_bus(phb);
  199. if (rc)
  200. return -EIO;
  201. device_unregister(phb_bus->bridge);
  202. pci_remove_bus(phb_bus);
  203. return 0;
  204. }
  205. static int dlpar_remove_phb(struct slot *slot)
  206. {
  207. struct pci_controller *phb;
  208. struct device_node *dn;
  209. int rc = 0;
  210. dn = slot->dn;
  211. if (!dn) {
  212. printk(KERN_ERR "%s: unexpected NULL slot device node\n",
  213. __FUNCTION__);
  214. return -EIO;
  215. }
  216. phb = dn->phb;
  217. if (!phb) {
  218. printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
  219. __FUNCTION__);
  220. return -EIO;
  221. }
  222. if (rpaphp_remove_slot(slot)) {
  223. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  224. __FUNCTION__, slot->location);
  225. return -EIO;
  226. }
  227. rc = dlpar_remove_root_bus(phb);
  228. if (rc < 0)
  229. return rc;
  230. return 0;
  231. }
  232. static int dlpar_add_phb(struct device_node *dn)
  233. {
  234. struct pci_controller *phb;
  235. phb = init_phb_dynamic(dn);
  236. if (!phb)
  237. return -EINVAL;
  238. return 0;
  239. }
  240. /**
  241. * dlpar_add_slot - DLPAR add an I/O Slot
  242. * @drc_name: drc-name of newly added slot
  243. *
  244. * Make the hotplug module and the kernel aware
  245. * of a newly added I/O Slot.
  246. * Return Codes -
  247. * 0 Success
  248. * -ENODEV Not a valid drc_name
  249. * -EINVAL Slot already added
  250. * -ERESTARTSYS Signalled before obtaining lock
  251. * -EIO Internal PCI Error
  252. */
  253. int dlpar_add_slot(char *drc_name)
  254. {
  255. struct device_node *dn = NULL;
  256. int node_type;
  257. int rc = 0;
  258. if (down_interruptible(&rpadlpar_sem))
  259. return -ERESTARTSYS;
  260. /* Check for existing hotplug slot */
  261. if (find_slot(drc_name)) {
  262. rc = -EINVAL;
  263. goto exit;
  264. }
  265. dn = find_newly_added_node(drc_name, &node_type);
  266. if (!dn) {
  267. rc = -ENODEV;
  268. goto exit;
  269. }
  270. switch (node_type) {
  271. case NODE_TYPE_VIO:
  272. /* Just add hotplug slot */
  273. break;
  274. case NODE_TYPE_SLOT:
  275. rc = dlpar_add_pci_slot(drc_name, dn);
  276. break;
  277. case NODE_TYPE_PHB:
  278. rc = dlpar_add_phb(dn);
  279. break;
  280. default:
  281. printk("%s: unexpected node type\n", __FUNCTION__);
  282. return -EIO;
  283. }
  284. if (!rc && rpaphp_add_slot(dn)) {
  285. printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
  286. __FUNCTION__, drc_name);
  287. rc = -EIO;
  288. }
  289. exit:
  290. up(&rpadlpar_sem);
  291. return rc;
  292. }
  293. /**
  294. * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
  295. * @drc_name: drc-name of newly added slot
  296. *
  297. * Remove the kernel and hotplug representations
  298. * of an I/O Slot.
  299. * Return Codes:
  300. * 0 Success
  301. * -EIO Internal Error
  302. */
  303. int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
  304. {
  305. /* Remove hotplug slot */
  306. if (rpaphp_remove_slot(slot)) {
  307. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  308. __FUNCTION__, drc_name);
  309. return -EIO;
  310. }
  311. return 0;
  312. }
  313. /**
  314. * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
  315. * @drc_name: drc-name of newly added slot
  316. *
  317. * Remove the kernel and hotplug representations
  318. * of a PCI I/O Slot.
  319. * Return Codes:
  320. * 0 Success
  321. * -ENODEV Not a valid drc_name
  322. * -EIO Internal PCI Error
  323. */
  324. int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
  325. {
  326. struct pci_dev *bridge_dev;
  327. bridge_dev = slot->bridge;
  328. if (!bridge_dev) {
  329. printk(KERN_ERR "%s: unexpected null bridge device\n",
  330. __FUNCTION__);
  331. return -EIO;
  332. }
  333. /* Remove hotplug slot */
  334. if (rpaphp_remove_slot(slot)) {
  335. printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
  336. __FUNCTION__, drc_name);
  337. return -EIO;
  338. }
  339. /* Remove pci bus */
  340. if (dlpar_pci_remove_bus(bridge_dev)) {
  341. printk(KERN_ERR "%s: unable to remove pci bus %s\n",
  342. __FUNCTION__, drc_name);
  343. return -EIO;
  344. }
  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 slot *slot;
  363. int rc = 0;
  364. if (down_interruptible(&rpadlpar_sem))
  365. return -ERESTARTSYS;
  366. if (!find_php_slot_vio_node(drc_name) &&
  367. !find_php_slot_pci_node(drc_name, "SLOT") &&
  368. !find_php_slot_pci_node(drc_name, "PHB")) {
  369. rc = -ENODEV;
  370. goto exit;
  371. }
  372. slot = find_slot(drc_name);
  373. if (!slot) {
  374. rc = -EINVAL;
  375. goto exit;
  376. }
  377. if (slot->type == PHB) {
  378. rc = dlpar_remove_phb(slot);
  379. } else {
  380. switch (slot->dev_type) {
  381. case PCI_DEV:
  382. rc = dlpar_remove_pci_slot(slot, drc_name);
  383. break;
  384. case VIO_DEV:
  385. rc = dlpar_remove_vio_slot(slot, drc_name);
  386. break;
  387. }
  388. }
  389. exit:
  390. up(&rpadlpar_sem);
  391. return rc;
  392. }
  393. static inline int is_dlpar_capable(void)
  394. {
  395. int rc = rtas_token("ibm,configure-connector");
  396. return (int) (rc != RTAS_UNKNOWN_SERVICE);
  397. }
  398. int __init rpadlpar_io_init(void)
  399. {
  400. int rc = 0;
  401. if (!is_dlpar_capable()) {
  402. printk(KERN_WARNING "%s: partition not DLPAR capable\n",
  403. __FUNCTION__);
  404. return -EPERM;
  405. }
  406. rc = dlpar_sysfs_init();
  407. return rc;
  408. }
  409. void rpadlpar_io_exit(void)
  410. {
  411. dlpar_sysfs_exit();
  412. return;
  413. }
  414. module_init(rpadlpar_io_init);
  415. module_exit(rpadlpar_io_exit);
  416. MODULE_LICENSE("GPL");