VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / pci / hotplug / rpaphp_pci.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/pci.h>
26 #include <asm/pci-bridge.h>
27 #include <asm/rtas.h>
28 #include "../pci.h"             /* for pci_add_new_bus */
29
30 #include "rpaphp.h"
31
32 struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
33 {
34         struct pci_dev *retval_dev = NULL, *dev = NULL;
35         char bus_id[BUS_ID_SIZE];
36
37         sprintf(bus_id, "%04x:%02x:%02x.%d",dn->phb->global_number,
38                 dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn));
39         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
40                 if (!strcmp(pci_name(dev), bus_id)) {
41                         retval_dev = dev;
42                         break;
43                 }
44         }
45         return retval_dev;
46 }
47
48 EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
49
50 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
51 {
52         struct resource *res = &dev->resource[resource];
53         struct resource *root = pci_find_parent_resource(dev, res);
54         char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
55         int err = -EINVAL;
56
57         if (root != NULL) {
58                 err = request_resource(root, res);
59         }
60
61         if (err) {
62                 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
63                     root ? "Address space collision on" :
64                     "No parent found for",
65                     resource, dtype, pci_name(dev), res->start, res->end);
66         }
67         return err;
68 }
69
70 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
71
72 static struct pci_dev *rpaphp_find_bridge_pdev(struct slot *slot)
73 {
74         return rpaphp_find_pci_dev(slot->dn);
75 }
76
77 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
78 {
79         int rc;
80         int setlevel;
81
82         rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
83
84         if (rc) {
85                 if (rc == NEED_POWER || rc == PWR_ONLY) {
86                         dbg("%s: slot must be power up to get sensor-state\n",
87                             __FUNCTION__);
88
89                         /* some slots have to be powered up 
90                          * before get-sensor will succeed.
91                          */
92                         rc = rtas_set_power_level(slot->power_domain, POWER_ON,
93                                                   &setlevel);
94                         if (rc) {
95                                 dbg("%s: power on slot[%s] failed rc=%d.\n",
96                                     __FUNCTION__, slot->name, rc);
97                         } else {
98                                 rc = rtas_get_sensor(DR_ENTITY_SENSE,
99                                                      slot->index, state);
100                         }
101                 } else if (rc == ERR_SENSE_USE)
102                         info("%s: slot is unusable\n", __FUNCTION__);
103                 else
104                         err("%s failed to get sensor state\n", __FUNCTION__);
105         }
106         return rc;
107 }
108
109 /**
110  * get_pci_adapter_status - get the status of a slot
111  * 
112  * 0-- slot is empty
113  * 1-- adapter is configured
114  * 2-- adapter is not configured
115  * 3-- not valid
116  */
117 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
118 {
119         int state, rc;
120         *value = NOT_VALID;
121
122         rc = rpaphp_get_sensor_state(slot, &state);
123         if (rc)
124                 goto exit;
125         if (state == PRESENT) {
126                 if (!is_init)
127                         /* at run-time slot->state can be changed by */
128                         /* config/unconfig adapter */
129                         *value = slot->state;
130                 else {
131                         if (!slot->dn->child)
132                                 dbg("%s: %s is not valid OFDT node\n",
133                                     __FUNCTION__, slot->dn->full_name);
134                         else if (rpaphp_find_pci_dev(slot->dn->child))
135                                 *value = CONFIGURED;
136                         else {
137                                 err("%s: can't find pdev of adapter in slot[%s]\n", 
138                                         __FUNCTION__, slot->dn->full_name);
139                                 *value = NOT_CONFIGURED;
140                         }
141                 }
142         } else if (state == EMPTY) {
143                 dbg("slot is empty\n");
144                 *value = state;
145         }
146
147 exit:
148         return rc;
149 }
150
151 /* Must be called before pci_bus_add_devices */
152 static void 
153 rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
154 {
155         struct pci_dev *dev;
156
157         list_for_each_entry(dev, &bus->devices, bus_list) {
158                 /*
159                  * Skip already-present devices (which are on the
160                  * global device list.)
161                  */
162                 if (list_empty(&dev->global_list)) {
163                         int i;
164                         
165                         if(fix_bus)
166                                 pcibios_fixup_device_resources(dev, bus);
167                         pci_read_irq_line(dev);
168                         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
169                                 struct resource *r = &dev->resource[i];
170
171                                 if (r->parent || !r->start || !r->flags)
172                                         continue;
173                                 rpaphp_claim_resource(dev, i);
174                         }
175                 }
176         }
177 }
178
179 static int rpaphp_pci_config_bridge(struct pci_dev *dev);
180
181 /*****************************************************************************
182  rpaphp_pci_config_slot() will  configure all devices under the 
183  given slot->dn and return the the first pci_dev.
184  *****************************************************************************/
185 static struct pci_dev *
186 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
187 {
188         struct device_node *eads_first_child = dn->child;
189         struct pci_dev *dev;
190         int num;
191         
192         dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
193
194         if (eads_first_child) {
195                 /* pci_scan_slot should find all children of EADs */
196                 num = pci_scan_slot(bus, PCI_DEVFN(PCI_SLOT(eads_first_child->devfn), 0));
197                 if (num) {
198                         rpaphp_fixup_new_pci_devices(bus, 1); 
199                         pci_bus_add_devices(bus);
200                 }
201                 dev = rpaphp_find_pci_dev(eads_first_child);
202                 if (!dev) {
203                         err("No new device found\n");
204                         return NULL;
205                 }
206                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 
207                         rpaphp_pci_config_bridge(dev);
208         }
209         return dev;
210 }
211
212 static int rpaphp_pci_config_bridge(struct pci_dev *dev)
213 {
214         u8 sec_busno;
215         struct pci_bus *child_bus;
216         struct pci_dev *child_dev;
217
218         dbg("Enter %s:  BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
219
220         /* get busno of downstream bus */
221         pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
222                 
223         /* add to children of PCI bridge dev->bus */
224         child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
225         if (!child_bus) {
226                 err("%s: could not add second bus\n", __FUNCTION__);
227                 return -EIO;
228         }
229         sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
230         /* do pci_scan_child_bus */
231         pci_scan_child_bus(child_bus);
232
233         list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
234                 eeh_add_device_late(child_dev);
235         }
236
237          /* fixup new pci devices without touching bus struct */
238         rpaphp_fixup_new_pci_devices(child_bus, 0);
239
240         /* Make the discovered devices available */
241         pci_bus_add_devices(child_bus);
242         return 0;
243 }
244
245 static void enable_eeh(struct device_node *dn)
246 {
247         struct device_node *sib;
248
249         for (sib = dn->child; sib; sib = sib->sibling) 
250                 enable_eeh(sib);
251         eeh_add_device_early(dn);
252         return;
253         
254 }
255
256 #ifdef DEBUG
257 static void print_slot_pci_funcs(struct slot *slot)
258 {
259         struct list_head *l;
260
261         if (slot->dev_type == PCI_DEV) {
262                 printk("pci_funcs of slot[%s]\n", slot->name);
263                 if (list_empty(&slot->dev.pci_funcs))
264                         printk("        pci_funcs is EMPTY\n");
265
266                 list_for_each (l, &slot->dev.pci_funcs) {
267                         struct rpaphp_pci_func *func =
268                                 list_entry(l, struct rpaphp_pci_func, sibling);
269                         printk("        FOUND dev=%s\n", pci_name(func->pci_dev));
270                 }
271         }
272         return;
273 }
274 #else
275 static void print_slot_pci_funcs(struct slot *slot)
276 {
277         return;
278 }
279 #endif
280
281 static int init_slot_pci_funcs(struct slot *slot)
282 {
283         struct device_node *child;
284
285         for (child = slot->dn->child; child != NULL; child = child->sibling) {
286                 struct pci_dev *pdev = rpaphp_find_pci_dev(child);
287
288                 if (pdev) {
289                         struct rpaphp_pci_func *func;
290                         func = kmalloc(sizeof(struct rpaphp_pci_func), GFP_KERNEL);
291                         if (!func) 
292                                 return -ENOMEM;
293                         memset(func, 0, sizeof(struct rpaphp_pci_func));
294                         INIT_LIST_HEAD(&func->sibling);
295                         func->pci_dev = pdev;
296                         list_add_tail(&func->sibling, &slot->dev.pci_funcs);
297                         print_slot_pci_funcs(slot);
298                 } else {
299                         err("%s: dn=%s has no pci_dev\n", 
300                                 __FUNCTION__, child->full_name);
301                         return -EIO;
302                 }
303         }
304         return 0;
305 }
306
307 static int rpaphp_config_pci_adapter(struct slot *slot)
308 {
309         struct pci_bus *pci_bus;
310         struct pci_dev *dev;
311         int rc = -ENODEV;
312
313         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
314
315         if (slot->bridge) {
316
317                 pci_bus = slot->bridge->subordinate;
318                 if (!pci_bus) {
319                         err("%s: can't find bus structure\n", __FUNCTION__);
320                         goto exit;
321                 }
322                 enable_eeh(slot->dn);
323                 dev = rpaphp_pci_config_slot(slot->dn, pci_bus);
324                 if (!dev) {
325                         err("%s: can't find any devices.\n", __FUNCTION__);
326                         goto exit;
327                 }
328                 /* associate corresponding pci_dev */   
329                 rc = init_slot_pci_funcs(slot);
330                 if (rc)
331                         goto exit;
332                 print_slot_pci_funcs(slot);
333                 if (!list_empty(&slot->dev.pci_funcs)) 
334                         rc = 0;
335         } else {
336                 /* slot is not enabled */
337                 err("slot doesn't have pci_dev structure\n");
338         }
339 exit:
340         dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
341         return rc;
342 }
343
344
345 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
346 {
347         eeh_remove_device(dev);
348         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
349                 struct pci_bus *bus = dev->subordinate;
350                 struct list_head *ln;
351                 if (!bus)
352                         return; 
353                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
354                         struct pci_dev *pdev = pci_dev_b(ln);
355                         if (pdev)
356                                 rpaphp_eeh_remove_bus_device(pdev);
357                 }
358
359         }
360         return;
361 }
362
363 int rpaphp_unconfig_pci_adapter(struct slot *slot)
364 {
365         int retval = 0;
366         struct list_head *ln;
367
368         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
369         if (list_empty(&slot->dev.pci_funcs)) {
370                 err("%s: slot[%s] doesn't have any devices.\n", __FUNCTION__, 
371                         slot->name);
372
373                 retval = -EINVAL;
374                 goto exit;
375         }
376         /* remove the devices from the pci core */
377         list_for_each (ln, &slot->dev.pci_funcs) {
378                 struct rpaphp_pci_func *func;
379         
380                 func = list_entry(ln, struct rpaphp_pci_func, sibling);
381                 if (func->pci_dev) {
382                         pci_remove_bus_device(func->pci_dev); 
383                         rpaphp_eeh_remove_bus_device(func->pci_dev);
384                 }
385                 kfree(func);
386         }
387         INIT_LIST_HEAD(&slot->dev.pci_funcs);
388         slot->state = NOT_CONFIGURED;
389         info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
390              slot->name);
391 exit:
392         dbg("Exit %s, rc=0x%x\n", __FUNCTION__, retval);
393         return retval;
394 }
395
396 static int setup_pci_hotplug_slot_info(struct slot *slot)
397 {
398         dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
399             __FUNCTION__);
400         rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
401         rpaphp_get_pci_adapter_status(slot, 1,
402                                       &slot->hotplug_slot->info->
403                                       adapter_status);
404         if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
405                 err("%s: NOT_VALID: skip dn->full_name=%s\n",
406                     __FUNCTION__, slot->dn->full_name);
407                 return -1;
408         }
409         return 0;
410 }
411
412 static int setup_pci_slot(struct slot *slot)
413 {
414         slot->bridge = rpaphp_find_bridge_pdev(slot);
415         if (!slot->bridge) {    /* slot being added doesn't have pci_dev yet */
416                 err("%s: no pci_dev for bridge dn %s\n", __FUNCTION__, slot->name);
417                 goto exit_rc;
418         }
419         dbg("%s set slot->name to %s\n",  __FUNCTION__, pci_name(slot->bridge));
420         strcpy(slot->name, pci_name(slot->bridge));
421
422         /* find slot's pci_dev if it's not empty */
423         if (slot->hotplug_slot->info->adapter_status == EMPTY) {
424                 slot->state = EMPTY;    /* slot is empty */
425         } else {
426                 /* slot is occupied */
427                 if (!(slot->dn->child)) {
428                         /* non-empty slot has to have child */
429                         err("%s: slot[%s]'s device_node doesn't have child for adapter\n", 
430                                 __FUNCTION__, slot->name);
431                         goto exit_rc;
432                 }
433                 if (init_slot_pci_funcs(slot)) {
434                         err("%s: init_slot_pci_funcs failed\n", __FUNCTION__);
435                         goto exit_rc;
436                 }
437                 print_slot_pci_funcs(slot);
438                 if (!list_empty(&slot->dev.pci_funcs)) {
439                         slot->state = CONFIGURED;
440         
441                 } else {
442                         /* DLPAR add as opposed to 
443                          * boot time */
444                         slot->state = NOT_CONFIGURED;
445                 }
446         }
447         return 0;
448 exit_rc:
449         dealloc_slot_struct(slot);
450         return 1;
451 }
452
453 int register_pci_slot(struct slot *slot)
454 {
455         int rc = 1;
456
457         slot->dev_type = PCI_DEV;
458         if (slot->type == EMBEDDED)
459                 slot->removable = EMBEDDED;
460         else
461                 slot->removable = HOTPLUG;
462         INIT_LIST_HEAD(&slot->dev.pci_funcs);
463         if (setup_pci_hotplug_slot_info(slot))
464                 goto exit_rc;
465         if (setup_pci_slot(slot))
466                 goto exit_rc;
467         rc = register_slot(slot);
468 exit_rc:
469         return rc;
470 }
471
472 int rpaphp_enable_pci_slot(struct slot *slot)
473 {
474         int retval = 0, state;
475
476         retval = rpaphp_get_sensor_state(slot, &state);
477         if (retval)
478                 goto exit;
479         dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
480         /* if slot is not empty, enable the adapter */
481         if (state == PRESENT) {
482                 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
483                 retval = rpaphp_config_pci_adapter(slot);
484                 if (!retval) {
485                         slot->state = CONFIGURED;
486                         dbg("%s: PCI devices in slot[%s] has been configured\n", 
487                                 __FUNCTION__, slot->name);
488                 } else {
489                         slot->state = NOT_CONFIGURED;
490                         dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
491                             __FUNCTION__, slot->name);
492                 }
493         } else if (state == EMPTY) {
494                 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
495                 slot->state = EMPTY;
496         } else {
497                 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
498                     slot->name);
499                 slot->state = NOT_VALID;
500                 retval = -EINVAL;
501         }
502 exit:
503         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
504         return retval;
505 }
506
507 struct hotplug_slot *rpaphp_find_hotplug_slot(struct pci_dev *dev)
508 {
509         struct list_head        *tmp, *n;
510         struct slot             *slot;
511
512         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
513                 struct pci_bus *bus;
514                 struct list_head *ln;
515
516                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
517                 if (slot->bridge == NULL) {
518                         if (slot->dev_type == PCI_DEV) {
519                                 printk(KERN_WARNING "PCI slot missing bridge %s %s \n", 
520                                                     slot->name, slot->location);
521                         }
522                         continue;
523                 }
524
525                 bus = slot->bridge->subordinate;
526                 if (!bus) {
527                         continue;  /* should never happen? */
528                 }
529                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
530                                 struct pci_dev *pdev = pci_dev_b(ln);
531                                 if (pdev == dev)
532                                         return slot->hotplug_slot;
533                 }
534         }
535
536         return NULL;
537 }
538
539 EXPORT_SYMBOL_GPL(rpaphp_find_hotplug_slot);