VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / pci / hotplug / rpaphp_slot.c
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 <asm/rtas.h>
31 #include "rpaphp.h"
32
33 static ssize_t removable_read_file (struct hotplug_slot *php_slot, char *buf)
34 {
35         u8 value;
36         int retval = -ENOENT;
37         struct slot *slot = (struct slot *)php_slot->private;
38
39         if (!slot)
40                 return retval;
41
42         value = slot->removable;
43         retval = sprintf (buf, "%d\n", value);
44         return retval;
45 }
46
47 static struct hotplug_slot_attribute hotplug_slot_attr_removable = {
48         .attr = {.name = "phy_removable", .mode = S_IFREG | S_IRUGO},
49         .show = removable_read_file,
50 };
51
52 static void rpaphp_sysfs_add_attr_removable (struct hotplug_slot *slot)
53 {
54         sysfs_create_file(&slot->kobj, &hotplug_slot_attr_removable.attr);
55 }
56
57 static void rpaphp_sysfs_remove_attr_removable (struct hotplug_slot *slot)
58 {
59         sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_removable.attr);
60 }
61
62 static ssize_t location_read_file (struct hotplug_slot *php_slot, char *buf)
63 {
64         char *value;
65         int retval = -ENOENT;
66         struct slot *slot = (struct slot *)php_slot->private;
67
68         if (!slot)
69                 return retval;
70
71         value = slot->location;
72         retval = sprintf (buf, "%s\n", value);
73         return retval;
74 }
75
76 static struct hotplug_slot_attribute hotplug_slot_attr_location = {
77         .attr = {.name = "phy_location", .mode = S_IFREG | S_IRUGO},
78         .show = location_read_file,
79 };
80
81 static void rpaphp_sysfs_add_attr_location (struct hotplug_slot *slot)
82 {
83         sysfs_create_file(&slot->kobj, &hotplug_slot_attr_location.attr);
84 }
85
86 static void rpaphp_sysfs_remove_attr_location (struct hotplug_slot *slot)
87 {
88         sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_location.attr);
89 }
90
91 /* free up the memory used by a slot */
92 static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot)
93 {
94         struct slot *slot = (struct slot *) hotplug_slot->private;
95
96         dealloc_slot_struct(slot);
97 }
98
99 void dealloc_slot_struct(struct slot *slot)
100 {
101         struct list_head *ln, *n;
102
103         if (slot->dev_type == PCI_DEV) {
104                 list_for_each_safe (ln, n, &slot->dev.pci_funcs) {
105                         struct rpaphp_pci_func *func;
106
107                         func = list_entry(ln, struct rpaphp_pci_func, sibling);
108                         kfree(func);
109                 }
110         }
111
112         kfree(slot->hotplug_slot->info);
113         kfree(slot->hotplug_slot->name);
114         kfree(slot->hotplug_slot);
115         kfree(slot);
116         return;
117 }
118
119 struct slot *alloc_slot_struct(struct device_node *dn, int drc_index, char *drc_name,
120                   int power_domain)
121 {
122         struct slot *slot;
123         
124         slot = kmalloc(sizeof (struct slot), GFP_KERNEL);
125         if (!slot)
126                 goto error_nomem;
127         memset(slot, 0, sizeof (struct slot));
128         slot->hotplug_slot = kmalloc(sizeof (struct hotplug_slot), GFP_KERNEL);
129         if (!slot->hotplug_slot)
130                 goto error_slot;        
131         memset(slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
132         slot->hotplug_slot->info = kmalloc(sizeof (struct hotplug_slot_info),
133                                            GFP_KERNEL);
134         if (!slot->hotplug_slot->info)
135                 goto error_hpslot;
136         memset(slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
137         slot->hotplug_slot->name = kmalloc(BUS_ID_SIZE + 1, GFP_KERNEL);
138         if (!slot->hotplug_slot->name)
139                 goto error_info;        
140         slot->location = kmalloc(strlen(drc_name) + 1, GFP_KERNEL);
141         if (!slot->location)
142                 goto error_name;
143         slot->name = slot->hotplug_slot->name;
144         slot->dn = dn;
145         slot->index = drc_index;
146         strcpy(slot->location, drc_name);
147         slot->power_domain = power_domain;
148         slot->hotplug_slot->private = slot;
149         slot->hotplug_slot->ops = &rpaphp_hotplug_slot_ops;
150         slot->hotplug_slot->release = &rpaphp_release_slot;
151         
152         return (slot);
153
154 error_name:
155         kfree(slot->hotplug_slot->name);
156 error_info:
157         kfree(slot->hotplug_slot->info);
158 error_hpslot:
159         kfree(slot->hotplug_slot);
160 error_slot:
161         kfree(slot);
162 error_nomem:
163         return NULL;
164 }
165
166 static int is_registered(struct slot *slot)
167 {
168         struct slot             *tmp_slot;
169
170         list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
171                 if (!strcmp(tmp_slot->name, slot->name))
172                         return 1;
173         }       
174         return 0;
175 }
176
177 int deregister_slot(struct slot *slot)
178 {
179         int retval = 0;
180         struct hotplug_slot *php_slot = slot->hotplug_slot;
181
182          dbg("%s - Entry: deregistering slot=%s\n",
183                 __FUNCTION__, slot->name);
184
185         list_del(&slot->rpaphp_slot_list);
186         
187         /* remove "phy_location" file */
188         rpaphp_sysfs_remove_attr_location(php_slot);
189
190         /* remove "phy_removable" file */
191         rpaphp_sysfs_remove_attr_removable(php_slot);
192
193         retval = pci_hp_deregister(php_slot);
194         if (retval)
195                 err("Problem unregistering a slot %s\n", slot->name);
196         else
197                 num_slots--;
198
199         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
200         return retval;
201 }
202
203 int register_slot(struct slot *slot)
204 {
205         int retval;
206
207         dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n", 
208                 __FUNCTION__, slot->dn->full_name, slot->index, slot->name, 
209                 slot->power_domain, slot->type);
210         /* should not try to register the same slot twice */
211         if (is_registered(slot)) { /* should't be here */
212                 err("register_slot: slot[%s] is already registered\n", slot->name);
213                 rpaphp_release_slot(slot->hotplug_slot);
214                 return 1;
215         }       
216         retval = pci_hp_register(slot->hotplug_slot);
217         if (retval) {
218                 err("pci_hp_register failed with error %d\n", retval);
219                 rpaphp_release_slot(slot->hotplug_slot);
220                 return retval;
221         }
222         
223         /* create "phy_locatoin" file */
224         rpaphp_sysfs_add_attr_location(slot->hotplug_slot);     
225
226         /* create "phy_removable" file */
227         rpaphp_sysfs_add_attr_removable(slot->hotplug_slot);    
228
229         /* add slot to our internal list */
230         dbg("%s adding slot[%s] to rpaphp_slot_list\n",
231             __FUNCTION__, slot->name);
232
233         list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
234
235         if (slot->dev_type == VIO_DEV)
236                 info("Slot [%s](VIO location=%s) registered\n",
237                      slot->name, slot->location);
238         else
239                 info("Slot [%s](PCI location=%s) registered\n",
240                      slot->name, slot->location);
241         num_slots++;
242         return 0;
243 }
244
245 int rpaphp_get_power_status(struct slot *slot, u8 * value)
246 {
247         int rc = 0;
248         
249         if (slot->type == EMBEDDED) {
250                 dbg("%s set to POWER_ON for EMBEDDED slot %s\n",
251                         __FUNCTION__, slot->location);
252                 *value = POWER_ON;
253         }
254         else {
255                 rc = rtas_get_power_level(slot->power_domain, (int *) value);
256                 if (rc)
257                         err("failed to get power-level for slot(%s), rc=0x%x\n",
258                                 slot->location, rc);
259         }
260
261         return rc;
262 }
263
264 int rpaphp_set_attention_status(struct slot *slot, u8 status)
265 {
266         int rc;
267
268         /* status: LED_OFF or LED_ON */
269         rc = rtas_set_indicator(DR_INDICATOR, slot->index, status);
270         if (rc)
271                 err("slot(name=%s location=%s index=0x%x) set attention-status(%d) failed! rc=0x%x\n",
272                     slot->name, slot->location, slot->index, status, rc);
273
274         return rc;
275 }