rpaphp_pci.c 12 KB

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