1 /*
2  * RPA Virtual I/O device functions
3  * Copyright (C) 2004 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/kernel.h>
26 #include <linux/module.h>
27 #include <linux/kobject.h>
28 #include <linux/sysfs.h>
29 #include <linux/pci.h>
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 
33 #include <asm/rtas.h>
34 #include "rpaphp.h"
35 
36 static ssize_t address_read_file (struct hotplug_slot *php_slot, char *buf)
37 {
38 	int retval;
39 	struct slot *slot = (struct slot *)php_slot->private;
40 	struct pci_bus *bus;
41 
42 	if (!slot)
43 		return -ENOENT;
44 
45 	bus = slot->bus;
46 	if (!bus)
47 		return -ENOENT;
48 
49 	if (bus->self)
50 		retval = sprintf(buf, pci_name(bus->self));
51 	else
52 		retval = sprintf(buf, "%04x:%02x:00.0",
53 		        pci_domain_nr(bus), bus->number);
54 
55 	return retval;
56 }
57 
58 static struct hotplug_slot_attribute php_attr_address = {
59 	.attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
60 	.show = address_read_file,
61 };
62 
63 /* free up the memory used by a slot */
64 static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot)
65 {
66 	struct slot *slot = (struct slot *) hotplug_slot->private;
67 	dealloc_slot_struct(slot);
68 }
69 
70 void dealloc_slot_struct(struct slot *slot)
71 {
72 	kfree(slot->hotplug_slot->info);
73 	kfree(slot->hotplug_slot->name);
74 	kfree(slot->hotplug_slot);
75 	kfree(slot);
76 }
77 
78 struct slot *alloc_slot_struct(struct device_node *dn,
79                        int drc_index, char *drc_name, int power_domain)
80 {
81 	struct slot *slot;
82 
83 	slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
84 	if (!slot)
85 		goto error_nomem;
86 	slot->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
87 	if (!slot->hotplug_slot)
88 		goto error_slot;
89 	slot->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
90 					   GFP_KERNEL);
91 	if (!slot->hotplug_slot->info)
92 		goto error_hpslot;
93 	slot->hotplug_slot->name = kmalloc(strlen(drc_name) + 1, GFP_KERNEL);
94 	if (!slot->hotplug_slot->name)
95 		goto error_info;
96 	slot->name = slot->hotplug_slot->name;
97 	strcpy(slot->name, drc_name);
98 	slot->dn = dn;
99 	slot->index = drc_index;
100 	slot->power_domain = power_domain;
101 	slot->hotplug_slot->private = slot;
102 	slot->hotplug_slot->ops = &rpaphp_hotplug_slot_ops;
103 	slot->hotplug_slot->release = &rpaphp_release_slot;
104 
105 	return (slot);
106 
107 error_info:
108 	kfree(slot->hotplug_slot->info);
109 error_hpslot:
110 	kfree(slot->hotplug_slot);
111 error_slot:
112 	kfree(slot);
113 error_nomem:
114 	return NULL;
115 }
116 
117 static int is_registered(struct slot *slot)
118 {
119 	struct slot *tmp_slot;
120 
121 	list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
122 		if (!strcmp(tmp_slot->name, slot->name))
123 			return 1;
124 	}
125 	return 0;
126 }
127 
128 int rpaphp_deregister_slot(struct slot *slot)
129 {
130 	int retval = 0;
131 	struct hotplug_slot *php_slot = slot->hotplug_slot;
132 
133 	 dbg("%s - Entry: deregistering slot=%s\n",
134 		__FUNCTION__, slot->name);
135 
136 	list_del(&slot->rpaphp_slot_list);
137 
138 	/* remove "address" file */
139 	sysfs_remove_file(&php_slot->kobj, &php_attr_address.attr);
140 
141 	retval = pci_hp_deregister(php_slot);
142 	if (retval)
143 		err("Problem unregistering a slot %s\n", slot->name);
144 
145 	dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
146 	return retval;
147 }
148 EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
149 
150 int rpaphp_register_slot(struct slot *slot)
151 {
152 	struct hotplug_slot *php_slot = slot->hotplug_slot;
153 	int retval;
154 
155 	dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n",
156 		__FUNCTION__, slot->dn->full_name, slot->index, slot->name,
157 		slot->power_domain, slot->type);
158 
159 	/* should not try to register the same slot twice */
160 	if (is_registered(slot)) {
161 		err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
162 		return -EAGAIN;
163 	}
164 
165 	retval = pci_hp_register(php_slot);
166 	if (retval) {
167 		err("pci_hp_register failed with error %d\n", retval);
168 		return retval;
169 	}
170 
171 	/* create "address" file */
172 	retval = sysfs_create_file(&php_slot->kobj, &php_attr_address.attr);
173 	if (retval) {
174 		err("sysfs_create_file failed with error %d\n", retval);
175 		goto sysfs_fail;
176 	}
177 
178 	/* add slot to our internal list */
179 	list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
180 	info("Slot [%s] registered\n", slot->name);
181 	return 0;
182 
183 sysfs_fail:
184 	pci_hp_deregister(php_slot);
185 	return retval;
186 }
187 
188