1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * RPA Virtual I/O device functions
4*4882a593Smuzhiyun * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Send feedback to <lxie@us.ibm.com>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/sysfs.h>
14*4882a593Smuzhiyun #include <linux/pci.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <asm/rtas.h>
19*4882a593Smuzhiyun #include "rpaphp.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /* free up the memory used by a slot */
dealloc_slot_struct(struct slot * slot)22*4882a593Smuzhiyun void dealloc_slot_struct(struct slot *slot)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun of_node_put(slot->dn);
25*4882a593Smuzhiyun kfree(slot->name);
26*4882a593Smuzhiyun kfree(slot);
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun
alloc_slot_struct(struct device_node * dn,int drc_index,char * drc_name,int power_domain)29*4882a593Smuzhiyun struct slot *alloc_slot_struct(struct device_node *dn,
30*4882a593Smuzhiyun int drc_index, char *drc_name, int power_domain)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun struct slot *slot;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
35*4882a593Smuzhiyun if (!slot)
36*4882a593Smuzhiyun goto error_nomem;
37*4882a593Smuzhiyun slot->name = kstrdup(drc_name, GFP_KERNEL);
38*4882a593Smuzhiyun if (!slot->name)
39*4882a593Smuzhiyun goto error_slot;
40*4882a593Smuzhiyun slot->dn = of_node_get(dn);
41*4882a593Smuzhiyun slot->index = drc_index;
42*4882a593Smuzhiyun slot->power_domain = power_domain;
43*4882a593Smuzhiyun slot->hotplug_slot.ops = &rpaphp_hotplug_slot_ops;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun return (slot);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun error_slot:
48*4882a593Smuzhiyun kfree(slot);
49*4882a593Smuzhiyun error_nomem:
50*4882a593Smuzhiyun return NULL;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
is_registered(struct slot * slot)53*4882a593Smuzhiyun static int is_registered(struct slot *slot)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun struct slot *tmp_slot;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
58*4882a593Smuzhiyun if (!strcmp(tmp_slot->name, slot->name))
59*4882a593Smuzhiyun return 1;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
rpaphp_deregister_slot(struct slot * slot)64*4882a593Smuzhiyun int rpaphp_deregister_slot(struct slot *slot)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun int retval = 0;
67*4882a593Smuzhiyun struct hotplug_slot *php_slot = &slot->hotplug_slot;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun dbg("%s - Entry: deregistering slot=%s\n",
70*4882a593Smuzhiyun __func__, slot->name);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun list_del(&slot->rpaphp_slot_list);
73*4882a593Smuzhiyun pci_hp_deregister(php_slot);
74*4882a593Smuzhiyun dealloc_slot_struct(slot);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun dbg("%s - Exit: rc[%d]\n", __func__, retval);
77*4882a593Smuzhiyun return retval;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
80*4882a593Smuzhiyun
rpaphp_register_slot(struct slot * slot)81*4882a593Smuzhiyun int rpaphp_register_slot(struct slot *slot)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct hotplug_slot *php_slot = &slot->hotplug_slot;
84*4882a593Smuzhiyun struct device_node *child;
85*4882a593Smuzhiyun u32 my_index;
86*4882a593Smuzhiyun int retval;
87*4882a593Smuzhiyun int slotno = -1;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun dbg("%s registering slot:path[%pOF] index[%x], name[%s] pdomain[%x] type[%d]\n",
90*4882a593Smuzhiyun __func__, slot->dn, slot->index, slot->name,
91*4882a593Smuzhiyun slot->power_domain, slot->type);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* should not try to register the same slot twice */
94*4882a593Smuzhiyun if (is_registered(slot)) {
95*4882a593Smuzhiyun err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
96*4882a593Smuzhiyun return -EAGAIN;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun for_each_child_of_node(slot->dn, child) {
100*4882a593Smuzhiyun retval = of_property_read_u32(child, "ibm,my-drc-index", &my_index);
101*4882a593Smuzhiyun if (my_index == slot->index) {
102*4882a593Smuzhiyun slotno = PCI_SLOT(PCI_DN(child)->devfn);
103*4882a593Smuzhiyun of_node_put(child);
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
109*4882a593Smuzhiyun if (retval) {
110*4882a593Smuzhiyun err("pci_hp_register failed with error %d\n", retval);
111*4882a593Smuzhiyun return retval;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* add slot to our internal list */
115*4882a593Smuzhiyun list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
116*4882a593Smuzhiyun info("Slot [%s] registered\n", slot->name);
117*4882a593Smuzhiyun return 0;
118*4882a593Smuzhiyun }
119