rpaphp_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. if (!dn->phb || !dn->phb->bus)
  50. return NULL;
  51. return find_bus_among_children(dn->phb->bus, dn);
  52. }
  53. EXPORT_SYMBOL_GPL(rpaphp_find_pci_bus);
  54. int rpaphp_claim_resource(struct pci_dev *dev, int resource)
  55. {
  56. struct resource *res = &dev->resource[resource];
  57. struct resource *root = pci_find_parent_resource(dev, res);
  58. char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
  59. int err = -EINVAL;
  60. if (root != NULL) {
  61. err = request_resource(root, res);
  62. }
  63. if (err) {
  64. err("PCI: %s region %d of %s %s [%lx:%lx]\n",
  65. root ? "Address space collision on" :
  66. "No parent found for",
  67. resource, dtype, pci_name(dev), res->start, res->end);
  68. }
  69. return err;
  70. }
  71. EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
  72. static int rpaphp_get_sensor_state(struct slot *slot, int *state)
  73. {
  74. int rc;
  75. int setlevel;
  76. rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
  77. if (rc < 0) {
  78. if (rc == -EFAULT || rc == -EEXIST) {
  79. dbg("%s: slot must be power up to get sensor-state\n",
  80. __FUNCTION__);
  81. /* some slots have to be powered up
  82. * before get-sensor will succeed.
  83. */
  84. rc = rtas_set_power_level(slot->power_domain, POWER_ON,
  85. &setlevel);
  86. if (rc < 0) {
  87. dbg("%s: power on slot[%s] failed rc=%d.\n",
  88. __FUNCTION__, slot->name, rc);
  89. } else {
  90. rc = rtas_get_sensor(DR_ENTITY_SENSE,
  91. slot->index, state);
  92. }
  93. } else if (rc == -ENODEV)
  94. info("%s: slot is unusable\n", __FUNCTION__);
  95. else
  96. err("%s failed to get sensor state\n", __FUNCTION__);
  97. }
  98. return rc;
  99. }
  100. /**
  101. * get_pci_adapter_status - get the status of a slot
  102. *
  103. * 0-- slot is empty
  104. * 1-- adapter is configured
  105. * 2-- adapter is not configured
  106. * 3-- not valid
  107. */
  108. int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
  109. {
  110. struct pci_bus *bus;
  111. int state, rc;
  112. *value = NOT_VALID;
  113. rc = rpaphp_get_sensor_state(slot, &state);
  114. if (rc)
  115. goto exit;
  116. if (state == EMPTY)
  117. *value = EMPTY;
  118. else if (state == PRESENT) {
  119. if (!is_init) {
  120. /* at run-time slot->state can be changed by */
  121. /* config/unconfig adapter */
  122. *value = slot->state;
  123. } else {
  124. bus = rpaphp_find_pci_bus(slot->dn);
  125. if (bus && !list_empty(&bus->devices))
  126. *value = CONFIGURED;
  127. else
  128. *value = NOT_CONFIGURED;
  129. }
  130. }
  131. exit:
  132. return rc;
  133. }
  134. /* Must be called before pci_bus_add_devices */
  135. static void
  136. rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
  137. {
  138. struct pci_dev *dev;
  139. list_for_each_entry(dev, &bus->devices, bus_list) {
  140. /*
  141. * Skip already-present devices (which are on the
  142. * global device list.)
  143. */
  144. if (list_empty(&dev->global_list)) {
  145. int i;
  146. /* Need to setup IOMMU tables */
  147. ppc_md.iommu_dev_setup(dev);
  148. if(fix_bus)
  149. pcibios_fixup_device_resources(dev, bus);
  150. pci_read_irq_line(dev);
  151. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  152. struct resource *r = &dev->resource[i];
  153. if (r->parent || !r->start || !r->flags)
  154. continue;
  155. rpaphp_claim_resource(dev, i);
  156. }
  157. }
  158. }
  159. }
  160. static int rpaphp_pci_config_bridge(struct pci_dev *dev)
  161. {
  162. u8 sec_busno;
  163. struct pci_bus *child_bus;
  164. struct pci_dev *child_dev;
  165. dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
  166. /* get busno of downstream bus */
  167. pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
  168. /* add to children of PCI bridge dev->bus */
  169. child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
  170. if (!child_bus) {
  171. err("%s: could not add second bus\n", __FUNCTION__);
  172. return -EIO;
  173. }
  174. sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
  175. /* do pci_scan_child_bus */
  176. pci_scan_child_bus(child_bus);
  177. list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
  178. eeh_add_device_late(child_dev);
  179. }
  180. /* fixup new pci devices without touching bus struct */
  181. rpaphp_fixup_new_pci_devices(child_bus, 0);
  182. /* Make the discovered devices available */
  183. pci_bus_add_devices(child_bus);
  184. return 0;
  185. }
  186. /*****************************************************************************
  187. rpaphp_pci_config_slot() will configure all devices under the
  188. given slot->dn and return the the first pci_dev.
  189. *****************************************************************************/
  190. static struct pci_dev *
  191. rpaphp_pci_config_slot(struct pci_bus *bus)
  192. {
  193. struct device_node *dn = pci_bus_to_OF_node(bus);
  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 || !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 pci_bus *bus)
  226. {
  227. struct device_node *dn;
  228. struct pci_dev *dev;
  229. dn = pci_bus_to_OF_node(bus);
  230. if (!dn)
  231. return;
  232. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
  233. list_for_each_entry (dev, &bus->devices, bus_list)
  234. dbg("\t%s\n", pci_name(dev));
  235. return;
  236. }
  237. int rpaphp_config_pci_adapter(struct pci_bus *bus)
  238. {
  239. struct device_node *dn = pci_bus_to_OF_node(bus);
  240. struct pci_dev *dev;
  241. int rc = -ENODEV;
  242. dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
  243. if (!dn)
  244. goto exit;
  245. enable_eeh(dn);
  246. dev = rpaphp_pci_config_slot(bus);
  247. if (!dev) {
  248. err("%s: can't find any devices.\n", __FUNCTION__);
  249. goto exit;
  250. }
  251. print_slot_pci_funcs(bus);
  252. rc = 0;
  253. exit:
  254. dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
  255. return rc;
  256. }
  257. EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
  258. static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
  259. {
  260. eeh_remove_device(dev);
  261. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  262. struct pci_bus *bus = dev->subordinate;
  263. struct list_head *ln;
  264. if (!bus)
  265. return;
  266. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  267. struct pci_dev *pdev = pci_dev_b(ln);
  268. if (pdev)
  269. rpaphp_eeh_remove_bus_device(pdev);
  270. }
  271. }
  272. return;
  273. }
  274. int rpaphp_unconfig_pci_adapter(struct slot *slot)
  275. {
  276. struct pci_dev *dev, *tmp;
  277. int retval = 0;
  278. list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
  279. rpaphp_eeh_remove_bus_device(dev);
  280. pci_remove_bus_device(dev);
  281. }
  282. slot->state = NOT_CONFIGURED;
  283. info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
  284. slot->name);
  285. return retval;
  286. }
  287. static int setup_pci_hotplug_slot_info(struct slot *slot)
  288. {
  289. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  290. __FUNCTION__);
  291. rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
  292. rpaphp_get_pci_adapter_status(slot, 1,
  293. &slot->hotplug_slot->info->
  294. adapter_status);
  295. if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
  296. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  297. __FUNCTION__, slot->dn->full_name);
  298. return -EINVAL;
  299. }
  300. return 0;
  301. }
  302. static void set_slot_name(struct slot *slot)
  303. {
  304. struct pci_bus *bus = slot->bus;
  305. struct pci_dev *bridge;
  306. bridge = bus->self;
  307. if (bridge)
  308. strcpy(slot->name, pci_name(bridge));
  309. else
  310. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  311. bus->number);
  312. }
  313. static int setup_pci_slot(struct slot *slot)
  314. {
  315. struct device_node *dn = slot->dn;
  316. struct pci_bus *bus;
  317. BUG_ON(!dn);
  318. bus = rpaphp_find_pci_bus(dn);
  319. if (!bus) {
  320. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  321. goto exit_rc;
  322. }
  323. slot->bus = bus;
  324. slot->pci_devs = &bus->devices;
  325. set_slot_name(slot);
  326. /* find slot's pci_dev if it's not empty */
  327. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  328. slot->state = EMPTY; /* slot is empty */
  329. } else {
  330. /* slot is occupied */
  331. if (!dn->child) {
  332. /* non-empty slot has to have child */
  333. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  334. __FUNCTION__, slot->name);
  335. goto exit_rc;
  336. }
  337. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  338. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  339. __FUNCTION__, slot->name);
  340. if (rpaphp_config_pci_adapter(slot->bus)) {
  341. err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
  342. goto exit_rc;
  343. }
  344. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  345. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  346. __FUNCTION__, slot->name);
  347. goto exit_rc;
  348. }
  349. print_slot_pci_funcs(slot->bus);
  350. if (!list_empty(slot->pci_devs)) {
  351. slot->state = CONFIGURED;
  352. } else {
  353. /* DLPAR add as opposed to
  354. * boot time */
  355. slot->state = NOT_CONFIGURED;
  356. }
  357. }
  358. return 0;
  359. exit_rc:
  360. dealloc_slot_struct(slot);
  361. return -EINVAL;
  362. }
  363. int register_pci_slot(struct slot *slot)
  364. {
  365. int rc = -EINVAL;
  366. if (setup_pci_hotplug_slot_info(slot))
  367. goto exit_rc;
  368. if (setup_pci_slot(slot))
  369. goto exit_rc;
  370. rc = register_slot(slot);
  371. exit_rc:
  372. return rc;
  373. }
  374. int rpaphp_enable_pci_slot(struct slot *slot)
  375. {
  376. int retval = 0, state;
  377. retval = rpaphp_get_sensor_state(slot, &state);
  378. if (retval)
  379. goto exit;
  380. dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
  381. /* if slot is not empty, enable the adapter */
  382. if (state == PRESENT) {
  383. dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
  384. retval = rpaphp_config_pci_adapter(slot->bus);
  385. if (!retval) {
  386. slot->state = CONFIGURED;
  387. dbg("%s: PCI devices in slot[%s] has been configured\n",
  388. __FUNCTION__, slot->name);
  389. } else {
  390. slot->state = NOT_CONFIGURED;
  391. dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
  392. __FUNCTION__, slot->name);
  393. }
  394. } else if (state == EMPTY) {
  395. dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
  396. slot->state = EMPTY;
  397. } else {
  398. err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
  399. slot->name);
  400. slot->state = NOT_VALID;
  401. retval = -EINVAL;
  402. }
  403. exit:
  404. dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
  405. return retval;
  406. }