[SCSI] host state model update: mediate host add/remove race
[safe/jmp/linux-2.6] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_root")
41
42 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
43 #define ACPI_PCI_ROOT_HID               "PNP0A03"
44 #define ACPI_PCI_ROOT_DRIVER_NAME       "ACPI PCI Root Bridge Driver"
45 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
46
47 static int acpi_pci_root_add (struct acpi_device *device);
48 static int acpi_pci_root_remove (struct acpi_device *device, int type);
49 static int acpi_pci_root_start (struct acpi_device *device);
50
51 static struct acpi_driver acpi_pci_root_driver = {
52         .name =         ACPI_PCI_ROOT_DRIVER_NAME,
53         .class =        ACPI_PCI_ROOT_CLASS,
54         .ids =          ACPI_PCI_ROOT_HID,
55         .ops =          {
56                                 .add =    acpi_pci_root_add,
57                                 .remove = acpi_pci_root_remove,
58                                 .start =  acpi_pci_root_start,
59                         },
60 };
61
62 struct acpi_pci_root {
63         struct list_head        node;
64         acpi_handle             handle;
65         struct acpi_pci_id      id;
66         struct pci_bus          *bus;
67 };
68
69 static LIST_HEAD(acpi_pci_roots);
70
71 static struct acpi_pci_driver *sub_driver;
72
73 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
74 {
75         int n = 0;
76         struct list_head *entry;
77
78         struct acpi_pci_driver **pptr = &sub_driver;
79         while (*pptr)
80                 pptr = &(*pptr)->next;
81         *pptr = driver;
82
83         if (!driver->add)
84                 return 0;
85
86         list_for_each(entry, &acpi_pci_roots) {
87                 struct acpi_pci_root *root;
88                 root = list_entry(entry, struct acpi_pci_root, node);
89                 driver->add(root->handle);
90                 n++;
91         }
92
93         return n;
94 }
95 EXPORT_SYMBOL(acpi_pci_register_driver);
96
97 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
98 {
99         struct list_head *entry;
100
101         struct acpi_pci_driver **pptr = &sub_driver;
102         while (*pptr) {
103                 if (*pptr != driver)
104                         continue;
105                 *pptr = (*pptr)->next;
106                 break;
107         }
108
109         if (!driver->remove)
110                 return;
111
112         list_for_each(entry, &acpi_pci_roots) {
113                 struct acpi_pci_root *root;
114                 root = list_entry(entry, struct acpi_pci_root, node);
115                 driver->remove(root->handle);
116         }
117 }
118 EXPORT_SYMBOL(acpi_pci_unregister_driver);
119
120 static acpi_status
121 get_root_bridge_busnr_callback (struct acpi_resource *resource, void *data)
122 {
123         int *busnr = (int *)data;
124         struct acpi_resource_address64 address;
125
126         if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
127             resource->id != ACPI_RSTYPE_ADDRESS32 &&
128             resource->id != ACPI_RSTYPE_ADDRESS64)
129                 return AE_OK;
130
131         acpi_resource_to_address64(resource, &address);
132         if ((address.address_length > 0) && 
133            (address.resource_type == ACPI_BUS_NUMBER_RANGE))
134                 *busnr = address.min_address_range;
135
136         return AE_OK;
137 }
138
139 static acpi_status 
140 try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
141 {
142         acpi_status status;
143
144         *busnum = -1;
145         status = acpi_walk_resources(handle, METHOD_NAME__CRS, get_root_bridge_busnr_callback, busnum);
146         if (ACPI_FAILURE(status))
147                 return status;
148         /* Check if we really get a bus number from _CRS */
149         if (*busnum == -1)
150                 return AE_ERROR;
151         return AE_OK;
152 }
153
154 static int
155 acpi_pci_root_add (
156         struct acpi_device      *device)
157 {
158         int                     result = 0;
159         struct acpi_pci_root    *root = NULL;
160         struct acpi_pci_root    *tmp;
161         acpi_status             status = AE_OK;
162         unsigned long           value = 0;
163         acpi_handle             handle = NULL;
164
165         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
166
167         if (!device)
168                 return_VALUE(-EINVAL);
169
170         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
171         if (!root)
172                 return_VALUE(-ENOMEM);
173         memset(root, 0, sizeof(struct acpi_pci_root));
174         INIT_LIST_HEAD(&root->node);
175
176         root->handle = device->handle;
177         strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
178         strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
179         acpi_driver_data(device) = root;
180
181         /*
182          * TBD: Doesn't the bus driver automatically set this?
183          */
184         device->ops.bind = acpi_pci_bind;
185
186         /* 
187          * Segment
188          * -------
189          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
190          */
191         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
192                 &value);
193         switch (status) {
194         case AE_OK:
195                 root->id.segment = (u16) value;
196                 break;
197         case AE_NOT_FOUND:
198                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
199                         "Assuming segment 0 (no _SEG)\n"));
200                 root->id.segment = 0;
201                 break;
202         default:
203                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
204                 result = -ENODEV;
205                 goto end;
206         }
207
208         /* 
209          * Bus
210          * ---
211          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
212          */
213         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
214                 &value);
215         switch (status) {
216         case AE_OK:
217                 root->id.bus = (u16) value;
218                 break;
219         case AE_NOT_FOUND:
220                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
221                 root->id.bus = 0;
222                 break;
223         default:
224                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
225                 result = -ENODEV;
226                 goto end;
227         }
228
229         /* Some systems have wrong _BBN */
230         list_for_each_entry(tmp, &acpi_pci_roots, node) {
231                 if ((tmp->id.segment == root->id.segment)
232                                 && (tmp->id.bus == root->id.bus)) {
233                         int bus = 0;
234                         acpi_status status;
235
236                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
237                                 "Wrong _BBN value, please reboot and using option 'pci=noacpi'\n"));
238
239                         status = try_get_root_bridge_busnr(root->handle, &bus);
240                         if (ACPI_FAILURE(status))
241                                 break;
242                         if (bus != root->id.bus) {
243                                 printk(KERN_INFO PREFIX "PCI _CRS %d overrides _BBN 0\n", bus);
244                                 root->id.bus = bus;
245                         }
246                         break;
247                 }
248         }
249         /*
250          * Device & Function
251          * -----------------
252          * Obtained from _ADR (which has already been evaluated for us).
253          */
254         root->id.device = device->pnp.bus_address >> 16;
255         root->id.function = device->pnp.bus_address & 0xFFFF;
256
257         /*
258          * TBD: Need PCI interface for enumeration/configuration of roots.
259          */
260
261         /* TBD: Locking */
262         list_add_tail(&root->node, &acpi_pci_roots);
263
264         printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n", 
265                 acpi_device_name(device), acpi_device_bid(device),
266                 root->id.segment, root->id.bus);
267
268         /*
269          * Scan the Root Bridge
270          * --------------------
271          * Must do this prior to any attempt to bind the root device, as the
272          * PCI namespace does not get created until this call is made (and 
273          * thus the root bridge's pci_dev does not exist).
274          */
275         root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
276         if (!root->bus) {
277                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
278                         "Bus %04x:%02x not present in PCI namespace\n", 
279                         root->id.segment, root->id.bus));
280                 result = -ENODEV;
281                 goto end;
282         }
283
284         /*
285          * Attach ACPI-PCI Context
286          * -----------------------
287          * Thus binding the ACPI and PCI devices.
288          */
289         result = acpi_pci_bind_root(device, &root->id, root->bus);
290         if (result)
291                 goto end;
292
293         /*
294          * PCI Routing Table
295          * -----------------
296          * Evaluate and parse _PRT, if exists.
297          */
298         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
299         if (ACPI_SUCCESS(status))
300                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
301                         root->id.bus);
302
303 end:
304         if (result) {
305                 if (!list_empty(&root->node))
306                         list_del(&root->node);
307                 kfree(root);
308         }
309
310         return_VALUE(result);
311 }
312
313 static int
314 acpi_pci_root_start (
315         struct acpi_device      *device)
316 {
317         struct acpi_pci_root    *root;
318
319         ACPI_FUNCTION_TRACE("acpi_pci_root_start");
320
321         list_for_each_entry(root, &acpi_pci_roots, node) {
322                 if (root->handle == device->handle) {
323                         pci_bus_add_devices(root->bus);
324                         return_VALUE(0);
325                 }
326         }
327         return_VALUE(-ENODEV);
328 }
329
330 static int
331 acpi_pci_root_remove (
332         struct acpi_device      *device,
333         int                     type)
334 {
335         struct acpi_pci_root    *root = NULL;
336
337         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
338
339         if (!device || !acpi_driver_data(device))
340                 return_VALUE(-EINVAL);
341
342         root = (struct acpi_pci_root *) acpi_driver_data(device);
343
344         kfree(root);
345
346         return_VALUE(0);
347 }
348
349
350 static int __init acpi_pci_root_init (void)
351 {
352         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
353
354         if (acpi_pci_disabled)
355                 return_VALUE(0);
356
357         /* DEBUG:
358         acpi_dbg_layer = ACPI_PCI_COMPONENT;
359         acpi_dbg_level = 0xFFFFFFFF;
360          */
361
362         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
363                 return_VALUE(-ENODEV);
364
365         return_VALUE(0);
366 }
367
368 subsys_initcall(acpi_pci_root_init);
369