0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/pci.h>
0011 #include <linux/string.h>
0012 #include <linux/export.h>
0013 #include <linux/init.h>
0014 #include <linux/gfp.h>
0015 #include <linux/of.h>
0016
0017 #include <asm/io.h>
0018 #include <asm/pci-bridge.h>
0019 #include <asm/ppc-pci.h>
0020 #include <asm/firmware.h>
0021 #include <asm/eeh.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030 static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
0031 {
0032 struct pci_bus *pbus;
0033 struct device_node *dn;
0034 struct pci_dn *pdn;
0035
0036
0037
0038
0039
0040 pbus = bus;
0041 while (pbus) {
0042 if (pci_is_root_bus(pbus) || pbus->self)
0043 break;
0044
0045 pbus = pbus->parent;
0046 }
0047
0048
0049
0050
0051
0052 dn = pci_bus_to_OF_node(pbus);
0053 pdn = dn ? PCI_DN(dn) : NULL;
0054
0055 return pdn;
0056 }
0057
0058 struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
0059 int devfn)
0060 {
0061 struct device_node *dn = NULL;
0062 struct pci_dn *parent, *pdn;
0063 struct pci_dev *pdev = NULL;
0064
0065
0066 list_for_each_entry(pdev, &bus->devices, bus_list) {
0067 if (pdev->devfn == devfn) {
0068 if (pdev->dev.archdata.pci_data)
0069 return pdev->dev.archdata.pci_data;
0070
0071 dn = pci_device_to_OF_node(pdev);
0072 break;
0073 }
0074 }
0075
0076
0077 pdn = dn ? PCI_DN(dn) : NULL;
0078 if (pdn)
0079 return pdn;
0080
0081
0082 parent = pci_bus_to_pdn(bus);
0083 if (!parent)
0084 return NULL;
0085
0086 list_for_each_entry(pdn, &parent->child_list, list) {
0087 if (pdn->busno == bus->number &&
0088 pdn->devfn == devfn)
0089 return pdn;
0090 }
0091
0092 return NULL;
0093 }
0094
0095 struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
0096 {
0097 struct device_node *dn;
0098 struct pci_dn *parent, *pdn;
0099
0100
0101 if (pdev->dev.archdata.pci_data)
0102 return pdev->dev.archdata.pci_data;
0103
0104
0105 dn = pci_device_to_OF_node(pdev);
0106 pdn = dn ? PCI_DN(dn) : NULL;
0107 if (pdn)
0108 return pdn;
0109
0110
0111
0112
0113
0114 parent = pci_bus_to_pdn(pdev->bus);
0115 if (!parent)
0116 return NULL;
0117
0118 list_for_each_entry(pdn, &parent->child_list, list) {
0119 if (pdn->busno == pdev->bus->number &&
0120 pdn->devfn == pdev->devfn)
0121 return pdn;
0122 }
0123
0124 return NULL;
0125 }
0126
0127 #ifdef CONFIG_EEH
0128 static struct eeh_dev *eeh_dev_init(struct pci_dn *pdn)
0129 {
0130 struct eeh_dev *edev;
0131
0132
0133 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
0134 if (!edev)
0135 return NULL;
0136
0137
0138 pdn->edev = edev;
0139 edev->pdn = pdn;
0140 edev->bdfn = (pdn->busno << 8) | pdn->devfn;
0141 edev->controller = pdn->phb;
0142
0143 return edev;
0144 }
0145 #endif
0146
0147 #ifdef CONFIG_PCI_IOV
0148 static struct pci_dn *add_one_sriov_vf_pdn(struct pci_dn *parent,
0149 int busno, int devfn)
0150 {
0151 struct pci_dn *pdn;
0152
0153
0154 if (!parent)
0155 return NULL;
0156
0157 pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
0158 if (!pdn)
0159 return NULL;
0160
0161 pdn->phb = parent->phb;
0162 pdn->parent = parent;
0163 pdn->busno = busno;
0164 pdn->devfn = devfn;
0165 pdn->pe_number = IODA_INVALID_PE;
0166 INIT_LIST_HEAD(&pdn->child_list);
0167 INIT_LIST_HEAD(&pdn->list);
0168 list_add_tail(&pdn->list, &parent->child_list);
0169
0170 return pdn;
0171 }
0172
0173 struct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev)
0174 {
0175 struct pci_dn *parent, *pdn;
0176 int i;
0177
0178
0179 if (WARN_ON(!pdev->is_physfn))
0180 return NULL;
0181
0182
0183 pdn = pci_get_pdn(pdev);
0184 if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
0185 return NULL;
0186
0187 pdn->flags |= PCI_DN_FLAG_IOV_VF;
0188 parent = pci_bus_to_pdn(pdev->bus);
0189 if (!parent)
0190 return NULL;
0191
0192 for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
0193 struct eeh_dev *edev __maybe_unused;
0194
0195 pdn = add_one_sriov_vf_pdn(parent,
0196 pci_iov_virtfn_bus(pdev, i),
0197 pci_iov_virtfn_devfn(pdev, i));
0198 if (!pdn) {
0199 dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
0200 __func__, i);
0201 return NULL;
0202 }
0203
0204 #ifdef CONFIG_EEH
0205
0206 edev = eeh_dev_init(pdn);
0207 BUG_ON(!edev);
0208
0209
0210 edev->physfn = pdev;
0211 edev->vf_index = i;
0212 #endif
0213 }
0214 return pci_get_pdn(pdev);
0215 }
0216
0217 void remove_sriov_vf_pdns(struct pci_dev *pdev)
0218 {
0219 struct pci_dn *parent;
0220 struct pci_dn *pdn, *tmp;
0221 int i;
0222
0223
0224 if (WARN_ON(!pdev->is_physfn))
0225 return;
0226
0227
0228 pdn = pci_get_pdn(pdev);
0229 if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
0230 return;
0231
0232 pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
0233 parent = pci_bus_to_pdn(pdev->bus);
0234 if (!parent)
0235 return;
0236
0237
0238
0239
0240
0241
0242 for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
0243 struct eeh_dev *edev __maybe_unused;
0244
0245 list_for_each_entry_safe(pdn, tmp,
0246 &parent->child_list, list) {
0247 if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
0248 pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
0249 continue;
0250
0251 #ifdef CONFIG_EEH
0252
0253
0254
0255
0256
0257
0258 edev = pdn_to_eeh_dev(pdn);
0259 if (edev) {
0260
0261
0262
0263
0264
0265 if (edev->pe)
0266 eeh_pe_tree_remove(edev);
0267
0268 pdn->edev = NULL;
0269 kfree(edev);
0270 }
0271 #endif
0272
0273 if (!list_empty(&pdn->list))
0274 list_del(&pdn->list);
0275
0276 kfree(pdn);
0277 }
0278 }
0279 }
0280 #endif
0281
0282 struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
0283 struct device_node *dn)
0284 {
0285 const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
0286 const __be32 *regs;
0287 struct device_node *parent;
0288 struct pci_dn *pdn;
0289 #ifdef CONFIG_EEH
0290 struct eeh_dev *edev;
0291 #endif
0292
0293 pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
0294 if (pdn == NULL)
0295 return NULL;
0296 dn->data = pdn;
0297 pdn->phb = hose;
0298 pdn->pe_number = IODA_INVALID_PE;
0299 regs = of_get_property(dn, "reg", NULL);
0300 if (regs) {
0301 u32 addr = of_read_number(regs, 1);
0302
0303
0304 pdn->busno = (addr >> 16) & 0xff;
0305 pdn->devfn = (addr >> 8) & 0xff;
0306 }
0307
0308
0309 regs = of_get_property(dn, "vendor-id", NULL);
0310 pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
0311 regs = of_get_property(dn, "device-id", NULL);
0312 pdn->device_id = regs ? of_read_number(regs, 1) : 0;
0313 regs = of_get_property(dn, "class-code", NULL);
0314 pdn->class_code = regs ? of_read_number(regs, 1) : 0;
0315
0316
0317 pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
0318
0319
0320 #ifdef CONFIG_EEH
0321 edev = eeh_dev_init(pdn);
0322 if (!edev) {
0323 kfree(pdn);
0324 return NULL;
0325 }
0326 #endif
0327
0328
0329 INIT_LIST_HEAD(&pdn->child_list);
0330 INIT_LIST_HEAD(&pdn->list);
0331 parent = of_get_parent(dn);
0332 pdn->parent = parent ? PCI_DN(parent) : NULL;
0333 if (pdn->parent)
0334 list_add_tail(&pdn->list, &pdn->parent->child_list);
0335
0336 return pdn;
0337 }
0338 EXPORT_SYMBOL_GPL(pci_add_device_node_info);
0339
0340 void pci_remove_device_node_info(struct device_node *dn)
0341 {
0342 struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
0343 struct device_node *parent;
0344 struct pci_dev *pdev;
0345 #ifdef CONFIG_EEH
0346 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
0347
0348 if (edev)
0349 edev->pdn = NULL;
0350 #endif
0351
0352 if (!pdn)
0353 return;
0354
0355 WARN_ON(!list_empty(&pdn->child_list));
0356 list_del(&pdn->list);
0357
0358
0359 parent = of_get_parent(dn);
0360 if (parent)
0361 of_node_put(parent);
0362
0363
0364
0365
0366
0367
0368 pdev = pci_get_domain_bus_and_slot(pdn->phb->global_number,
0369 pdn->busno, pdn->devfn);
0370 if (pdev) {
0371
0372 pci_dbg(pdev, "marked pdn (from %pOF) as dead\n", dn);
0373 pdn->flags |= PCI_DN_FLAG_DEAD;
0374 } else {
0375 dn->data = NULL;
0376 kfree(pdn);
0377 }
0378
0379 pci_dev_put(pdev);
0380 }
0381 EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401 void *pci_traverse_device_nodes(struct device_node *start,
0402 void *(*fn)(struct device_node *, void *),
0403 void *data)
0404 {
0405 struct device_node *dn, *nextdn;
0406 void *ret;
0407
0408
0409 for (dn = start->child; dn; dn = nextdn) {
0410 const __be32 *classp;
0411 u32 class = 0;
0412
0413 nextdn = NULL;
0414 classp = of_get_property(dn, "class-code", NULL);
0415 if (classp)
0416 class = of_read_number(classp, 1);
0417
0418 if (fn) {
0419 ret = fn(dn, data);
0420 if (ret)
0421 return ret;
0422 }
0423
0424
0425 if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
0426 (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
0427
0428 nextdn = dn->child;
0429 else if (dn->sibling)
0430
0431 nextdn = dn->sibling;
0432 if (!nextdn) {
0433
0434 do {
0435 dn = dn->parent;
0436 if (dn == start)
0437 return NULL;
0438 } while (dn->sibling == NULL);
0439 nextdn = dn->sibling;
0440 }
0441 }
0442 return NULL;
0443 }
0444 EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
0445
0446 static void *add_pdn(struct device_node *dn, void *data)
0447 {
0448 struct pci_controller *hose = data;
0449 struct pci_dn *pdn;
0450
0451 pdn = pci_add_device_node_info(hose, dn);
0452 if (!pdn)
0453 return ERR_PTR(-ENOMEM);
0454
0455 return NULL;
0456 }
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466 void pci_devs_phb_init_dynamic(struct pci_controller *phb)
0467 {
0468 struct device_node *dn = phb->dn;
0469 struct pci_dn *pdn;
0470
0471
0472 pdn = pci_add_device_node_info(phb, dn);
0473 if (pdn) {
0474 pdn->devfn = pdn->busno = -1;
0475 pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
0476 pdn->phb = phb;
0477 phb->pci_data = pdn;
0478 }
0479
0480
0481 pci_traverse_device_nodes(dn, add_pdn, phb);
0482 }
0483
0484 static void pci_dev_pdn_setup(struct pci_dev *pdev)
0485 {
0486 struct pci_dn *pdn;
0487
0488 if (pdev->dev.archdata.pci_data)
0489 return;
0490
0491
0492 pdn = pci_get_pdn(pdev);
0493 pdev->dev.archdata.pci_data = pdn;
0494 }
0495 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);