ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 "../pci.h"             /* for pci_add_new_bus */
28
29 #include "rpaphp.h"
30
31 struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
32 {
33         struct pci_dev *retval_dev = NULL, *dev = NULL;
34         char bus_id[BUS_ID_SIZE];
35
36         sprintf(bus_id, "%04x:%02x:%02x.%d",dn->phb->global_number,
37                 dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn));
38
39         dbg("Enter rpaphp_find_pci_dev() full_name=%s bus_id=%s\n", 
40                 dn->full_name, bus_id);
41         
42         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
43                if (!strcmp(pci_name(dev), bus_id)) {
44                         retval_dev = dev;
45                         dbg("rpaphp_find_pci_dev(): found dev=%p\n\n", dev);
46                         break;
47                 }
48         }
49         return retval_dev;
50
51 }
52
53 EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
54
55 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
56 {
57         struct resource *res = &dev->resource[resource];
58         struct resource *root = pci_find_parent_resource(dev, res);
59         char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
60         int err;
61
62         err = -EINVAL;
63         if (root != NULL) {
64                 err = request_resource(root, res);
65         }
66
67         if (err) {
68                 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
69                     root ? "Address space collision on" :
70                     "No parent found for",
71                     resource, dtype, pci_name(dev), res->start, res->end);
72         }
73         return err;
74 }
75
76 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
77
78 static struct pci_dev *rpaphp_find_bridge_pdev(struct slot *slot)
79 {
80         return rpaphp_find_pci_dev(slot->dn);
81 }
82
83 static struct pci_dev *rpaphp_find_adapter_pdev(struct slot *slot)
84 {
85         return rpaphp_find_pci_dev(slot->dn->child);
86 }
87
88 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
89 {
90         int rc;
91         int setlevel;
92
93         rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
94
95         if (rc) {
96                 if (rc == NEED_POWER || rc == PWR_ONLY) {
97                         dbg("%s: slot must be power up to get sensor-state\n",
98                             __FUNCTION__);
99
100                         /* some slots have to be powered up 
101                          * before get-sensor will succeed.
102                          */
103                         rc = rtas_set_power_level(slot->power_domain, POWER_ON,
104                                                   &setlevel);
105                         if (rc) {
106                                 dbg("%s: power on slot[%s] failed rc=%d.\n",
107                                     __FUNCTION__, slot->name, rc);
108                         } else {
109                                 rc = rtas_get_sensor(DR_ENTITY_SENSE,
110                                                      slot->index, state);
111                         }
112                 } else if (rc == ERR_SENSE_USE)
113                         info("%s: slot is unusable\n", __FUNCTION__);
114                 else
115                         err("%s failed to get sensor state\n", __FUNCTION__);
116         }
117         return rc;
118 }
119
120 /*
121  * get_pci_adapter_status - get  the status of a slot
122  * 
123  * 0-- slot is empty
124  * 1-- adapter is configured
125  * 2-- adapter is not configured
126  * 3-- not valid
127  */
128 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
129 {
130         int state, rc;
131         *value = NOT_VALID;
132
133         rc = rpaphp_get_sensor_state(slot, &state);
134         if (rc)
135                 goto exit;
136         if (state == PRESENT) {
137                 if (!is_init)
138                         /* at run-time slot->state can be changed by */
139                         /* config/unconfig adapter                        */
140                         *value = slot->state;
141                 else {
142                         if (!slot->dn->child)
143                                 dbg("%s: %s is not valid OFDT node\n",
144                                     __FUNCTION__, slot->dn->full_name);
145                         else if (rpaphp_find_pci_dev(slot->dn->child))
146                                 *value = CONFIGURED;
147                         else {
148                                 dbg("%s: can't find pdev of adapter in slot[%s]\n", __FUNCTION__, slot->name);
149                                 *value = NOT_CONFIGURED;
150                         }
151                 }
152         } else if (state == EMPTY) {
153                 dbg("slot is empty\n");
154                 *value = state;
155         }
156
157       exit:
158         return rc;
159 }
160
161 /* Must be called before pci_bus_add_devices */
162 static void rpaphp_fixup_new_pci_devices(struct pci_bus *bus)
163 {
164         struct pci_dev *dev;
165
166         list_for_each_entry(dev, &bus->devices, bus_list) {
167                 /*
168                  * Skip already-present devices (which are on the
169                  * global device list.)
170                  */
171                 if (list_empty(&dev->global_list)) {
172                         int i;
173
174                         pcibios_fixup_device_resources(dev, bus);
175                         pci_read_irq_line(dev);
176                         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
177                                 struct resource *r = &dev->resource[i];
178                                 if (r->parent || !r->start || !r->flags)
179                                         continue;
180                                 rpaphp_claim_resource(dev, i);
181                         }
182                 }
183         }
184 }
185
186 static void rpaphp_pci_config_device(struct pci_bus *pci_bus, struct device_node *dn)
187 {
188         int num;
189
190         num = pci_scan_slot(pci_bus, PCI_DEVFN(PCI_SLOT(dn->devfn), 0));
191         if (num) {
192                 rpaphp_fixup_new_pci_devices(pci_bus);
193                 pci_bus_add_devices(pci_bus);
194         }
195         return;
196 }
197
198 static int rpaphp_pci_config_bridge(struct pci_dev *dev, struct device_node *dn);
199
200 /*****************************************************************************
201  rpaphp_pci_config_dn() will recursively configure all devices under the 
202  given slot->dn and return the dn's pci_dev.
203  *****************************************************************************/
204 static struct pci_dev *rpaphp_pci_config_dn(struct device_node *dn, struct pci_bus *bus)
205 {
206         struct device_node *local;
207         struct pci_dev *dev;
208
209         for (local = dn->child; local; local = local->sibling) {
210                 rpaphp_pci_config_device(bus, local);
211                 dev = rpaphp_find_pci_dev(local);
212                 if (!rpaphp_pci_config_bridge(dev, local))
213                         return NULL;
214         }
215
216         return dev;
217 }
218
219 static int rpaphp_pci_config_bridge(struct pci_dev *dev, struct device_node *dn)
220 {
221         if (dev && dn->child) { /* dn is a PCI bridge node */
222                 struct pci_bus *child;
223                 u8 sec_busno;
224
225                 /* get busno of downstream bus */
226                 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
227
228                 /* add to children of PCI bridge dev->bus */
229                 child = pci_add_new_bus(dev->bus, dev, sec_busno);
230                 if (!child) {
231                         err("%s: could not add second bus\n", __FUNCTION__);
232                         return 0;
233                 }
234                 sprintf(child->name, "PCI Bus #%02x", child->number);
235                 /* Fixup subordinate bridge bases and resureces */
236                 pcibios_fixup_bus(child);
237
238                 /* may need do more stuff here */
239                 rpaphp_pci_config_dn(dn, dev->subordinate);
240         }
241         return 1;
242 }
243
244 static struct pci_dev *rpaphp_config_pci_adapter(struct slot *slot)
245 {
246         struct pci_bus *pci_bus;
247         struct pci_dev *dev = NULL;
248
249         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
250
251         if (slot->bridge) {
252
253                 pci_bus = slot->bridge->subordinate;
254                 if (!pci_bus) {
255                         err("%s: can't find bus structure\n", __FUNCTION__);
256                         goto exit;
257                 }
258
259                 dev = rpaphp_pci_config_dn(slot->dn, pci_bus);
260                 eeh_add_device(dev);
261         } else {
262                 /* slot is not enabled */
263                 err("slot doesn't have pci_dev structure\n");
264                 dev = NULL;
265                 goto exit;
266         }
267
268       exit:
269         dbg("Exit %s: pci_dev %s\n", __FUNCTION__, dev ? "found" : "not found");
270         return dev;
271 }
272
273 int rpaphp_unconfig_pci_adapter(struct slot *slot)
274 {
275         int retval = 0;
276
277         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
278         if (!slot->dev.pci_dev) {
279                 info("%s: no card in slot[%s]\n", __FUNCTION__, slot->name);
280
281                 retval = -EINVAL;
282                 goto exit;
283         }
284         /* remove the device from the pci core */
285         eeh_remove_device(slot->dev.pci_dev);
286         pci_remove_bus_device(slot->dev.pci_dev);
287
288         slot->state = NOT_CONFIGURED;
289         info("%s: adapter in slot[%s] unconfigured.\n", __FUNCTION__,
290              slot->name);
291 exit:
292         dbg("Exit %s, rc=0x%x\n", __FUNCTION__, retval);
293         return retval;
294 }
295
296 static int setup_pci_hotplug_slot_info(struct slot *slot)
297 {
298         dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
299             __FUNCTION__);
300         rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
301         rpaphp_get_pci_adapter_status(slot, 1,
302                                       &slot->hotplug_slot->info->
303                                       adapter_status);
304         if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
305                 dbg("%s: NOT_VALID: skip dn->full_name=%s\n",
306                     __FUNCTION__, slot->dn->full_name);
307                 return (-1);
308         }
309         return (0);
310 }
311
312 static int setup_pci_slot(struct slot *slot)
313 {
314         slot->bridge = rpaphp_find_bridge_pdev(slot);
315         if (!slot->bridge) {    /* slot being added doesn't have pci_dev yet */
316                 dbg("%s: no pci_dev for bridge dn %s\n", __FUNCTION__, slot->name);
317                 dealloc_slot_struct(slot);
318                 return 1;
319         }
320
321         /* find slot's pci_dev if it's not empty */
322         if (slot->hotplug_slot->info->adapter_status == EMPTY) {
323                 slot->state = EMPTY;    /* slot is empty */
324                 slot->dev.pci_dev = NULL;
325         } else {
326                 /* slot is occupied */
327                 if (!(slot->dn->child)) {
328                         /* non-empty slot has to have child */
329                         err("%s: slot[%s]'s device_node doesn't have child for adapter\n", __FUNCTION__, slot->name);
330                         dealloc_slot_struct(slot);
331                         return 1;
332                 }
333                 slot->dev.pci_dev = rpaphp_find_adapter_pdev(slot);
334                 if (slot->dev.pci_dev) {
335                         slot->state = CONFIGURED;
336                 
337                 } else {
338                         /* DLPAR add as opposed to 
339                          * boot time */
340                         slot->state = NOT_CONFIGURED;
341                 }
342         }
343         return 0;
344 }
345
346 int register_pci_slot(struct slot *slot)
347 {
348         int rc = 1;
349
350         slot->dev_type = PCI_DEV;
351         if (setup_pci_hotplug_slot_info(slot))
352                 goto exit_rc;
353         if (setup_pci_slot(slot))
354                 goto exit_rc;
355         rc = register_slot(slot);
356       exit_rc:
357         if (rc)
358                 dealloc_slot_struct(slot);
359         return rc;
360 }
361
362 int rpaphp_enable_pci_slot(struct slot *slot)
363 {
364         int retval = 0, state;
365
366         retval = rpaphp_get_sensor_state(slot, &state);
367         if (retval)
368                 goto exit;
369         dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
370         /* if slot is not empty, enable the adapter */
371         if (state == PRESENT) {
372                 dbg("%s : slot[%s] is occupid.\n", __FUNCTION__, slot->name);
373                 if ((slot->dev.pci_dev =
374                      rpaphp_config_pci_adapter(slot)) != NULL) {
375                         slot->state = CONFIGURED;
376                         dbg("%s: PCI adapter %s in slot[%s] has been configured\n", 
377                                 __FUNCTION__, pci_name(slot->dev.pci_dev), slot->name);
378                 } else {
379                         slot->state = NOT_CONFIGURED;
380                         dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
381                             __FUNCTION__, slot->name);
382                 }
383         } else if (state == EMPTY) {
384                 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
385                 slot->state = EMPTY;
386         } else {
387                 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
388                     slot->name);
389                 slot->state = NOT_VALID;
390                 retval = -EINVAL;
391         }
392       exit:
393         if (slot->state != NOT_VALID)
394                 rpaphp_set_attention_status(slot, LED_ON);
395         else
396                 rpaphp_set_attention_status(slot, LED_ID);
397         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
398         return retval;
399 }