PCI Hotplug: cpqphp: eliminate stray braces
[linux-2.6] / drivers / pci / hotplug / cpqphp_core.c
1 /*
2  * Compaq Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman <greg@kroah.com>
6  * Copyright (C) 2001 IBM Corp.
7  *
8  * All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18  * NON INFRINGEMENT.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Send feedback to <greg@kroah.com>
26  *
27  * Jan 12, 2003 -       Added 66/100/133MHz PCI-X support,
28  *                      Torben Mathiasen <torben.mathiasen@hp.com>
29  */
30
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/proc_fs.h>
36 #include <linux/slab.h>
37 #include <linux/workqueue.h>
38 #include <linux/pci.h>
39 #include <linux/pci_hotplug.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42
43 #include <asm/uaccess.h>
44
45 #include "cpqphp.h"
46 #include "cpqphp_nvram.h"
47 #include <asm/pci_x86.h>
48
49
50 /* Global variables */
51 int cpqhp_debug;
52 int cpqhp_legacy_mode;
53 struct controller *cpqhp_ctrl_list;     /* = NULL */
54 struct pci_func *cpqhp_slot_list[256];
55
56 /* local variables */
57 static void __iomem *smbios_table;
58 static void __iomem *smbios_start;
59 static void __iomem *cpqhp_rom_start;
60 static int power_mode;
61 static int debug;
62 static int initialized;
63
64 #define DRIVER_VERSION  "0.9.8"
65 #define DRIVER_AUTHOR   "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>"
66 #define DRIVER_DESC     "Compaq Hot Plug PCI Controller Driver"
67
68 MODULE_AUTHOR(DRIVER_AUTHOR);
69 MODULE_DESCRIPTION(DRIVER_DESC);
70 MODULE_LICENSE("GPL");
71
72 module_param(power_mode, bool, 0644);
73 MODULE_PARM_DESC(power_mode, "Power mode enabled or not");
74
75 module_param(debug, bool, 0644);
76 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
77
78 #define CPQHPC_MODULE_MINOR 208
79
80 static inline int is_slot64bit(struct slot *slot)
81 {
82         return (readb(slot->p_sm_slot + SMBIOS_SLOT_WIDTH) == 0x06) ? 1 : 0;
83 }
84
85 static inline int is_slot66mhz(struct slot *slot)
86 {
87         return (readb(slot->p_sm_slot + SMBIOS_SLOT_TYPE) == 0x0E) ? 1 : 0;
88 }
89
90 /**
91  * detect_SMBIOS_pointer - find the System Management BIOS Table in mem region.
92  * @begin: begin pointer for region to be scanned.
93  * @end: end pointer for region to be scanned.
94  *
95  * Returns pointer to the head of the SMBIOS tables (or %NULL).
96  */
97 static void __iomem * detect_SMBIOS_pointer(void __iomem *begin, void __iomem *end)
98 {
99         void __iomem *fp;
100         void __iomem *endp;
101         u8 temp1, temp2, temp3, temp4;
102         int status = 0;
103
104         endp = (end - sizeof(u32) + 1);
105
106         for (fp = begin; fp <= endp; fp += 16) {
107                 temp1 = readb(fp);
108                 temp2 = readb(fp+1);
109                 temp3 = readb(fp+2);
110                 temp4 = readb(fp+3);
111                 if (temp1 == '_' &&
112                     temp2 == 'S' &&
113                     temp3 == 'M' &&
114                     temp4 == '_') {
115                         status = 1;
116                         break;
117                 }
118         }
119
120         if (!status)
121                 fp = NULL;
122
123         dbg("Discovered SMBIOS Entry point at %p\n", fp);
124
125         return fp;
126 }
127
128 /**
129  * init_SERR - Initializes the per slot SERR generation.
130  * @ctrl: controller to use
131  *
132  * For unexpected switch opens
133  */
134 static int init_SERR(struct controller * ctrl)
135 {
136         u32 tempdword;
137         u32 number_of_slots;
138         u8 physical_slot;
139
140         if (!ctrl)
141                 return 1;
142
143         tempdword = ctrl->first_slot;
144
145         number_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0F;
146         /* Loop through slots */
147         while (number_of_slots) {
148                 physical_slot = tempdword;
149                 writeb(0, ctrl->hpc_reg + SLOT_SERR);
150                 tempdword++;
151                 number_of_slots--;
152         }
153
154         return 0;
155 }
156
157 /* nice debugging output */
158 static int pci_print_IRQ_route (void)
159 {
160         struct irq_routing_table *routing_table;
161         int len;
162         int loop;
163
164         u8 tbus, tdevice, tslot;
165
166         routing_table = pcibios_get_irq_routing_table();
167         if (routing_table == NULL) {
168                 err("No BIOS Routing Table??? Not good\n");
169                 return -ENOMEM;
170         }
171
172         len = (routing_table->size - sizeof(struct irq_routing_table)) /
173                         sizeof(struct irq_info);
174         /* Make sure I got at least one entry */
175         if (len == 0) {
176                 kfree(routing_table);
177                 return -1;
178         }
179
180         dbg("bus dev func slot\n");
181
182         for (loop = 0; loop < len; ++loop) {
183                 tbus = routing_table->slots[loop].bus;
184                 tdevice = routing_table->slots[loop].devfn;
185                 tslot = routing_table->slots[loop].slot;
186                 dbg("%d %d %d %d\n", tbus, tdevice >> 3, tdevice & 0x7, tslot);
187
188         }
189         kfree(routing_table);
190         return 0;
191 }
192
193
194 /**
195  * get_subsequent_smbios_entry: get the next entry from bios table.
196  * @smbios_start: where to start in the SMBIOS table
197  * @smbios_table: location of the SMBIOS table
198  * @curr: %NULL or pointer to previously returned structure
199  *
200  * Gets the first entry if previous == NULL;
201  * otherwise, returns the next entry.
202  * Uses global SMBIOS Table pointer.
203  *
204  * Returns a pointer to an SMBIOS structure or NULL if none found.
205  */
206 static void __iomem *get_subsequent_smbios_entry(void __iomem *smbios_start,
207                                                 void __iomem *smbios_table,
208                                                 void __iomem *curr)
209 {
210         u8 bail = 0;
211         u8 previous_byte = 1;
212         void __iomem *p_temp;
213         void __iomem *p_max;
214
215         if (!smbios_table || !curr)
216                 return NULL;
217
218         /* set p_max to the end of the table */
219         p_max = smbios_start + readw(smbios_table + ST_LENGTH);
220
221         p_temp = curr;
222         p_temp += readb(curr + SMBIOS_GENERIC_LENGTH);
223
224         while ((p_temp < p_max) && !bail) {
225                 /* Look for the double NULL terminator
226                  * The first condition is the previous byte
227                  * and the second is the curr
228                  */
229                 if (!previous_byte && !(readb(p_temp)))
230                         bail = 1;
231
232                 previous_byte = readb(p_temp);
233                 p_temp++;
234         }
235
236         if (p_temp < p_max)
237                 return p_temp;
238         else
239                 return NULL;
240 }
241
242
243 /**
244  * get_SMBIOS_entry - return the requested SMBIOS entry or %NULL
245  * @smbios_start: where to start in the SMBIOS table
246  * @smbios_table: location of the SMBIOS table
247  * @type: SMBIOS structure type to be returned
248  * @previous: %NULL or pointer to previously returned structure
249  *
250  * Gets the first entry of the specified type if previous == %NULL;
251  * Otherwise, returns the next entry of the given type.
252  * Uses global SMBIOS Table pointer.
253  * Uses get_subsequent_smbios_entry.
254  *
255  * Returns a pointer to an SMBIOS structure or %NULL if none found.
256  */
257 static void __iomem *get_SMBIOS_entry(void __iomem *smbios_start,
258                                         void __iomem *smbios_table,
259                                         u8 type,
260                                         void __iomem *previous)
261 {
262         if (!smbios_table)
263                 return NULL;
264
265         if (!previous)
266                 previous = smbios_start;
267         else
268                 previous = get_subsequent_smbios_entry(smbios_start,
269                                         smbios_table, previous);
270
271         while (previous)
272                 if (readb(previous + SMBIOS_GENERIC_TYPE) != type)
273                         previous = get_subsequent_smbios_entry(smbios_start,
274                                                 smbios_table, previous);
275                 else
276                         break;
277
278         return previous;
279 }
280
281 static void release_slot(struct hotplug_slot *hotplug_slot)
282 {
283         struct slot *slot = hotplug_slot->private;
284
285         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
286
287         kfree(slot->hotplug_slot->info);
288         kfree(slot->hotplug_slot);
289         kfree(slot);
290 }
291
292 static int ctrl_slot_cleanup (struct controller * ctrl)
293 {
294         struct slot *old_slot, *next_slot;
295
296         old_slot = ctrl->slot;
297         ctrl->slot = NULL;
298
299         while (old_slot) {
300                 /* memory will be freed by the release_slot callback */
301                 next_slot = old_slot->next;
302                 pci_hp_deregister (old_slot->hotplug_slot);
303                 old_slot = next_slot;
304         }
305
306         cpqhp_remove_debugfs_files(ctrl);
307
308         /* Free IRQ associated with hot plug device */
309         free_irq(ctrl->interrupt, ctrl);
310         /* Unmap the memory */
311         iounmap(ctrl->hpc_reg);
312         /* Finally reclaim PCI mem */
313         release_mem_region(pci_resource_start(ctrl->pci_dev, 0),
314                            pci_resource_len(ctrl->pci_dev, 0));
315
316         return 0;
317 }
318
319
320 /**
321  * get_slot_mapping - determine logical slot mapping for PCI device
322  *
323  * Won't work for more than one PCI-PCI bridge in a slot.
324  *
325  * @bus_num - bus number of PCI device
326  * @dev_num - device number of PCI device
327  * @slot - Pointer to u8 where slot number will be returned
328  *
329  * Output:      SUCCESS or FAILURE
330  */
331 static int
332 get_slot_mapping(struct pci_bus *bus, u8 bus_num, u8 dev_num, u8 *slot)
333 {
334         struct irq_routing_table *PCIIRQRoutingInfoLength;
335         u32 work;
336         long len;
337         long loop;
338
339         u8 tbus, tdevice, tslot, bridgeSlot;
340
341         dbg("%s: %p, %d, %d, %p\n", __func__, bus, bus_num, dev_num, slot);
342
343         bridgeSlot = 0xFF;
344
345         PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
346         if (!PCIIRQRoutingInfoLength)
347                 return -1;
348
349         len = (PCIIRQRoutingInfoLength->size -
350                sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
351         /* Make sure I got at least one entry */
352         if (len == 0) {
353                 kfree(PCIIRQRoutingInfoLength);
354                 return -1;
355         }
356
357         for (loop = 0; loop < len; ++loop) {
358                 tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
359                 tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn >> 3;
360                 tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
361
362                 if ((tbus == bus_num) && (tdevice == dev_num)) {
363                         *slot = tslot;
364                         kfree(PCIIRQRoutingInfoLength);
365                         return 0;
366                 } else {
367                         /* Did not get a match on the target PCI device. Check
368                          * if the current IRQ table entry is a PCI-to-PCI
369                          * bridge device.  If so, and it's secondary bus
370                          * matches the bus number for the target device, I need
371                          * to save the bridge's slot number.  If I can not find
372                          * an entry for the target device, I will have to
373                          * assume it's on the other side of the bridge, and
374                          * assign it the bridge's slot.
375                          */
376                         bus->number = tbus;
377                         pci_bus_read_config_dword(bus, PCI_DEVFN(tdevice, 0),
378                                                 PCI_CLASS_REVISION, &work);
379
380                         if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
381                                 pci_bus_read_config_dword(bus,
382                                                         PCI_DEVFN(tdevice, 0),
383                                                         PCI_PRIMARY_BUS, &work);
384                                 // See if bridge's secondary bus matches target bus.
385                                 if (((work >> 8) & 0x000000FF) == (long) bus_num)
386                                         bridgeSlot = tslot;
387                         }
388                 }
389
390         }
391
392         /* If we got here, we didn't find an entry in the IRQ mapping table for
393          * the target PCI device.  If we did determine that the target device
394          * is on the other side of a PCI-to-PCI bridge, return the slot number
395          * for the bridge.
396          */
397         if (bridgeSlot != 0xFF) {
398                 *slot = bridgeSlot;
399                 kfree(PCIIRQRoutingInfoLength);
400                 return 0;
401         }
402         kfree(PCIIRQRoutingInfoLength);
403         /* Couldn't find an entry in the routing table for this PCI device */
404         return -1;
405 }
406
407
408 /**
409  * cpqhp_set_attention_status - Turns the Amber LED for a slot on or off
410  * @ctrl: struct controller to use
411  * @func: PCI device/function info
412  * @status: LED control flag: 1 = LED on, 0 = LED off
413  */
414 static int
415 cpqhp_set_attention_status(struct controller *ctrl, struct pci_func *func,
416                                 u32 status)
417 {
418         u8 hp_slot;
419
420         if (func == NULL)
421                 return 1;
422
423         hp_slot = func->device - ctrl->slot_device_offset;
424
425         /* Wait for exclusive access to hardware */
426         mutex_lock(&ctrl->crit_sect);
427
428         if (status == 1)
429                 amber_LED_on (ctrl, hp_slot);
430         else if (status == 0)
431                 amber_LED_off (ctrl, hp_slot);
432         else {
433                 /* Done with exclusive hardware access */
434                 mutex_unlock(&ctrl->crit_sect);
435                 return 1;
436         }
437
438         set_SOGO(ctrl);
439
440         /* Wait for SOBS to be unset */
441         wait_for_ctrl_irq (ctrl);
442
443         /* Done with exclusive hardware access */
444         mutex_unlock(&ctrl->crit_sect);
445
446         return 0;
447 }
448
449
450 /**
451  * set_attention_status - Turns the Amber LED for a slot on or off
452  * @hotplug_slot: slot to change LED on
453  * @status: LED control flag
454  */
455 static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
456 {
457         struct pci_func *slot_func;
458         struct slot *slot = hotplug_slot->private;
459         struct controller *ctrl = slot->ctrl;
460         u8 bus;
461         u8 devfn;
462         u8 device;
463         u8 function;
464
465         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
466
467         if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
468                 return -ENODEV;
469
470         device = devfn >> 3;
471         function = devfn & 0x7;
472         dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);
473
474         slot_func = cpqhp_slot_find(bus, device, function);
475         if (!slot_func)
476                 return -ENODEV;
477
478         return cpqhp_set_attention_status(ctrl, slot_func, status);
479 }
480
481
482 static int process_SI(struct hotplug_slot *hotplug_slot)
483 {
484         struct pci_func *slot_func;
485         struct slot *slot = hotplug_slot->private;
486         struct controller *ctrl = slot->ctrl;
487         u8 bus;
488         u8 devfn;
489         u8 device;
490         u8 function;
491
492         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
493
494         if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
495                 return -ENODEV;
496
497         device = devfn >> 3;
498         function = devfn & 0x7;
499         dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);
500
501         slot_func = cpqhp_slot_find(bus, device, function);
502         if (!slot_func)
503                 return -ENODEV;
504
505         slot_func->bus = bus;
506         slot_func->device = device;
507         slot_func->function = function;
508         slot_func->configured = 0;
509         dbg("board_added(%p, %p)\n", slot_func, ctrl);
510         return cpqhp_process_SI(ctrl, slot_func);
511 }
512
513
514 static int process_SS(struct hotplug_slot *hotplug_slot)
515 {
516         struct pci_func *slot_func;
517         struct slot *slot = hotplug_slot->private;
518         struct controller *ctrl = slot->ctrl;
519         u8 bus;
520         u8 devfn;
521         u8 device;
522         u8 function;
523
524         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
525
526         if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
527                 return -ENODEV;
528
529         device = devfn >> 3;
530         function = devfn & 0x7;
531         dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);
532
533         slot_func = cpqhp_slot_find(bus, device, function);
534         if (!slot_func)
535                 return -ENODEV;
536
537         dbg("In %s, slot_func = %p, ctrl = %p\n", __func__, slot_func, ctrl);
538         return cpqhp_process_SS(ctrl, slot_func);
539 }
540
541
542 static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value)
543 {
544         struct slot *slot = hotplug_slot->private;
545         struct controller *ctrl = slot->ctrl;
546
547         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
548
549         return cpqhp_hardware_test(ctrl, value);
550 }
551
552
553 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
554 {
555         struct slot *slot = hotplug_slot->private;
556         struct controller *ctrl = slot->ctrl;
557
558         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
559
560         *value = get_slot_enabled(ctrl, slot);
561         return 0;
562 }
563
564 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
565 {
566         struct slot *slot = hotplug_slot->private;
567         struct controller *ctrl = slot->ctrl;
568
569         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
570
571         *value = cpq_get_attention_status(ctrl, slot);
572         return 0;
573 }
574
575 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
576 {
577         struct slot *slot = hotplug_slot->private;
578         struct controller *ctrl = slot->ctrl;
579
580         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
581
582         *value = cpq_get_latch_status(ctrl, slot);
583
584         return 0;
585 }
586
587 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
588 {
589         struct slot *slot = hotplug_slot->private;
590         struct controller *ctrl = slot->ctrl;
591
592         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
593
594         *value = get_presence_status(ctrl, slot);
595
596         return 0;
597 }
598
599 static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
600 {
601         struct slot *slot = hotplug_slot->private;
602         struct controller *ctrl = slot->ctrl;
603
604         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
605
606         *value = ctrl->speed_capability;
607
608         return 0;
609 }
610
611 static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
612 {
613         struct slot *slot = hotplug_slot->private;
614         struct controller *ctrl = slot->ctrl;
615
616         dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
617
618         *value = ctrl->speed;
619
620         return 0;
621 }
622
623 static struct hotplug_slot_ops cpqphp_hotplug_slot_ops = {
624         .owner =                THIS_MODULE,
625         .set_attention_status = set_attention_status,
626         .enable_slot =          process_SI,
627         .disable_slot =         process_SS,
628         .hardware_test =        hardware_test,
629         .get_power_status =     get_power_status,
630         .get_attention_status = get_attention_status,
631         .get_latch_status =     get_latch_status,
632         .get_adapter_status =   get_adapter_status,
633         .get_max_bus_speed =    get_max_bus_speed,
634         .get_cur_bus_speed =    get_cur_bus_speed,
635 };
636
637 #define SLOT_NAME_SIZE 10
638
639 static int ctrl_slot_setup(struct controller *ctrl,
640                         void __iomem *smbios_start,
641                         void __iomem *smbios_table)
642 {
643         struct slot *slot;
644         struct hotplug_slot *hotplug_slot;
645         struct hotplug_slot_info *hotplug_slot_info;
646         u8 number_of_slots;
647         u8 slot_device;
648         u8 slot_number;
649         u8 ctrl_slot;
650         u32 tempdword;
651         char name[SLOT_NAME_SIZE];
652         void __iomem *slot_entry= NULL;
653         int result = -ENOMEM;
654
655         dbg("%s\n", __func__);
656
657         tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
658
659         number_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0F;
660         slot_device = readb(ctrl->hpc_reg + SLOT_MASK) >> 4;
661         slot_number = ctrl->first_slot;
662
663         while (number_of_slots) {
664                 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
665                 if (!slot)
666                         goto error;
667
668                 slot->hotplug_slot = kzalloc(sizeof(*(slot->hotplug_slot)),
669                                                 GFP_KERNEL);
670                 if (!slot->hotplug_slot)
671                         goto error_slot;
672                 hotplug_slot = slot->hotplug_slot;
673
674                 hotplug_slot->info = kzalloc(sizeof(*(hotplug_slot->info)),
675                                                         GFP_KERNEL);
676                 if (!hotplug_slot->info)
677                         goto error_hpslot;
678                 hotplug_slot_info = hotplug_slot->info;
679
680                 slot->ctrl = ctrl;
681                 slot->bus = ctrl->bus;
682                 slot->device = slot_device;
683                 slot->number = slot_number;
684                 dbg("slot->number = %u\n", slot->number);
685
686                 slot_entry = get_SMBIOS_entry(smbios_start, smbios_table, 9,
687                                         slot_entry);
688
689                 while (slot_entry && (readw(slot_entry + SMBIOS_SLOT_NUMBER) !=
690                                 slot->number)) {
691                         slot_entry = get_SMBIOS_entry(smbios_start,
692                                                 smbios_table, 9, slot_entry);
693                 }
694
695                 slot->p_sm_slot = slot_entry;
696
697                 init_timer(&slot->task_event);
698                 slot->task_event.expires = jiffies + 5 * HZ;
699                 slot->task_event.function = cpqhp_pushbutton_thread;
700
701                 /*FIXME: these capabilities aren't used but if they are
702                  *       they need to be correctly implemented
703                  */
704                 slot->capabilities |= PCISLOT_REPLACE_SUPPORTED;
705                 slot->capabilities |= PCISLOT_INTERLOCK_SUPPORTED;
706
707                 if (is_slot64bit(slot))
708                         slot->capabilities |= PCISLOT_64_BIT_SUPPORTED;
709                 if (is_slot66mhz(slot))
710                         slot->capabilities |= PCISLOT_66_MHZ_SUPPORTED;
711                 if (ctrl->speed == PCI_SPEED_66MHz)
712                         slot->capabilities |= PCISLOT_66_MHZ_OPERATION;
713
714                 ctrl_slot =
715                         slot_device - (readb(ctrl->hpc_reg + SLOT_MASK) >> 4);
716
717                 /* Check presence */
718                 slot->capabilities |=
719                         ((((~tempdword) >> 23) |
720                          ((~tempdword) >> 15)) >> ctrl_slot) & 0x02;
721                 /* Check the switch state */
722                 slot->capabilities |=
723                         ((~tempdword & 0xFF) >> ctrl_slot) & 0x01;
724                 /* Check the slot enable */
725                 slot->capabilities |=
726                         ((read_slot_enable(ctrl) << 2) >> ctrl_slot) & 0x04;
727
728                 /* register this slot with the hotplug pci core */
729                 hotplug_slot->release = &release_slot;
730                 hotplug_slot->private = slot;
731                 snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
732                 hotplug_slot->ops = &cpqphp_hotplug_slot_ops;
733
734                 hotplug_slot_info->power_status = get_slot_enabled(ctrl, slot);
735                 hotplug_slot_info->attention_status =
736                         cpq_get_attention_status(ctrl, slot);
737                 hotplug_slot_info->latch_status =
738                         cpq_get_latch_status(ctrl, slot);
739                 hotplug_slot_info->adapter_status =
740                         get_presence_status(ctrl, slot);
741
742                 dbg("registering bus %d, dev %d, number %d, "
743                                 "ctrl->slot_device_offset %d, slot %d\n",
744                                 slot->bus, slot->device,
745                                 slot->number, ctrl->slot_device_offset,
746                                 slot_number);
747                 result = pci_hp_register(hotplug_slot,
748                                          ctrl->pci_dev->bus,
749                                          slot->device,
750                                          name);
751                 if (result) {
752                         err("pci_hp_register failed with error %d\n", result);
753                         goto error_info;
754                 }
755
756                 slot->next = ctrl->slot;
757                 ctrl->slot = slot;
758
759                 number_of_slots--;
760                 slot_device++;
761                 slot_number++;
762         }
763
764         return 0;
765 error_info:
766         kfree(hotplug_slot_info);
767 error_hpslot:
768         kfree(hotplug_slot);
769 error_slot:
770         kfree(slot);
771 error:
772         return result;
773 }
774
775 static int one_time_init(void)
776 {
777         int loop;
778         int retval = 0;
779
780         if (initialized)
781                 return 0;
782
783         power_mode = 0;
784
785         retval = pci_print_IRQ_route();
786         if (retval)
787                 goto error;
788
789         dbg("Initialize + Start the notification mechanism \n");
790
791         retval = cpqhp_event_start_thread();
792         if (retval)
793                 goto error;
794
795         dbg("Initialize slot lists\n");
796         for (loop = 0; loop < 256; loop++)
797                 cpqhp_slot_list[loop] = NULL;
798
799         /* FIXME: We also need to hook the NMI handler eventually.
800          * this also needs to be worked with Christoph
801          * register_NMI_handler();
802          */
803         /* Map rom address */
804         cpqhp_rom_start = ioremap(ROM_PHY_ADDR, ROM_PHY_LEN);
805         if (!cpqhp_rom_start) {
806                 err ("Could not ioremap memory region for ROM\n");
807                 retval = -EIO;
808                 goto error;
809         }
810
811         /* Now, map the int15 entry point if we are on compaq specific
812          * hardware
813          */
814         compaq_nvram_init(cpqhp_rom_start);
815
816         /* Map smbios table entry point structure */
817         smbios_table = detect_SMBIOS_pointer(cpqhp_rom_start,
818                                         cpqhp_rom_start + ROM_PHY_LEN);
819         if (!smbios_table) {
820                 err ("Could not find the SMBIOS pointer in memory\n");
821                 retval = -EIO;
822                 goto error_rom_start;
823         }
824
825         smbios_start = ioremap(readl(smbios_table + ST_ADDRESS),
826                                         readw(smbios_table + ST_LENGTH));
827         if (!smbios_start) {
828                 err ("Could not ioremap memory region taken from SMBIOS values\n");
829                 retval = -EIO;
830                 goto error_smbios_start;
831         }
832
833         initialized = 1;
834
835         return retval;
836
837 error_smbios_start:
838         iounmap(smbios_start);
839 error_rom_start:
840         iounmap(cpqhp_rom_start);
841 error:
842         return retval;
843 }
844
845 static int cpqhpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
846 {
847         u8 num_of_slots = 0;
848         u8 hp_slot = 0;
849         u8 device;
850         u8 bus_cap;
851         u16 temp_word;
852         u16 vendor_id;
853         u16 subsystem_vid;
854         u16 subsystem_deviceid;
855         u32 rc;
856         struct controller *ctrl;
857         struct pci_func *func;
858         int err;
859
860         err = pci_enable_device(pdev);
861         if (err) {
862                 printk(KERN_ERR MY_NAME ": cannot enable PCI device %s (%d)\n",
863                         pci_name(pdev), err);
864                 return err;
865         }
866
867         /* Need to read VID early b/c it's used to differentiate CPQ and INTC
868          * discovery
869          */
870         rc = pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor_id);
871         if (rc || ((vendor_id != PCI_VENDOR_ID_COMPAQ) && (vendor_id != PCI_VENDOR_ID_INTEL))) {
872                 err(msg_HPC_non_compaq_or_intel);
873                 rc = -ENODEV;
874                 goto err_disable_device;
875         }
876         dbg("Vendor ID: %x\n", vendor_id);
877
878         dbg("revision: %d\n", pdev->revision);
879         if ((vendor_id == PCI_VENDOR_ID_COMPAQ) && (!pdev->revision)) {
880                 err(msg_HPC_rev_error);
881                 rc = -ENODEV;
882                 goto err_disable_device;
883         }
884
885         /* Check for the proper subsytem ID's
886          * Intel uses a different SSID programming model than Compaq.
887          * For Intel, each SSID bit identifies a PHP capability.
888          * Also Intel HPC's may have RID=0.
889          */
890         if ((pdev->revision > 2) || (vendor_id == PCI_VENDOR_ID_INTEL)) {
891                 /* TODO: This code can be made to support non-Compaq or Intel
892                  * subsystem IDs
893                  */
894                 rc = pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vid);
895                 if (rc) {
896                         err("%s : pci_read_config_word failed\n", __func__);
897                         goto err_disable_device;
898                 }
899                 dbg("Subsystem Vendor ID: %x\n", subsystem_vid);
900                 if ((subsystem_vid != PCI_VENDOR_ID_COMPAQ) && (subsystem_vid != PCI_VENDOR_ID_INTEL)) {
901                         err(msg_HPC_non_compaq_or_intel);
902                         rc = -ENODEV;
903                         goto err_disable_device;
904                 }
905
906                 ctrl = kzalloc(sizeof(struct controller), GFP_KERNEL);
907                 if (!ctrl) {
908                         err("%s : out of memory\n", __func__);
909                         rc = -ENOMEM;
910                         goto err_disable_device;
911                 }
912
913                 rc = pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsystem_deviceid);
914                 if (rc) {
915                         err("%s : pci_read_config_word failed\n", __func__);
916                         goto err_free_ctrl;
917                 }
918
919                 info("Hot Plug Subsystem Device ID: %x\n", subsystem_deviceid);
920
921                 /* Set Vendor ID, so it can be accessed later from other
922                  * functions
923                  */
924                 ctrl->vendor_id = vendor_id;
925
926                 switch (subsystem_vid) {
927                         case PCI_VENDOR_ID_COMPAQ:
928                                 if (pdev->revision >= 0x13) { /* CIOBX */
929                                         ctrl->push_flag = 1;
930                                         ctrl->slot_switch_type = 1;
931                                         ctrl->push_button = 1;
932                                         ctrl->pci_config_space = 1;
933                                         ctrl->defeature_PHP = 1;
934                                         ctrl->pcix_support = 1;
935                                         ctrl->pcix_speed_capability = 1;
936                                         pci_read_config_byte(pdev, 0x41, &bus_cap);
937                                         if (bus_cap & 0x80) {
938                                                 dbg("bus max supports 133MHz PCI-X\n");
939                                                 ctrl->speed_capability = PCI_SPEED_133MHz_PCIX;
940                                                 break;
941                                         }
942                                         if (bus_cap & 0x40) {
943                                                 dbg("bus max supports 100MHz PCI-X\n");
944                                                 ctrl->speed_capability = PCI_SPEED_100MHz_PCIX;
945                                                 break;
946                                         }
947                                         if (bus_cap & 20) {
948                                                 dbg("bus max supports 66MHz PCI-X\n");
949                                                 ctrl->speed_capability = PCI_SPEED_66MHz_PCIX;
950                                                 break;
951                                         }
952                                         if (bus_cap & 10) {
953                                                 dbg("bus max supports 66MHz PCI\n");
954                                                 ctrl->speed_capability = PCI_SPEED_66MHz;
955                                                 break;
956                                         }
957
958                                         break;
959                                 }
960
961                                 switch (subsystem_deviceid) {
962                                         case PCI_SUB_HPC_ID:
963                                                 /* Original 6500/7000 implementation */
964                                                 ctrl->slot_switch_type = 1;
965                                                 ctrl->speed_capability = PCI_SPEED_33MHz;
966                                                 ctrl->push_button = 0;
967                                                 ctrl->pci_config_space = 1;
968                                                 ctrl->defeature_PHP = 1;
969                                                 ctrl->pcix_support = 0;
970                                                 ctrl->pcix_speed_capability = 0;
971                                                 break;
972                                         case PCI_SUB_HPC_ID2:
973                                                 /* First Pushbutton implementation */
974                                                 ctrl->push_flag = 1;
975                                                 ctrl->slot_switch_type = 1;
976                                                 ctrl->speed_capability = PCI_SPEED_33MHz;
977                                                 ctrl->push_button = 1;
978                                                 ctrl->pci_config_space = 1;
979                                                 ctrl->defeature_PHP = 1;
980                                                 ctrl->pcix_support = 0;
981                                                 ctrl->pcix_speed_capability = 0;
982                                                 break;
983                                         case PCI_SUB_HPC_ID_INTC:
984                                                 /* Third party (6500/7000) */
985                                                 ctrl->slot_switch_type = 1;
986                                                 ctrl->speed_capability = PCI_SPEED_33MHz;
987                                                 ctrl->push_button = 0;
988                                                 ctrl->pci_config_space = 1;
989                                                 ctrl->defeature_PHP = 1;
990                                                 ctrl->pcix_support = 0;
991                                                 ctrl->pcix_speed_capability = 0;
992                                                 break;
993                                         case PCI_SUB_HPC_ID3:
994                                                 /* First 66 Mhz implementation */
995                                                 ctrl->push_flag = 1;
996                                                 ctrl->slot_switch_type = 1;
997                                                 ctrl->speed_capability = PCI_SPEED_66MHz;
998                                                 ctrl->push_button = 1;
999                                                 ctrl->pci_config_space = 1;
1000                                                 ctrl->defeature_PHP = 1;
1001                                                 ctrl->pcix_support = 0;
1002                                                 ctrl->pcix_speed_capability = 0;
1003                                                 break;
1004                                         case PCI_SUB_HPC_ID4:
1005                                                 /* First PCI-X implementation, 100MHz */
1006                                                 ctrl->push_flag = 1;
1007                                                 ctrl->slot_switch_type = 1;
1008                                                 ctrl->speed_capability = PCI_SPEED_100MHz_PCIX;
1009                                                 ctrl->push_button = 1;
1010                                                 ctrl->pci_config_space = 1;
1011                                                 ctrl->defeature_PHP = 1;
1012                                                 ctrl->pcix_support = 1;
1013                                                 ctrl->pcix_speed_capability = 0;        
1014                                                 break;
1015                                         default:
1016                                                 err(msg_HPC_not_supported);
1017                                                 rc = -ENODEV;
1018                                                 goto err_free_ctrl;
1019                                 }
1020                                 break;
1021
1022                         case PCI_VENDOR_ID_INTEL:
1023                                 /* Check for speed capability (0=33, 1=66) */
1024                                 if (subsystem_deviceid & 0x0001) {
1025                                         ctrl->speed_capability = PCI_SPEED_66MHz;
1026                                 } else {
1027                                         ctrl->speed_capability = PCI_SPEED_33MHz;
1028                                 }
1029
1030                                 /* Check for push button */
1031                                 if (subsystem_deviceid & 0x0002) {
1032                                         /* no push button */
1033                                         ctrl->push_button = 0;
1034                                 } else {
1035                                         /* push button supported */
1036                                         ctrl->push_button = 1;
1037                                 }
1038
1039                                 /* Check for slot switch type (0=mechanical, 1=not mechanical) */
1040                                 if (subsystem_deviceid & 0x0004) {
1041                                         /* no switch */
1042                                         ctrl->slot_switch_type = 0;
1043                                 } else {
1044                                         /* switch */
1045                                         ctrl->slot_switch_type = 1;
1046                                 }
1047
1048                                 /* PHP Status (0=De-feature PHP, 1=Normal operation) */
1049                                 if (subsystem_deviceid & 0x0008) {
1050                                         ctrl->defeature_PHP = 1;        /* PHP supported */
1051                                 } else {
1052                                         ctrl->defeature_PHP = 0;        /* PHP not supported */
1053                                 }
1054
1055                                 /* Alternate Base Address Register Interface (0=not supported, 1=supported) */
1056                                 if (subsystem_deviceid & 0x0010) {
1057                                         ctrl->alternate_base_address = 1;       /* supported */
1058                                 } else {
1059                                         ctrl->alternate_base_address = 0;       /* not supported */
1060                                 }
1061
1062                                 /* PCI Config Space Index (0=not supported, 1=supported) */
1063                                 if (subsystem_deviceid & 0x0020) {
1064                                         ctrl->pci_config_space = 1;             /* supported */
1065                                 } else {
1066                                         ctrl->pci_config_space = 0;             /* not supported */
1067                                 }
1068
1069                                 /* PCI-X support */
1070                                 if (subsystem_deviceid & 0x0080) {
1071                                         /* PCI-X capable */
1072                                         ctrl->pcix_support = 1;
1073                                         /* Frequency of operation in PCI-X mode */
1074                                         if (subsystem_deviceid & 0x0040) {
1075                                                 /* 133MHz PCI-X if bit 7 is 1 */
1076                                                 ctrl->pcix_speed_capability = 1;
1077                                         } else {
1078                                                 /* 100MHz PCI-X if bit 7 is 1 and bit 0 is 0, */
1079                                                 /* 66MHz PCI-X if bit 7 is 1 and bit 0 is 1 */
1080                                                 ctrl->pcix_speed_capability = 0;
1081                                         }
1082                                 } else {
1083                                         /* Conventional PCI */
1084                                         ctrl->pcix_support = 0;
1085                                         ctrl->pcix_speed_capability = 0;
1086                                 }
1087                                 break;
1088
1089                         default:
1090                                 err(msg_HPC_not_supported);
1091                                 rc = -ENODEV;
1092                                 goto err_free_ctrl;
1093                 }
1094
1095         } else {
1096                 err(msg_HPC_not_supported);
1097                 return -ENODEV;
1098         }
1099
1100         /* Tell the user that we found one. */
1101         info("Initializing the PCI hot plug controller residing on PCI bus %d\n",
1102                                         pdev->bus->number);
1103
1104         dbg("Hotplug controller capabilities:\n");
1105         dbg("    speed_capability       %d\n", ctrl->speed_capability);
1106         dbg("    slot_switch_type       %s\n", ctrl->slot_switch_type ?
1107                                         "switch present" : "no switch");
1108         dbg("    defeature_PHP          %s\n", ctrl->defeature_PHP ?
1109                                         "PHP supported" : "PHP not supported");
1110         dbg("    alternate_base_address %s\n", ctrl->alternate_base_address ?
1111                                         "supported" : "not supported");
1112         dbg("    pci_config_space       %s\n", ctrl->pci_config_space ?
1113                                         "supported" : "not supported");
1114         dbg("    pcix_speed_capability  %s\n", ctrl->pcix_speed_capability ?
1115                                         "supported" : "not supported");
1116         dbg("    pcix_support           %s\n", ctrl->pcix_support ?
1117                                         "supported" : "not supported");
1118
1119         ctrl->pci_dev = pdev;
1120         pci_set_drvdata(pdev, ctrl);
1121
1122         /* make our own copy of the pci bus structure,
1123          * as we like tweaking it a lot */
1124         ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
1125         if (!ctrl->pci_bus) {
1126                 err("out of memory\n");
1127                 rc = -ENOMEM;
1128                 goto err_free_ctrl;
1129         }
1130         memcpy(ctrl->pci_bus, pdev->bus, sizeof(*ctrl->pci_bus));
1131
1132         ctrl->bus = pdev->bus->number;
1133         ctrl->rev = pdev->revision;
1134         dbg("bus device function rev: %d %d %d %d\n", ctrl->bus,
1135                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), ctrl->rev);
1136
1137         mutex_init(&ctrl->crit_sect);
1138         init_waitqueue_head(&ctrl->queue);
1139
1140         /* initialize our threads if they haven't already been started up */
1141         rc = one_time_init();
1142         if (rc) {
1143                 goto err_free_bus;
1144         }
1145
1146         dbg("pdev = %p\n", pdev);
1147         dbg("pci resource start %llx\n", (unsigned long long)pci_resource_start(pdev, 0));
1148         dbg("pci resource len %llx\n", (unsigned long long)pci_resource_len(pdev, 0));
1149
1150         if (!request_mem_region(pci_resource_start(pdev, 0),
1151                                 pci_resource_len(pdev, 0), MY_NAME)) {
1152                 err("cannot reserve MMIO region\n");
1153                 rc = -ENOMEM;
1154                 goto err_free_bus;
1155         }
1156
1157         ctrl->hpc_reg = ioremap(pci_resource_start(pdev, 0),
1158                                         pci_resource_len(pdev, 0));
1159         if (!ctrl->hpc_reg) {
1160                 err("cannot remap MMIO region %llx @ %llx\n",
1161                     (unsigned long long)pci_resource_len(pdev, 0),
1162                     (unsigned long long)pci_resource_start(pdev, 0));
1163                 rc = -ENODEV;
1164                 goto err_free_mem_region;
1165         }
1166
1167         // Check for 66Mhz operation
1168         ctrl->speed = get_controller_speed(ctrl);
1169
1170
1171         /********************************************************
1172          *
1173          *              Save configuration headers for this and
1174          *              subordinate PCI buses
1175          *
1176          ********************************************************/
1177
1178         /* find the physical slot number of the first hot plug slot */
1179
1180         /* Get slot won't work for devices behind bridges, but
1181          * in this case it will always be called for the "base"
1182          * bus/dev/func of a slot.
1183          * CS: this is leveraging the PCIIRQ routing code from the kernel
1184          * (pci-pc.c: get_irq_routing_table) */
1185         rc = get_slot_mapping(ctrl->pci_bus, pdev->bus->number,
1186                                 (readb(ctrl->hpc_reg + SLOT_MASK) >> 4),
1187                                 &(ctrl->first_slot));
1188         dbg("get_slot_mapping: first_slot = %d, returned = %d\n",
1189                                 ctrl->first_slot, rc);
1190         if (rc) {
1191                 err(msg_initialization_err, rc);
1192                 goto err_iounmap;
1193         }
1194
1195         /* Store PCI Config Space for all devices on this bus */
1196         rc = cpqhp_save_config(ctrl, ctrl->bus, readb(ctrl->hpc_reg + SLOT_MASK));
1197         if (rc) {
1198                 err("%s: unable to save PCI configuration data, error %d\n",
1199                                 __func__, rc);
1200                 goto err_iounmap;
1201         }
1202
1203         /*
1204          * Get IO, memory, and IRQ resources for new devices
1205          */
1206         /* The next line is required for cpqhp_find_available_resources */
1207         ctrl->interrupt = pdev->irq;
1208         if (ctrl->interrupt < 0x10) {
1209                 cpqhp_legacy_mode = 1;
1210                 dbg("System seems to be configured for Full Table Mapped MPS mode\n");
1211         }
1212
1213         ctrl->cfgspc_irq = 0;
1214         pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &ctrl->cfgspc_irq);
1215
1216         rc = cpqhp_find_available_resources(ctrl, cpqhp_rom_start);
1217         ctrl->add_support = !rc;
1218         if (rc) {
1219                 dbg("cpqhp_find_available_resources = 0x%x\n", rc);
1220                 err("unable to locate PCI configuration resources for hot plug add.\n");
1221                 goto err_iounmap;
1222         }
1223
1224         /*
1225          * Finish setting up the hot plug ctrl device
1226          */
1227         ctrl->slot_device_offset = readb(ctrl->hpc_reg + SLOT_MASK) >> 4;
1228         dbg("NumSlots %d \n", ctrl->slot_device_offset);
1229
1230         ctrl->next_event = 0;
1231
1232         /* Setup the slot information structures */
1233         rc = ctrl_slot_setup(ctrl, smbios_start, smbios_table);
1234         if (rc) {
1235                 err(msg_initialization_err, 6);
1236                 err("%s: unable to save PCI configuration data, error %d\n",
1237                         __func__, rc);
1238                 goto err_iounmap;
1239         }
1240
1241         /* Mask all general input interrupts */
1242         writel(0xFFFFFFFFL, ctrl->hpc_reg + INT_MASK);
1243
1244         /* set up the interrupt */
1245         dbg("HPC interrupt = %d \n", ctrl->interrupt);
1246         if (request_irq(ctrl->interrupt, cpqhp_ctrl_intr,
1247                         IRQF_SHARED, MY_NAME, ctrl)) {
1248                 err("Can't get irq %d for the hotplug pci controller\n",
1249                         ctrl->interrupt);
1250                 rc = -ENODEV;
1251                 goto err_iounmap;
1252         }
1253
1254         /* Enable Shift Out interrupt and clear it, also enable SERR on power
1255          * fault
1256          */
1257         temp_word = readw(ctrl->hpc_reg + MISC);
1258         temp_word |= 0x4006;
1259         writew(temp_word, ctrl->hpc_reg + MISC);
1260
1261         /* Changed 05/05/97 to clear all interrupts at start */
1262         writel(0xFFFFFFFFL, ctrl->hpc_reg + INT_INPUT_CLEAR);
1263
1264         ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
1265
1266         writel(0x0L, ctrl->hpc_reg + INT_MASK);
1267
1268         if (!cpqhp_ctrl_list) {
1269                 cpqhp_ctrl_list = ctrl;
1270                 ctrl->next = NULL;
1271         } else {
1272                 ctrl->next = cpqhp_ctrl_list;
1273                 cpqhp_ctrl_list = ctrl;
1274         }
1275
1276         /* turn off empty slots here unless command line option "ON" set
1277          * Wait for exclusive access to hardware
1278          */
1279         mutex_lock(&ctrl->crit_sect);
1280
1281         num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0F;
1282
1283         /* find first device number for the ctrl */
1284         device = readb(ctrl->hpc_reg + SLOT_MASK) >> 4;
1285
1286         while (num_of_slots) {
1287                 dbg("num_of_slots: %d\n", num_of_slots);
1288                 func = cpqhp_slot_find(ctrl->bus, device, 0);
1289                 if (!func)
1290                         break;
1291
1292                 hp_slot = func->device - ctrl->slot_device_offset;
1293                 dbg("hp_slot: %d\n", hp_slot);
1294
1295                 /* We have to save the presence info for these slots */
1296                 temp_word = ctrl->ctrl_int_comp >> 16;
1297                 func->presence_save = (temp_word >> hp_slot) & 0x01;
1298                 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
1299
1300                 if (ctrl->ctrl_int_comp & (0x1L << hp_slot))
1301                         func->switch_save = 0;
1302                 else
1303                         func->switch_save = 0x10;
1304
1305                 if (!power_mode)
1306                         if (!func->is_a_board) {
1307                                 green_LED_off(ctrl, hp_slot);
1308                                 slot_disable(ctrl, hp_slot);
1309                         }
1310
1311                 device++;
1312                 num_of_slots--;
1313         }
1314
1315         if (!power_mode) {
1316                 set_SOGO(ctrl);
1317                 /* Wait for SOBS to be unset */
1318                 wait_for_ctrl_irq(ctrl);
1319         }
1320
1321         rc = init_SERR(ctrl);
1322         if (rc) {
1323                 err("init_SERR failed\n");
1324                 mutex_unlock(&ctrl->crit_sect);
1325                 goto err_free_irq;
1326         }
1327
1328         /* Done with exclusive hardware access */
1329         mutex_unlock(&ctrl->crit_sect);
1330
1331         cpqhp_create_debugfs_files(ctrl);
1332
1333         return 0;
1334
1335 err_free_irq:
1336         free_irq(ctrl->interrupt, ctrl);
1337 err_iounmap:
1338         iounmap(ctrl->hpc_reg);
1339 err_free_mem_region:
1340         release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
1341 err_free_bus:
1342         kfree(ctrl->pci_bus);
1343 err_free_ctrl:
1344         kfree(ctrl);
1345 err_disable_device:
1346         pci_disable_device(pdev);
1347         return rc;
1348 }
1349
1350 static void __exit unload_cpqphpd(void)
1351 {
1352         struct pci_func *next;
1353         struct pci_func *TempSlot;
1354         int loop;
1355         u32 rc;
1356         struct controller *ctrl;
1357         struct controller *tctrl;
1358         struct pci_resource *res;
1359         struct pci_resource *tres;
1360
1361         rc = compaq_nvram_store(cpqhp_rom_start);
1362
1363         ctrl = cpqhp_ctrl_list;
1364
1365         while (ctrl) {
1366                 if (ctrl->hpc_reg) {
1367                         u16 misc;
1368                         rc = read_slot_enable (ctrl);
1369
1370                         writeb(0, ctrl->hpc_reg + SLOT_SERR);
1371                         writel(0xFFFFFFC0L | ~rc, ctrl->hpc_reg + INT_MASK);
1372
1373                         misc = readw(ctrl->hpc_reg + MISC);
1374                         misc &= 0xFFFD;
1375                         writew(misc, ctrl->hpc_reg + MISC);
1376                 }
1377
1378                 ctrl_slot_cleanup(ctrl);
1379
1380                 res = ctrl->io_head;
1381                 while (res) {
1382                         tres = res;
1383                         res = res->next;
1384                         kfree(tres);
1385                 }
1386
1387                 res = ctrl->mem_head;
1388                 while (res) {
1389                         tres = res;
1390                         res = res->next;
1391                         kfree(tres);
1392                 }
1393
1394                 res = ctrl->p_mem_head;
1395                 while (res) {
1396                         tres = res;
1397                         res = res->next;
1398                         kfree(tres);
1399                 }
1400
1401                 res = ctrl->bus_head;
1402                 while (res) {
1403                         tres = res;
1404                         res = res->next;
1405                         kfree(tres);
1406                 }
1407
1408                 kfree (ctrl->pci_bus);
1409
1410                 tctrl = ctrl;
1411                 ctrl = ctrl->next;
1412                 kfree(tctrl);
1413         }
1414
1415         for (loop = 0; loop < 256; loop++) {
1416                 next = cpqhp_slot_list[loop];
1417                 while (next != NULL) {
1418                         res = next->io_head;
1419                         while (res) {
1420                                 tres = res;
1421                                 res = res->next;
1422                                 kfree(tres);
1423                         }
1424
1425                         res = next->mem_head;
1426                         while (res) {
1427                                 tres = res;
1428                                 res = res->next;
1429                                 kfree(tres);
1430                         }
1431
1432                         res = next->p_mem_head;
1433                         while (res) {
1434                                 tres = res;
1435                                 res = res->next;
1436                                 kfree(tres);
1437                         }
1438
1439                         res = next->bus_head;
1440                         while (res) {
1441                                 tres = res;
1442                                 res = res->next;
1443                                 kfree(tres);
1444                         }
1445
1446                         TempSlot = next;
1447                         next = next->next;
1448                         kfree(TempSlot);
1449                 }
1450         }
1451
1452         /* Stop the notification mechanism */
1453         if (initialized)
1454                 cpqhp_event_stop_thread();
1455
1456         /* unmap the rom address */
1457         if (cpqhp_rom_start)
1458                 iounmap(cpqhp_rom_start);
1459         if (smbios_start)
1460                 iounmap(smbios_start);
1461 }
1462
1463 static struct pci_device_id hpcd_pci_tbl[] = {
1464         {
1465         /* handle any PCI Hotplug controller */
1466         .class =        ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1467         .class_mask =   ~0,
1468
1469         /* no matter who makes it */
1470         .vendor =       PCI_ANY_ID,
1471         .device =       PCI_ANY_ID,
1472         .subvendor =    PCI_ANY_ID,
1473         .subdevice =    PCI_ANY_ID,
1474
1475         }, { /* end: all zeroes */ }
1476 };
1477
1478 MODULE_DEVICE_TABLE(pci, hpcd_pci_tbl);
1479
1480 static struct pci_driver cpqhpc_driver = {
1481         .name =         "compaq_pci_hotplug",
1482         .id_table =     hpcd_pci_tbl,
1483         .probe =        cpqhpc_probe,
1484         /* remove:      cpqhpc_remove_one, */
1485 };
1486
1487 static int __init cpqhpc_init(void)
1488 {
1489         int result;
1490
1491         cpqhp_debug = debug;
1492
1493         info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
1494         cpqhp_initialize_debugfs();
1495         result = pci_register_driver(&cpqhpc_driver);
1496         dbg("pci_register_driver = %d\n", result);
1497         return result;
1498 }
1499
1500 static void __exit cpqhpc_cleanup(void)
1501 {
1502         dbg("unload_cpqphpd()\n");
1503         unload_cpqphpd();
1504
1505         dbg("pci_unregister_driver\n");
1506         pci_unregister_driver(&cpqhpc_driver);
1507         cpqhp_shutdown_debugfs();
1508 }
1509
1510 module_init(cpqhpc_init);
1511 module_exit(cpqhpc_cleanup);