rpaphp_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. void 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 void rpaphp_eeh_add_bus_device(struct pci_bus *bus)
  162. {
  163. struct pci_dev *dev;
  164. list_for_each_entry(dev, &bus->devices, bus_list) {
  165. eeh_add_device_late(dev);
  166. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  167. struct pci_bus *subbus = dev->subordinate;
  168. if (subbus)
  169. rpaphp_eeh_add_bus_device (subbus);
  170. }
  171. }
  172. }
  173. static int rpaphp_pci_config_bridge(struct pci_dev *dev)
  174. {
  175. u8 sec_busno;
  176. struct pci_bus *child_bus;
  177. struct pci_dev *child_dev;
  178. dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
  179. /* get busno of downstream bus */
  180. pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
  181. /* add to children of PCI bridge dev->bus */
  182. child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
  183. if (!child_bus) {
  184. err("%s: could not add second bus\n", __FUNCTION__);
  185. return -EIO;
  186. }
  187. sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
  188. /* do pci_scan_child_bus */
  189. pci_scan_child_bus(child_bus);
  190. list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
  191. eeh_add_device_late(child_dev);
  192. }
  193. /* fixup new pci devices without touching bus struct */
  194. rpaphp_fixup_new_pci_devices(child_bus, 0);
  195. /* Make the discovered devices available */
  196. pci_bus_add_devices(child_bus);
  197. return 0;
  198. }
  199. void rpaphp_init_new_devs(struct pci_bus *bus)
  200. {
  201. rpaphp_fixup_new_pci_devices(bus, 0);
  202. rpaphp_eeh_add_bus_device(bus);
  203. }
  204. EXPORT_SYMBOL_GPL(rpaphp_init_new_devs);
  205. /*****************************************************************************
  206. rpaphp_pci_config_slot() will configure all devices under the
  207. given slot->dn and return the the first pci_dev.
  208. *****************************************************************************/
  209. static struct pci_dev *
  210. rpaphp_pci_config_slot(struct pci_bus *bus)
  211. {
  212. struct device_node *dn = pci_bus_to_OF_node(bus);
  213. struct pci_dev *dev = NULL;
  214. int slotno;
  215. int num;
  216. dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
  217. if (!dn || !dn->child)
  218. return NULL;
  219. if (_machine == PLATFORM_PSERIES_LPAR) {
  220. of_scan_bus(dn, bus);
  221. if (list_empty(&bus->devices)) {
  222. err("%s: No new device found\n", __FUNCTION__);
  223. return NULL;
  224. }
  225. rpaphp_init_new_devs(bus);
  226. pci_bus_add_devices(bus);
  227. dev = list_entry(&bus->devices, struct pci_dev, bus_list);
  228. } else {
  229. slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
  230. /* pci_scan_slot should find all children */
  231. num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  232. if (num) {
  233. rpaphp_fixup_new_pci_devices(bus, 1);
  234. pci_bus_add_devices(bus);
  235. }
  236. if (list_empty(&bus->devices)) {
  237. err("%s: No new device found\n", __FUNCTION__);
  238. return NULL;
  239. }
  240. list_for_each_entry(dev, &bus->devices, bus_list) {
  241. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  242. rpaphp_pci_config_bridge(dev);
  243. rpaphp_eeh_add_bus_device(bus);
  244. }
  245. }
  246. return dev;
  247. }
  248. void rpaphp_eeh_init_nodes(struct device_node *dn)
  249. {
  250. struct device_node *sib;
  251. for (sib = dn->child; sib; sib = sib->sibling)
  252. rpaphp_eeh_init_nodes(sib);
  253. eeh_add_device_early(dn);
  254. return;
  255. }
  256. EXPORT_SYMBOL_GPL(rpaphp_eeh_init_nodes);
  257. static void print_slot_pci_funcs(struct pci_bus *bus)
  258. {
  259. struct device_node *dn;
  260. struct pci_dev *dev;
  261. dn = pci_bus_to_OF_node(bus);
  262. if (!dn)
  263. return;
  264. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
  265. list_for_each_entry (dev, &bus->devices, bus_list)
  266. dbg("\t%s\n", pci_name(dev));
  267. return;
  268. }
  269. int rpaphp_config_pci_adapter(struct pci_bus *bus)
  270. {
  271. struct device_node *dn = pci_bus_to_OF_node(bus);
  272. struct pci_dev *dev;
  273. int rc = -ENODEV;
  274. dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
  275. if (!dn)
  276. goto exit;
  277. rpaphp_eeh_init_nodes(dn);
  278. dev = rpaphp_pci_config_slot(bus);
  279. if (!dev) {
  280. err("%s: can't find any devices.\n", __FUNCTION__);
  281. goto exit;
  282. }
  283. print_slot_pci_funcs(bus);
  284. rc = 0;
  285. exit:
  286. dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
  287. return rc;
  288. }
  289. EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
  290. static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
  291. {
  292. eeh_remove_device(dev);
  293. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  294. struct pci_bus *bus = dev->subordinate;
  295. struct list_head *ln;
  296. if (!bus)
  297. return;
  298. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  299. struct pci_dev *pdev = pci_dev_b(ln);
  300. if (pdev)
  301. rpaphp_eeh_remove_bus_device(pdev);
  302. }
  303. }
  304. return;
  305. }
  306. int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
  307. {
  308. struct pci_dev *dev, *tmp;
  309. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  310. rpaphp_eeh_remove_bus_device(dev);
  311. pci_remove_bus_device(dev);
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(rpaphp_unconfig_pci_adapter);
  316. static int setup_pci_hotplug_slot_info(struct slot *slot)
  317. {
  318. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  319. __FUNCTION__);
  320. rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
  321. rpaphp_get_pci_adapter_status(slot, 1,
  322. &slot->hotplug_slot->info->
  323. adapter_status);
  324. if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
  325. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  326. __FUNCTION__, slot->dn->full_name);
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. static void set_slot_name(struct slot *slot)
  332. {
  333. struct pci_bus *bus = slot->bus;
  334. struct pci_dev *bridge;
  335. bridge = bus->self;
  336. if (bridge)
  337. strcpy(slot->name, pci_name(bridge));
  338. else
  339. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  340. bus->number);
  341. }
  342. static int setup_pci_slot(struct slot *slot)
  343. {
  344. struct device_node *dn = slot->dn;
  345. struct pci_bus *bus;
  346. BUG_ON(!dn);
  347. bus = rpaphp_find_pci_bus(dn);
  348. if (!bus) {
  349. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  350. goto exit_rc;
  351. }
  352. slot->bus = bus;
  353. slot->pci_devs = &bus->devices;
  354. set_slot_name(slot);
  355. /* find slot's pci_dev if it's not empty */
  356. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  357. slot->state = EMPTY; /* slot is empty */
  358. } else {
  359. /* slot is occupied */
  360. if (!dn->child) {
  361. /* non-empty slot has to have child */
  362. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  363. __FUNCTION__, slot->name);
  364. goto exit_rc;
  365. }
  366. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  367. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  368. __FUNCTION__, slot->name);
  369. if (rpaphp_config_pci_adapter(slot->bus)) {
  370. err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
  371. goto exit_rc;
  372. }
  373. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  374. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  375. __FUNCTION__, slot->name);
  376. goto exit_rc;
  377. }
  378. print_slot_pci_funcs(slot->bus);
  379. if (!list_empty(slot->pci_devs)) {
  380. slot->state = CONFIGURED;
  381. } else {
  382. /* DLPAR add as opposed to
  383. * boot time */
  384. slot->state = NOT_CONFIGURED;
  385. }
  386. }
  387. return 0;
  388. exit_rc:
  389. dealloc_slot_struct(slot);
  390. return -EINVAL;
  391. }
  392. int register_pci_slot(struct slot *slot)
  393. {
  394. int rc = -EINVAL;
  395. if (setup_pci_hotplug_slot_info(slot))
  396. goto exit_rc;
  397. if (setup_pci_slot(slot))
  398. goto exit_rc;
  399. rc = register_slot(slot);
  400. exit_rc:
  401. return rc;
  402. }
  403. int rpaphp_enable_pci_slot(struct slot *slot)
  404. {
  405. int retval = 0, state;
  406. retval = rpaphp_get_sensor_state(slot, &state);
  407. if (retval)
  408. goto exit;
  409. dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
  410. /* if slot is not empty, enable the adapter */
  411. if (state == PRESENT) {
  412. dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
  413. retval = rpaphp_config_pci_adapter(slot->bus);
  414. if (!retval) {
  415. slot->state = CONFIGURED;
  416. info("%s: devices in slot[%s] configured\n",
  417. __FUNCTION__, slot->name);
  418. } else {
  419. slot->state = NOT_CONFIGURED;
  420. dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
  421. __FUNCTION__, slot->name);
  422. }
  423. } else if (state == EMPTY) {
  424. dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
  425. slot->state = EMPTY;
  426. } else {
  427. err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
  428. slot->name);
  429. slot->state = NOT_VALID;
  430. retval = -EINVAL;
  431. }
  432. exit:
  433. dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
  434. return retval;
  435. }