ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pci / hotplug / shpchp_pci.c
1 /*
2  * Standard 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  * Copyright (C) 2003-2004 Intel Corporation
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <greg@kroah.com>, <dely.l.sy@intel.com>
27  *
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
36 #include <linux/proc_fs.h>
37 #include <linux/pci.h>
38 #include "../pci.h"
39 #include "shpchp.h"
40 #ifndef CONFIG_IA64
41 #include "../../../arch/i386/pci/pci.h"    /* horrible hack showing how processor dependant we are... */
42 #endif
43
44 int shpchp_configure_device (struct controller* ctrl, struct pci_func* func)  
45 {
46         unsigned char bus;
47         struct pci_bus *child;
48         int num;
49
50         if (func->pci_dev == NULL)
51                 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
52
53         /* Still NULL ? Well then scan for it ! */
54         if (func->pci_dev == NULL) {
55                 num = pci_scan_slot(ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function));
56                 if (num)
57                         dbg("%s: subordiante %p number %x\n", __FUNCTION__, ctrl->pci_dev->subordinate,
58                                 ctrl->pci_dev->subordinate->number);
59                         pci_bus_add_devices(ctrl->pci_dev->subordinate);                
60                 
61                 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
62                 if (func->pci_dev == NULL) {
63                         dbg("ERROR: pci_dev still null\n");
64                         return 0;
65                 }
66         }
67
68         if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
69                 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
70                 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
71                 pci_do_scan_bus(child);
72
73         }
74
75         return 0;
76 }
77
78
79 int shpchp_unconfigure_device(struct pci_func* func) 
80 {
81         int rc = 0;
82         int j;
83         
84         dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus, func->device, func->function);
85
86         for (j=0; j<8 ; j++) {
87                 struct pci_dev* temp = pci_find_slot(func->bus, (func->device << 3) | j);
88                 if (temp) {
89                         pci_remove_bus_device(temp);
90                 }
91         }
92         return rc;
93 }
94
95 /*
96  * shpchp_set_irq
97  *
98  * @bus_num: bus number of PCI device
99  * @dev_num: device number of PCI device
100  * @slot: pointer to u8 where slot number will be returned
101  */
102 int shpchp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
103 {
104 #if defined(CONFIG_X86) && !defined(CONFIG_X86_IO_APIC) && !defined(CONFIG_X86_64)
105         int rc;
106         u16 temp_word;
107         struct pci_dev fakedev;
108         struct pci_bus fakebus;
109
110         fakedev.devfn = dev_num << 3;
111         fakedev.bus = &fakebus;
112         fakebus.number = bus_num;
113         dbg("%s: dev %d, bus %d, pin %d, num %d\n",
114             __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
115         rc = pcibios_set_irq_routing(&fakedev, int_pin - 0x0a, irq_num);
116         dbg("%s: rc %d\n", __FUNCTION__, rc);
117         if (!rc)
118                 return !rc;
119
120         /* set the Edge Level Control Register (ELCR) */
121         temp_word = inb(0x4d0);
122         temp_word |= inb(0x4d1) << 8;
123
124         temp_word |= 0x01 << irq_num;
125
126         /* This should only be for x86 as it sets the Edge Level Control Register */
127         outb((u8) (temp_word & 0xFF), 0x4d0);
128         outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
129 #endif
130         return 0;
131 }
132
133 /* More PCI configuration routines; this time centered around hotplug controller */
134
135
136 /*
137  * shpchp_save_config
138  *
139  * Reads configuration for all slots in a PCI bus and saves info.
140  *
141  * Note:  For non-hot plug busses, the slot # saved is the device #
142  *
143  * returns 0 if success
144  */
145 int shpchp_save_config(struct controller *ctrl, int busnumber, int num_ctlr_slots, int first_device_num)
146 {
147         int rc;
148         u8 class_code;
149         u8 header_type;
150         u32 ID;
151         u8 secondary_bus;
152         struct pci_func *new_slot;
153         int sub_bus;
154         int FirstSupported;
155         int LastSupported;
156         int max_functions;
157         int function;
158         u8 DevError;
159         int device = 0;
160         int cloop = 0;
161         int stop_it;
162         int index;
163         int is_hot_plug = num_ctlr_slots || first_device_num;
164         struct pci_bus lpci_bus, *pci_bus;
165
166         dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__, num_ctlr_slots, first_device_num);
167
168         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
169         pci_bus = &lpci_bus;
170
171         dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__, num_ctlr_slots, first_device_num);
172
173         /*   Decide which slots are supported */
174         if (is_hot_plug) {
175                 /*********************************
176                  *  is_hot_plug is the slot mask
177                  *********************************/
178                 FirstSupported = first_device_num;
179                 LastSupported = FirstSupported + num_ctlr_slots - 1;
180         } else {
181                 FirstSupported = 0;
182                 LastSupported = 0x1F;
183         }
184
185         dbg("FirstSupported = %d, LastSupported = %d\n", FirstSupported, LastSupported);
186
187         /*   Save PCI configuration space for all devices in supported slots */
188         pci_bus->number = busnumber;
189         for (device = FirstSupported; device <= LastSupported; device++) {
190                 ID = 0xFFFFFFFF;
191                 rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
192
193                 if (ID != 0xFFFFFFFF) {   /*  device in slot */
194                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
195                         if (rc)
196                                 return rc;
197
198                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
199                         if (rc)
200                                 return rc;
201
202                         dbg("class_code = %x, header_type = %x\n", class_code, header_type);
203
204                         /* If multi-function device, set max_functions to 8 */
205                         if (header_type & 0x80)
206                                 max_functions = 8;
207                         else
208                                 max_functions = 1;
209
210                         function = 0;
211
212                         do {
213                                 DevError = 0;
214
215                                 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {   /* P-P Bridge */
216                                         /* Recurse the subordinate bus
217                                          * get the subordinate bus number
218                                          */
219                                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), 
220                                                 PCI_SECONDARY_BUS, &secondary_bus);
221                                         if (rc) {
222                                                 return rc;
223                                         } else {
224                                                 sub_bus = (int) secondary_bus;
225
226                                                 /* Save secondary bus cfg spc with this recursive call. */
227                                                 rc = shpchp_save_config(ctrl, sub_bus, 0, 0);
228                                                 if (rc)
229                                                         return rc;
230                                         }
231                                 }
232
233                                 index = 0;
234                                 new_slot = shpchp_slot_find(busnumber, device, index++);
235
236                                 dbg("new_slot = %p\n", new_slot);
237
238                                 while (new_slot && (new_slot->function != (u8) function)) {
239                                         new_slot = shpchp_slot_find(busnumber, device, index++);
240                                         dbg("new_slot = %p\n", new_slot);
241                                 }
242                                 if (!new_slot) {
243                                         /* Setup slot structure. */
244                                         new_slot = shpchp_slot_create(busnumber);
245                                         dbg("new_slot = %p\n", new_slot);
246
247                                         if (new_slot == NULL)
248                                                 return(1);
249                                 }
250
251                                 new_slot->bus = (u8) busnumber;
252                                 new_slot->device = (u8) device;
253                                 new_slot->function = (u8) function;
254                                 new_slot->is_a_board = 1;
255                                 new_slot->switch_save = 0x10;
256                                 /* In case of unsupported board */
257                                 new_slot->status = DevError;
258                                 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
259                                 dbg("new_slot->pci_dev = %p\n", new_slot->pci_dev);
260
261                                 for (cloop = 0; cloop < 0x20; cloop++) {
262                                         rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, function), 
263                                                 cloop << 2, (u32 *) & (new_slot->config_space [cloop]));
264                                         /* dbg("new_slot->config_space[%x] = %x\n", cloop, new_slot->config_space[cloop]); */
265                                         if (rc)
266                                                 return rc;
267                                 }
268
269                                 function++;
270
271                                 stop_it = 0;
272
273                                 /*  this loop skips to the next present function
274                                  *  reading in Class Code and Header type.
275                                  */
276
277                                 while ((function < max_functions)&&(!stop_it)) {
278                                         rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
279
280                                         if (ID == 0xFFFFFFFF) {  /* nothing there. */
281                                                 function++;
282                                                 dbg("Nothing there\n");
283                                         } else {  /* Something there */
284                                                 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), 
285                                                         0x0B, &class_code);
286                                                 if (rc)
287                                                         return rc;
288
289                                                 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), 
290                                                         PCI_HEADER_TYPE, &header_type);
291                                                 if (rc)
292                                                         return rc;
293
294                                                 dbg("class_code = %x, header_type = %x\n", class_code, header_type);
295                                                 stop_it++;
296                                         }
297                                 }
298
299                         } while (function < max_functions);
300                         /* End of IF (device in slot?) */
301                 } else if (is_hot_plug) {
302                         /* Setup slot structure with entry for empty slot */
303                         new_slot = shpchp_slot_create(busnumber);
304
305                         if (new_slot == NULL) {
306                                 return(1);
307                         }
308                         dbg("new_slot = %p\n", new_slot);
309
310                         new_slot->bus = (u8) busnumber;
311                         new_slot->device = (u8) device;
312                         new_slot->function = 0;
313                         new_slot->is_a_board = 0;
314                         new_slot->presence_save = 0;
315                         new_slot->switch_save = 0;
316                 }
317         }                       /* End of FOR loop */
318
319         return(0);
320 }
321
322
323 /*
324  * shpchp_save_slot_config
325  *
326  * Saves configuration info for all PCI devices in a given slot
327  * including subordinate busses.
328  *
329  * returns 0 if success
330  */
331 int shpchp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
332 {
333         int rc;
334         u8 class_code;
335         u8 header_type;
336         u32 ID;
337         u8 secondary_bus;
338         int sub_bus;
339         int max_functions;
340         int function;
341         int cloop = 0;
342         int stop_it;
343         struct pci_bus lpci_bus, *pci_bus;
344         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
345         pci_bus = &lpci_bus;
346         pci_bus->number = new_slot->bus;
347
348         ID = 0xFFFFFFFF;
349
350         pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
351
352         if (ID != 0xFFFFFFFF) {   /*  device in slot */
353                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
354
355                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
356
357                 if (header_type & 0x80) /* Multi-function device */
358                         max_functions = 8;
359                 else
360                         max_functions = 1;
361
362                 function = 0;
363
364                 do {
365                         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {     /* PCI-PCI Bridge */
366                                 /*  Recurse the subordinate bus */
367                                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), 
368                                         PCI_SECONDARY_BUS, &secondary_bus);
369
370                                 sub_bus = (int) secondary_bus;
371
372                                 /* Save the config headers for the secondary bus. */
373                                 rc = shpchp_save_config(ctrl, sub_bus, 0, 0);
374
375                                 if (rc)
376                                         return(rc);
377
378                         }       /* End of IF */
379
380                         new_slot->status = 0;
381
382                         for (cloop = 0; cloop < 0x20; cloop++) {
383                                 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, function), 
384                                         cloop << 2, (u32 *) & (new_slot->config_space [cloop]));
385                         }
386
387                         function++;
388
389                         stop_it = 0;
390
391                         /*  this loop skips to the next present function
392                          *  reading in the Class Code and the Header type.
393                          */
394
395                         while ((function < max_functions) && (!stop_it)) {
396                                 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
397
398                                 if (ID == 0xFFFFFFFF) {  /* nothing there. */
399                                         function++;
400                                 } else {  /* Something there */
401                                         pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
402
403                                         pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, 
404                                                 &header_type);
405
406                                         stop_it++;
407                                 }
408                         }
409
410                 } while (function < max_functions);
411         }                       /* End of IF (device in slot?) */
412         else {
413                 return(2);
414         }
415
416         return(0);
417 }
418
419
420 /*
421  * shpchp_save_used_resources
422  *
423  * Stores used resource information for existing boards.  this is
424  * for boards that were in the system when this driver was loaded.
425  * this function is for hot plug ADD
426  *
427  * returns 0 if success
428  * if disable  == 1(DISABLE_CARD),
429  *  it loops for all functions of the slot and disables them.
430  * else, it just get resources of the function and return.
431  */
432 int shpchp_save_used_resources (struct controller *ctrl, struct pci_func *func, int disable)
433 {
434         u8 cloop;
435         u8 header_type;
436         u8 secondary_bus;
437         u8 temp_byte;
438         u16 command;
439         u16 save_command;
440         u16 w_base, w_length;
441         u32 temp_register;
442         u32 save_base;
443         u32 base, length;
444         u64 base64 = 0;
445         int index = 0;
446         unsigned int devfn;
447         struct pci_resource *mem_node = NULL;
448         struct pci_resource *p_mem_node = NULL;
449         struct pci_resource *t_mem_node;
450         struct pci_resource *io_node;
451         struct pci_resource *bus_node;
452         struct pci_bus lpci_bus, *pci_bus;
453         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
454         pci_bus = &lpci_bus;
455
456         if (disable)
457                 func = shpchp_slot_find(func->bus, func->device, index++);
458
459         while ((func != NULL) && func->is_a_board) {
460                 pci_bus->number = func->bus;
461                 devfn = PCI_DEVFN(func->device, func->function);
462
463                 /* Save the command register */
464                 pci_bus_read_config_word (pci_bus, devfn, PCI_COMMAND, &save_command);
465
466                 if (disable) {
467                         /* disable card */
468                         command = 0x00;
469                         pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
470                 }
471
472                 /* Check for Bridge */
473                 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
474
475                 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {     /* PCI-PCI Bridge */
476                         dbg("Save_used_res of PCI bridge b:d=0x%x:%x, sc=0x%x\n", func->bus, func->device, save_command);
477                         if (disable) {
478                                 /* Clear Bridge Control Register */
479                                 command = 0x00;
480                                 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
481                         }
482
483                         pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
484                         pci_bus_read_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
485
486                         bus_node =(struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
487                         if (!bus_node)
488                                 return -ENOMEM;
489
490                         bus_node->base = (ulong)secondary_bus;
491                         bus_node->length = (ulong)(temp_byte - secondary_bus + 1);
492
493                         bus_node->next = func->bus_head;
494                         func->bus_head = bus_node;
495
496                         /* Save IO base and Limit registers */
497                         pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_BASE, &temp_byte);
498                         base = temp_byte;
499                         pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_LIMIT, &temp_byte);
500                         length = temp_byte;
501
502                         if ((base <= length) && (!disable || (save_command & PCI_COMMAND_IO))) {
503                                 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
504                                 if (!io_node)
505                                         return -ENOMEM;
506
507                                 io_node->base = (ulong)(base & PCI_IO_RANGE_MASK) << 8;
508                                 io_node->length = (ulong)(length - base + 0x10) << 8;
509
510                                 io_node->next = func->io_head;
511                                 func->io_head = io_node;
512                         }
513
514                         /* Save memory base and Limit registers */
515                         pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
516                         pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
517
518                         if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
519                                 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
520                                 if (!mem_node)
521                                         return -ENOMEM;
522
523                                 mem_node->base = (ulong)w_base << 16;
524                                 mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
525
526                                 mem_node->next = func->mem_head;
527                                 func->mem_head = mem_node;
528                         }
529                         /* Save prefetchable memory base and Limit registers */
530                         pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
531                         pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
532
533                         if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
534                                 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
535                                 if (!p_mem_node)
536                                         return -ENOMEM;
537
538                                 p_mem_node->base = (ulong)w_base << 16;
539                                 p_mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
540
541                                 p_mem_node->next = func->p_mem_head;
542                                 func->p_mem_head = p_mem_node;
543                         }
544                 } else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
545                         dbg("Save_used_res of PCI adapter b:d=0x%x:%x, sc=0x%x\n", func->bus, func->device, save_command);
546
547                         /* Figure out IO and memory base lengths */
548                         for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
549                                 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
550
551                                 temp_register = 0xFFFFFFFF;
552                                 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
553                                 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
554
555                                 if (!disable) {
556                                         pci_bus_write_config_dword (pci_bus, devfn, cloop, save_base);
557                                 }
558
559                                 if (!temp_register)
560                                         continue;
561
562                                 base = temp_register;
563
564                                 if ((base & PCI_BASE_ADDRESS_SPACE_IO) && (!disable || (save_command & PCI_COMMAND_IO))) {
565                                         /* IO base */
566                                         /* set temp_register = amount of IO space requested */
567                                         base = base & 0xFFFFFFFCL;
568                                         base = (~base) + 1;
569
570                                         io_node = (struct pci_resource *) kmalloc(sizeof (struct pci_resource), GFP_KERNEL);
571                                         if (!io_node)
572                                                 return -ENOMEM;
573
574                                         io_node->base = (ulong)save_base & PCI_BASE_ADDRESS_IO_MASK;
575                                         io_node->length = (ulong)base;
576                                         dbg("sur adapter: IO bar=0x%x(length=0x%x)\n", io_node->base, io_node->length);
577
578                                         io_node->next = func->io_head;
579                                         func->io_head = io_node;
580                                 } else {  /* map Memory */
581                                         int prefetchable = 1;
582                                         /* struct pci_resources **res_node; */
583                                         char *res_type_str = "PMEM";
584                                         u32 temp_register2;
585
586                                         t_mem_node = (struct pci_resource *) kmalloc(sizeof (struct pci_resource), GFP_KERNEL);
587                                         if (!t_mem_node)
588                                                 return -ENOMEM;
589
590                                         if (!(base & PCI_BASE_ADDRESS_MEM_PREFETCH) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
591                                                 prefetchable = 0;
592                                                 mem_node = t_mem_node;
593                                                 res_type_str++;
594                                         } else
595                                                 p_mem_node = t_mem_node;
596
597                                         base = base & 0xFFFFFFF0L;
598                                         base = (~base) + 1;
599
600                                         switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
601                                         case PCI_BASE_ADDRESS_MEM_TYPE_32:
602                                                 if (prefetchable) {
603                                                         p_mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
604                                                         p_mem_node->length = (ulong)base;
605                                                         dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n", res_type_str, 
606                                                                 p_mem_node->base, p_mem_node->length);
607
608                                                         p_mem_node->next = func->p_mem_head;
609                                                         func->p_mem_head = p_mem_node;
610                                                 } else {
611                                                         mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
612                                                         mem_node->length = (ulong)base;
613                                                         dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n", res_type_str, 
614                                                                 mem_node->base, mem_node->length);
615
616                                                         mem_node->next = func->mem_head;
617                                                         func->mem_head = mem_node;
618                                                 }
619                                                 break;
620                                         case PCI_BASE_ADDRESS_MEM_TYPE_64:
621                                                 pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
622                                                 base64 = temp_register2;
623                                                 base64 = (base64 << 32) | save_base;
624
625                                                 if (temp_register2) {
626                                                         dbg("sur adapter: 64 %s high dword of base64(0x%x:%x) masked to 0\n", 
627                                                                 res_type_str, temp_register2, (u32)base64);
628                                                         base64 &= 0x00000000FFFFFFFFL;
629                                                 }
630
631                                                 if (prefetchable) {
632                                                         p_mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
633                                                         p_mem_node->length = base;
634                                                         dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n", res_type_str, 
635                                                                 p_mem_node->base, p_mem_node->length);
636
637                                                         p_mem_node->next = func->p_mem_head;
638                                                         func->p_mem_head = p_mem_node;
639                                                 } else {
640                                                         mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
641                                                         mem_node->length = base;
642                                                         dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n", res_type_str, 
643                                                                 mem_node->base, mem_node->length);
644
645                                                         mem_node->next = func->mem_head;
646                                                         func->mem_head = mem_node;
647                                                 }
648                                                 cloop += 4;
649                                                 break;
650                                         default:
651                                                 dbg("asur: reserved BAR type=0x%x\n", temp_register);
652                                                 break;
653                                         }
654                                 } 
655                         }       /* End of base register loop */
656                 } else {        /* Some other unknown header type */
657                         dbg("Save_used_res of PCI unknown type b:d=0x%x:%x. skip.\n", func->bus, func->device);
658                 }
659
660                 /* find the next device in this slot */
661                 if (!disable)
662                         break;
663                 func = shpchp_slot_find(func->bus, func->device, index++);
664         }
665
666         return(0);
667 }
668
669
670 /*
671  * shpchp_return_board_resources
672  *
673  * this routine returns all resources allocated to a board to
674  * the available pool.
675  *
676  * returns 0 if success
677  */
678 int shpchp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
679 {
680         int rc = 0;
681         struct pci_resource *node;
682         struct pci_resource *t_node;
683         dbg("%s\n", __FUNCTION__);
684
685         if (!func)
686                 return(1);
687
688         node = func->io_head;
689         func->io_head = NULL;
690         while (node) {
691                 t_node = node->next;
692                 return_resource(&(resources->io_head), node);
693                 node = t_node;
694         }
695
696         node = func->mem_head;
697         func->mem_head = NULL;
698         while (node) {
699                 t_node = node->next;
700                 return_resource(&(resources->mem_head), node);
701                 node = t_node;
702         }
703
704         node = func->p_mem_head;
705         func->p_mem_head = NULL;
706         while (node) {
707                 t_node = node->next;
708                 return_resource(&(resources->p_mem_head), node);
709                 node = t_node;
710         }
711
712         node = func->bus_head;
713         func->bus_head = NULL;
714         while (node) {
715                 t_node = node->next;
716                 return_resource(&(resources->bus_head), node);
717                 node = t_node;
718         }
719
720         rc |= shpchp_resource_sort_and_combine(&(resources->mem_head));
721         rc |= shpchp_resource_sort_and_combine(&(resources->p_mem_head));
722         rc |= shpchp_resource_sort_and_combine(&(resources->io_head));
723         rc |= shpchp_resource_sort_and_combine(&(resources->bus_head));
724
725         return(rc);
726 }
727
728
729 /*
730  * shpchp_destroy_resource_list
731  *
732  * Puts node back in the resource list pointed to by head
733  */
734 void shpchp_destroy_resource_list (struct resource_lists * resources)
735 {
736         struct pci_resource *res, *tres;
737
738         res = resources->io_head;
739         resources->io_head = NULL;
740
741         while (res) {
742                 tres = res;
743                 res = res->next;
744                 kfree(tres);
745         }
746
747         res = resources->mem_head;
748         resources->mem_head = NULL;
749
750         while (res) {
751                 tres = res;
752                 res = res->next;
753                 kfree(tres);
754         }
755
756         res = resources->p_mem_head;
757         resources->p_mem_head = NULL;
758
759         while (res) {
760                 tres = res;
761                 res = res->next;
762                 kfree(tres);
763         }
764
765         res = resources->bus_head;
766         resources->bus_head = NULL;
767
768         while (res) {
769                 tres = res;
770                 res = res->next;
771                 kfree(tres);
772         }
773 }
774
775
776 /*
777  * shpchp_destroy_board_resources
778  *
779  * Puts node back in the resource list pointed to by head
780  */
781 void shpchp_destroy_board_resources (struct pci_func * func)
782 {
783         struct pci_resource *res, *tres;
784
785         res = func->io_head;
786         func->io_head = NULL;
787
788         while (res) {
789                 tres = res;
790                 res = res->next;
791                 kfree(tres);
792         }
793
794         res = func->mem_head;
795         func->mem_head = NULL;
796
797         while (res) {
798                 tres = res;
799                 res = res->next;
800                 kfree(tres);
801         }
802
803         res = func->p_mem_head;
804         func->p_mem_head = NULL;
805
806         while (res) {
807                 tres = res;
808                 res = res->next;
809                 kfree(tres);
810         }
811
812         res = func->bus_head;
813         func->bus_head = NULL;
814
815         while (res) {
816                 tres = res;
817                 res = res->next;
818                 kfree(tres);
819         }
820 }
821