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