rpaphp_pci.c 11 KB

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