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