]> Pileus Git - ~andy/linux/blob - drivers/pci/hotplug/acpiphp_glue.c
Merge branch 'pci/yinghai-root-bus-hotplug' into next
[~andy/linux] / drivers / pci / hotplug / acpiphp_glue.c
1 /*
2  * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3  *
4  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6  * Copyright (C) 2002,2003 NEC Corporation
7  * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8  * Copyright (C) 2003-2005 Hewlett Packard
9  * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10  * Copyright (C) 2005 Intel Corporation
11  *
12  * All rights reserved.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or (at
17  * your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22  * NON INFRINGEMENT.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  * Send feedback to <kristen.c.accardi@intel.com>
30  *
31  */
32
33 /*
34  * Lifetime rules for pci_dev:
35  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36  *    when the bridge is scanned and it loses a refcount when the bridge
37  *    is removed.
38  *  - When a P2P bridge is present, we elevate the refcount on the subordinate
39  *    bus. It loses the refcount when the the driver unloads.
40  */
41
42 #include <linux/init.h>
43 #include <linux/module.h>
44
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.h>
50 #include <linux/slab.h>
51 #include <linux/acpi.h>
52
53 #include "../pci.h"
54 #include "acpiphp.h"
55
56 static LIST_HEAD(bridge_list);
57
58 #define MY_NAME "acpiphp_glue"
59
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
64
65 /* callback routine to check for the existence of a pci dock device */
66 static acpi_status
67 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68 {
69         int *count = (int *)context;
70
71         if (is_dock_device(handle)) {
72                 (*count)++;
73                 return AE_CTRL_TERMINATE;
74         } else {
75                 return AE_OK;
76         }
77 }
78
79 /*
80  * the _DCK method can do funny things... and sometimes not
81  * hah-hah funny.
82  *
83  * TBD - figure out a way to only call fixups for
84  * systems that require them.
85  */
86 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87         void *v)
88 {
89         struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90         struct pci_bus *bus = func->slot->bridge->pci_bus;
91         u32 buses;
92
93         if (!bus->self)
94                 return  NOTIFY_OK;
95
96         /* fixup bad _DCK function that rewrites
97          * secondary bridge on slot
98          */
99         pci_read_config_dword(bus->self,
100                         PCI_PRIMARY_BUS,
101                         &buses);
102
103         if (((buses >> 8) & 0xff) != bus->busn_res.start) {
104                 buses = (buses & 0xff000000)
105                         | ((unsigned int)(bus->primary)     <<  0)
106                         | ((unsigned int)(bus->busn_res.start)   <<  8)
107                         | ((unsigned int)(bus->busn_res.end) << 16);
108                 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109         }
110         return NOTIFY_OK;
111 }
112
113
114 static const struct acpi_dock_ops acpiphp_dock_ops = {
115         .handler = handle_hotplug_event_func,
116 };
117
118 /* Check whether the PCI device is managed by native PCIe hotplug driver */
119 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
120 {
121         u32 reg32;
122         acpi_handle tmp;
123         struct acpi_pci_root *root;
124
125         /* Check whether the PCIe port supports native PCIe hotplug */
126         if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
127                 return false;
128         if (!(reg32 & PCI_EXP_SLTCAP_HPC))
129                 return false;
130
131         /*
132          * Check whether native PCIe hotplug has been enabled for
133          * this PCIe hierarchy.
134          */
135         tmp = acpi_find_root_bridge_handle(pdev);
136         if (!tmp)
137                 return false;
138         root = acpi_pci_find_root(tmp);
139         if (!root)
140                 return false;
141         if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
142                 return false;
143
144         return true;
145 }
146
147 /* callback routine to register each ACPI PCI slot object */
148 static acpi_status
149 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
150 {
151         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
152         struct acpiphp_slot *slot;
153         struct acpiphp_func *newfunc;
154         acpi_handle tmp;
155         acpi_status status = AE_OK;
156         unsigned long long adr, sun;
157         int device, function, retval;
158         struct pci_bus *pbus = bridge->pci_bus;
159         struct pci_dev *pdev;
160
161         if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
162                 return AE_OK;
163
164         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
165         if (ACPI_FAILURE(status)) {
166                 warn("can't evaluate _ADR (%#x)\n", status);
167                 return AE_OK;
168         }
169
170         device = (adr >> 16) & 0xffff;
171         function = adr & 0xffff;
172
173         pdev = pbus->self;
174         if (pdev && device_is_managed_by_native_pciehp(pdev))
175                 return AE_OK;
176
177         newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
178         if (!newfunc)
179                 return AE_NO_MEMORY;
180
181         INIT_LIST_HEAD(&newfunc->sibling);
182         newfunc->handle = handle;
183         newfunc->function = function;
184
185         if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
186                 newfunc->flags = FUNC_HAS_EJ0;
187
188         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
189                 newfunc->flags |= FUNC_HAS_STA;
190
191         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
192                 newfunc->flags |= FUNC_HAS_PS0;
193
194         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
195                 newfunc->flags |= FUNC_HAS_PS3;
196
197         if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
198                 newfunc->flags |= FUNC_HAS_DCK;
199
200         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
201         if (ACPI_FAILURE(status)) {
202                 /*
203                  * use the count of the number of slots we've found
204                  * for the number of the slot
205                  */
206                 sun = bridge->nr_slots+1;
207         }
208
209         /* search for objects that share the same slot */
210         for (slot = bridge->slots; slot; slot = slot->next)
211                 if (slot->device == device) {
212                         if (slot->sun != sun)
213                                 warn("sibling found, but _SUN doesn't match!\n");
214                         break;
215                 }
216
217         if (!slot) {
218                 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
219                 if (!slot) {
220                         kfree(newfunc);
221                         return AE_NO_MEMORY;
222                 }
223
224                 slot->bridge = bridge;
225                 slot->device = device;
226                 slot->sun = sun;
227                 INIT_LIST_HEAD(&slot->funcs);
228                 mutex_init(&slot->crit_sect);
229
230                 slot->next = bridge->slots;
231                 bridge->slots = slot;
232
233                 bridge->nr_slots++;
234
235                 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
236                     slot->sun, pci_domain_nr(pbus), pbus->number, device);
237                 retval = acpiphp_register_hotplug_slot(slot);
238                 if (retval) {
239                         if (retval == -EBUSY)
240                                 warn("Slot %llu already registered by another "
241                                         "hotplug driver\n", slot->sun);
242                         else
243                                 warn("acpiphp_register_hotplug_slot failed "
244                                         "(err code = 0x%x)\n", retval);
245                         goto err_exit;
246                 }
247         }
248
249         newfunc->slot = slot;
250         list_add_tail(&newfunc->sibling, &slot->funcs);
251
252         pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
253         if (pdev) {
254                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
255                 pci_dev_put(pdev);
256         }
257
258         if (is_dock_device(handle)) {
259                 /* we don't want to call this device's _EJ0
260                  * because we want the dock notify handler
261                  * to call it after it calls _DCK
262                  */
263                 newfunc->flags &= ~FUNC_HAS_EJ0;
264                 if (register_hotplug_dock_device(handle,
265                         &acpiphp_dock_ops, newfunc))
266                         dbg("failed to register dock device\n");
267
268                 /* we need to be notified when dock events happen
269                  * outside of the hotplug operation, since we may
270                  * need to do fixups before we can hotplug.
271                  */
272                 newfunc->nb.notifier_call = post_dock_fixups;
273                 if (register_dock_notifier(&newfunc->nb))
274                         dbg("failed to register a dock notifier");
275         }
276
277         /* install notify handler */
278         if (!(newfunc->flags & FUNC_HAS_DCK)) {
279                 status = acpi_install_notify_handler(handle,
280                                              ACPI_SYSTEM_NOTIFY,
281                                              handle_hotplug_event_func,
282                                              newfunc);
283
284                 if (ACPI_FAILURE(status))
285                         err("failed to register interrupt notify handler\n");
286         } else
287                 status = AE_OK;
288
289         return status;
290
291  err_exit:
292         bridge->nr_slots--;
293         bridge->slots = slot->next;
294         kfree(slot);
295         kfree(newfunc);
296
297         return AE_OK;
298 }
299
300
301 /* see if it's worth looking at this bridge */
302 static int detect_ejectable_slots(acpi_handle handle)
303 {
304         int found = acpi_pci_detect_ejectable(handle);
305         if (!found) {
306                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
307                                     is_pci_dock_device, NULL, (void *)&found, NULL);
308         }
309         return found;
310 }
311
312 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313 static void init_bridge_misc(struct acpiphp_bridge *bridge)
314 {
315         acpi_status status;
316
317         /* must be added to the list prior to calling register_slot */
318         list_add(&bridge->list, &bridge_list);
319
320         /* register all slot objects under this bridge */
321         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
322                                      register_slot, NULL, bridge, NULL);
323         if (ACPI_FAILURE(status)) {
324                 list_del(&bridge->list);
325                 return;
326         }
327
328         /* install notify handler for P2P bridges */
329         if (!pci_is_root_bus(bridge->pci_bus)) {
330                 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
331                         status = acpi_remove_notify_handler(bridge->func->handle,
332                                                 ACPI_SYSTEM_NOTIFY,
333                                                 handle_hotplug_event_func);
334                         if (ACPI_FAILURE(status))
335                                 err("failed to remove notify handler\n");
336                 }
337                 status = acpi_install_notify_handler(bridge->handle,
338                                              ACPI_SYSTEM_NOTIFY,
339                                              handle_hotplug_event_bridge,
340                                              bridge);
341
342                 if (ACPI_FAILURE(status)) {
343                         err("failed to register interrupt notify handler\n");
344                 }
345         }
346 }
347
348
349 /* find acpiphp_func from acpiphp_bridge */
350 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
351 {
352         struct acpiphp_bridge *bridge;
353         struct acpiphp_slot *slot;
354         struct acpiphp_func *func;
355
356         list_for_each_entry(bridge, &bridge_list, list) {
357                 for (slot = bridge->slots; slot; slot = slot->next) {
358                         list_for_each_entry(func, &slot->funcs, sibling) {
359                                 if (func->handle == handle)
360                                         return func;
361                         }
362                 }
363         }
364
365         return NULL;
366 }
367
368
369 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
370 {
371         acpi_handle dummy_handle;
372         struct acpiphp_func *func;
373
374         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
375                                         "_EJ0", &dummy_handle))) {
376                 bridge->flags |= BRIDGE_HAS_EJ0;
377
378                 dbg("found ejectable p2p bridge\n");
379
380                 /* make link between PCI bridge and PCI function */
381                 func = acpiphp_bridge_handle_to_function(bridge->handle);
382                 if (!func)
383                         return;
384                 bridge->func = func;
385                 func->bridge = bridge;
386         }
387 }
388
389
390 /* allocate and initialize host bridge data structure */
391 static void add_host_bridge(struct acpi_pci_root *root)
392 {
393         struct acpiphp_bridge *bridge;
394         acpi_handle handle = root->device->handle;
395
396         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
397         if (bridge == NULL)
398                 return;
399
400         bridge->handle = handle;
401
402         bridge->pci_bus = root->bus;
403
404         init_bridge_misc(bridge);
405 }
406
407
408 /* allocate and initialize PCI-to-PCI bridge data structure */
409 static void add_p2p_bridge(acpi_handle *handle)
410 {
411         struct acpiphp_bridge *bridge;
412
413         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
414         if (bridge == NULL) {
415                 err("out of memory\n");
416                 return;
417         }
418
419         bridge->handle = handle;
420         config_p2p_bridge_flags(bridge);
421
422         bridge->pci_dev = acpi_get_pci_dev(handle);
423         bridge->pci_bus = bridge->pci_dev->subordinate;
424         if (!bridge->pci_bus) {
425                 err("This is not a PCI-to-PCI bridge!\n");
426                 goto err;
427         }
428
429         /*
430          * Grab a ref to the subordinate PCI bus in case the bus is
431          * removed via PCI core logical hotplug. The ref pins the bus
432          * (which we access during module unload).
433          */
434         get_device(&bridge->pci_bus->dev);
435
436         init_bridge_misc(bridge);
437         return;
438  err:
439         pci_dev_put(bridge->pci_dev);
440         kfree(bridge);
441         return;
442 }
443
444
445 /* callback routine to find P2P bridges */
446 static acpi_status
447 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
448 {
449         acpi_status status;
450         struct pci_dev *dev;
451
452         dev = acpi_get_pci_dev(handle);
453         if (!dev || !dev->subordinate)
454                 goto out;
455
456         /* check if this bridge has ejectable slots */
457         if ((detect_ejectable_slots(handle) > 0)) {
458                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
459                 add_p2p_bridge(handle);
460         }
461
462         /* search P2P bridges under this p2p bridge */
463         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
464                                      find_p2p_bridge, NULL, NULL, NULL);
465         if (ACPI_FAILURE(status))
466                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
467
468  out:
469         pci_dev_put(dev);
470         return AE_OK;
471 }
472
473
474 /* find hot-pluggable slots, and then find P2P bridge */
475 static int add_bridge(struct acpi_pci_root *root)
476 {
477         acpi_status status;
478         unsigned long long tmp;
479         acpi_handle dummy_handle;
480         acpi_handle handle = root->device->handle;
481
482         /* if the bridge doesn't have _STA, we assume it is always there */
483         status = acpi_get_handle(handle, "_STA", &dummy_handle);
484         if (ACPI_SUCCESS(status)) {
485                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
486                 if (ACPI_FAILURE(status)) {
487                         dbg("%s: _STA evaluation failure\n", __func__);
488                         return 0;
489                 }
490                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
491                         /* don't register this object */
492                         return 0;
493         }
494
495         /* check if this bridge has ejectable slots */
496         if (detect_ejectable_slots(handle) > 0) {
497                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
498                 add_host_bridge(root);
499         }
500
501         /* search P2P bridges under this host bridge */
502         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
503                                      find_p2p_bridge, NULL, NULL, NULL);
504
505         if (ACPI_FAILURE(status))
506                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
507
508         return 0;
509 }
510
511 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
512 {
513         struct acpiphp_bridge *bridge;
514
515         list_for_each_entry(bridge, &bridge_list, list)
516                 if (bridge->handle == handle)
517                         return bridge;
518
519         return NULL;
520 }
521
522 static void cleanup_bridge(struct acpiphp_bridge *bridge)
523 {
524         struct acpiphp_slot *slot, *next;
525         struct acpiphp_func *func, *tmp;
526         acpi_status status;
527         acpi_handle handle = bridge->handle;
528
529         if (!pci_is_root_bus(bridge->pci_bus)) {
530                 status = acpi_remove_notify_handler(handle,
531                                             ACPI_SYSTEM_NOTIFY,
532                                             handle_hotplug_event_bridge);
533                 if (ACPI_FAILURE(status))
534                         err("failed to remove notify handler\n");
535         }
536
537         if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
538                 status = acpi_install_notify_handler(bridge->func->handle,
539                                                 ACPI_SYSTEM_NOTIFY,
540                                                 handle_hotplug_event_func,
541                                                 bridge->func);
542                 if (ACPI_FAILURE(status))
543                         err("failed to install interrupt notify handler\n");
544         }
545
546         slot = bridge->slots;
547         while (slot) {
548                 next = slot->next;
549                 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
550                         if (is_dock_device(func->handle)) {
551                                 unregister_hotplug_dock_device(func->handle);
552                                 unregister_dock_notifier(&func->nb);
553                         }
554                         if (!(func->flags & FUNC_HAS_DCK)) {
555                                 status = acpi_remove_notify_handler(func->handle,
556                                                 ACPI_SYSTEM_NOTIFY,
557                                                 handle_hotplug_event_func);
558                                 if (ACPI_FAILURE(status))
559                                         err("failed to remove notify handler\n");
560                         }
561                         list_del(&func->sibling);
562                         kfree(func);
563                 }
564                 acpiphp_unregister_hotplug_slot(slot);
565                 list_del(&slot->funcs);
566                 kfree(slot);
567                 slot = next;
568         }
569
570         /*
571          * Only P2P bridges have a pci_dev
572          */
573         if (bridge->pci_dev)
574                 put_device(&bridge->pci_bus->dev);
575
576         pci_dev_put(bridge->pci_dev);
577         list_del(&bridge->list);
578         kfree(bridge);
579 }
580
581 static acpi_status
582 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
583 {
584         struct acpiphp_bridge *bridge;
585
586         /* cleanup p2p bridges under this P2P bridge
587            in a depth-first manner */
588         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
589                                 cleanup_p2p_bridge, NULL, NULL, NULL);
590
591         bridge = acpiphp_handle_to_bridge(handle);
592         if (bridge)
593                 cleanup_bridge(bridge);
594
595         return AE_OK;
596 }
597
598 static void remove_bridge(struct acpi_pci_root *root)
599 {
600         struct acpiphp_bridge *bridge;
601         acpi_handle handle = root->device->handle;
602
603         /* cleanup p2p bridges under this host bridge
604            in a depth-first manner */
605         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
606                                 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
607
608         /*
609          * On root bridges with hotplug slots directly underneath (ie,
610          * no p2p bridge between), we call cleanup_bridge(). 
611          *
612          * The else clause cleans up root bridges that either had no
613          * hotplug slots at all, or had a p2p bridge underneath.
614          */
615         bridge = acpiphp_handle_to_bridge(handle);
616         if (bridge)
617                 cleanup_bridge(bridge);
618 }
619
620 static int power_on_slot(struct acpiphp_slot *slot)
621 {
622         acpi_status status;
623         struct acpiphp_func *func;
624         int retval = 0;
625
626         /* if already enabled, just skip */
627         if (slot->flags & SLOT_POWEREDON)
628                 goto err_exit;
629
630         list_for_each_entry(func, &slot->funcs, sibling) {
631                 if (func->flags & FUNC_HAS_PS0) {
632                         dbg("%s: executing _PS0\n", __func__);
633                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
634                         if (ACPI_FAILURE(status)) {
635                                 warn("%s: _PS0 failed\n", __func__);
636                                 retval = -1;
637                                 goto err_exit;
638                         } else
639                                 break;
640                 }
641         }
642
643         /* TBD: evaluate _STA to check if the slot is enabled */
644
645         slot->flags |= SLOT_POWEREDON;
646
647  err_exit:
648         return retval;
649 }
650
651
652 static int power_off_slot(struct acpiphp_slot *slot)
653 {
654         acpi_status status;
655         struct acpiphp_func *func;
656
657         int retval = 0;
658
659         /* if already disabled, just skip */
660         if ((slot->flags & SLOT_POWEREDON) == 0)
661                 goto err_exit;
662
663         list_for_each_entry(func, &slot->funcs, sibling) {
664                 if (func->flags & FUNC_HAS_PS3) {
665                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
666                         if (ACPI_FAILURE(status)) {
667                                 warn("%s: _PS3 failed\n", __func__);
668                                 retval = -1;
669                                 goto err_exit;
670                         } else
671                                 break;
672                 }
673         }
674
675         /* TBD: evaluate _STA to check if the slot is disabled */
676
677         slot->flags &= (~SLOT_POWEREDON);
678
679  err_exit:
680         return retval;
681 }
682
683
684
685 /**
686  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
687  * @bus: bus to start search with
688  */
689 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
690 {
691         struct list_head *tmp;
692         unsigned char max, n;
693
694         /*
695          * pci_bus_max_busnr will return the highest
696          * reserved busnr for all these children.
697          * that is equivalent to the bus->subordinate
698          * value.  We don't want to use the parent's
699          * bus->subordinate value because it could have
700          * padding in it.
701          */
702         max = bus->busn_res.start;
703
704         list_for_each(tmp, &bus->children) {
705                 n = pci_bus_max_busnr(pci_bus_b(tmp));
706                 if (n > max)
707                         max = n;
708         }
709         return max;
710 }
711
712
713 /**
714  * acpiphp_bus_add - add a new bus to acpi subsystem
715  * @func: acpiphp_func of the bridge
716  */
717 static int acpiphp_bus_add(struct acpiphp_func *func)
718 {
719         struct acpi_device *device;
720         int ret_val;
721
722         if (!acpi_bus_get_device(func->handle, &device)) {
723                 dbg("bus exists... trim\n");
724                 /* this shouldn't be in here, so remove
725                  * the bus then re-add it...
726                  */
727                 ret_val = acpi_bus_trim(device);
728                 dbg("acpi_bus_trim return %x\n", ret_val);
729         }
730
731         ret_val = acpi_bus_scan(func->handle);
732         if (!ret_val)
733                 ret_val = acpi_bus_get_device(func->handle, &device);
734
735         if (ret_val)
736                 dbg("error adding bus, %x\n", -ret_val);
737
738         return ret_val;
739 }
740
741
742 /**
743  * acpiphp_bus_trim - trim a bus from acpi subsystem
744  * @handle: handle to acpi namespace
745  */
746 static int acpiphp_bus_trim(acpi_handle handle)
747 {
748         struct acpi_device *device;
749         int retval;
750
751         retval = acpi_bus_get_device(handle, &device);
752         if (retval) {
753                 dbg("acpi_device not found\n");
754                 return retval;
755         }
756
757         retval = acpi_bus_trim(device);
758         if (retval)
759                 err("cannot remove from acpi list\n");
760
761         return retval;
762 }
763
764 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
765 {
766         struct acpiphp_func *func;
767         union acpi_object params[2];
768         struct acpi_object_list arg_list;
769
770         list_for_each_entry(func, &slot->funcs, sibling) {
771                 arg_list.count = 2;
772                 arg_list.pointer = params;
773                 params[0].type = ACPI_TYPE_INTEGER;
774                 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
775                 params[1].type = ACPI_TYPE_INTEGER;
776                 params[1].integer.value = 1;
777                 /* _REG is optional, we don't care about if there is failure */
778                 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
779         }
780 }
781
782 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
783 {
784         struct acpiphp_func *func;
785
786         if (!dev->subordinate)
787                 return;
788
789         /* quirk, or pcie could set it already */
790         if (dev->is_hotplug_bridge)
791                 return;
792
793         if (PCI_SLOT(dev->devfn) != slot->device)
794                 return;
795
796         list_for_each_entry(func, &slot->funcs, sibling) {
797                 if (PCI_FUNC(dev->devfn) == func->function) {
798                         /* check if this bridge has ejectable slots */
799                         if ((detect_ejectable_slots(func->handle) > 0))
800                                 dev->is_hotplug_bridge = 1;
801                         break;
802                 }
803         }
804 }
805 /**
806  * enable_device - enable, configure a slot
807  * @slot: slot to be enabled
808  *
809  * This function should be called per *physical slot*,
810  * not per each slot object in ACPI namespace.
811  */
812 static int __ref enable_device(struct acpiphp_slot *slot)
813 {
814         struct pci_dev *dev;
815         struct pci_bus *bus = slot->bridge->pci_bus;
816         struct acpiphp_func *func;
817         int retval = 0;
818         int num, max, pass;
819         acpi_status status;
820
821         if (slot->flags & SLOT_ENABLED)
822                 goto err_exit;
823
824         list_for_each_entry(func, &slot->funcs, sibling)
825                 acpiphp_bus_add(func);
826
827         num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
828         if (num == 0) {
829                 /* Maybe only part of funcs are added. */
830                 dbg("No new device found\n");
831                 goto err_exit;
832         }
833
834         max = acpiphp_max_busnr(bus);
835         for (pass = 0; pass < 2; pass++) {
836                 list_for_each_entry(dev, &bus->devices, bus_list) {
837                         if (PCI_SLOT(dev->devfn) != slot->device)
838                                 continue;
839                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
840                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
841                                 max = pci_scan_bridge(bus, dev, max, pass);
842                                 if (pass && dev->subordinate) {
843                                         check_hotplug_bridge(slot, dev);
844                                         pci_bus_size_bridges(dev->subordinate);
845                                 }
846                         }
847                 }
848         }
849
850         pci_bus_assign_resources(bus);
851         acpiphp_sanitize_bus(bus);
852         acpiphp_set_hpp_values(bus);
853         acpiphp_set_acpi_region(slot);
854         pci_enable_bridges(bus);
855
856         list_for_each_entry(dev, &bus->devices, bus_list) {
857                 /* Assume that newly added devices are powered on already. */
858                 if (!dev->is_added)
859                         dev->current_state = PCI_D0;
860         }
861
862         pci_bus_add_devices(bus);
863
864         slot->flags |= SLOT_ENABLED;
865         list_for_each_entry(func, &slot->funcs, sibling) {
866                 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
867                                                   func->function));
868                 if (!dev) {
869                         /* Do not set SLOT_ENABLED flag if some funcs
870                            are not added. */
871                         slot->flags &= (~SLOT_ENABLED);
872                         continue;
873                 }
874
875                 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
876                     dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
877                         pci_dev_put(dev);
878                         continue;
879                 }
880
881                 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
882                 if (ACPI_FAILURE(status))
883                         warn("find_p2p_bridge failed (error code = 0x%x)\n",
884                                 status);
885                 pci_dev_put(dev);
886         }
887
888
889  err_exit:
890         return retval;
891 }
892
893 /* return first device in slot, acquiring a reference on it */
894 static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
895 {
896         struct pci_bus *bus = slot->bridge->pci_bus;
897         struct pci_dev *dev;
898         struct pci_dev *ret = NULL;
899
900         down_read(&pci_bus_sem);
901         list_for_each_entry(dev, &bus->devices, bus_list)
902                 if (PCI_SLOT(dev->devfn) == slot->device) {
903                         ret = pci_dev_get(dev);
904                         break;
905                 }
906         up_read(&pci_bus_sem);
907
908         return ret;
909 }
910
911 /**
912  * disable_device - disable a slot
913  * @slot: ACPI PHP slot
914  */
915 static int disable_device(struct acpiphp_slot *slot)
916 {
917         struct acpiphp_func *func;
918         struct pci_dev *pdev;
919         struct pci_bus *bus = slot->bridge->pci_bus;
920
921         /* The slot will be enabled when func 0 is added, so check
922            func 0 before disable the slot. */
923         pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
924         if (!pdev)
925                 goto err_exit;
926         pci_dev_put(pdev);
927
928         list_for_each_entry(func, &slot->funcs, sibling) {
929                 if (func->bridge) {
930                         /* cleanup p2p bridges under this P2P bridge */
931                         cleanup_p2p_bridge(func->bridge->handle,
932                                                 (u32)1, NULL, NULL);
933                         func->bridge = NULL;
934                 }
935         }
936
937         /*
938          * enable_device() enumerates all functions in this device via
939          * pci_scan_slot(), whether they have associated ACPI hotplug
940          * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
941          * here.
942          */
943         while ((pdev = dev_in_slot(slot))) {
944                 pci_stop_and_remove_bus_device(pdev);
945                 pci_dev_put(pdev);
946         }
947
948         list_for_each_entry(func, &slot->funcs, sibling) {
949                 acpiphp_bus_trim(func->handle);
950         }
951
952         slot->flags &= (~SLOT_ENABLED);
953
954 err_exit:
955         return 0;
956 }
957
958
959 /**
960  * get_slot_status - get ACPI slot status
961  * @slot: ACPI PHP slot
962  *
963  * If a slot has _STA for each function and if any one of them
964  * returned non-zero status, return it.
965  *
966  * If a slot doesn't have _STA and if any one of its functions'
967  * configuration space is configured, return 0x0f as a _STA.
968  *
969  * Otherwise return 0.
970  */
971 static unsigned int get_slot_status(struct acpiphp_slot *slot)
972 {
973         acpi_status status;
974         unsigned long long sta = 0;
975         u32 dvid;
976         struct acpiphp_func *func;
977
978         list_for_each_entry(func, &slot->funcs, sibling) {
979                 if (func->flags & FUNC_HAS_STA) {
980                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
981                         if (ACPI_SUCCESS(status) && sta)
982                                 break;
983                 } else {
984                         pci_bus_read_config_dword(slot->bridge->pci_bus,
985                                                   PCI_DEVFN(slot->device,
986                                                             func->function),
987                                                   PCI_VENDOR_ID, &dvid);
988                         if (dvid != 0xffffffff) {
989                                 sta = ACPI_STA_ALL;
990                                 break;
991                         }
992                 }
993         }
994
995         return (unsigned int)sta;
996 }
997
998 /**
999  * acpiphp_eject_slot - physically eject the slot
1000  * @slot: ACPI PHP slot
1001  */
1002 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1003 {
1004         acpi_status status;
1005         struct acpiphp_func *func;
1006         struct acpi_object_list arg_list;
1007         union acpi_object arg;
1008
1009         list_for_each_entry(func, &slot->funcs, sibling) {
1010                 /* We don't want to call _EJ0 on non-existing functions. */
1011                 if ((func->flags & FUNC_HAS_EJ0)) {
1012                         /* _EJ0 method take one argument */
1013                         arg_list.count = 1;
1014                         arg_list.pointer = &arg;
1015                         arg.type = ACPI_TYPE_INTEGER;
1016                         arg.integer.value = 1;
1017
1018                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1019                         if (ACPI_FAILURE(status)) {
1020                                 warn("%s: _EJ0 failed\n", __func__);
1021                                 return -1;
1022                         } else
1023                                 break;
1024                 }
1025         }
1026         return 0;
1027 }
1028
1029 /**
1030  * acpiphp_check_bridge - re-enumerate devices
1031  * @bridge: where to begin re-enumeration
1032  *
1033  * Iterate over all slots under this bridge and make sure that if a
1034  * card is present they are enabled, and if not they are disabled.
1035  */
1036 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1037 {
1038         struct acpiphp_slot *slot;
1039         int retval = 0;
1040         int enabled, disabled;
1041
1042         enabled = disabled = 0;
1043
1044         for (slot = bridge->slots; slot; slot = slot->next) {
1045                 unsigned int status = get_slot_status(slot);
1046                 if (slot->flags & SLOT_ENABLED) {
1047                         if (status == ACPI_STA_ALL)
1048                                 continue;
1049                         retval = acpiphp_disable_slot(slot);
1050                         if (retval) {
1051                                 err("Error occurred in disabling\n");
1052                                 goto err_exit;
1053                         } else {
1054                                 acpiphp_eject_slot(slot);
1055                         }
1056                         disabled++;
1057                 } else {
1058                         if (status != ACPI_STA_ALL)
1059                                 continue;
1060                         retval = acpiphp_enable_slot(slot);
1061                         if (retval) {
1062                                 err("Error occurred in enabling\n");
1063                                 goto err_exit;
1064                         }
1065                         enabled++;
1066                 }
1067         }
1068
1069         dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1070
1071  err_exit:
1072         return retval;
1073 }
1074
1075 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1076 {
1077         struct pci_dev *dev;
1078
1079         list_for_each_entry(dev, &bus->devices, bus_list)
1080                 pci_configure_slot(dev);
1081 }
1082
1083 /*
1084  * Remove devices for which we could not assign resources, call
1085  * arch specific code to fix-up the bus
1086  */
1087 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1088 {
1089         struct pci_dev *dev;
1090         int i;
1091         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1092
1093         list_for_each_entry(dev, &bus->devices, bus_list) {
1094                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1095                         struct resource *res = &dev->resource[i];
1096                         if ((res->flags & type_mask) && !res->start &&
1097                                         res->end) {
1098                                 /* Could not assign a required resources
1099                                  * for this device, remove it */
1100                                 pci_stop_and_remove_bus_device(dev);
1101                                 break;
1102                         }
1103                 }
1104         }
1105 }
1106
1107 /*
1108  * ACPI event handlers
1109  */
1110
1111 static acpi_status
1112 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1113 {
1114         struct acpiphp_bridge *bridge;
1115         char objname[64];
1116         struct acpi_buffer buffer = { .length = sizeof(objname),
1117                                       .pointer = objname };
1118
1119         bridge = acpiphp_handle_to_bridge(handle);
1120         if (bridge) {
1121                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1122                 dbg("%s: re-enumerating slots under %s\n",
1123                         __func__, objname);
1124                 acpiphp_check_bridge(bridge);
1125         }
1126         return AE_OK ;
1127 }
1128
1129 static void _handle_hotplug_event_bridge(struct work_struct *work)
1130 {
1131         struct acpiphp_bridge *bridge;
1132         char objname[64];
1133         struct acpi_buffer buffer = { .length = sizeof(objname),
1134                                       .pointer = objname };
1135         struct acpi_hp_work *hp_work;
1136         acpi_handle handle;
1137         u32 type;
1138
1139         hp_work = container_of(work, struct acpi_hp_work, work);
1140         handle = hp_work->handle;
1141         type = hp_work->type;
1142         bridge = (struct acpiphp_bridge *)hp_work->context;
1143
1144         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1145
1146         switch (type) {
1147         case ACPI_NOTIFY_BUS_CHECK:
1148                 /* bus re-enumerate */
1149                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1150                 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1151                 acpiphp_check_bridge(bridge);
1152                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1153                         ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1154                 break;
1155
1156         case ACPI_NOTIFY_DEVICE_CHECK:
1157                 /* device check */
1158                 dbg("%s: Device check notify on %s\n", __func__, objname);
1159                 acpiphp_check_bridge(bridge);
1160                 break;
1161
1162         case ACPI_NOTIFY_DEVICE_WAKE:
1163                 /* wake event */
1164                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1165                 break;
1166
1167         case ACPI_NOTIFY_EJECT_REQUEST:
1168                 /* request device eject */
1169                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1170                 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1171                         struct acpiphp_slot *slot;
1172                         slot = bridge->func->slot;
1173                         if (!acpiphp_disable_slot(slot))
1174                                 acpiphp_eject_slot(slot);
1175                 }
1176                 break;
1177
1178         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1179                 printk(KERN_ERR "Device %s cannot be configured due"
1180                                 " to a frequency mismatch\n", objname);
1181                 break;
1182
1183         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1184                 printk(KERN_ERR "Device %s cannot be configured due"
1185                                 " to a bus mode mismatch\n", objname);
1186                 break;
1187
1188         case ACPI_NOTIFY_POWER_FAULT:
1189                 printk(KERN_ERR "Device %s has suffered a power fault\n",
1190                                 objname);
1191                 break;
1192
1193         default:
1194                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1195                 break;
1196         }
1197
1198         kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1199 }
1200
1201 /**
1202  * handle_hotplug_event_bridge - handle ACPI event on bridges
1203  * @handle: Notify()'ed acpi_handle
1204  * @type: Notify code
1205  * @context: pointer to acpiphp_bridge structure
1206  *
1207  * Handles ACPI event notification on {host,p2p} bridges.
1208  */
1209 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1210                                         void *context)
1211 {
1212         /*
1213          * Currently the code adds all hotplug events to the kacpid_wq
1214          * queue when it should add hotplug events to the kacpi_hotplug_wq.
1215          * The proper way to fix this is to reorganize the code so that
1216          * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1217          * For now just re-add this work to the kacpi_hotplug_wq so we
1218          * don't deadlock on hotplug actions.
1219          */
1220         alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1221 }
1222
1223 static void _handle_hotplug_event_func(struct work_struct *work)
1224 {
1225         struct acpiphp_func *func;
1226         char objname[64];
1227         struct acpi_buffer buffer = { .length = sizeof(objname),
1228                                       .pointer = objname };
1229         struct acpi_hp_work *hp_work;
1230         acpi_handle handle;
1231         u32 type;
1232
1233         hp_work = container_of(work, struct acpi_hp_work, work);
1234         handle = hp_work->handle;
1235         type = hp_work->type;
1236         func = (struct acpiphp_func *)hp_work->context;
1237
1238         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1239
1240         switch (type) {
1241         case ACPI_NOTIFY_BUS_CHECK:
1242                 /* bus re-enumerate */
1243                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1244                 acpiphp_enable_slot(func->slot);
1245                 break;
1246
1247         case ACPI_NOTIFY_DEVICE_CHECK:
1248                 /* device check : re-enumerate from parent bus */
1249                 dbg("%s: Device check notify on %s\n", __func__, objname);
1250                 acpiphp_check_bridge(func->slot->bridge);
1251                 break;
1252
1253         case ACPI_NOTIFY_DEVICE_WAKE:
1254                 /* wake event */
1255                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1256                 break;
1257
1258         case ACPI_NOTIFY_EJECT_REQUEST:
1259                 /* request device eject */
1260                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1261                 if (!(acpiphp_disable_slot(func->slot)))
1262                         acpiphp_eject_slot(func->slot);
1263                 break;
1264
1265         default:
1266                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1267                 break;
1268         }
1269
1270         kfree(hp_work); /* allocated in handle_hotplug_event_func */
1271 }
1272
1273 /**
1274  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1275  * @handle: Notify()'ed acpi_handle
1276  * @type: Notify code
1277  * @context: pointer to acpiphp_func structure
1278  *
1279  * Handles ACPI event notification on slots.
1280  */
1281 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1282                                       void *context)
1283 {
1284         /*
1285          * Currently the code adds all hotplug events to the kacpid_wq
1286          * queue when it should add hotplug events to the kacpi_hotplug_wq.
1287          * The proper way to fix this is to reorganize the code so that
1288          * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1289          * For now just re-add this work to the kacpi_hotplug_wq so we
1290          * don't deadlock on hotplug actions.
1291          */
1292         alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1293 }
1294
1295 static struct acpi_pci_driver acpi_pci_hp_driver = {
1296         .add =          add_bridge,
1297         .remove =       remove_bridge,
1298 };
1299
1300 /**
1301  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1302  */
1303 int __init acpiphp_glue_init(void)
1304 {
1305         acpi_pci_register_driver(&acpi_pci_hp_driver);
1306
1307         return 0;
1308 }
1309
1310
1311 /**
1312  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1313  *
1314  * This function frees all data allocated in acpiphp_glue_init().
1315  */
1316 void  acpiphp_glue_exit(void)
1317 {
1318         acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1319 }
1320
1321 /**
1322  * acpiphp_enable_slot - power on slot
1323  * @slot: ACPI PHP slot
1324  */
1325 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1326 {
1327         int retval;
1328
1329         mutex_lock(&slot->crit_sect);
1330
1331         /* wake up all functions */
1332         retval = power_on_slot(slot);
1333         if (retval)
1334                 goto err_exit;
1335
1336         if (get_slot_status(slot) == ACPI_STA_ALL) {
1337                 /* configure all functions */
1338                 retval = enable_device(slot);
1339                 if (retval)
1340                         power_off_slot(slot);
1341         } else {
1342                 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1343                 power_off_slot(slot);
1344         }
1345
1346  err_exit:
1347         mutex_unlock(&slot->crit_sect);
1348         return retval;
1349 }
1350
1351 /**
1352  * acpiphp_disable_slot - power off slot
1353  * @slot: ACPI PHP slot
1354  */
1355 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1356 {
1357         int retval = 0;
1358
1359         mutex_lock(&slot->crit_sect);
1360
1361         /* unconfigure all functions */
1362         retval = disable_device(slot);
1363         if (retval)
1364                 goto err_exit;
1365
1366         /* power off all functions */
1367         retval = power_off_slot(slot);
1368         if (retval)
1369                 goto err_exit;
1370
1371  err_exit:
1372         mutex_unlock(&slot->crit_sect);
1373         return retval;
1374 }
1375
1376
1377 /*
1378  * slot enabled:  1
1379  * slot disabled: 0
1380  */
1381 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1382 {
1383         return (slot->flags & SLOT_POWEREDON);
1384 }
1385
1386
1387 /*
1388  * latch   open:  1
1389  * latch closed:  0
1390  */
1391 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1392 {
1393         unsigned int sta;
1394
1395         sta = get_slot_status(slot);
1396
1397         return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1398 }
1399
1400
1401 /*
1402  * adapter presence : 1
1403  *          absence : 0
1404  */
1405 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1406 {
1407         unsigned int sta;
1408
1409         sta = get_slot_status(slot);
1410
1411         return (sta == 0) ? 0 : 1;
1412 }