rpaphp_pci.c 12 KB

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