vserver 1.9.3
[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 static int rpaphp_disable_slot(struct pci_dev *dev);
67
68 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
69         .owner = THIS_MODULE,
70         .enable_slot = enable_slot,
71         .disable_slot = disable_slot,
72         .set_attention_status = set_attention_status,
73         .get_power_status = get_power_status,
74         .get_attention_status = get_attention_status,
75         .get_adapter_status = get_adapter_status,
76         .get_max_bus_speed = get_max_bus_speed,
77 };
78
79 static int rpaphp_get_attention_status(struct slot *slot)
80 {
81         return slot->hotplug_slot->info->attention_status;
82 }
83
84 /**
85  * set_attention_status - set attention LED
86  * echo 0 > attention -- set LED OFF
87  * echo 1 > attention -- set LED ON
88  * echo 2 > attention -- set LED ID(identify, light is blinking)
89  *
90  */
91 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
92 {
93         int retval = 0;
94         struct slot *slot = (struct slot *)hotplug_slot->private;
95
96         down(&rpaphp_sem);
97         switch (value) {
98         case 0:
99                 retval = rpaphp_set_attention_status(slot, LED_OFF);
100                 hotplug_slot->info->attention_status = 0;
101                 break;
102         case 1:
103         default:
104                 retval = rpaphp_set_attention_status(slot, LED_ON);
105                 hotplug_slot->info->attention_status = 1;
106                 break;
107         case 2:
108                 retval = rpaphp_set_attention_status(slot, LED_ID);
109                 hotplug_slot->info->attention_status = 2;
110                 break;
111         }
112         up(&rpaphp_sem);
113         return retval;
114 }
115
116 /**
117  * get_power_status - get power status of a slot
118  * @hotplug_slot: slot to get status
119  * @value: pointer to store status
120  *
121  *
122  */
123 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
124 {
125         int retval;
126         struct slot *slot = (struct slot *)hotplug_slot->private;
127
128         down(&rpaphp_sem);
129         retval = rpaphp_get_power_status(slot, value);
130         up(&rpaphp_sem);
131         return retval;
132 }
133
134 /**
135  * get_attention_status - get attention LED status
136  *
137  *
138  */
139 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
140 {
141         int retval = 0;
142         struct slot *slot = (struct slot *)hotplug_slot->private;
143
144         down(&rpaphp_sem);
145         *value = rpaphp_get_attention_status(slot);
146         up(&rpaphp_sem);
147         return retval;
148 }
149
150 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
151 {
152         struct slot *slot = (struct slot *)hotplug_slot->private;
153         int retval = 0;
154
155         down(&rpaphp_sem);
156         /*  have to go through this */
157         switch (slot->dev_type) {
158         case PCI_DEV:
159                 retval = rpaphp_get_pci_adapter_status(slot, 0, value);
160                 break;
161         case VIO_DEV:
162                 retval = rpaphp_get_vio_adapter_status(slot, 0, value);
163                 break;
164         default:
165                 retval = -EINVAL;
166         }
167         up(&rpaphp_sem);
168         return retval;
169 }
170
171 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
172 {
173         struct slot *slot = (struct slot *)hotplug_slot->private;
174
175         down(&rpaphp_sem);
176         switch (slot->type) {
177         case 1:
178         case 2:
179         case 3:
180         case 4:
181         case 5:
182         case 6:
183                 *value = PCI_SPEED_33MHz;       /* speed for case 1-6 */
184                 break;
185         case 7:
186         case 8:
187                 *value = PCI_SPEED_66MHz;
188                 break;
189         case 11:
190         case 14:
191                 *value = PCI_SPEED_66MHz_PCIX;
192                 break;
193         case 12:
194         case 15:
195                 *value = PCI_SPEED_100MHz_PCIX;
196                 break;
197         case 13:
198         case 16:
199                 *value = PCI_SPEED_133MHz_PCIX;
200                 break;
201         default:
202                 *value = PCI_SPEED_UNKNOWN;
203                 break;
204
205         }
206         up(&rpaphp_sem);
207         return 0;
208 }
209
210 int rpaphp_remove_slot(struct slot *slot)
211 {
212         return deregister_slot(slot);
213 }
214
215 static int get_dn_properties(struct device_node *dn, int **indexes, int **names, 
216         int **types, int **power_domains)
217 {
218         *indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
219
220         /* &names[1] contains NULL terminated slot names */
221         *names = (int *) get_property(dn, "ibm,drc-names", NULL);
222
223         /* &types[1] contains NULL terminated slot types */
224         *types = (int *) get_property(dn, "ibm,drc-types", NULL);
225
226         /* power_domains[1...n] are the slot power domains */
227         *power_domains = (int *) get_property(dn, "ibm,drc-power-domains", NULL);
228         
229         if (*indexes && *names && *types && *power_domains) 
230                 return (1);
231         
232         return (0);
233 }
234
235 static int is_php_dn(struct device_node *dn, int **indexes, int **names, int **types,
236           int **power_domains)
237 {
238         if (!is_hotplug_capable(dn))
239                 return (0);
240         if (!get_dn_properties(dn, indexes, names, types, power_domains))
241                 return (0);
242         return (1);
243 }
244
245 static int is_dr_dn(struct device_node *dn, int **indexes, int **names, int **types,
246           int **power_domains, int **my_drc_index)
247 {
248         if (!is_hotplug_capable(dn))
249                 return (0);
250
251         *my_drc_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
252         if(!*my_drc_index)              
253                 return (0);
254
255         if (!dn->parent)
256                 return (0);
257
258         return get_dn_properties(dn->parent, indexes, names, types, power_domains);
259 }
260
261 static inline int is_vdevice_root(struct device_node *dn)
262 {
263         return !strcmp(dn->name, "vdevice");
264 }
265
266 char *rpaphp_get_drc_name(struct device_node *dn)
267 {
268         char *name, *ptr = NULL;
269         int *drc_names, *drc_indexes, i;
270         struct device_node *parent = dn->parent;        
271         u32 *my_drc_index;
272
273         if (!parent)
274                 return NULL;
275
276         my_drc_index = (u32 *) get_property(dn, "ibm,my-drc-index", NULL);
277         if (!my_drc_index)
278                 return NULL;    
279
280         drc_names = (int *) get_property(parent, "ibm,drc-names", NULL);
281         drc_indexes = (int *) get_property(parent, "ibm,drc-indexes", NULL);
282         if (!drc_names || !drc_indexes)
283                 return NULL;
284
285         name = (char *) &drc_names[1];
286         for (i = 0; i < drc_indexes[0]; i++, name += (strlen(name) + 1)) {
287                 if (drc_indexes[i + 1] == *my_drc_index) {
288                         ptr = (char *) name;
289                         break;
290                 }
291         }
292
293         return ptr;
294 }
295
296 /****************************************************************
297  *      rpaphp not only registers PCI hotplug slots(HOTPLUG), 
298  *      but also logical DR slots(EMBEDDED).
299  *      HOTPLUG slot: An adapter can be physically added/removed. 
300  *      EMBEDDED slot: An adapter can be logically removed/added
301  *                from/to a partition with the slot.
302  ***************************************************************/
303 int rpaphp_add_slot(struct device_node *dn)
304 {
305         struct slot *slot;
306         int retval = 0;
307         int i, *my_drc_index, slot_type;
308         int *indexes, *names, *types, *power_domains;
309         char *name, *type;
310
311         dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
312
313         if (dn->parent && is_vdevice_root(dn->parent)) {
314                 /* register a VIO device */
315                 retval = register_vio_slot(dn);
316                 goto exit;
317         }
318
319         /* register PCI devices */
320         if (dn->name != 0 && strcmp(dn->name, "pci") == 0) {
321                 if (is_php_dn(dn, &indexes, &names, &types, &power_domains))  
322                         slot_type = HOTPLUG;
323                 else if (is_dr_dn(dn, &indexes, &names, &types, &power_domains, &my_drc_index)) 
324                         slot_type = EMBEDDED;
325                 else goto exit;
326
327                 name = (char *) &names[1];
328                 type = (char *) &types[1];
329                 for (i = 0; i < indexes[0]; i++,
330                         name += (strlen(name) + 1), type += (strlen(type) + 1)) {
331
332                         if ( slot_type == HOTPLUG || 
333                                 (slot_type == EMBEDDED && indexes[i + 1] == my_drc_index[0])) {
334                                 
335                                 if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
336                                                power_domains[i + 1]))) {
337                                         retval = -ENOMEM;
338                                         goto exit;
339                                 }
340                                 if (slot_type == EMBEDDED)
341                                         slot->type = EMBEDDED;
342                                 else
343                                         slot->type = simple_strtoul(type, NULL, 10);
344                                 
345                                 dbg("    Found drc-index:0x%x drc-name:%s drc-type:%s\n",
346                                         indexes[i + 1], name, type);
347
348                                 retval = register_pci_slot(slot);
349                                 if (slot_type == EMBEDDED)
350                                         goto exit;
351                         }
352                 }
353         }
354 exit:
355         dbg("%s - Exit: num_slots=%d rc[%d]\n",
356             __FUNCTION__, num_slots, retval);
357         return retval;
358 }
359
360 /*
361  * init_slots - initialize 'struct slot' structures for each slot
362  *
363  */
364 static void init_slots(void)
365 {
366         struct device_node *dn;
367
368         for (dn = find_all_nodes(); dn; dn = dn->next)
369                 rpaphp_add_slot(dn);
370 }
371
372 static int __init init_rpa(void)
373 {
374
375         init_MUTEX(&rpaphp_sem);
376
377         /* initialize internal data structure etc. */
378         init_slots();
379         if (!num_slots)
380                 return -ENODEV;
381
382         return 0;
383 }
384
385 static void __exit cleanup_slots(void)
386 {
387         struct list_head *tmp, *n;
388         struct slot *slot;
389
390         /*
391          * Unregister all of our slots with the pci_hotplug subsystem,
392          * and free up all memory that we had allocated.
393          * memory will be freed in release_slot callback. 
394          */
395
396         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
397                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
398                 list_del(&slot->rpaphp_slot_list);
399                 pci_hp_deregister(slot->hotplug_slot);
400         }
401         return;
402 }
403
404 static int __init rpaphp_init(void)
405 {
406         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
407
408         /* read all the PRA info from the system */
409         return init_rpa();
410 }
411
412 static void __exit rpaphp_exit(void)
413 {
414         cleanup_slots();
415 }
416
417 static int enable_slot(struct hotplug_slot *hotplug_slot)
418 {
419         int retval = 0;
420         struct slot *slot = (struct slot *)hotplug_slot->private;
421
422         if (slot->state == CONFIGURED) {
423                 dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name);
424                 goto exit;
425         }
426
427         dbg("ENABLING SLOT %s\n", slot->name);
428         down(&rpaphp_sem);
429         switch (slot->dev_type) {
430         case PCI_DEV:
431                 retval = rpaphp_enable_pci_slot(slot);
432                 break;
433         case VIO_DEV:
434                 retval = rpaphp_enable_vio_slot(slot);
435                 break;
436         default:
437                 retval = -EINVAL;
438         }
439         up(&rpaphp_sem);
440 exit:
441         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
442         return retval;
443 }
444
445 static int rpaphp_disable_slot(struct pci_dev *dev)
446 {
447         return disable_slot(rpaphp_find_hotplug_slot(dev));
448 }
449
450 static int disable_slot(struct hotplug_slot *hotplug_slot)
451 {
452         int retval = -EINVAL;
453         struct slot *slot = (struct slot *)hotplug_slot->private;
454
455         dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name);
456
457         if (slot->state == NOT_CONFIGURED) {
458                 dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name);
459                 goto exit;
460         }
461
462         dbg("DISABLING SLOT %s\n", slot->name);
463         down(&rpaphp_sem);
464         switch (slot->dev_type) {
465         case PCI_DEV:
466                 retval = rpaphp_unconfig_pci_adapter(slot);
467                 break;
468         case VIO_DEV:
469                 retval = rpaphp_unconfig_vio_adapter(slot);
470                 break;
471         default:
472                 retval = -ENODEV;
473         }
474         up(&rpaphp_sem);
475 exit:
476         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
477         return retval;
478 }
479
480 module_init(rpaphp_init);
481 module_exit(rpaphp_exit);
482
483 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
484 EXPORT_SYMBOL_GPL(rpaphp_remove_slot);
485 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
486 EXPORT_SYMBOL_GPL(rpaphp_get_drc_name);