PCI: rpaphp: remove a call that does nothing but a pointer lookup
[sfrench/cifs-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/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/pci.h>
29 #include <linux/pci_hotplug.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
41 int debug;
42 static struct semaphore rpaphp_sem;
43 LIST_HEAD(rpaphp_slot_head);
44
45 #define DRIVER_VERSION  "0.1"
46 #define DRIVER_AUTHOR   "Linda Xie <lxie@us.ibm.com>"
47 #define DRIVER_DESC     "RPA HOT Plug PCI Controller Driver"
48
49 #define MAX_LOC_CODE 128
50
51 MODULE_AUTHOR(DRIVER_AUTHOR);
52 MODULE_DESCRIPTION(DRIVER_DESC);
53 MODULE_LICENSE("GPL");
54
55 module_param(debug, bool, 0644);
56
57 /**
58  * set_attention_status - set attention LED
59  * echo 0 > attention -- set LED OFF
60  * echo 1 > attention -- set LED ON
61  * echo 2 > attention -- set LED ID(identify, light is blinking)
62  *
63  */
64 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
65 {
66         int retval = 0;
67         struct slot *slot = (struct slot *)hotplug_slot->private;
68
69         down(&rpaphp_sem);
70         switch (value) {
71         case 0:
72                 retval = rpaphp_set_attention_status(slot, LED_OFF);
73                 hotplug_slot->info->attention_status = 0;
74                 break;
75         case 1:
76         default:
77                 retval = rpaphp_set_attention_status(slot, LED_ON);
78                 hotplug_slot->info->attention_status = 1;
79                 break;
80         case 2:
81                 retval = rpaphp_set_attention_status(slot, LED_ID);
82                 hotplug_slot->info->attention_status = 2;
83                 break;
84         }
85         up(&rpaphp_sem);
86         return retval;
87 }
88
89 /**
90  * get_power_status - get power status of a slot
91  * @hotplug_slot: slot to get status
92  * @value: pointer to store status
93  */
94 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
95 {
96         int retval, level;
97         struct slot *slot = (struct slot *)hotplug_slot->private;
98
99         down(&rpaphp_sem);
100         retval = rtas_get_power_level (slot->power_domain, &level);
101         if (!retval)
102                 *value = level;
103         up(&rpaphp_sem);
104         return retval;
105 }
106
107 /**
108  * get_attention_status - get attention LED status
109  */
110 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
111 {
112         struct slot *slot = (struct slot *)hotplug_slot->private;
113         *value = slot->hotplug_slot->info->attention_status;
114         return 0;
115 }
116
117 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
118 {
119         struct slot *slot = (struct slot *)hotplug_slot->private;
120         int rc, state;
121
122         down(&rpaphp_sem);
123         rc = rpaphp_get_sensor_state(slot, &state);
124         up(&rpaphp_sem);
125
126         *value = NOT_VALID;
127         if (rc)
128                 return rc;
129
130         if (state == EMPTY)
131                 *value = EMPTY;
132         else if (state == PRESENT)
133                 *value = slot->state;
134
135         return 0;
136 }
137
138 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
139 {
140         struct slot *slot = (struct slot *)hotplug_slot->private;
141
142         down(&rpaphp_sem);
143         switch (slot->type) {
144         case 1:
145         case 2:
146         case 3:
147         case 4:
148         case 5:
149         case 6:
150                 *value = PCI_SPEED_33MHz;       /* speed for case 1-6 */
151                 break;
152         case 7:
153         case 8:
154                 *value = PCI_SPEED_66MHz;
155                 break;
156         case 11:
157         case 14:
158                 *value = PCI_SPEED_66MHz_PCIX;
159                 break;
160         case 12:
161         case 15:
162                 *value = PCI_SPEED_100MHz_PCIX;
163                 break;
164         case 13:
165         case 16:
166                 *value = PCI_SPEED_133MHz_PCIX;
167                 break;
168         default:
169                 *value = PCI_SPEED_UNKNOWN;
170                 break;
171
172         }
173         up(&rpaphp_sem);
174         return 0;
175 }
176
177 static int get_children_props(struct device_node *dn, const int **drc_indexes,
178                 const int **drc_names, const int **drc_types,
179                 const int **drc_power_domains)
180 {
181         const int *indexes, *names, *types, *domains;
182
183         indexes = get_property(dn, "ibm,drc-indexes", NULL);
184         names = get_property(dn, "ibm,drc-names", NULL);
185         types = get_property(dn, "ibm,drc-types", NULL);
186         domains = get_property(dn, "ibm,drc-power-domains", NULL);
187
188         if (!indexes || !names || !types || !domains) {
189                 /* Slot does not have dynamically-removable children */
190                 return -EINVAL;
191         }
192         if (drc_indexes)
193                 *drc_indexes = indexes;
194         if (drc_names)
195                 /* &drc_names[1] contains NULL terminated slot names */
196                 *drc_names = names;
197         if (drc_types)
198                 /* &drc_types[1] contains NULL terminated slot types */
199                 *drc_types = types;
200         if (drc_power_domains)
201                 *drc_power_domains = domains;
202
203         return 0;
204 }
205
206 /* To get the DRC props describing the current node, first obtain it's
207  * my-drc-index property.  Next obtain the DRC list from it's parent.  Use
208  * the my-drc-index for correlation, and obtain the requested properties.
209  */
210 int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
211                 char **drc_name, char **drc_type, int *drc_power_domain)
212 {
213         const int *indexes, *names;
214         const int *types, *domains;
215         const unsigned int *my_index;
216         char *name_tmp, *type_tmp;
217         int i, rc;
218
219         my_index = get_property(dn, "ibm,my-drc-index", NULL);
220         if (!my_index) {
221                 /* Node isn't DLPAR/hotplug capable */
222                 return -EINVAL;
223         }
224
225         rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
226         if (rc < 0) {
227                 return -EINVAL;
228         }
229
230         name_tmp = (char *) &names[1];
231         type_tmp = (char *) &types[1];
232
233         /* Iterate through parent properties, looking for my-drc-index */
234         for (i = 0; i < indexes[0]; i++) {
235                 if ((unsigned int) indexes[i + 1] == *my_index) {
236                         if (drc_name)
237                                 *drc_name = name_tmp;
238                         if (drc_type)
239                                 *drc_type = type_tmp;
240                         if (drc_index)
241                                 *drc_index = *my_index;
242                         if (drc_power_domain)
243                                 *drc_power_domain = domains[i+1];
244                         return 0;
245                 }
246                 name_tmp += (strlen(name_tmp) + 1);
247                 type_tmp += (strlen(type_tmp) + 1);
248         }
249
250         return -EINVAL;
251 }
252
253 static int is_php_type(char *drc_type)
254 {
255         unsigned long value;
256         char *endptr;
257
258         /* PCI Hotplug nodes have an integer for drc_type */
259         value = simple_strtoul(drc_type, &endptr, 10);
260         if (endptr == drc_type)
261                 return 0;
262
263         return 1;
264 }
265
266 static int is_php_dn(struct device_node *dn, const int **indexes,
267                 const int **names, const int **types, const int **power_domains)
268 {
269         const int *drc_types;
270         int rc;
271
272         rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
273         if (rc >= 0) {
274                 if (is_php_type((char *) &drc_types[1])) {
275                         *types = drc_types;
276                         return 1;
277                 }
278         }
279
280         return 0;
281 }
282
283 /**
284  * rpaphp_add_slot -- add hotplug or dlpar slot
285  *
286  *      rpaphp not only registers PCI hotplug slots(HOTPLUG), 
287  *      but also logical DR slots(EMBEDDED).
288  *      HOTPLUG slot: An adapter can be physically added/removed. 
289  *      EMBEDDED slot: An adapter can be logically removed/added
290  *                from/to a partition with the slot.
291  */
292 int rpaphp_add_slot(struct device_node *dn)
293 {
294         struct slot *slot;
295         int retval = 0;
296         int i;
297         const int *indexes, *names, *types, *power_domains;
298         char *name, *type;
299
300         if (!dn->name || strcmp(dn->name, "pci"))
301                 return 0;
302
303         if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
304                 return 0;
305
306         dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
307
308         /* register PCI devices */
309         name = (char *) &names[1];
310         type = (char *) &types[1];
311         for (i = 0; i < indexes[0]; i++) {
312
313                 slot = alloc_slot_struct(dn, indexes[i + 1], name, power_domains[i + 1]);
314                 if (!slot)
315                         return -ENOMEM;
316
317                 slot->type = simple_strtoul(type, NULL, 10);
318                                 
319                 dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
320                                 indexes[i + 1], name, type);
321
322                 retval = rpaphp_register_pci_slot(slot);
323                 if (retval)
324                         dealloc_slot_struct(slot);
325
326                 name += strlen(name) + 1;
327                 type += strlen(type) + 1;
328         }
329         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
330
331         /* XXX FIXME: reports a failure only if last entry in loop failed */
332         return retval;
333 }
334
335 static void __exit cleanup_slots(void)
336 {
337         struct list_head *tmp, *n;
338         struct slot *slot;
339
340         /*
341          * Unregister all of our slots with the pci_hotplug subsystem,
342          * and free up all memory that we had allocated.
343          * memory will be freed in release_slot callback. 
344          */
345
346         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
347                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
348                 list_del(&slot->rpaphp_slot_list);
349                 pci_hp_deregister(slot->hotplug_slot);
350         }
351         return;
352 }
353
354 static int __init rpaphp_init(void)
355 {
356         struct device_node *dn = NULL;
357
358         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
359         init_MUTEX(&rpaphp_sem);
360
361         while ((dn = of_find_node_by_name(dn, "pci")))
362                 rpaphp_add_slot(dn);
363
364         return 0;
365 }
366
367 static void __exit rpaphp_exit(void)
368 {
369         cleanup_slots();
370 }
371
372 static int __enable_slot(struct slot *slot)
373 {
374         int state;
375         int retval;
376
377         if (slot->state == CONFIGURED)
378                 return 0;
379
380         retval = rpaphp_get_sensor_state(slot, &state);
381         if (retval)
382                 return retval;
383
384         if (state == PRESENT) {
385                 pcibios_add_pci_devices(slot->bus);
386                 slot->state = CONFIGURED;
387         } else if (state == EMPTY) {
388                 slot->state = EMPTY;
389         } else {
390                 err("%s: slot[%s] is in invalid state\n", __FUNCTION__, slot->name);
391                 slot->state = NOT_VALID;
392                 return -EINVAL;
393         }
394         return 0;
395 }
396
397 static int enable_slot(struct hotplug_slot *hotplug_slot)
398 {
399         int retval;
400         struct slot *slot = (struct slot *)hotplug_slot->private;
401
402         down(&rpaphp_sem);
403         retval = __enable_slot(slot);
404         up(&rpaphp_sem);
405
406         return retval;
407 }
408
409 static int __disable_slot(struct slot *slot)
410 {
411         struct pci_dev *dev, *tmp;
412
413         if (slot->state == NOT_CONFIGURED)
414                 return -EINVAL;
415
416         list_for_each_entry_safe(dev, tmp, &slot->bus->devices, bus_list) {
417                 eeh_remove_bus_device(dev);
418                 pci_remove_bus_device(dev);
419         }
420
421         slot->state = NOT_CONFIGURED;
422         return 0;
423 }
424
425 static int disable_slot(struct hotplug_slot *hotplug_slot)
426 {
427         struct slot *slot = (struct slot *)hotplug_slot->private;
428         int retval;
429
430         down(&rpaphp_sem);
431         retval = __disable_slot (slot);
432         up(&rpaphp_sem);
433
434         return retval;
435 }
436
437 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
438         .owner = THIS_MODULE,
439         .enable_slot = enable_slot,
440         .disable_slot = disable_slot,
441         .set_attention_status = set_attention_status,
442         .get_power_status = get_power_status,
443         .get_attention_status = get_attention_status,
444         .get_adapter_status = get_adapter_status,
445         .get_max_bus_speed = get_max_bus_speed,
446 };
447
448 module_init(rpaphp_init);
449 module_exit(rpaphp_exit);
450
451 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
452 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
453 EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);