ACPI / scan: Drop the second argument of acpi_bus_trim()
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / pci / hotplug / sgi_hotplug.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
7  *
8  * This work was based on the 2.4/2.6 kernel development by Dick Reigner.
9  * Work to add BIOS PROM support was completed by Mike Habeck.
10  */
11
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/pci_hotplug.h>
17 #include <linux/proc_fs.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/mutex.h>
21
22 #include <asm/sn/addrs.h>
23 #include <asm/sn/geo.h>
24 #include <asm/sn/l1.h>
25 #include <asm/sn/module.h>
26 #include <asm/sn/pcibr_provider.h>
27 #include <asm/sn/pcibus_provider_defs.h>
28 #include <asm/sn/pcidev.h>
29 #include <asm/sn/sn_feature_sets.h>
30 #include <asm/sn/sn_sal.h>
31 #include <asm/sn/types.h>
32 #include <linux/acpi.h>
33 #include <asm/sn/acpi.h>
34
35 #include "../pci.h"
36
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
39 MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
40
41
42 /* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
43 #define PCI_SLOT_ALREADY_UP             2       /* slot already up */
44 #define PCI_SLOT_ALREADY_DOWN           3       /* slot already down */
45 #define PCI_L1_ERR                      7       /* L1 console command error */
46 #define PCI_EMPTY_33MHZ                 15      /* empty 33 MHz bus */
47
48
49 #define PCIIO_ASIC_TYPE_TIOCA           4
50 #define PCI_L1_QSIZE                    128     /* our L1 message buffer size */
51 #define SN_MAX_HP_SLOTS                 32      /* max hotplug slots */
52 #define SN_SLOT_NAME_SIZE               33      /* size of name string */
53
54 /* internal list head */
55 static struct list_head sn_hp_list;
56
57 /* hotplug_slot struct's private pointer */
58 struct slot {
59         int device_num;
60         struct pci_bus *pci_bus;
61         /* this struct for glue internal only */
62         struct hotplug_slot *hotplug_slot;
63         struct list_head hp_list;
64         char physical_path[SN_SLOT_NAME_SIZE];
65 };
66
67 struct pcibr_slot_enable_resp {
68         int resp_sub_errno;
69         char resp_l1_msg[PCI_L1_QSIZE + 1];
70 };
71
72 struct pcibr_slot_disable_resp {
73         int resp_sub_errno;
74         char resp_l1_msg[PCI_L1_QSIZE + 1];
75 };
76
77 enum sn_pci_req_e {
78         PCI_REQ_SLOT_ELIGIBLE,
79         PCI_REQ_SLOT_DISABLE
80 };
81
82 static int enable_slot(struct hotplug_slot *slot);
83 static int disable_slot(struct hotplug_slot *slot);
84 static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
85
86 static struct hotplug_slot_ops sn_hotplug_slot_ops = {
87         .enable_slot            = enable_slot,
88         .disable_slot           = disable_slot,
89         .get_power_status       = get_power_status,
90 };
91
92 static DEFINE_MUTEX(sn_hotplug_mutex);
93
94 static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
95 {
96         int retval = -ENOENT;
97         struct slot *slot = pci_slot->hotplug->private;
98
99         if (!slot)
100                 return retval;
101
102         retval = sprintf (buf, "%s\n", slot->physical_path);
103         return retval;
104 }
105
106 static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
107
108 static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
109 {
110         struct pcibus_info *pcibus_info;
111         u16 busnum, segment, ioboard_type;
112
113         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
114
115         /* Check to see if this is a valid slot on 'pci_bus' */
116         if (!(pcibus_info->pbi_valid_devices & (1 << device)))
117                 return -EPERM;
118
119         ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
120         busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
121         segment = pci_domain_nr(pci_bus) & 0xf;
122
123         /* Do not allow hotplug operations on base I/O cards */
124         if ((ioboard_type == L1_BRICKTYPE_IX ||
125              ioboard_type == L1_BRICKTYPE_IA) &&
126             (segment == 1 && busnum == 0 && device != 1))
127                 return -EPERM;
128
129         return 1;
130 }
131
132 static int sn_pci_bus_valid(struct pci_bus *pci_bus)
133 {
134         struct pcibus_info *pcibus_info;
135         u32 asic_type;
136         u16 ioboard_type;
137
138         /* Don't register slots hanging off the TIOCA bus */
139         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
140         asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
141         if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
142                 return -EPERM;
143
144         /* Only register slots in I/O Bricks that support hotplug */
145         ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
146         switch (ioboard_type) {
147                 case L1_BRICKTYPE_IX:
148                 case L1_BRICKTYPE_PX:
149                 case L1_BRICKTYPE_IA:
150                 case L1_BRICKTYPE_PA:
151                 case L1_BOARDTYPE_PCIX3SLOT:
152                         return 1;
153                         break;
154                 default:
155                         return -EPERM;
156                         break;
157         }
158
159         return -EIO;
160 }
161
162 static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
163                                     struct pci_bus *pci_bus, int device,
164                                     char *name)
165 {
166         struct pcibus_info *pcibus_info;
167         struct slot *slot;
168
169         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
170
171         slot = kzalloc(sizeof(*slot), GFP_KERNEL);
172         if (!slot)
173                 return -ENOMEM;
174         bss_hotplug_slot->private = slot;
175
176         slot->device_num = device;
177         slot->pci_bus = pci_bus;
178         sprintf(name, "%04x:%02x:%02x",
179                 pci_domain_nr(pci_bus),
180                 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
181                 device + 1);
182
183         sn_generate_path(pci_bus, slot->physical_path);
184
185         slot->hotplug_slot = bss_hotplug_slot;
186         list_add(&slot->hp_list, &sn_hp_list);
187
188         return 0;
189 }
190
191 static struct hotplug_slot * sn_hp_destroy(void)
192 {
193         struct slot *slot;
194         struct pci_slot *pci_slot;
195         struct hotplug_slot *bss_hotplug_slot = NULL;
196
197         list_for_each_entry(slot, &sn_hp_list, hp_list) {
198                 bss_hotplug_slot = slot->hotplug_slot;
199                 pci_slot = bss_hotplug_slot->pci_slot;
200                 list_del(&((struct slot *)bss_hotplug_slot->private)->
201                          hp_list);
202                 sysfs_remove_file(&pci_slot->kobj,
203                                   &sn_slot_path_attr.attr);
204                 break;
205         }
206         return bss_hotplug_slot;
207 }
208
209 static void sn_bus_free_data(struct pci_dev *dev)
210 {
211         struct pci_bus *subordinate_bus;
212         struct pci_dev *child;
213
214         /* Recursively clean up sn_irq_info structs */
215         if (dev->subordinate) {
216                 subordinate_bus = dev->subordinate;
217                 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
218                         sn_bus_free_data(child);
219         }
220         /*
221          * Some drivers may use dma accesses during the
222          * driver remove function. We release the sysdata
223          * areas after the driver remove functions have
224          * been called.
225          */
226         sn_bus_store_sysdata(dev);
227         sn_pci_unfixup_slot(dev);
228 }
229
230 static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
231                           int device_num, char **ssdt)
232 {
233         struct slot *slot = bss_hotplug_slot->private;
234         struct pcibus_info *pcibus_info;
235         struct pcibr_slot_enable_resp resp;
236         int rc;
237
238         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
239
240         /*
241          * Power-on and initialize the slot in the SN
242          * PCI infrastructure.
243          */
244         rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
245
246
247         if (rc == PCI_SLOT_ALREADY_UP) {
248                 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
249                 return 1; /* return 1 to user */
250         }
251
252         if (rc == PCI_L1_ERR) {
253                 dev_dbg(&slot->pci_bus->self->dev,
254                         "L1 failure %d with message: %s",
255                         resp.resp_sub_errno, resp.resp_l1_msg);
256                 return -EPERM;
257         }
258
259         if (rc) {
260                 dev_dbg(&slot->pci_bus->self->dev,
261                         "insert failed with error %d sub-error %d\n",
262                         rc, resp.resp_sub_errno);
263                 return -EIO;
264         }
265
266         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
267         pcibus_info->pbi_enabled_devices |= (1 << device_num);
268
269         return 0;
270 }
271
272 static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
273                            int device_num, int action)
274 {
275         struct slot *slot = bss_hotplug_slot->private;
276         struct pcibus_info *pcibus_info;
277         struct pcibr_slot_disable_resp resp;
278         int rc;
279
280         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
281
282         rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
283
284         if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
285             (rc == PCI_SLOT_ALREADY_DOWN)) {
286                 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
287                 return 1; /* return 1 to user */
288         }
289
290         if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
291                 dev_dbg(&slot->pci_bus->self->dev,
292                         "Cannot remove last 33MHz card\n");
293                 return -EPERM;
294         }
295
296         if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
297                 dev_dbg(&slot->pci_bus->self->dev,
298                         "L1 failure %d with message \n%s\n",
299                         resp.resp_sub_errno, resp.resp_l1_msg);
300                 return -EPERM;
301         }
302
303         if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
304                 dev_dbg(&slot->pci_bus->self->dev,
305                         "remove failed with error %d sub-error %d\n",
306                         rc, resp.resp_sub_errno);
307                 return -EIO;
308         }
309
310         if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
311                 return 0;
312
313         if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
314                 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
315                 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
316                 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
317                 return 0;
318         }
319
320         if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
321                 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
322         }
323
324         return rc;
325 }
326
327 /*
328  * Power up and configure the slot via a SAL call to PROM.
329  * Scan slot (and any children), do any platform specific fixup,
330  * and find device driver.
331  */
332 static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
333 {
334         struct slot *slot = bss_hotplug_slot->private;
335         struct pci_bus *new_bus = NULL;
336         struct pci_dev *dev;
337         int func, num_funcs;
338         int new_ppb = 0;
339         int rc;
340         char *ssdt = NULL;
341         void pcibios_fixup_device_resources(struct pci_dev *);
342
343         /* Serialize the Linux PCI infrastructure */
344         mutex_lock(&sn_hotplug_mutex);
345
346         /*
347          * Power-on and initialize the slot in the SN
348          * PCI infrastructure. Also, retrieve the ACPI SSDT
349          * table for the slot (if ACPI capable PROM).
350          */
351         rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
352         if (rc) {
353                 mutex_unlock(&sn_hotplug_mutex);
354                 return rc;
355         }
356
357         if (ssdt)
358                 ssdt = __va(ssdt);
359         /* Add the new SSDT for the slot to the ACPI namespace */
360         if (SN_ACPI_BASE_SUPPORT() && ssdt) {
361                 acpi_status ret;
362
363                 ret = acpi_load_table((struct acpi_table_header *)ssdt);
364                 if (ACPI_FAILURE(ret)) {
365                         printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
366                                __func__, ret);
367                         /* try to continue on */
368                 }
369         }
370
371         num_funcs = pci_scan_slot(slot->pci_bus,
372                                   PCI_DEVFN(slot->device_num + 1, 0));
373         if (!num_funcs) {
374                 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
375                 mutex_unlock(&sn_hotplug_mutex);
376                 return -ENODEV;
377         }
378
379         /*
380          * Map SN resources for all functions on the card
381          * to the Linux PCI interface and tell the drivers
382          * about them.
383          */
384         for (func = 0; func < num_funcs;  func++) {
385                 dev = pci_get_slot(slot->pci_bus,
386                                    PCI_DEVFN(slot->device_num + 1,
387                                              PCI_FUNC(func)));
388                 if (dev) {
389                         /* Need to do slot fixup on PPB before fixup of children
390                          * (PPB's pcidev_info needs to be in pcidev_info list
391                          * before child's SN_PCIDEV_INFO() call to setup
392                          * pdi_host_pcidev_info).
393                          */
394                         pcibios_fixup_device_resources(dev);
395                         if (SN_ACPI_BASE_SUPPORT())
396                                 sn_acpi_slot_fixup(dev);
397                         else
398                                 sn_io_slot_fixup(dev);
399                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
400                                 pci_hp_add_bridge(dev);
401                                 if (dev->subordinate) {
402                                         new_bus = dev->subordinate;
403                                         new_ppb = 1;
404                                 }
405                         }
406                         pci_dev_put(dev);
407                 }
408         }
409
410         /*
411          * Add the slot's devices to the ACPI infrastructure */
412         if (SN_ACPI_BASE_SUPPORT() && ssdt) {
413                 unsigned long long adr;
414                 struct acpi_device *pdevice;
415                 acpi_handle phandle;
416                 acpi_handle chandle = NULL;
417                 acpi_handle rethandle;
418                 acpi_status ret;
419
420                 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
421
422                 if (acpi_bus_get_device(phandle, &pdevice)) {
423                         dev_dbg(&slot->pci_bus->self->dev,
424                                 "no parent device, assuming NULL\n");
425                         pdevice = NULL;
426                 }
427
428                 /*
429                  * Walk the rootbus node's immediate children looking for
430                  * the slot's device node(s). There can be more than
431                  * one for multifunction devices.
432                  */
433                 for (;;) {
434                         rethandle = NULL;
435                         ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
436                                                    phandle, chandle,
437                                                    &rethandle);
438
439                         if (ret == AE_NOT_FOUND || rethandle == NULL)
440                                 break;
441
442                         chandle = rethandle;
443
444                         ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
445                                                     NULL, &adr);
446
447                         if (ACPI_SUCCESS(ret) &&
448                             (adr>>16) == (slot->device_num + 1)) {
449
450                                 ret = acpi_bus_add(chandle);
451                                 if (ACPI_FAILURE(ret)) {
452                                         printk(KERN_ERR "%s: acpi_bus_add "
453                                                "failed (0x%x) for slot %d "
454                                                "func %d\n", __func__,
455                                                ret, (int)(adr>>16),
456                                                (int)(adr&0xffff));
457                                         /* try to continue on */
458                                 }
459                         }
460                 }
461         }
462
463         /* Call the driver for the new device */
464         pci_bus_add_devices(slot->pci_bus);
465         /* Call the drivers for the new devices subordinate to PPB */
466         if (new_ppb)
467                 pci_bus_add_devices(new_bus);
468
469         mutex_unlock(&sn_hotplug_mutex);
470
471         if (rc == 0)
472                 dev_dbg(&slot->pci_bus->self->dev,
473                         "insert operation successful\n");
474         else
475                 dev_dbg(&slot->pci_bus->self->dev,
476                         "insert operation failed rc = %d\n", rc);
477
478         return rc;
479 }
480
481 static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
482 {
483         struct slot *slot = bss_hotplug_slot->private;
484         struct pci_dev *dev;
485         int func;
486         int rc;
487         acpi_owner_id ssdt_id = 0;
488
489         /* Acquire update access to the bus */
490         mutex_lock(&sn_hotplug_mutex);
491
492         /* is it okay to bring this slot down? */
493         rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
494                              PCI_REQ_SLOT_ELIGIBLE);
495         if (rc)
496                 goto leaving;
497
498         /* free the ACPI resources for the slot */
499         if (SN_ACPI_BASE_SUPPORT() &&
500             PCI_CONTROLLER(slot->pci_bus)->acpi_handle) {
501                 unsigned long long adr;
502                 struct acpi_device *device;
503                 acpi_handle phandle;
504                 acpi_handle chandle = NULL;
505                 acpi_handle rethandle;
506                 acpi_status ret;
507
508                 /* Get the rootbus node pointer */
509                 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
510
511                 /*
512                  * Walk the rootbus node's immediate children looking for
513                  * the slot's device node(s). There can be more than
514                  * one for multifunction devices.
515                  */
516                 for (;;) {
517                         rethandle = NULL;
518                         ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
519                                                    phandle, chandle,
520                                                    &rethandle);
521
522                         if (ret == AE_NOT_FOUND || rethandle == NULL)
523                                 break;
524
525                         chandle = rethandle;
526
527                         ret = acpi_evaluate_integer(chandle,
528                                                     METHOD_NAME__ADR,
529                                                     NULL, &adr);
530                         if (ACPI_SUCCESS(ret) &&
531                             (adr>>16) == (slot->device_num + 1)) {
532                                 /* retain the owner id */
533                                 acpi_get_id(chandle, &ssdt_id);
534
535                                 ret = acpi_bus_get_device(chandle,
536                                                           &device);
537                                 if (ACPI_SUCCESS(ret))
538                                         acpi_bus_trim(device);
539                         }
540                 }
541
542         }
543
544         /* Free the SN resources assigned to the Linux device.*/
545         for (func = 0; func < 8;  func++) {
546                 dev = pci_get_slot(slot->pci_bus,
547                                    PCI_DEVFN(slot->device_num + 1,
548                                              PCI_FUNC(func)));
549                 if (dev) {
550                         sn_bus_free_data(dev);
551                         pci_stop_and_remove_bus_device(dev);
552                         pci_dev_put(dev);
553                 }
554         }
555
556         /* Remove the SSDT for the slot from the ACPI namespace */
557         if (SN_ACPI_BASE_SUPPORT() && ssdt_id) {
558                 acpi_status ret;
559                 ret = acpi_unload_table_id(ssdt_id);
560                 if (ACPI_FAILURE(ret)) {
561                         printk(KERN_ERR "%s: acpi_unload_table_id "
562                                "failed (0x%x) for id %d\n",
563                                __func__, ret, ssdt_id);
564                         /* try to continue on */
565                 }
566         }
567
568         /* free the collected sysdata pointers */
569         sn_bus_free_sysdata();
570
571         /* Deactivate slot */
572         rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
573                              PCI_REQ_SLOT_DISABLE);
574  leaving:
575         /* Release the bus lock */
576         mutex_unlock(&sn_hotplug_mutex);
577
578         return rc;
579 }
580
581 static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
582                                    u8 *value)
583 {
584         struct slot *slot = bss_hotplug_slot->private;
585         struct pcibus_info *pcibus_info;
586         u32 power;
587
588         pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
589         mutex_lock(&sn_hotplug_mutex);
590         power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
591         *value = power ? 1 : 0;
592         mutex_unlock(&sn_hotplug_mutex);
593         return 0;
594 }
595
596 static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
597 {
598         kfree(bss_hotplug_slot->info);
599         kfree(bss_hotplug_slot->private);
600         kfree(bss_hotplug_slot);
601 }
602
603 static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
604 {
605         int device;
606         struct pci_slot *pci_slot;
607         struct hotplug_slot *bss_hotplug_slot;
608         char name[SN_SLOT_NAME_SIZE];
609         int rc = 0;
610
611         /*
612          * Currently only four devices are supported,
613          * in the future there maybe more -- up to 32.
614          */
615
616         for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
617                 if (sn_pci_slot_valid(pci_bus, device) != 1)
618                         continue;
619
620                 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
621                                            GFP_KERNEL);
622                 if (!bss_hotplug_slot) {
623                         rc = -ENOMEM;
624                         goto alloc_err;
625                 }
626
627                 bss_hotplug_slot->info =
628                         kzalloc(sizeof(struct hotplug_slot_info),
629                                 GFP_KERNEL);
630                 if (!bss_hotplug_slot->info) {
631                         rc = -ENOMEM;
632                         goto alloc_err;
633                 }
634
635                 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
636                                              pci_bus, device, name)) {
637                         rc = -ENOMEM;
638                         goto alloc_err;
639                 }
640                 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
641                 bss_hotplug_slot->release = &sn_release_slot;
642
643                 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
644                 if (rc)
645                         goto register_err;
646
647                 pci_slot = bss_hotplug_slot->pci_slot;
648                 rc = sysfs_create_file(&pci_slot->kobj,
649                                        &sn_slot_path_attr.attr);
650                 if (rc)
651                         goto register_err;
652         }
653         dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
654         return rc;
655
656 register_err:
657         dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
658                 rc);
659
660 alloc_err:
661         if (rc == -ENOMEM)
662                 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
663
664         /* destroy THIS element */
665         if (bss_hotplug_slot)
666                 sn_release_slot(bss_hotplug_slot);
667
668         /* destroy anything else on the list */
669         while ((bss_hotplug_slot = sn_hp_destroy()))
670                 pci_hp_deregister(bss_hotplug_slot);
671
672         return rc;
673 }
674
675 static int __init sn_pci_hotplug_init(void)
676 {
677         struct pci_bus *pci_bus = NULL;
678         int rc;
679         int registered = 0;
680
681         if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
682                 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
683                        __func__);
684                 return -EPERM;
685         }
686
687         INIT_LIST_HEAD(&sn_hp_list);
688
689         while ((pci_bus = pci_find_next_bus(pci_bus))) {
690                 if (!pci_bus->sysdata)
691                         continue;
692
693                 rc = sn_pci_bus_valid(pci_bus);
694                 if (rc != 1) {
695                         dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
696                         continue;
697                 }
698                 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
699
700                 rc = sn_hotplug_slot_register(pci_bus);
701                 if (!rc) {
702                         registered = 1;
703                 } else {
704                         registered = 0;
705                         break;
706                 }
707         }
708
709         return registered == 1 ? 0 : -ENODEV;
710 }
711
712 static void __exit sn_pci_hotplug_exit(void)
713 {
714         struct hotplug_slot *bss_hotplug_slot;
715
716         while ((bss_hotplug_slot = sn_hp_destroy()))
717                 pci_hp_deregister(bss_hotplug_slot);
718
719         if (!list_empty(&sn_hp_list))
720                 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
721 }
722
723 module_init(sn_pci_hotplug_init);
724 module_exit(sn_pci_hotplug_exit);