rpaphp_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
  48. {
  49. struct pci_dn *pdn = dn->data;
  50. if (!pdn || !pdn->phb || !pdn->phb->bus)
  51. return NULL;
  52. return find_bus_among_children(pdn->phb->bus, dn);
  53. }
  54. EXPORT_SYMBOL_GPL(rpaphp_find_pci_bus);
  55. int rpaphp_claim_resource(struct pci_dev *dev, int resource)
  56. {
  57. struct resource *res = &dev->resource[resource];
  58. struct resource *root = pci_find_parent_resource(dev, res);
  59. char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  60. int err = -EINVAL;
  61. if (root != NULL) {
  62. err = request_resource(root, res);
  63. }
  64. if (err) {
  65. err("PCI: %s region %d of %s %s [%lx:%lx]\n",
  66. root ? "Address space collision on" :
  67. "No parent found for",
  68. resource, dtype, pci_name(dev), res->start, res->end);
  69. }
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
  73. static int rpaphp_get_sensor_state(struct slot *slot, int *state)
  74. {
  75. int rc;
  76. int setlevel;
  77. rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
  78. if (rc < 0) {
  79. if (rc == -EFAULT || rc == -EEXIST) {
  80. dbg("%s: slot must be power up to get sensor-state\n",
  81. __FUNCTION__);
  82. /* some slots have to be powered up
  83. * before get-sensor will succeed.
  84. */
  85. rc = rtas_set_power_level(slot->power_domain, POWER_ON,
  86. &setlevel);
  87. if (rc < 0) {
  88. dbg("%s: power on slot[%s] failed rc=%d.\n",
  89. __FUNCTION__, slot->name, rc);
  90. } else {
  91. rc = rtas_get_sensor(DR_ENTITY_SENSE,
  92. slot->index, state);
  93. }
  94. } else if (rc == -ENODEV)
  95. info("%s: slot is unusable\n", __FUNCTION__);
  96. else
  97. err("%s failed to get sensor state\n", __FUNCTION__);
  98. }
  99. return rc;
  100. }
  101. /**
  102. * get_pci_adapter_status - get the status of a slot
  103. *
  104. * 0-- slot is empty
  105. * 1-- adapter is configured
  106. * 2-- adapter is not configured
  107. * 3-- not valid
  108. */
  109. int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
  110. {
  111. struct pci_bus *bus;
  112. int state, rc;
  113. *value = NOT_VALID;
  114. rc = rpaphp_get_sensor_state(slot, &state);
  115. if (rc)
  116. goto exit;
  117. if (state == EMPTY)
  118. *value = EMPTY;
  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 pci_bus *bus)
  193. {
  194. struct device_node *dn = pci_bus_to_OF_node(bus);
  195. struct pci_dev *dev = NULL;
  196. int slotno;
  197. int num;
  198. dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
  199. if (!dn || !dn->child)
  200. return NULL;
  201. slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
  202. /* pci_scan_slot should find all children */
  203. num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  204. if (num) {
  205. rpaphp_fixup_new_pci_devices(bus, 1);
  206. pci_bus_add_devices(bus);
  207. }
  208. if (list_empty(&bus->devices)) {
  209. err("%s: No new device found\n", __FUNCTION__);
  210. return NULL;
  211. }
  212. list_for_each_entry(dev, &bus->devices, bus_list) {
  213. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  214. rpaphp_pci_config_bridge(dev);
  215. }
  216. return dev;
  217. }
  218. static void enable_eeh(struct device_node *dn)
  219. {
  220. struct device_node *sib;
  221. for (sib = dn->child; sib; sib = sib->sibling)
  222. enable_eeh(sib);
  223. eeh_add_device_early(dn);
  224. return;
  225. }
  226. static void print_slot_pci_funcs(struct pci_bus *bus)
  227. {
  228. struct device_node *dn;
  229. struct pci_dev *dev;
  230. dn = pci_bus_to_OF_node(bus);
  231. if (!dn)
  232. return;
  233. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
  234. list_for_each_entry (dev, &bus->devices, bus_list)
  235. dbg("\t%s\n", pci_name(dev));
  236. return;
  237. }
  238. int rpaphp_config_pci_adapter(struct pci_bus *bus)
  239. {
  240. struct device_node *dn = pci_bus_to_OF_node(bus);
  241. struct pci_dev *dev;
  242. int rc = -ENODEV;
  243. dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
  244. if (!dn)
  245. goto exit;
  246. enable_eeh(dn);
  247. dev = rpaphp_pci_config_slot(bus);
  248. if (!dev) {
  249. err("%s: can't find any devices.\n", __FUNCTION__);
  250. goto exit;
  251. }
  252. print_slot_pci_funcs(bus);
  253. rc = 0;
  254. exit:
  255. dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
  256. return rc;
  257. }
  258. EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
  259. static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
  260. {
  261. eeh_remove_device(dev);
  262. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  263. struct pci_bus *bus = dev->subordinate;
  264. struct list_head *ln;
  265. if (!bus)
  266. return;
  267. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  268. struct pci_dev *pdev = pci_dev_b(ln);
  269. if (pdev)
  270. rpaphp_eeh_remove_bus_device(pdev);
  271. }
  272. }
  273. return;
  274. }
  275. int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
  276. {
  277. struct pci_dev *dev, *tmp;
  278. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  279. rpaphp_eeh_remove_bus_device(dev);
  280. pci_remove_bus_device(dev);
  281. }
  282. return 0;
  283. }
  284. static int setup_pci_hotplug_slot_info(struct slot *slot)
  285. {
  286. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  287. __FUNCTION__);
  288. rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
  289. rpaphp_get_pci_adapter_status(slot, 1,
  290. &slot->hotplug_slot->info->
  291. adapter_status);
  292. if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
  293. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  294. __FUNCTION__, slot->dn->full_name);
  295. return -EINVAL;
  296. }
  297. return 0;
  298. }
  299. static void set_slot_name(struct slot *slot)
  300. {
  301. struct pci_bus *bus = slot->bus;
  302. struct pci_dev *bridge;
  303. bridge = bus->self;
  304. if (bridge)
  305. strcpy(slot->name, pci_name(bridge));
  306. else
  307. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  308. bus->number);
  309. }
  310. static int setup_pci_slot(struct slot *slot)
  311. {
  312. struct device_node *dn = slot->dn;
  313. struct pci_bus *bus;
  314. BUG_ON(!dn);
  315. bus = rpaphp_find_pci_bus(dn);
  316. if (!bus) {
  317. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  318. goto exit_rc;
  319. }
  320. slot->bus = bus;
  321. slot->pci_devs = &bus->devices;
  322. set_slot_name(slot);
  323. /* find slot's pci_dev if it's not empty */
  324. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  325. slot->state = EMPTY; /* slot is empty */
  326. } else {
  327. /* slot is occupied */
  328. if (!dn->child) {
  329. /* non-empty slot has to have child */
  330. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  331. __FUNCTION__, slot->name);
  332. goto exit_rc;
  333. }
  334. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  335. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  336. __FUNCTION__, slot->name);
  337. if (rpaphp_config_pci_adapter(slot->bus)) {
  338. err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
  339. goto exit_rc;
  340. }
  341. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  342. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  343. __FUNCTION__, slot->name);
  344. goto exit_rc;
  345. }
  346. print_slot_pci_funcs(slot->bus);
  347. if (!list_empty(slot->pci_devs)) {
  348. slot->state = CONFIGURED;
  349. } else {
  350. /* DLPAR add as opposed to
  351. * boot time */
  352. slot->state = NOT_CONFIGURED;
  353. }
  354. }
  355. return 0;
  356. exit_rc:
  357. dealloc_slot_struct(slot);
  358. return -EINVAL;
  359. }
  360. int register_pci_slot(struct slot *slot)
  361. {
  362. int rc = -EINVAL;
  363. if (setup_pci_hotplug_slot_info(slot))
  364. goto exit_rc;
  365. if (setup_pci_slot(slot))
  366. goto exit_rc;
  367. rc = register_slot(slot);
  368. exit_rc:
  369. return rc;
  370. }
  371. int rpaphp_enable_pci_slot(struct slot *slot)
  372. {
  373. int retval = 0, state;
  374. retval = rpaphp_get_sensor_state(slot, &state);
  375. if (retval)
  376. goto exit;
  377. dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
  378. /* if slot is not empty, enable the adapter */
  379. if (state == PRESENT) {
  380. dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
  381. retval = rpaphp_config_pci_adapter(slot->bus);
  382. if (!retval) {
  383. slot->state = CONFIGURED;
  384. dbg("%s: PCI devices in slot[%s] has been configured\n",
  385. __FUNCTION__, slot->name);
  386. } else {
  387. slot->state = NOT_CONFIGURED;
  388. dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
  389. __FUNCTION__, slot->name);
  390. }
  391. } else if (state == EMPTY) {
  392. dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
  393. slot->state = EMPTY;
  394. } else {
  395. err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
  396. slot->name);
  397. slot->state = NOT_VALID;
  398. retval = -EINVAL;
  399. }
  400. exit:
  401. dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
  402. return retval;
  403. }