brute-forced more changes from MontaVista's tree. SCSI partition table read still...
[linux-2.4.git] / drivers / 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/tqueue.h>
36 #include <linux/proc_fs.h>
37 #include <linux/pci.h>
38 #include "shpchp.h"
39 #ifndef CONFIG_IA64
40 #include "../../arch/i386/kernel/pci-i386.h"    /* horrible hack showing how processor dependant we are... */
41 #endif
42
43 static int is_pci_dev_in_use(struct pci_dev* dev) 
44 {
45         /* 
46          * dev->driver will be set if the device is in use by a new-style 
47          * driver -- otherwise, check the device's regions to see if any
48          * driver has claimed them
49          */
50
51         int i, inuse=0;
52
53         if (dev->driver) return 1; /* Assume driver feels responsible */
54
55         for (i = 0; !dev->driver && !inuse && (i < 6); i++) {
56                 if (!pci_resource_start(dev, i))
57                         continue;
58
59                 if (pci_resource_flags(dev, i) & IORESOURCE_IO)
60                         inuse = check_region(pci_resource_start(dev, i),
61                                              pci_resource_len(dev, i));
62                 else if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
63                         inuse = check_mem_region(pci_resource_start(dev, i),
64                                                  pci_resource_len(dev, i));
65         }
66
67         return inuse;
68
69 }
70
71
72 static int pci_hp_remove_device(struct pci_dev *dev)
73 {
74         if (is_pci_dev_in_use(dev)) {
75                 err("***Cannot safely power down device -- "
76                        "it appears to be in use***\n");
77                 return -EBUSY;
78         }
79         pci_remove_device(dev);
80         return 0;
81 }
82
83
84 static int configure_visit_pci_dev (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus) 
85 {
86         struct pci_bus* bus = wrapped_bus->bus;
87         struct pci_dev* dev = wrapped_dev->dev;
88         struct pci_func *temp_func;
89         int i=0;
90
91         dbg("%s: Enter\n", __FUNCTION__);
92         /* We need to fix up the hotplug function representation with the linux representation */
93         do {
94                 temp_func = shpchp_slot_find(dev->bus->number, dev->devfn >> 3, i++);
95                 dbg("%s: i %d temp_func %p, bus %x dev %x\n", __FUNCTION__, i, temp_func, 
96                         dev->bus->number, dev->devfn >>3);
97         } while (temp_func && (temp_func->function != (dev->devfn & 0x07)));
98
99         if (temp_func) {
100                 temp_func->pci_dev = dev;
101                 dbg("%s: dev %p dev->irq %x\n", __FUNCTION__, dev, dev->irq);
102         } else {
103                 /* We did not even find a hotplug rep of the function, create it
104                  * This code might be taken out if we can guarantee the creation of functions
105                  * in parallel (hotplug and Linux at the same time).
106                  */
107                 dbg("@@@@@@@@@@@ shpchp_slot_create in %s\n", __FUNCTION__);
108                 temp_func = shpchp_slot_create(bus->number);
109                 if (temp_func == NULL)
110                         return -ENOMEM;
111                 temp_func->pci_dev = dev;
112         }
113
114         /* Create /proc/bus/pci proc entry for this device and bus device is on */
115         /* Notify the drivers of the change */
116         if (temp_func->pci_dev) {
117                 dbg("%s: PCI_ID=%04X:%04X\n", __FUNCTION__, temp_func->pci_dev->vendor,
118                         temp_func->pci_dev->device);
119                 dbg("%s: PCI BUS %x DEVFN %x\n", __FUNCTION__, temp_func->pci_dev->bus->number,
120                         temp_func->pci_dev->devfn);
121                 dbg("%s: PCI_SLOT_NAME %s\n", __FUNCTION__, 
122                         temp_func->pci_dev->slot_name);
123                 pci_enable_device(temp_func->pci_dev);
124                 pci_proc_attach_device(temp_func->pci_dev);
125                 pci_announce_device_to_drivers(temp_func->pci_dev);
126         }
127
128         return 0;
129 }
130
131
132 static int unconfigure_visit_pci_dev_phase2 (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus) 
133 {
134         struct pci_dev* dev = wrapped_dev->dev;
135
136         struct pci_func *temp_func;
137         int i=0;
138
139         /* We need to remove the hotplug function representation with the linux representation */
140         do {
141                 temp_func = shpchp_slot_find(dev->bus->number, dev->devfn >> 3, i++);
142                 if (temp_func) {
143                         dbg("temp_func->function = %d\n", temp_func->function);
144                 }
145         } while (temp_func && (temp_func->function != (dev->devfn & 0x07)));
146
147         /* Now, remove the Linux Representation */
148         if (dev) {
149                 if (pci_hp_remove_device(dev) == 0) {
150                         kfree(dev); /* Now, remove */
151                 } else {
152                         return -1; /* problems while freeing, abort visitation */
153                 }
154         }
155
156         if (temp_func) {
157                 temp_func->pci_dev = NULL;
158         } else {
159                 dbg("No pci_func representation for bus, devfn = %d, %x\n", dev->bus->number, dev->devfn);
160         }
161
162         return 0;
163 }
164
165
166 static int unconfigure_visit_pci_bus_phase2 (struct pci_bus_wrapped *wrapped_bus, struct pci_dev_wrapped *wrapped_dev) 
167 {
168         struct pci_bus* bus = wrapped_bus->bus;
169
170         /* The cleanup code for proc entries regarding buses should be in the kernel...*/
171         if (bus->procdir)
172                 dbg("detach_pci_bus %s\n", bus->procdir->name);
173         pci_proc_detach_bus(bus);
174         /* The cleanup code should live in the kernel... */
175         bus->self->subordinate = NULL;
176         /* Unlink from parent bus */
177         list_del(&bus->node);
178
179         /* Now, remove */
180         if (bus)
181                 kfree(bus);
182
183         return 0;
184 }
185
186
187 static int unconfigure_visit_pci_dev_phase1 (struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_bus) 
188 {
189         struct pci_dev* dev = wrapped_dev->dev;
190         int rc;
191
192         dbg("attempting removal of driver for device (%x, %x, %x)\n", dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
193         /* Now, remove the Linux Driver Representation */
194         if (dev->driver) {
195                 if (dev->driver->remove) {
196                         dev->driver->remove(dev);
197                         dbg("driver was properly removed\n");
198                 }
199                 dev->driver = NULL;
200         }
201
202         rc = is_pci_dev_in_use(dev);
203         if (rc)
204                 info("%s: device still in use\n", __FUNCTION__);
205         return rc;
206 }
207
208
209 static struct pci_visit configure_functions = {
210         .visit_pci_dev =        configure_visit_pci_dev,
211 };
212
213
214 static struct pci_visit unconfigure_functions_phase1 = {
215         .post_visit_pci_dev =   unconfigure_visit_pci_dev_phase1
216 };
217
218 static struct pci_visit unconfigure_functions_phase2 = {
219         .post_visit_pci_bus =   unconfigure_visit_pci_bus_phase2,               
220         .post_visit_pci_dev =   unconfigure_visit_pci_dev_phase2
221 };
222
223
224 int shpchp_configure_device (struct controller* ctrl, struct pci_func* func)  
225 {
226         unsigned char bus;
227         struct pci_dev dev0;
228         struct pci_bus *child;
229         struct pci_dev* temp;
230         int rc = 0;
231
232         struct pci_dev_wrapped wrapped_dev;
233         struct pci_bus_wrapped wrapped_bus;
234         
235         dbg("%s: Enter\n", __FUNCTION__);
236         memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
237         memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
238
239         memset(&dev0, 0, sizeof(struct pci_dev));
240
241         dbg("%s: func->pci_dev %p\n", __FUNCTION__, func->pci_dev);
242         if (func->pci_dev != NULL)
243                 dbg("%s: func->pci_dev->irq %x\n", __FUNCTION__, func->pci_dev->irq);
244         if (func->pci_dev == NULL)
245                 func->pci_dev = pci_find_slot(func->bus, (func->device << 3) | (func->function & 0x7));
246         dbg("%s: after pci_find_slot, func->pci_dev %p\n", __FUNCTION__, func->pci_dev);
247         if (func->pci_dev != NULL)
248                 dbg("%s: after pci_find_slot, func->pci_dev->irq %x\n", __FUNCTION__, func->pci_dev->irq);
249
250         /* Still NULL ? Well then scan for it ! */
251         if (func->pci_dev == NULL) {
252                 dbg("%s: pci_dev still null. do pci_scan_slot\n", __FUNCTION__);
253                 dev0.bus = ctrl->pci_dev->subordinate;
254                 dbg("%s: dev0.bus %p\n", __FUNCTION__, dev0.bus);
255                 dev0.bus->number = func->bus;
256                 dbg("%s: dev0.bus->number %x\n", __FUNCTION__, func->bus);
257                 dev0.devfn = PCI_DEVFN(func->device, func->function);
258                 dev0.sysdata = ctrl->pci_dev->sysdata;
259
260                 /* this will generate pci_dev structures for all functions, 
261                  * but we will only call this case when lookup fails
262                  */
263                 dbg("%s: dev0.irq %x\n", __FUNCTION__, dev0.irq);
264                 func->pci_dev = pci_scan_slot(&dev0);
265                 dbg("%s: func->pci_dev->irq %x\n", __FUNCTION__, 
266                         func->pci_dev->irq);
267                 if (func->pci_dev == NULL) {
268                         dbg("ERROR: pci_dev still null\n");
269                         return 0;
270                 }
271         }
272
273         if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
274                 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
275                 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
276                 dbg("%s: calling pci_do_scan_bus\n", __FUNCTION__);
277                 pci_do_scan_bus(child);
278
279         }
280
281         temp = func->pci_dev;
282         dbg("%s: func->pci_dev->irq %x\n", __FUNCTION__, func->pci_dev->irq);
283
284         if (temp) {
285                 wrapped_dev.dev = temp;
286                 wrapped_bus.bus = temp->bus;
287                 rc = pci_visit_dev(&configure_functions, &wrapped_dev, &wrapped_bus);
288         }
289                 
290         dbg("%s: Exit\n", __FUNCTION__);
291         return rc;
292 }
293
294
295 int shpchp_unconfigure_device(struct pci_func* func) 
296 {
297         int rc = 0;
298         int j;
299         struct pci_dev_wrapped wrapped_dev;
300         struct pci_bus_wrapped wrapped_bus;
301         
302         memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
303         memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
304
305         dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus, func->device, func->function);
306
307         for (j=0; j<8 ; j++) {
308                 struct pci_dev* temp = pci_find_slot(func->bus, (func->device << 3) | j);
309                 if (temp) {
310                         wrapped_dev.dev = temp;
311                         wrapped_bus.bus = temp->bus;
312                         rc = pci_visit_dev(&unconfigure_functions_phase1, &wrapped_dev, &wrapped_bus);
313                         if (rc)
314                                 break;
315
316                         rc = pci_visit_dev(&unconfigure_functions_phase2, &wrapped_dev, &wrapped_bus);
317                         if (rc)
318                                 break;
319                 }
320         }
321         return rc;
322 }
323
324 /*
325  * shpchp_set_irq
326  *
327  * @bus_num: bus number of PCI device
328  * @dev_num: device number of PCI device
329  * @slot: pointer to u8 where slot number will be returned
330  */
331 int shpchp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
332 {
333 #if defined(CONFIG_X86) && !defined(CONFIG_X86_IO_APIC) && !defined(CONFIG_X86_64)
334         int rc;
335         u16 temp_word;
336         struct pci_dev fakedev;
337         struct pci_bus fakebus;
338
339         fakedev.devfn = dev_num << 3;
340         fakedev.bus = &fakebus;
341         fakebus.number = bus_num;
342         dbg("%s: dev %d, bus %d, pin %d, num %d\n",
343             __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
344         rc = pcibios_set_irq_routing(&fakedev, int_pin - 0x0a, irq_num);
345         dbg("%s: rc %d\n", __FUNCTION__, rc);
346         if (!rc)
347                 return !rc;
348
349         /* set the Edge Level Control Register (ELCR) */
350         temp_word = inb(0x4d0);
351         temp_word |= inb(0x4d1) << 8;
352
353         temp_word |= 0x01 << irq_num;
354
355         /* This should only be for x86 as it sets the Edge Level Control Register */
356         outb((u8) (temp_word & 0xFF), 0x4d0);
357         outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
358 #endif
359         return 0;
360 }
361
362 /* More PCI configuration routines; this time centered around hotplug controller */
363
364
365 /*
366  * shpchp_save_config
367  *
368  * Reads configuration for all slots in a PCI bus and saves info.
369  *
370  * Note:  For non-hot plug busses, the slot # saved is the device #
371  *
372  * returns 0 if success
373  */
374 int shpchp_save_config(struct controller *ctrl, int busnumber, int num_ctlr_slots, int first_device_num)
375 {
376         int rc;
377         u8 class_code;
378         u8 header_type;
379         u32 ID;
380         u8 secondary_bus;
381         struct pci_func *new_slot;
382         int sub_bus;
383         int FirstSupported;
384         int LastSupported;
385         int max_functions;
386         int function;
387         u8 DevError;
388         int device = 0;
389         int cloop = 0;
390         int stop_it;
391         int index;
392         int is_hot_plug = num_ctlr_slots || first_device_num;
393         struct pci_bus lpci_bus, *pci_bus;
394
395         dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__, num_ctlr_slots, first_device_num);
396
397         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
398         pci_bus = &lpci_bus;
399
400         dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__, num_ctlr_slots, first_device_num);
401
402         /*   Decide which slots are supported */
403         if (is_hot_plug) {
404                 /*********************************
405                  *  is_hot_plug is the slot mask
406                  *********************************/
407                 FirstSupported = first_device_num;
408                 LastSupported = FirstSupported + num_ctlr_slots - 1;
409         } else {
410                 FirstSupported = 0;
411                 LastSupported = 0x1F;
412         }
413
414         dbg("FirstSupported = %d, LastSupported = %d\n", FirstSupported, LastSupported);
415
416         /*   Save PCI configuration space for all devices in supported slots */
417         pci_bus->number = busnumber;
418         for (device = FirstSupported; device <= LastSupported; device++) {
419                 ID = 0xFFFFFFFF;
420                 rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
421
422                 if (ID != 0xFFFFFFFF) {   /*  device in slot */
423                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
424                         if (rc)
425                                 return rc;
426
427                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
428                         if (rc)
429                                 return rc;
430
431                         dbg("class_code = %x, header_type = %x\n", class_code, header_type);
432
433                         /* If multi-function device, set max_functions to 8 */
434                         if (header_type & 0x80)
435                                 max_functions = 8;
436                         else
437                                 max_functions = 1;
438
439                         function = 0;
440
441                         do {
442                                 DevError = 0;
443
444                                 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {   /* P-P Bridge */
445                                         /* Recurse the subordinate bus
446                                          * get the subordinate bus number
447                                          */
448                                         rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
449                                         if (rc) {
450                                                 return rc;
451                                         } else {
452                                                 sub_bus = (int) secondary_bus;
453
454                                                 /* Save secondary bus cfg spc with this recursive call. */
455                                                 rc = shpchp_save_config(ctrl, sub_bus, 0, 0);
456                                                 if (rc)
457                                                         return rc;
458                                         }
459                                 }
460
461                                 index = 0;
462                                 new_slot = shpchp_slot_find(busnumber, device, index++);
463
464                                 dbg("new_slot = %p\n", new_slot);
465
466                                 while (new_slot && (new_slot->function != (u8) function)) {
467                                         new_slot = shpchp_slot_find(busnumber, device, index++);
468                                         dbg("new_slot = %p\n", new_slot);
469                                 }
470                                 if (!new_slot) {
471                                         /* Setup slot structure. */
472                                         new_slot = shpchp_slot_create(busnumber);
473                                         dbg("new_slot = %p\n", new_slot);
474
475                                         if (new_slot == NULL)
476                                                 return(1);
477                                 }
478
479                                 new_slot->bus = (u8) busnumber;
480                                 new_slot->device = (u8) device;
481                                 new_slot->function = (u8) function;
482                                 new_slot->is_a_board = 1;
483                                 new_slot->switch_save = 0x10;
484                                 new_slot->pwr_save = 1;
485                                 /* In case of unsupported board */
486                                 new_slot->status = DevError;
487                                 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
488                                 dbg("new_slot->pci_dev = %p\n", new_slot->pci_dev);
489
490                                 for (cloop = 0; cloop < 0x20; cloop++) {
491                                         rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot->config_space [cloop]));
492                                         dbg("%s: new_slot->config_space[%x] = %x\n", __FUNCTION__, cloop, new_slot->config_space[cloop]); 
493                                         if (rc)
494                                                 return rc;
495                                 }
496
497                                 function++;
498
499                                 stop_it = 0;
500
501                                 /*  This loop skips to the next present function
502                                  *  reading in Class Code and Header type.
503                                  */
504
505                                 while ((function < max_functions)&&(!stop_it)) {
506                                         rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
507
508                                         if (ID == 0xFFFFFFFF) {  /* nothing there. */
509                                                 function++;
510                                                 dbg("Nothing there\n");
511                                         } else {  /* Something there */
512                                                 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
513                                                 if (rc)
514                                                         return rc;
515
516                                                 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
517                                                 if (rc)
518                                                         return rc;
519
520                                                 dbg("class_code = %x, header_type = %x\n", class_code, header_type);
521                                                 stop_it++;
522                                         }
523                                 }
524
525                         } while (function < max_functions);
526                 }               /* End of IF (device in slot?) */
527                 else if (is_hot_plug) {
528                         /* Setup slot structure with entry for empty slot */
529                         new_slot = shpchp_slot_create(busnumber);
530
531                         if (new_slot == NULL) {
532                                 return(1);
533                         }
534                         dbg("new_slot = %p\n", new_slot);
535
536                         new_slot->bus = (u8) busnumber;
537                         new_slot->device = (u8) device;
538                         new_slot->function = 0;
539                         new_slot->is_a_board = 0;
540                         new_slot->presence_save = 0;
541                         new_slot->switch_save = 0;
542                 }
543         }                       /* End of FOR loop */
544
545         return(0);
546 }
547
548
549 /*
550  * shpchp_save_slot_config
551  *
552  * Saves configuration info for all PCI devices in a given slot
553  * including subordinate busses.
554  *
555  * returns 0 if success
556  */
557 int shpchp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
558 {
559         int rc;
560         u8 class_code;
561         u8 header_type;
562         u32 ID;
563         u8 secondary_bus;
564         int sub_bus;
565         int max_functions;
566         int function;
567         int cloop = 0;
568         int stop_it;
569         struct pci_bus lpci_bus, *pci_bus;
570
571         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
572         pci_bus = &lpci_bus;
573         pci_bus->number = new_slot->bus;
574
575         ID = 0xFFFFFFFF;
576
577         pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
578
579         if (ID != 0xFFFFFFFF) {   /*  device in slot */
580                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
581
582                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
583
584                 if (header_type & 0x80) /* Multi-function device */
585                         max_functions = 8;
586                 else
587                         max_functions = 1;
588
589                 function = 0;
590
591                 do {
592                         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {     /* PCI-PCI Bridge */
593                                 /*  Recurse the subordinate bus */
594                                 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
595
596                                 sub_bus = (int) secondary_bus;
597
598                                 /* Save the config headers for the secondary bus. */
599                                 rc = shpchp_save_config(ctrl, sub_bus, 0, 0);
600
601                                 if (rc)
602                                         return(rc);
603
604                         }       /* End of IF */
605
606                         new_slot->status = 0;
607
608                         for (cloop = 0; cloop < 0x20; cloop++) {
609                                 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot->config_space [cloop]));
610                                 dbg("%s: new_slot->config_space[%x] = %x\n", __FUNCTION__,cloop, new_slot->config_space[cloop]); 
611                         }
612
613                         function++;
614
615                         stop_it = 0;
616
617                         /*  this loop skips to the next present function
618                          *  reading in the Class Code and the Header type.
619                          */
620
621                         while ((function < max_functions) && (!stop_it)) {
622                                 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
623
624                                 if (ID == 0xFFFFFFFF) {  /* nothing there. */
625                                         function++;
626                                 } else {  /* Something there */
627                                         pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
628
629                                         pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
630
631                                         stop_it++;
632                                 }
633                         }
634
635                 } while (function < max_functions);
636         }                       /* End of IF (device in slot?) */
637         else {
638                 return(2);
639         }
640
641         return(0);
642 }
643
644
645 /*
646  * shpchp_save_used_resources
647  *
648  * Stores used resource information for existing boards.  this is
649  * for boards that were in the system when this driver was loaded.
650  * this function is for hot plug ADD
651  *
652  * returns 0 if success
653  * if disable  == 1(DISABLE_CARD),
654  *  it loops for all functions of the slot and disables them.
655  * else, it just get resources of the function and return.
656  */
657 int shpchp_save_used_resources (struct controller *ctrl, struct pci_func *func, int disable)
658 {
659         u8 cloop;
660         u8 header_type;
661         u8 secondary_bus;
662         u8 temp_byte;
663         u16 command;
664         u16 save_command;
665         u16 w_base, w_length;
666         u32 temp_register;
667         u32 save_base;
668         u32 base, length;
669         u64 base64 = 0;
670         int index = 0;
671         unsigned int devfn;
672         struct pci_resource *mem_node = NULL;
673         struct pci_resource *p_mem_node = NULL;
674         struct pci_resource *t_mem_node;
675         struct pci_resource *io_node;
676         struct pci_resource *bus_node;
677         struct pci_bus lpci_bus, *pci_bus;
678
679         memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
680         pci_bus = &lpci_bus;
681
682         if (disable)
683                 func = shpchp_slot_find(func->bus, func->device, index++);
684
685         while ((func != NULL) && func->is_a_board) {
686                 pci_bus->number = func->bus;
687                 devfn = PCI_DEVFN(func->device, func->function);
688
689                 /* Save the command register */
690                 pci_bus_read_config_word (pci_bus, devfn, PCI_COMMAND, &save_command);
691
692                 if (disable) {
693                         /* disable card */
694                         command = 0x00;
695                         pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
696                 }
697
698                 /* Check for Bridge */
699                 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
700
701                 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {     /* PCI-PCI Bridge */
702                         dbg("Save_used_res of PCI bridge b:d=0x%x:%x, sc=0x%x\n", func->bus, func->device, save_command);
703                         if (disable) {
704                                 /* Clear Bridge Control Register */
705                                 command = 0x00;
706                                 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
707                         }
708
709                         pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
710                         pci_bus_read_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
711
712                         bus_node =(struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
713                         if (!bus_node)
714                                 return -ENOMEM;
715
716                         bus_node->base = (ulong)secondary_bus;
717                         bus_node->length = (ulong)(temp_byte - secondary_bus + 1);
718
719                         bus_node->next = func->bus_head;
720                         func->bus_head = bus_node;
721
722                         /* Save IO base and Limit registers */
723                         pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_BASE, &temp_byte);
724                         base = temp_byte;
725                         pci_bus_read_config_byte (pci_bus, devfn, PCI_IO_LIMIT, &temp_byte);
726                         length = temp_byte;
727
728                         if ((base <= length) && (!disable || (save_command & PCI_COMMAND_IO))) {
729                                 io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
730                                 if (!io_node)
731                                         return -ENOMEM;
732
733                                 io_node->base = (ulong)(base & PCI_IO_RANGE_MASK) << 8;
734                                 io_node->length = (ulong)(length - base + 0x10) << 8;
735
736                                 io_node->next = func->io_head;
737                                 func->io_head = io_node;
738                         }
739
740                         /* Save memory base and Limit registers */
741                         pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
742                         pci_bus_read_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
743
744                         if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
745                                 mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
746                                 if (!mem_node)
747                                         return -ENOMEM;
748
749                                 mem_node->base = (ulong)w_base << 16;
750                                 mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
751
752                                 mem_node->next = func->mem_head;
753                                 func->mem_head = mem_node;
754                         }
755                         /* Save prefetchable memory base and Limit registers */
756                         pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
757                         pci_bus_read_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
758
759                         if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
760                                 p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
761                                 if (!p_mem_node)
762                                         return -ENOMEM;
763
764                                 p_mem_node->base = (ulong)w_base << 16;
765                                 p_mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
766
767                                 p_mem_node->next = func->p_mem_head;
768                                 func->p_mem_head = p_mem_node;
769                         }
770                 } else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
771                         dbg("Save_used_res of PCI adapter b:d=0x%x:%x, sc=0x%x\n", func->bus, func->device, save_command);
772
773                         /* Figure out IO and memory base lengths */
774                         for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
775                                 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
776
777                                 temp_register = 0xFFFFFFFF;
778                                 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
779                                 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
780
781                                 if (!disable) {
782                                         pci_bus_write_config_dword (pci_bus, devfn, cloop, save_base);
783                                 }
784
785                                 if (!temp_register)
786                                         continue;
787
788                                 base = temp_register;
789
790                                 if ((base & PCI_BASE_ADDRESS_SPACE_IO) && (!disable || (save_command & PCI_COMMAND_IO))) {
791                                         /* IO base */
792                                         /* set temp_register = amount of IO space requested */
793                                         base = base & 0xFFFFFFFCL;
794                                         base = (~base) + 1;
795
796                                         io_node = (struct pci_resource *) kmalloc(sizeof (struct pci_resource), GFP_KERNEL);
797                                         if (!io_node)
798                                                 return -ENOMEM;
799
800                                         io_node->base = (ulong)save_base & PCI_BASE_ADDRESS_IO_MASK;
801                                         io_node->length = (ulong)base;
802                                         dbg("sur adapter: IO bar=0x%x(length=0x%x)\n", io_node->base, io_node->length);
803
804                                         io_node->next = func->io_head;
805                                         func->io_head = io_node;
806                                 } else {  /* map Memory */
807                                         int prefetchable = 1;
808                                         /* struct pci_resources **res_node; */
809                                         char *res_type_str = "PMEM";
810                                         u32 temp_register2;
811
812                                         t_mem_node = (struct pci_resource *) kmalloc(sizeof (struct pci_resource), GFP_KERNEL);
813                                         if (!t_mem_node)
814                                                 return -ENOMEM;
815
816                                         if (!(base & PCI_BASE_ADDRESS_MEM_PREFETCH) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
817                                                 prefetchable = 0;
818                                                 mem_node = t_mem_node;
819                                                 res_type_str++;
820                                         } else
821                                                 p_mem_node = t_mem_node;
822
823                                         base = base & 0xFFFFFFF0L;
824                                         base = (~base) + 1;
825
826                                         switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
827                                         case PCI_BASE_ADDRESS_MEM_TYPE_32:
828                                                 if (prefetchable) {
829                                                         p_mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
830                                                         p_mem_node->length = (ulong)base;
831                                                         dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n", res_type_str, p_mem_node->base, p_mem_node->length);
832
833                                                         p_mem_node->next = func->p_mem_head;
834                                                         func->p_mem_head = p_mem_node;
835                                                 } else {
836                                                         mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
837                                                         mem_node->length = (ulong)base;
838                                                         dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n", res_type_str, mem_node->base, mem_node->length);
839
840                                                         mem_node->next = func->mem_head;
841                                                         func->mem_head = mem_node;
842                                                 }
843                                                 break;
844                                         case PCI_BASE_ADDRESS_MEM_TYPE_64:
845                                                 pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
846                                                 base64 = temp_register2;
847                                                 base64 = (base64 << 32) | save_base;
848
849                                                 if (temp_register2) {
850                                                         dbg("sur adapter: 64 %s high dword of base64(0x%x:%x) masked to 0\n", res_type_str, temp_register2, (u32)base64);
851                                                         base64 &= 0x00000000FFFFFFFFL;
852                                                 }
853
854                                                 if (prefetchable) {
855                                                         p_mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
856                                                         p_mem_node->length = base;
857                                                         dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n", res_type_str, p_mem_node->base, p_mem_node->length);
858
859                                                         p_mem_node->next = func->p_mem_head;
860                                                         func->p_mem_head = p_mem_node;
861                                                 } else {
862                                                         mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
863                                                         mem_node->length = base;
864                                                         dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n", res_type_str, mem_node->base, mem_node->length);
865
866                                                         mem_node->next = func->mem_head;
867                                                         func->mem_head = mem_node;
868                                                 }
869                                                 cloop += 4;
870                                                 break;
871                                         default:
872                                                 dbg("asur: reserved BAR type=0x%x\n", temp_register);
873                                                 break;
874                                         }
875                                 } 
876                         }       /* End of base register loop */
877                 } else {        /* Some other unknown header type */
878                         dbg("Save_used_res of PCI unknown type b:d=0x%x:%x. skip.\n", func->bus, func->device);
879                 }
880
881                 /* find the next device in this slot */
882                 if (!disable)
883                         break;
884                 func = shpchp_slot_find(func->bus, func->device, index++);
885         }
886
887         return(0);
888 }
889
890
891 /*
892  * shpchp_return_board_resources
893  *
894  * this routine returns all resources allocated to a board to
895  * the available pool.
896  *
897  * returns 0 if success
898  */
899 int shpchp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
900 {
901         int rc = 0;
902         struct pci_resource *node;
903         struct pci_resource *t_node;
904         dbg("%s\n", __FUNCTION__);
905
906         if (!func)
907                 return(1);
908
909         node = func->io_head;
910         func->io_head = NULL;
911         while (node) {
912                 t_node = node->next;
913                 return_resource(&(resources->io_head), node);
914                 node = t_node;
915         }
916
917         node = func->mem_head;
918         func->mem_head = NULL;
919         while (node) {
920                 t_node = node->next;
921                 return_resource(&(resources->mem_head), node);
922                 node = t_node;
923         }
924
925         node = func->p_mem_head;
926         func->p_mem_head = NULL;
927         while (node) {
928                 t_node = node->next;
929                 return_resource(&(resources->p_mem_head), node);
930                 node = t_node;
931         }
932
933         node = func->bus_head;
934         func->bus_head = NULL;
935         while (node) {
936                 t_node = node->next;
937                 return_resource(&(resources->bus_head), node);
938                 node = t_node;
939         }
940
941         rc |= shpchp_resource_sort_and_combine(&(resources->mem_head));
942         rc |= shpchp_resource_sort_and_combine(&(resources->p_mem_head));
943         rc |= shpchp_resource_sort_and_combine(&(resources->io_head));
944         rc |= shpchp_resource_sort_and_combine(&(resources->bus_head));
945
946         return(rc);
947 }
948
949
950 /*
951  * shpchp_destroy_resource_list
952  *
953  * Puts node back in the resource list pointed to by head
954  */
955 void shpchp_destroy_resource_list (struct resource_lists * resources)
956 {
957         struct pci_resource *res, *tres;
958
959         res = resources->io_head;
960         resources->io_head = NULL;
961
962         while (res) {
963                 tres = res;
964                 res = res->next;
965                 kfree(tres);
966         }
967
968         res = resources->mem_head;
969         resources->mem_head = NULL;
970
971         while (res) {
972                 tres = res;
973                 res = res->next;
974                 kfree(tres);
975         }
976
977         res = resources->p_mem_head;
978         resources->p_mem_head = NULL;
979
980         while (res) {
981                 tres = res;
982                 res = res->next;
983                 kfree(tres);
984         }
985
986         res = resources->bus_head;
987         resources->bus_head = NULL;
988
989         while (res) {
990                 tres = res;
991                 res = res->next;
992                 kfree(tres);
993         }
994 }
995
996
997 /*
998  * shpchp_destroy_board_resources
999  *
1000  * Puts node back in the resource list pointed to by head
1001  */
1002 void shpchp_destroy_board_resources (struct pci_func * func)
1003 {
1004         struct pci_resource *res, *tres;
1005
1006         dbg("%s: \n", __FUNCTION__);
1007
1008         res = func->io_head;
1009         func->io_head = NULL;
1010
1011         while (res) {
1012                 tres = res;
1013                 res = res->next;
1014                 kfree(tres);
1015         }
1016
1017         res = func->mem_head;
1018         func->mem_head = NULL;
1019
1020         while (res) {
1021                 tres = res;
1022                 res = res->next;
1023                 kfree(tres);
1024         }
1025
1026         res = func->p_mem_head;
1027         func->p_mem_head = NULL;
1028
1029         while (res) {
1030                 tres = res;
1031                 res = res->next;
1032                 kfree(tres);
1033         }
1034
1035         res = func->bus_head;
1036         func->bus_head = NULL;
1037
1038         while (res) {
1039                 tres = res;
1040                 res = res->next;
1041                 kfree(tres);
1042         }
1043 }
1044