rpaphp_pci.c 13 KB

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