patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pci / hotplug / rpaphp_core.c
1 /*
2  * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3  * Copyright (C) 2003 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/config.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/smp.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <asm/eeh.h>       /* for eeh_add_device() */
35 #include <asm/rtas.h>           /* rtas_call */
36 #include <asm/pci-bridge.h>     /* for pci_controller */
37 #include "../pci.h"             /* for pci_add_new_bus */
38                                 /* and pci_do_scan_bus */
39 #include "rpaphp.h"
40 #include "pci_hotplug.h"
41
42 int debug;
43 static struct semaphore rpaphp_sem;
44 LIST_HEAD(rpaphp_slot_head);
45 int num_slots;
46
47 #define DRIVER_VERSION  "0.1"
48 #define DRIVER_AUTHOR   "Linda Xie <lxie@us.ibm.com>"
49 #define DRIVER_DESC     "RPA HOT Plug PCI Controller Driver"
50
51 #define MAX_LOC_CODE 128
52
53 MODULE_AUTHOR(DRIVER_AUTHOR);
54 MODULE_DESCRIPTION(DRIVER_DESC);
55 MODULE_LICENSE("GPL");
56
57 module_param(debug, bool, 0644);
58
59 static int enable_slot(struct hotplug_slot *slot);
60 static int disable_slot(struct hotplug_slot *slot);
61 static int set_attention_status(struct hotplug_slot *slot, u8 value);
62 static int get_power_status(struct hotplug_slot *slot, u8 * value);
63 static int get_attention_status(struct hotplug_slot *slot, u8 * value);
64 static int get_adapter_status(struct hotplug_slot *slot, u8 * value);
65 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value);
66
67 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
68         .owner = THIS_MODULE,
69         .enable_slot = enable_slot,
70         .disable_slot = disable_slot,
71         .set_attention_status = set_attention_status,
72         .get_power_status = get_power_status,
73         .get_attention_status = get_attention_status,
74         .get_adapter_status = get_adapter_status,
75         .get_max_bus_speed = get_max_bus_speed,
76 };
77
78 static int rpaphp_get_attention_status(struct slot *slot)
79 {
80         return slot->hotplug_slot->info->attention_status;
81 }
82
83 /**
84  * set_attention_status - set attention LED
85  * echo 0 > attention -- set LED OFF
86  * echo 1 > attention -- set LED ON
87  * echo 2 > attention -- set LED ID(identify, light is blinking)
88  *
89  */
90 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
91 {
92         int retval;
93         struct slot *slot = (struct slot *)hotplug_slot->private;
94
95         down(&rpaphp_sem);
96         switch (value) {
97         case 0:
98                 retval = rpaphp_set_attention_status(slot, LED_OFF);
99                 hotplug_slot->info->attention_status = 0;
100                 break;
101         case 1:
102         default:
103                 retval = rpaphp_set_attention_status(slot, LED_ON);
104                 hotplug_slot->info->attention_status = 1;
105                 break;
106         case 2:
107                 retval = rpaphp_set_attention_status(slot, LED_ID);
108                 hotplug_slot->info->attention_status = 2;
109                 break;
110         }
111         up(&rpaphp_sem);
112         return retval;
113 }
114
115 /**
116  * get_power_status - get power status of a slot
117  * @hotplug_slot: slot to get status
118  * @value: pointer to store status
119  *
120  *
121  */
122 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
123 {
124         int retval;
125         struct slot *slot = (struct slot *)hotplug_slot->private;
126
127         down(&rpaphp_sem);
128         retval = rpaphp_get_power_status(slot, value);
129         up(&rpaphp_sem);
130         return retval;
131 }
132
133 /**
134  * get_attention_status - get attention LED status
135  *
136  *
137  */
138 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
139 {
140         int retval = 0;
141         struct slot *slot = (struct slot *)hotplug_slot->private;
142
143         down(&rpaphp_sem);
144         *value = rpaphp_get_attention_status(slot);
145         up(&rpaphp_sem);
146         return retval;
147 }
148
149 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
150 {
151         struct slot *slot = (struct slot *)hotplug_slot->private;
152         int retval = 0;
153
154         down(&rpaphp_sem);
155         /*  have to go through this */
156         switch (slot->dev_type) {
157         case PCI_DEV:
158                 retval = rpaphp_get_pci_adapter_status(slot, 0, value);
159                 break;
160         case VIO_DEV:
161                 retval = rpaphp_get_vio_adapter_status(slot, 0, value);
162                 break;
163         default:
164                 retval = -EINVAL;
165         }
166         up(&rpaphp_sem);
167         return retval;
168 }
169
170 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
171 {
172         struct slot *slot = (struct slot *)hotplug_slot->private;
173
174         down(&rpaphp_sem);
175         switch (slot->type) {
176         case 1:
177         case 2:
178         case 3:
179         case 4:
180         case 5:
181         case 6:
182                 *value = PCI_SPEED_33MHz;       /* speed for case 1-6 */
183                 break;
184         case 7:
185         case 8:
186                 *value = PCI_SPEED_66MHz;
187                 break;
188         case 11:
189         case 14:
190                 *value = PCI_SPEED_66MHz_PCIX;
191                 break;
192         case 12:
193         case 15:
194                 *value = PCI_SPEED_100MHz_PCIX;
195                 break;
196         case 13:
197         case 16:
198                 *value = PCI_SPEED_133MHz_PCIX;
199                 break;
200         default:
201                 *value = PCI_SPEED_UNKNOWN;
202                 break;
203
204         }
205         up(&rpaphp_sem);
206         return 0;
207 }
208
209 int rpaphp_remove_slot(struct slot *slot)
210 {
211         int retval = 0;
212         struct hotplug_slot *php_slot = slot->hotplug_slot;
213
214         list_del(&slot->rpaphp_slot_list);
215         
216         /* remove "php_location" file */
217         rpaphp_sysfs_remove_attr_location(php_slot);
218
219         retval = pci_hp_deregister(php_slot);
220         if (retval)
221                 err("Problem unregistering a slot %s\n", slot->name);
222
223         num_slots--;
224
225         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
226         return retval;
227 }
228
229 static int is_php_dn(struct device_node *dn, int **indexes, int **names, int **types,
230           int **power_domains)
231 {
232         *indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
233         if (!*indexes)
234                 return 0;
235         /* &names[1] contains NULL terminated slot names */
236         *names = (int *) get_property(dn, "ibm,drc-names", NULL);
237         if (!*names)
238                 return 0;
239         /* &types[1] contains NULL terminated slot types */
240         *types = (int *) get_property(dn, "ibm,drc-types", NULL);
241         if (!*types)
242                 return 0;
243         /* power_domains[1...n] are the slot power domains */
244         *power_domains = (int *) get_property(dn,
245                                               "ibm,drc-power-domains", NULL);
246         if (!*power_domains)
247                 return 0;
248         if (strcmp(dn->name, "pci") == 0 &&
249                         !get_property(dn, "ibm,fw-pci-hot-plug-ctrl", NULL))
250                 return 0;
251         return 1;
252 }
253
254 static inline int is_vdevice_root(struct device_node *dn)
255 {
256         return !strcmp(dn->name, "vdevice");
257 }
258
259 /**
260  * rpaphp_add_slot: Add  Hot Plug slot(s) to sysfs
261  *
262  */
263 int rpaphp_add_slot(struct device_node *dn)
264 {
265         struct slot *slot;
266         int retval = 0;
267         int i;
268         int *indexes, *names, *types, *power_domains;
269         char *name, *type;
270
271         dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
272
273         if (dn->parent && is_vdevice_root(dn->parent)) {
274                 /* register a VIO device */
275                 retval = register_vio_slot(dn);
276                 goto exit;
277         }
278
279         /* register PCI devices */
280         if (dn->name != 0 && strcmp(dn->name, "pci") == 0 &&
281             is_php_dn(dn, &indexes, &names, &types, &power_domains)) {
282
283                 name = (char *) &names[1];
284                 type = (char *) &types[1];
285                 for (i = 0; i < indexes[0];
286                                         i++,
287                                         name += (strlen(name) + 1),
288                                         type += (strlen(type) + 1)) {
289                         if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
290                                                        power_domains[i + 1]))) {
291                                 retval = -ENOMEM;
292                                 goto exit;
293                         }
294                         slot->type = simple_strtoul(type, NULL, 10);
295                         if (slot->type < 1 || slot->type > 16)
296                                 slot->type = 0;
297                         retval = register_pci_slot(slot);
298
299                 }               /* for indexes */
300         }                       /* end of PCI device_node */
301 exit:
302         dbg("%s - Exit: num_slots=%d rc[%d]\n",
303             __FUNCTION__, num_slots, retval);
304         return retval;
305 }
306
307 static int __init init_rpa(void)
308 {
309         struct device_node *dn;
310
311         init_MUTEX(&rpaphp_sem);
312
313         /* initialize internal data structure etc. */
314         for (dn = find_all_nodes(); dn; dn = dn->next)
315                 rpaphp_add_slot(dn);
316         if (!num_slots)
317                 return -ENODEV;
318
319         return 0;
320 }
321
322 static void __exit cleanup_slots(void)
323 {
324         struct list_head *tmp, *n;
325         struct slot *slot;
326
327         /*
328          * Unregister all of our slots with the pci_hotplug subsystem,
329          * and free up all memory that we had allocated.
330          * memory will be freed in release_slot callback. 
331          */
332
333         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
334                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
335                 list_del(&slot->rpaphp_slot_list);
336                 pci_hp_deregister(slot->hotplug_slot);
337         }
338         return;
339 }
340
341 static int __init rpaphp_init(void)
342 {
343         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
344
345         /* read all the PRA info from the system */
346         return init_rpa();
347 }
348
349 static void __exit rpaphp_exit(void)
350 {
351         cleanup_slots();
352 }
353
354 static int enable_slot(struct hotplug_slot *hotplug_slot)
355 {
356         int retval = 0;
357         struct slot *slot = (struct slot *)hotplug_slot->private;
358
359         if (slot->state == CONFIGURED) {
360                 dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name);
361                 goto exit;
362         }
363
364         dbg("ENABLING SLOT %s\n", slot->name);
365         down(&rpaphp_sem);
366         switch (slot->dev_type) {
367         case PCI_DEV:
368                 retval = rpaphp_enable_pci_slot(slot);
369                 break;
370         case VIO_DEV:
371                 retval = rpaphp_enable_vio_slot(slot);
372                 break;
373         default:
374                 retval = -EINVAL;
375         }
376         up(&rpaphp_sem);
377       exit:
378         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
379         return retval;
380 }
381
382 static int disable_slot(struct hotplug_slot *hotplug_slot)
383 {
384         int retval;
385         struct slot *slot = (struct slot *)hotplug_slot->private;
386
387         dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name);
388
389         if (slot->state == NOT_CONFIGURED) {
390                 dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name);
391                 goto exit;
392         }
393
394         dbg("DISABLING SLOT %s\n", slot->name);
395         down(&rpaphp_sem);
396         switch (slot->dev_type) {
397         case PCI_DEV:
398                 rpaphp_set_attention_status(slot, LED_ID);
399                 retval = rpaphp_unconfig_pci_adapter(slot);
400                 rpaphp_set_attention_status(slot, LED_OFF);
401                 break;
402         case VIO_DEV:
403                 retval = rpaphp_unconfig_vio_adapter(slot);
404                 break;
405         default:
406                 retval = -ENODEV;
407         }
408         up(&rpaphp_sem);
409       exit:
410         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
411         return retval;
412 }
413
414 module_init(rpaphp_init);
415 module_exit(rpaphp_exit);
416
417 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
418 EXPORT_SYMBOL_GPL(rpaphp_remove_slot);
419 EXPORT_SYMBOL_GPL(rpaphp_slot_head);