rpaphp_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
  3. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Send feedback to <lxie@us.ibm.com>
  23. *
  24. */
  25. #include <linux/pci.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/rtas.h>
  28. #include <asm/machdep.h>
  29. #include "../pci.h" /* for pci_add_new_bus */
  30. #include "rpaphp.h"
  31. static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
  32. struct device_node *dn)
  33. {
  34. struct pci_bus *child = NULL;
  35. struct list_head *tmp;
  36. struct device_node *busdn;
  37. busdn = pci_bus_to_OF_node(bus);
  38. if (busdn == dn)
  39. return bus;
  40. list_for_each(tmp, &bus->children) {
  41. child = find_bus_among_children(pci_bus_b(tmp), dn);
  42. if (child)
  43. break;
  44. }
  45. return child;
  46. }
  47. static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
  48. {
  49. if (!dn->phb || !dn->phb->bus)
  50. return NULL;
  51. return find_bus_among_children(dn->phb->bus, dn);
  52. }
  53. int rpaphp_claim_resource(struct pci_dev *dev, int resource)
  54. {
  55. struct resource *res = &dev->resource[resource];
  56. struct resource *root = pci_find_parent_resource(dev, res);
  57. char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  58. int err = -EINVAL;
  59. if (root != NULL) {
  60. err = request_resource(root, res);
  61. }
  62. if (err) {
  63. err("PCI: %s region %d of %s %s [%lx:%lx]\n",
  64. root ? "Address space collision on" :
  65. "No parent found for",
  66. resource, dtype, pci_name(dev), res->start, res->end);
  67. }
  68. return err;
  69. }
  70. EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
  71. static int rpaphp_get_sensor_state(struct slot *slot, int *state)
  72. {
  73. int rc;
  74. int setlevel;
  75. rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
  76. if (rc < 0) {
  77. if (rc == -EFAULT || rc == -EEXIST) {
  78. dbg("%s: slot must be power up to get sensor-state\n",
  79. __FUNCTION__);
  80. /* some slots have to be powered up
  81. * before get-sensor will succeed.
  82. */
  83. rc = rtas_set_power_level(slot->power_domain, POWER_ON,
  84. &setlevel);
  85. if (rc < 0) {
  86. dbg("%s: power on slot[%s] failed rc=%d.\n",
  87. __FUNCTION__, slot->name, rc);
  88. } else {
  89. rc = rtas_get_sensor(DR_ENTITY_SENSE,
  90. slot->index, state);
  91. }
  92. } else if (rc == -ENODEV)
  93. info("%s: slot is unusable\n", __FUNCTION__);
  94. else
  95. err("%s failed to get sensor state\n", __FUNCTION__);
  96. }
  97. return rc;
  98. }
  99. /**
  100. * get_pci_adapter_status - get the status of a slot
  101. *
  102. * 0-- slot is empty
  103. * 1-- adapter is configured
  104. * 2-- adapter is not configured
  105. * 3-- not valid
  106. */
  107. int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
  108. {
  109. struct pci_bus *bus;
  110. int state, rc;
  111. *value = NOT_VALID;
  112. rc = rpaphp_get_sensor_state(slot, &state);
  113. if (rc)
  114. goto exit;
  115. if ((state == EMPTY) || (slot->type == PHB)) {
  116. dbg("slot is empty\n");
  117. *value = EMPTY;
  118. }
  119. else if (state == PRESENT) {
  120. if (!is_init) {
  121. /* at run-time slot->state can be changed by */
  122. /* config/unconfig adapter */
  123. *value = slot->state;
  124. } else {
  125. bus = rpaphp_find_pci_bus(slot->dn);
  126. if (bus && !list_empty(&bus->devices))
  127. *value = CONFIGURED;
  128. else
  129. *value = NOT_CONFIGURED;
  130. }
  131. }
  132. exit:
  133. return rc;
  134. }
  135. /* Must be called before pci_bus_add_devices */
  136. static void
  137. rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
  138. {
  139. struct pci_dev *dev;
  140. list_for_each_entry(dev, &bus->devices, bus_list) {
  141. /*
  142. * Skip already-present devices (which are on the
  143. * global device list.)
  144. */
  145. if (list_empty(&dev->global_list)) {
  146. int i;
  147. /* Need to setup IOMMU tables */
  148. ppc_md.iommu_dev_setup(dev);
  149. if(fix_bus)
  150. pcibios_fixup_device_resources(dev, bus);
  151. pci_read_irq_line(dev);
  152. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  153. struct resource *r = &dev->resource[i];
  154. if (r->parent || !r->start || !r->flags)
  155. continue;
  156. rpaphp_claim_resource(dev, i);
  157. }
  158. }
  159. }
  160. }
  161. static int rpaphp_pci_config_bridge(struct pci_dev *dev)
  162. {
  163. u8 sec_busno;
  164. struct pci_bus *child_bus;
  165. struct pci_dev *child_dev;
  166. dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
  167. /* get busno of downstream bus */
  168. pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
  169. /* add to children of PCI bridge dev->bus */
  170. child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
  171. if (!child_bus) {
  172. err("%s: could not add second bus\n", __FUNCTION__);
  173. return -EIO;
  174. }
  175. sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
  176. /* do pci_scan_child_bus */
  177. pci_scan_child_bus(child_bus);
  178. list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
  179. eeh_add_device_late(child_dev);
  180. }
  181. /* fixup new pci devices without touching bus struct */
  182. rpaphp_fixup_new_pci_devices(child_bus, 0);
  183. /* Make the discovered devices available */
  184. pci_bus_add_devices(child_bus);
  185. return 0;
  186. }
  187. /*****************************************************************************
  188. rpaphp_pci_config_slot() will configure all devices under the
  189. given slot->dn and return the the first pci_dev.
  190. *****************************************************************************/
  191. static struct pci_dev *
  192. rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
  193. {
  194. struct pci_dev *dev = NULL;
  195. int slotno;
  196. int num;
  197. dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
  198. if (!dn->child)
  199. return NULL;
  200. slotno = PCI_SLOT(dn->child->devfn);
  201. /* pci_scan_slot should find all children */
  202. num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  203. if (num) {
  204. rpaphp_fixup_new_pci_devices(bus, 1);
  205. pci_bus_add_devices(bus);
  206. }
  207. if (list_empty(&bus->devices)) {
  208. err("%s: No new device found\n", __FUNCTION__);
  209. return NULL;
  210. }
  211. list_for_each_entry(dev, &bus->devices, bus_list) {
  212. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  213. rpaphp_pci_config_bridge(dev);
  214. }
  215. return dev;
  216. }
  217. static void enable_eeh(struct device_node *dn)
  218. {
  219. struct device_node *sib;
  220. for (sib = dn->child; sib; sib = sib->sibling)
  221. enable_eeh(sib);
  222. eeh_add_device_early(dn);
  223. return;
  224. }
  225. static void print_slot_pci_funcs(struct slot *slot)
  226. {
  227. struct pci_dev *dev;
  228. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, slot->name);
  229. list_for_each_entry (dev, slot->pci_devs, bus_list)
  230. dbg("\t%s\n", pci_name(dev));
  231. return;
  232. }
  233. static int rpaphp_config_pci_adapter(struct slot *slot)
  234. {
  235. struct pci_dev *dev;
  236. int rc = -ENODEV;
  237. dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
  238. enable_eeh(slot->dn);
  239. dev = rpaphp_pci_config_slot(slot->dn, slot->bus);
  240. if (!dev) {
  241. err("%s: can't find any devices.\n", __FUNCTION__);
  242. goto exit;
  243. }
  244. print_slot_pci_funcs(slot);
  245. rc = 0;
  246. exit:
  247. dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
  248. return rc;
  249. }
  250. static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
  251. {
  252. eeh_remove_device(dev);
  253. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  254. struct pci_bus *bus = dev->subordinate;
  255. struct list_head *ln;
  256. if (!bus)
  257. return;
  258. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  259. struct pci_dev *pdev = pci_dev_b(ln);
  260. if (pdev)
  261. rpaphp_eeh_remove_bus_device(pdev);
  262. }
  263. }
  264. return;
  265. }
  266. int rpaphp_unconfig_pci_adapter(struct slot *slot)
  267. {
  268. struct pci_dev *dev, *tmp;
  269. int retval = 0;
  270. list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
  271. rpaphp_eeh_remove_bus_device(dev);
  272. pci_remove_bus_device(dev);
  273. }
  274. slot->state = NOT_CONFIGURED;
  275. info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
  276. slot->name);
  277. return retval;
  278. }
  279. static int setup_pci_hotplug_slot_info(struct slot *slot)
  280. {
  281. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  282. __FUNCTION__);
  283. rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
  284. rpaphp_get_pci_adapter_status(slot, 1,
  285. &slot->hotplug_slot->info->
  286. adapter_status);
  287. if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
  288. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  289. __FUNCTION__, slot->dn->full_name);
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. static void set_slot_name(struct slot *slot)
  295. {
  296. struct pci_bus *bus = slot->bus;
  297. struct pci_dev *bridge;
  298. bridge = bus->self;
  299. if (bridge)
  300. strcpy(slot->name, pci_name(bridge));
  301. else
  302. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  303. bus->number);
  304. }
  305. static int setup_pci_slot(struct slot *slot)
  306. {
  307. struct device_node *dn = slot->dn;
  308. struct pci_bus *bus;
  309. BUG_ON(!dn);
  310. bus = rpaphp_find_pci_bus(dn);
  311. if (!bus) {
  312. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  313. goto exit_rc;
  314. }
  315. slot->bus = bus;
  316. slot->pci_devs = &bus->devices;
  317. set_slot_name(slot);
  318. /* find slot's pci_dev if it's not empty */
  319. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  320. slot->state = EMPTY; /* slot is empty */
  321. } else {
  322. /* slot is occupied */
  323. if (!dn->child) {
  324. /* non-empty slot has to have child */
  325. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  326. __FUNCTION__, slot->name);
  327. goto exit_rc;
  328. }
  329. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  330. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  331. __FUNCTION__, slot->name);
  332. if (rpaphp_config_pci_adapter(slot)) {
  333. err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
  334. goto exit_rc;
  335. }
  336. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  337. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  338. __FUNCTION__, slot->name);
  339. goto exit_rc;
  340. }
  341. print_slot_pci_funcs(slot);
  342. if (!list_empty(slot->pci_devs)) {
  343. slot->state = CONFIGURED;
  344. } else {
  345. /* DLPAR add as opposed to
  346. * boot time */
  347. slot->state = NOT_CONFIGURED;
  348. }
  349. }
  350. return 0;
  351. exit_rc:
  352. dealloc_slot_struct(slot);
  353. return -EINVAL;
  354. }
  355. int register_pci_slot(struct slot *slot)
  356. {
  357. int rc = -EINVAL;
  358. if ((slot->type == EMBEDDED) || (slot->type == PHB))
  359. slot->removable = 0;
  360. else
  361. slot->removable = 1;
  362. if (setup_pci_hotplug_slot_info(slot))
  363. goto exit_rc;
  364. if (setup_pci_slot(slot))
  365. goto exit_rc;
  366. rc = register_slot(slot);
  367. exit_rc:
  368. return rc;
  369. }
  370. int rpaphp_enable_pci_slot(struct slot *slot)
  371. {
  372. int retval = 0, state;
  373. retval = rpaphp_get_sensor_state(slot, &state);
  374. if (retval)
  375. goto exit;
  376. dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
  377. /* if slot is not empty, enable the adapter */
  378. if (state == PRESENT) {
  379. dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
  380. retval = rpaphp_config_pci_adapter(slot);
  381. if (!retval) {
  382. slot->state = CONFIGURED;
  383. dbg("%s: PCI devices in slot[%s] has been configured\n",
  384. __FUNCTION__, slot->name);
  385. } else {
  386. slot->state = NOT_CONFIGURED;
  387. dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
  388. __FUNCTION__, slot->name);
  389. }
  390. } else if (state == EMPTY) {
  391. dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
  392. slot->state = EMPTY;
  393. } else {
  394. err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
  395. slot->name);
  396. slot->state = NOT_VALID;
  397. retval = -EINVAL;
  398. }
  399. exit:
  400. dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
  401. return retval;
  402. }