rpaphp_pci.c 12 KB

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