pci_dn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * pci_dn.c
  3. *
  4. * Copyright (C) 2001 Todd Inglett, IBM Corporation
  5. *
  6. * PCI manipulation via device_nodes.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/string.h>
  25. #include <linux/export.h>
  26. #include <linux/init.h>
  27. #include <linux/gfp.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/ppc-pci.h>
  32. #include <asm/firmware.h>
  33. #include <asm/eeh.h>
  34. /*
  35. * The function is used to find the firmware data of one
  36. * specific PCI device, which is attached to the indicated
  37. * PCI bus. For VFs, their firmware data is linked to that
  38. * one of PF's bridge. For other devices, their firmware
  39. * data is linked to that of their bridge.
  40. */
  41. static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
  42. {
  43. struct pci_bus *pbus;
  44. struct device_node *dn;
  45. struct pci_dn *pdn;
  46. /*
  47. * We probably have virtual bus which doesn't
  48. * have associated bridge.
  49. */
  50. pbus = bus;
  51. while (pbus) {
  52. if (pci_is_root_bus(pbus) || pbus->self)
  53. break;
  54. pbus = pbus->parent;
  55. }
  56. /*
  57. * Except virtual bus, all PCI buses should
  58. * have device nodes.
  59. */
  60. dn = pci_bus_to_OF_node(pbus);
  61. pdn = dn ? PCI_DN(dn) : NULL;
  62. return pdn;
  63. }
  64. struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
  65. int devfn)
  66. {
  67. struct device_node *dn = NULL;
  68. struct pci_dn *parent, *pdn;
  69. struct pci_dev *pdev = NULL;
  70. /* Fast path: fetch from PCI device */
  71. list_for_each_entry(pdev, &bus->devices, bus_list) {
  72. if (pdev->devfn == devfn) {
  73. if (pdev->dev.archdata.pci_data)
  74. return pdev->dev.archdata.pci_data;
  75. dn = pci_device_to_OF_node(pdev);
  76. break;
  77. }
  78. }
  79. /* Fast path: fetch from device node */
  80. pdn = dn ? PCI_DN(dn) : NULL;
  81. if (pdn)
  82. return pdn;
  83. /* Slow path: fetch from firmware data hierarchy */
  84. parent = pci_bus_to_pdn(bus);
  85. if (!parent)
  86. return NULL;
  87. list_for_each_entry(pdn, &parent->child_list, list) {
  88. if (pdn->busno == bus->number &&
  89. pdn->devfn == devfn)
  90. return pdn;
  91. }
  92. return NULL;
  93. }
  94. struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
  95. {
  96. struct device_node *dn;
  97. struct pci_dn *parent, *pdn;
  98. /* Search device directly */
  99. if (pdev->dev.archdata.pci_data)
  100. return pdev->dev.archdata.pci_data;
  101. /* Check device node */
  102. dn = pci_device_to_OF_node(pdev);
  103. pdn = dn ? PCI_DN(dn) : NULL;
  104. if (pdn)
  105. return pdn;
  106. /*
  107. * VFs don't have device nodes. We hook their
  108. * firmware data to PF's bridge.
  109. */
  110. parent = pci_bus_to_pdn(pdev->bus);
  111. if (!parent)
  112. return NULL;
  113. list_for_each_entry(pdn, &parent->child_list, list) {
  114. if (pdn->busno == pdev->bus->number &&
  115. pdn->devfn == pdev->devfn)
  116. return pdn;
  117. }
  118. return NULL;
  119. }
  120. #ifdef CONFIG_PCI_IOV
  121. static struct pci_dn *add_one_dev_pci_data(struct pci_dn *parent,
  122. int vf_index,
  123. int busno, int devfn)
  124. {
  125. struct pci_dn *pdn;
  126. /* Except PHB, we always have the parent */
  127. if (!parent)
  128. return NULL;
  129. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  130. if (!pdn)
  131. return NULL;
  132. pdn->phb = parent->phb;
  133. pdn->parent = parent;
  134. pdn->busno = busno;
  135. pdn->devfn = devfn;
  136. pdn->vf_index = vf_index;
  137. pdn->pe_number = IODA_INVALID_PE;
  138. INIT_LIST_HEAD(&pdn->child_list);
  139. INIT_LIST_HEAD(&pdn->list);
  140. list_add_tail(&pdn->list, &parent->child_list);
  141. return pdn;
  142. }
  143. #endif
  144. struct pci_dn *add_dev_pci_data(struct pci_dev *pdev)
  145. {
  146. #ifdef CONFIG_PCI_IOV
  147. struct pci_dn *parent, *pdn;
  148. int i;
  149. /* Only support IOV for now */
  150. if (!pdev->is_physfn)
  151. return pci_get_pdn(pdev);
  152. /* Check if VFs have been populated */
  153. pdn = pci_get_pdn(pdev);
  154. if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
  155. return NULL;
  156. pdn->flags |= PCI_DN_FLAG_IOV_VF;
  157. parent = pci_bus_to_pdn(pdev->bus);
  158. if (!parent)
  159. return NULL;
  160. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  161. struct eeh_dev *edev __maybe_unused;
  162. pdn = add_one_dev_pci_data(parent, i,
  163. pci_iov_virtfn_bus(pdev, i),
  164. pci_iov_virtfn_devfn(pdev, i));
  165. if (!pdn) {
  166. dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
  167. __func__, i);
  168. return NULL;
  169. }
  170. #ifdef CONFIG_EEH
  171. /* Create the EEH device for the VF */
  172. edev = eeh_dev_init(pdn);
  173. BUG_ON(!edev);
  174. edev->physfn = pdev;
  175. #endif /* CONFIG_EEH */
  176. }
  177. #endif /* CONFIG_PCI_IOV */
  178. return pci_get_pdn(pdev);
  179. }
  180. void remove_dev_pci_data(struct pci_dev *pdev)
  181. {
  182. #ifdef CONFIG_PCI_IOV
  183. struct pci_dn *parent;
  184. struct pci_dn *pdn, *tmp;
  185. int i;
  186. /*
  187. * VF and VF PE are created/released dynamically, so we need to
  188. * bind/unbind them. Otherwise the VF and VF PE would be mismatched
  189. * when re-enabling SR-IOV.
  190. */
  191. if (pdev->is_virtfn) {
  192. pdn = pci_get_pdn(pdev);
  193. pdn->pe_number = IODA_INVALID_PE;
  194. return;
  195. }
  196. /* Only support IOV PF for now */
  197. if (!pdev->is_physfn)
  198. return;
  199. /* Check if VFs have been populated */
  200. pdn = pci_get_pdn(pdev);
  201. if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
  202. return;
  203. pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
  204. parent = pci_bus_to_pdn(pdev->bus);
  205. if (!parent)
  206. return;
  207. /*
  208. * We might introduce flag to pci_dn in future
  209. * so that we can release VF's firmware data in
  210. * a batch mode.
  211. */
  212. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  213. struct eeh_dev *edev __maybe_unused;
  214. list_for_each_entry_safe(pdn, tmp,
  215. &parent->child_list, list) {
  216. if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
  217. pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
  218. continue;
  219. #ifdef CONFIG_EEH
  220. /* Release EEH device for the VF */
  221. edev = pdn_to_eeh_dev(pdn);
  222. if (edev) {
  223. pdn->edev = NULL;
  224. kfree(edev);
  225. }
  226. #endif /* CONFIG_EEH */
  227. if (!list_empty(&pdn->list))
  228. list_del(&pdn->list);
  229. kfree(pdn);
  230. }
  231. }
  232. #endif /* CONFIG_PCI_IOV */
  233. }
  234. struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
  235. struct device_node *dn)
  236. {
  237. const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
  238. const __be32 *regs;
  239. struct device_node *parent;
  240. struct pci_dn *pdn;
  241. #ifdef CONFIG_EEH
  242. struct eeh_dev *edev;
  243. #endif
  244. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  245. if (pdn == NULL)
  246. return NULL;
  247. dn->data = pdn;
  248. pdn->phb = hose;
  249. pdn->pe_number = IODA_INVALID_PE;
  250. regs = of_get_property(dn, "reg", NULL);
  251. if (regs) {
  252. u32 addr = of_read_number(regs, 1);
  253. /* First register entry is addr (00BBSS00) */
  254. pdn->busno = (addr >> 16) & 0xff;
  255. pdn->devfn = (addr >> 8) & 0xff;
  256. }
  257. /* vendor/device IDs and class code */
  258. regs = of_get_property(dn, "vendor-id", NULL);
  259. pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
  260. regs = of_get_property(dn, "device-id", NULL);
  261. pdn->device_id = regs ? of_read_number(regs, 1) : 0;
  262. regs = of_get_property(dn, "class-code", NULL);
  263. pdn->class_code = regs ? of_read_number(regs, 1) : 0;
  264. /* Extended config space */
  265. pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
  266. /* Create EEH device */
  267. #ifdef CONFIG_EEH
  268. edev = eeh_dev_init(pdn);
  269. if (!edev) {
  270. kfree(pdn);
  271. return NULL;
  272. }
  273. #endif
  274. /* Attach to parent node */
  275. INIT_LIST_HEAD(&pdn->child_list);
  276. INIT_LIST_HEAD(&pdn->list);
  277. parent = of_get_parent(dn);
  278. pdn->parent = parent ? PCI_DN(parent) : NULL;
  279. if (pdn->parent)
  280. list_add_tail(&pdn->list, &pdn->parent->child_list);
  281. return pdn;
  282. }
  283. EXPORT_SYMBOL_GPL(pci_add_device_node_info);
  284. void pci_remove_device_node_info(struct device_node *dn)
  285. {
  286. struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
  287. struct device_node *parent;
  288. #ifdef CONFIG_EEH
  289. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  290. if (edev)
  291. edev->pdn = NULL;
  292. #endif
  293. if (!pdn)
  294. return;
  295. WARN_ON(!list_empty(&pdn->child_list));
  296. list_del(&pdn->list);
  297. parent = of_get_parent(dn);
  298. if (parent)
  299. of_node_put(parent);
  300. dn->data = NULL;
  301. kfree(pdn);
  302. }
  303. EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
  304. /*
  305. * Traverse a device tree stopping each PCI device in the tree.
  306. * This is done depth first. As each node is processed, a "pre"
  307. * function is called and the children are processed recursively.
  308. *
  309. * The "pre" func returns a value. If non-zero is returned from
  310. * the "pre" func, the traversal stops and this value is returned.
  311. * This return value is useful when using traverse as a method of
  312. * finding a device.
  313. *
  314. * NOTE: we do not run the func for devices that do not appear to
  315. * be PCI except for the start node which we assume (this is good
  316. * because the start node is often a phb which may be missing PCI
  317. * properties).
  318. * We use the class-code as an indicator. If we run into
  319. * one of these nodes we also assume its siblings are non-pci for
  320. * performance.
  321. */
  322. void *pci_traverse_device_nodes(struct device_node *start,
  323. void *(*fn)(struct device_node *, void *),
  324. void *data)
  325. {
  326. struct device_node *dn, *nextdn;
  327. void *ret;
  328. /* We started with a phb, iterate all childs */
  329. for (dn = start->child; dn; dn = nextdn) {
  330. const __be32 *classp;
  331. u32 class = 0;
  332. nextdn = NULL;
  333. classp = of_get_property(dn, "class-code", NULL);
  334. if (classp)
  335. class = of_read_number(classp, 1);
  336. if (fn) {
  337. ret = fn(dn, data);
  338. if (ret)
  339. return ret;
  340. }
  341. /* If we are a PCI bridge, go down */
  342. if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
  343. (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
  344. /* Depth first...do children */
  345. nextdn = dn->child;
  346. else if (dn->sibling)
  347. /* ok, try next sibling instead. */
  348. nextdn = dn->sibling;
  349. if (!nextdn) {
  350. /* Walk up to next valid sibling. */
  351. do {
  352. dn = dn->parent;
  353. if (dn == start)
  354. return NULL;
  355. } while (dn->sibling == NULL);
  356. nextdn = dn->sibling;
  357. }
  358. }
  359. return NULL;
  360. }
  361. EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
  362. static struct pci_dn *pci_dn_next_one(struct pci_dn *root,
  363. struct pci_dn *pdn)
  364. {
  365. struct list_head *next = pdn->child_list.next;
  366. if (next != &pdn->child_list)
  367. return list_entry(next, struct pci_dn, list);
  368. while (1) {
  369. if (pdn == root)
  370. return NULL;
  371. next = pdn->list.next;
  372. if (next != &pdn->parent->child_list)
  373. break;
  374. pdn = pdn->parent;
  375. }
  376. return list_entry(next, struct pci_dn, list);
  377. }
  378. void *traverse_pci_dn(struct pci_dn *root,
  379. void *(*fn)(struct pci_dn *, void *),
  380. void *data)
  381. {
  382. struct pci_dn *pdn = root;
  383. void *ret;
  384. /* Only scan the child nodes */
  385. for (pdn = pci_dn_next_one(root, pdn); pdn;
  386. pdn = pci_dn_next_one(root, pdn)) {
  387. ret = fn(pdn, data);
  388. if (ret)
  389. return ret;
  390. }
  391. return NULL;
  392. }
  393. static void *add_pdn(struct device_node *dn, void *data)
  394. {
  395. struct pci_controller *hose = data;
  396. struct pci_dn *pdn;
  397. pdn = pci_add_device_node_info(hose, dn);
  398. if (!pdn)
  399. return ERR_PTR(-ENOMEM);
  400. return NULL;
  401. }
  402. /**
  403. * pci_devs_phb_init_dynamic - setup pci devices under this PHB
  404. * phb: pci-to-host bridge (top-level bridge connecting to cpu)
  405. *
  406. * This routine is called both during boot, (before the memory
  407. * subsystem is set up, before kmalloc is valid) and during the
  408. * dynamic lpar operation of adding a PHB to a running system.
  409. */
  410. void pci_devs_phb_init_dynamic(struct pci_controller *phb)
  411. {
  412. struct device_node *dn = phb->dn;
  413. struct pci_dn *pdn;
  414. /* PHB nodes themselves must not match */
  415. pdn = pci_add_device_node_info(phb, dn);
  416. if (pdn) {
  417. pdn->devfn = pdn->busno = -1;
  418. pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
  419. pdn->phb = phb;
  420. phb->pci_data = pdn;
  421. }
  422. /* Update dn->phb ptrs for new phb and children devices */
  423. pci_traverse_device_nodes(dn, add_pdn, phb);
  424. }
  425. /**
  426. * pci_devs_phb_init - Initialize phbs and pci devs under them.
  427. *
  428. * This routine walks over all phb's (pci-host bridges) on the
  429. * system, and sets up assorted pci-related structures
  430. * (including pci info in the device node structs) for each
  431. * pci device found underneath. This routine runs once,
  432. * early in the boot sequence.
  433. */
  434. static int __init pci_devs_phb_init(void)
  435. {
  436. struct pci_controller *phb, *tmp;
  437. /* This must be done first so the device nodes have valid pci info! */
  438. list_for_each_entry_safe(phb, tmp, &hose_list, list_node)
  439. pci_devs_phb_init_dynamic(phb);
  440. return 0;
  441. }
  442. core_initcall(pci_devs_phb_init);
  443. static void pci_dev_pdn_setup(struct pci_dev *pdev)
  444. {
  445. struct pci_dn *pdn;
  446. if (pdev->dev.archdata.pci_data)
  447. return;
  448. /* Setup the fast path */
  449. pdn = pci_get_pdn(pdev);
  450. pdev->dev.archdata.pci_data = pdn;
  451. }
  452. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);