2 * IBM Hot Plug Controller Driver
4 * Written By: Irene Zubarev, IBM Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
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.
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
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.
26 * Send feedback to <gregkh@us.ibm.com>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/list.h>
37 static int configure_device(struct pci_func *);
38 static int configure_bridge(struct pci_func **, u8);
39 static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
40 static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
41 static u8 find_sec_number(u8 primary_busno, u8 slotno);
44 * NOTE..... If BIOS doesn't provide default routing, we assign:
45 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
46 * If adapter is bridged, then we assign 11 to it and devices behind it.
47 * We also assign the same irq numbers for multi function devices.
48 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
50 static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
53 for (j = 0; j < 4; j++) {
54 if (cur_func->irq[j] == 0xff) {
56 case PCI_BASE_CLASS_STORAGE:
57 cur_func->irq[j] = SCSI_IRQ;
59 case PCI_BASE_CLASS_NETWORK:
60 cur_func->irq[j] = LAN_IRQ;
63 cur_func->irq[j] = OTHER_IRQ;
71 * Configures the device to be added (will allocate needed resources if it
72 * can), the device can be a bridge or a regular pci device, can also be
75 * Input: function to be added
77 * TO DO: The error case with Multifunction device or multi function bridge,
78 * if there is an error, will need to go through all previous functions and
79 * unconfigure....or can add some code into unconfigure_card....
81 int ibmphp_configure_card(struct pci_func *func, u8 slotno)
86 u8 hdr_type, device, sec_number;
88 struct pci_func *newfunc; /* for multi devices */
89 struct pci_func *cur_func, *prev_func;
93 u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
95 debug("inside configure_card, func->busno = %x\n", func->busno);
97 device = func->device;
100 /* We only get bus and device from IRQ routing table. So at this point,
101 * func->busno is correct, and func->device contains only device (at the 5
105 /* For every function on the card */
106 for (function = 0x00; function < 0x08; function++) {
107 unsigned int devfn = PCI_DEVFN(device, function);
108 ibmphp_pci_bus->number = cur_func->busno;
110 cur_func->function = function;
112 debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
113 cur_func->busno, cur_func->device, cur_func->function);
115 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
117 debug("vendor_id is %x\n", vendor_id);
118 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
119 /* found correct device!!! */
120 debug("found valid device, vendor_id = %x\n", vendor_id);
124 /* header: x x x x x x x x
125 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
126 * |_=> 0 = single function device, 1 = multi-function device
129 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
130 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
132 class_code = class >> 24;
133 debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
134 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
135 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
136 err("The device %x is VGA compatible and as is not supported for hot plugging. "
137 "Please choose another device.\n", cur_func->device);
139 } else if (class == PCI_CLASS_DISPLAY_VGA) {
140 err("The device %x is not supported for hot plugging. Please choose another device.\n",
145 case PCI_HEADER_TYPE_NORMAL:
146 debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
147 assign_alt_irq(cur_func, class_code);
148 rc = configure_device(cur_func);
150 /* We need to do this in case some other BARs were properly inserted */
151 err("was not able to configure devfunc %x on bus %x.\n",
152 cur_func->device, cur_func->busno);
156 cur_func->next = NULL;
159 case PCI_HEADER_TYPE_MULTIDEVICE:
160 assign_alt_irq(cur_func, class_code);
161 rc = configure_device(cur_func);
163 /* We need to do this in case some other BARs were properly inserted */
164 err("was not able to configure devfunc %x on bus %x...bailing out\n",
165 cur_func->device, cur_func->busno);
169 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
173 newfunc->busno = cur_func->busno;
174 newfunc->device = device;
175 cur_func->next = newfunc;
177 for (j = 0; j < 4; j++)
178 newfunc->irq[j] = cur_func->irq[j];
180 case PCI_HEADER_TYPE_MULTIBRIDGE:
182 if (class != PCI_CLASS_BRIDGE_PCI) {
183 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
187 assign_alt_irq(cur_func, class_code);
188 rc = configure_bridge(&cur_func, slotno);
190 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
191 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
195 /* We need to do this in case some other BARs were properly inserted */
196 err("was not able to hot-add PPB properly.\n");
197 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
202 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
204 for (i = 0; i < 32; i++) {
205 if (func->devices[i]) {
206 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
210 newfunc->busno = sec_number;
211 newfunc->device = (u8) i;
212 for (j = 0; j < 4; j++)
213 newfunc->irq[j] = cur_func->irq[j];
216 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
217 prev_func->next = newfunc;
219 cur_func->next = newfunc;
221 rc = ibmphp_configure_card(newfunc, slotno);
222 /* This could only happen if kmalloc failed */
224 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
225 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
233 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
237 newfunc->busno = cur_func->busno;
238 newfunc->device = device;
239 for (j = 0; j < 4; j++)
240 newfunc->irq[j] = cur_func->irq[j];
241 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
242 prev_func->next = newfunc;
245 case PCI_HEADER_TYPE_BRIDGE:
247 debug("class now is %x\n", class);
248 if (class != PCI_CLASS_BRIDGE_PCI) {
249 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
254 assign_alt_irq(cur_func, class_code);
256 debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
257 rc = configure_bridge(&cur_func, slotno);
259 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
260 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
264 /* We need to do this in case some other BARs were properly inserted */
265 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
266 err("was not able to hot-add PPB properly.\n");
270 debug("cur_func->busno = %x, device = %x, function = %x\n",
271 cur_func->busno, device, function);
272 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
273 debug("after configuring bridge..., sec_number = %x\n", sec_number);
275 for (i = 0; i < 32; i++) {
276 if (func->devices[i]) {
277 debug("inside for loop, device is %x\n", i);
278 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
282 newfunc->busno = sec_number;
283 newfunc->device = (u8) i;
284 for (j = 0; j < 4; j++)
285 newfunc->irq[j] = cur_func->irq[j];
288 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
289 prev_func->next = newfunc;
291 cur_func->next = newfunc;
293 rc = ibmphp_configure_card(newfunc, slotno);
295 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
297 /* We need to do this in case some other BARs were properly inserted */
298 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
309 err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
312 } /* end of switch */
313 } /* end of valid device */
317 err("Cannot find any valid devices on the card. Or unable to read from card.\n");
324 for (i = 0; i < cleanup_count; i++) {
325 if (cur_func->io[i]) {
326 ibmphp_remove_resource(cur_func->io[i]);
327 cur_func->io[i] = NULL;
328 } else if (cur_func->pfmem[i]) {
329 ibmphp_remove_resource(cur_func->pfmem[i]);
330 cur_func->pfmem[i] = NULL;
331 } else if (cur_func->mem[i]) {
332 ibmphp_remove_resource(cur_func->mem[i]);
333 cur_func->mem[i] = NULL;
340 * This function configures the pci BARs of a single device.
341 * Input: pointer to the pci_func
342 * Output: configured PCI, 0, or error
344 static int configure_device(struct pci_func *func)
359 struct resource_node *io[6];
360 struct resource_node *mem[6];
361 struct resource_node *mem_tmp;
362 struct resource_node *pfmem[6];
365 debug("%s - inside\n", __func__);
367 devfn = PCI_DEVFN(func->device, func->function);
368 ibmphp_pci_bus->number = func->busno;
370 for (count = 0; address[count]; count++) { /* for 6 BARs */
372 /* not sure if i need this. per scott, said maybe need * something like this
373 if devices don't adhere 100% to the spec, so don't want to write
376 pcibios_read_config_byte(cur_func->busno, cur_func->device,
377 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
378 if (tmp & 0x01) // IO
379 pcibios_write_config_dword(cur_func->busno, cur_func->device,
380 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
382 pcibios_write_config_dword(cur_func->busno, cur_func->device,
383 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
385 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
386 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
388 if (!bar[count]) /* This BAR is not implemented */
391 debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
393 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
395 debug("inside IO SPACE\n");
397 len[count] = bar[count] & 0xFFFFFFFC;
398 len[count] = ~len[count] + 1;
400 debug("len[count] in IO %x, count %d\n", len[count], count);
402 io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
407 io[count]->type = IO;
408 io[count]->busno = func->busno;
409 io[count]->devfunc = PCI_DEVFN(func->device, func->function);
410 io[count]->len = len[count];
411 if (ibmphp_check_resource(io[count], 0) == 0) {
412 ibmphp_add_resource(io[count]);
413 func->io[count] = io[count];
415 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
416 func->busno, func->device, func->function, len[count]);
420 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
422 /* _______________This is for debugging purposes only_____________________ */
423 debug("b4 writing, the IO address is %x\n", func->io[count]->start);
424 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
425 debug("after writing.... the start address is %x\n", bar[count]);
426 /* _________________________________________________________________________*/
430 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
432 debug("PFMEM SPACE\n");
434 len[count] = bar[count] & 0xFFFFFFF0;
435 len[count] = ~len[count] + 1;
437 debug("len[count] in PFMEM %x, count %d\n", len[count], count);
439 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
443 pfmem[count]->type = PFMEM;
444 pfmem[count]->busno = func->busno;
445 pfmem[count]->devfunc = PCI_DEVFN(func->device,
447 pfmem[count]->len = len[count];
448 pfmem[count]->fromMem = 0;
449 if (ibmphp_check_resource(pfmem[count], 0) == 0) {
450 ibmphp_add_resource(pfmem[count]);
451 func->pfmem[count] = pfmem[count];
453 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
459 mem_tmp->busno = pfmem[count]->busno;
460 mem_tmp->devfunc = pfmem[count]->devfunc;
461 mem_tmp->len = pfmem[count]->len;
462 debug("there's no pfmem... going into mem.\n");
463 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
464 ibmphp_add_resource(mem_tmp);
465 pfmem[count]->fromMem = 1;
466 pfmem[count]->rangeno = mem_tmp->rangeno;
467 pfmem[count]->start = mem_tmp->start;
468 pfmem[count]->end = mem_tmp->end;
469 ibmphp_add_pfmem_from_mem(pfmem[count]);
470 func->pfmem[count] = pfmem[count];
472 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
473 func->busno, func->device, len[count]);
480 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
482 /*_______________This is for debugging purposes only______________________________*/
483 debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
484 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
485 debug("after writing, start address is %x\n", bar[count]);
486 /*_________________________________________________________________________________*/
488 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
489 debug("inside the mem 64 case, count %d\n", count);
491 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
492 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
496 debug("REGULAR MEM SPACE\n");
498 len[count] = bar[count] & 0xFFFFFFF0;
499 len[count] = ~len[count] + 1;
501 debug("len[count] in Mem %x, count %d\n", len[count], count);
503 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
507 mem[count]->type = MEM;
508 mem[count]->busno = func->busno;
509 mem[count]->devfunc = PCI_DEVFN(func->device,
511 mem[count]->len = len[count];
512 if (ibmphp_check_resource(mem[count], 0) == 0) {
513 ibmphp_add_resource(mem[count]);
514 func->mem[count] = mem[count];
516 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
517 func->busno, func->device, len[count]);
521 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
522 /* _______________________This is for debugging purposes only _______________________*/
523 debug("b4 writing, start address is %x\n", func->mem[count]->start);
524 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
525 debug("after writing, the address is %x\n", bar[count]);
526 /* __________________________________________________________________________________*/
528 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
529 /* takes up another dword */
530 debug("inside mem 64 case, reg. mem, count %d\n", count);
532 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
533 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
539 func->bus = 0; /* To indicate that this is not a PPB */
540 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
541 if ((irq > 0x00) && (irq < 0x05))
542 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
544 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
545 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
547 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
548 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
553 /******************************************************************************
554 * This routine configures a PCI-2-PCI bridge and the functions behind it
555 * Parameters: pci_func
557 ******************************************************************************/
558 static int configure_bridge(struct pci_func **func_passed, u8 slotno)
571 u8 need_io_upper = 0;
572 u8 need_pfmem_upper = 0;
573 struct res_needed *amount_needed = NULL;
574 struct resource_node *io = NULL;
575 struct resource_node *bus_io[2] = {NULL, NULL};
576 struct resource_node *mem = NULL;
577 struct resource_node *bus_mem[2] = {NULL, NULL};
578 struct resource_node *mem_tmp = NULL;
579 struct resource_node *pfmem = NULL;
580 struct resource_node *bus_pfmem[2] = {NULL, NULL};
581 struct bus_node *bus;
587 struct pci_func *func = *func_passed;
592 debug("%s - enter\n", __func__);
594 devfn = PCI_DEVFN(func->function, func->device);
595 ibmphp_pci_bus->number = func->busno;
597 /* Configuring necessary info for the bridge so that we could see the devices
601 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
603 /* _____________________For debugging purposes only __________________________
604 pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
605 debug("primary # written into the bridge is %x\n", pri_number);
606 ___________________________________________________________________________*/
608 /* in EBDA, only get allocated 1 additional bus # per slot */
609 sec_number = find_sec_number(func->busno, slotno);
610 if (sec_number == 0xff) {
611 err("cannot allocate secondary bus number for the bridged device\n");
615 debug("after find_sec_number, the number we got is %x\n", sec_number);
616 debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
618 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
620 /* __________________For debugging purposes only __________________________________
621 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
622 debug("sec_number after write/read is %x\n", sec_number);
623 ________________________________________________________________________________*/
625 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
627 /* __________________For debugging purposes only ____________________________________
628 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
629 debug("subordinate number after write/read is %x\n", sec_number);
630 __________________________________________________________________________________*/
632 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
633 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
634 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
636 debug("func->busno is %x\n", func->busno);
637 debug("sec_number after writing is %x\n", sec_number);
640 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
641 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
642 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
645 /* First we need to allocate mem/io for the bridge itself in case it needs it */
646 for (count = 0; address[count]; count++) { /* for 2 BARs */
647 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
648 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
651 /* This BAR is not implemented */
652 debug("so we come here then, eh?, count = %d\n", count);
655 // tmp_bar = bar[count];
657 debug("Bar %d wants %x\n", count, bar[count]);
659 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
661 len[count] = bar[count] & 0xFFFFFFFC;
662 len[count] = ~len[count] + 1;
664 debug("len[count] in IO = %x\n", len[count]);
666 bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
668 if (!bus_io[count]) {
672 bus_io[count]->type = IO;
673 bus_io[count]->busno = func->busno;
674 bus_io[count]->devfunc = PCI_DEVFN(func->device,
676 bus_io[count]->len = len[count];
677 if (ibmphp_check_resource(bus_io[count], 0) == 0) {
678 ibmphp_add_resource(bus_io[count]);
679 func->io[count] = bus_io[count];
681 err("cannot allocate requested io for bus %x, device %x, len %x\n",
682 func->busno, func->device, len[count]);
683 kfree(bus_io[count]);
687 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
691 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
693 len[count] = bar[count] & 0xFFFFFFF0;
694 len[count] = ~len[count] + 1;
696 debug("len[count] in PFMEM = %x\n", len[count]);
698 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
699 if (!bus_pfmem[count]) {
703 bus_pfmem[count]->type = PFMEM;
704 bus_pfmem[count]->busno = func->busno;
705 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
707 bus_pfmem[count]->len = len[count];
708 bus_pfmem[count]->fromMem = 0;
709 if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
710 ibmphp_add_resource(bus_pfmem[count]);
711 func->pfmem[count] = bus_pfmem[count];
713 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
719 mem_tmp->busno = bus_pfmem[count]->busno;
720 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
721 mem_tmp->len = bus_pfmem[count]->len;
722 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
723 ibmphp_add_resource(mem_tmp);
724 bus_pfmem[count]->fromMem = 1;
725 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
726 ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
727 func->pfmem[count] = bus_pfmem[count];
729 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
730 func->busno, func->device, len[count]);
732 kfree(bus_pfmem[count]);
737 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
739 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
740 /* takes up another dword */
742 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
743 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
748 len[count] = bar[count] & 0xFFFFFFF0;
749 len[count] = ~len[count] + 1;
751 debug("len[count] in Memory is %x\n", len[count]);
753 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
754 if (!bus_mem[count]) {
758 bus_mem[count]->type = MEM;
759 bus_mem[count]->busno = func->busno;
760 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
762 bus_mem[count]->len = len[count];
763 if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
764 ibmphp_add_resource(bus_mem[count]);
765 func->mem[count] = bus_mem[count];
767 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
768 func->busno, func->device, len[count]);
769 kfree(bus_mem[count]);
773 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
775 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
776 /* takes up another dword */
778 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
779 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
786 /* Now need to see how much space the devices behind the bridge needed */
787 amount_needed = scan_behind_bridge(func, sec_number);
788 if (amount_needed == NULL)
791 ibmphp_pci_bus->number = func->busno;
792 debug("after coming back from scan_behind_bridge\n");
793 debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
794 debug("amount_needed->io = %x\n", amount_needed->io);
795 debug("amount_needed->mem = %x\n", amount_needed->mem);
796 debug("amount_needed->pfmem = %x\n", amount_needed->pfmem);
798 if (amount_needed->not_correct) {
799 debug("amount_needed is not correct\n");
800 for (count = 0; address[count]; count++) {
803 ibmphp_remove_resource(bus_io[count]);
804 func->io[count] = NULL;
805 } else if (bus_pfmem[count]) {
806 ibmphp_remove_resource(bus_pfmem[count]);
807 func->pfmem[count] = NULL;
808 } else if (bus_mem[count]) {
809 ibmphp_remove_resource(bus_mem[count]);
810 func->mem[count] = NULL;
813 kfree(amount_needed);
817 if (!amount_needed->io) {
818 debug("it doesn't want IO?\n");
821 debug("it wants %x IO behind the bridge\n", amount_needed->io);
822 io = kzalloc(sizeof(*io), GFP_KERNEL);
829 io->busno = func->busno;
830 io->devfunc = PCI_DEVFN(func->device, func->function);
831 io->len = amount_needed->io;
832 if (ibmphp_check_resource(io, 1) == 0) {
833 debug("were we able to add io\n");
834 ibmphp_add_resource(io);
839 if (!amount_needed->mem) {
840 debug("it doesn't want n.e.memory?\n");
843 debug("it wants %x memory behind the bridge\n", amount_needed->mem);
844 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
850 mem->busno = func->busno;
851 mem->devfunc = PCI_DEVFN(func->device, func->function);
852 mem->len = amount_needed->mem;
853 if (ibmphp_check_resource(mem, 1) == 0) {
854 ibmphp_add_resource(mem);
856 debug("were we able to add mem\n");
860 if (!amount_needed->pfmem) {
861 debug("it doesn't want n.e.pfmem mem?\n");
864 debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
865 pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
871 pfmem->busno = func->busno;
872 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
873 pfmem->len = amount_needed->pfmem;
875 if (ibmphp_check_resource(pfmem, 1) == 0) {
876 ibmphp_add_resource(pfmem);
879 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
885 mem_tmp->busno = pfmem->busno;
886 mem_tmp->devfunc = pfmem->devfunc;
887 mem_tmp->len = pfmem->len;
888 if (ibmphp_check_resource(mem_tmp, 1) == 0) {
889 ibmphp_add_resource(mem_tmp);
891 pfmem->rangeno = mem_tmp->rangeno;
892 ibmphp_add_pfmem_from_mem(pfmem);
898 debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
899 debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
901 if (flag_io && flag_mem && flag_pfmem) {
902 /* If on bootup, there was a bridged card in this slot,
903 * then card was removed and ibmphp got unloaded and loaded
904 * back again, there's no way for us to remove the bus
905 * struct, so no need to kmalloc, can use existing node
907 bus = ibmphp_find_res_bus(sec_number);
909 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
914 bus->busno = sec_number;
915 debug("b4 adding new bus\n");
916 rc = add_new_bus(bus, io, mem, pfmem, func->busno);
917 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
918 rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
920 err("expected bus structure not empty?\n");
926 ibmphp_remove_bus(bus, func->busno);
927 kfree(amount_needed);
933 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
934 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
936 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
940 if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
942 need_pfmem_upper = 1;
945 if (bus->noIORanges) {
946 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
947 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
949 /* _______________This is for debugging purposes only ____________________
950 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
951 debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
952 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
953 debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
954 ________________________________________________________________________*/
956 if (need_io_upper) { /* since can't support n.e.ways */
957 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
958 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
961 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
962 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
965 if (bus->noMemRanges) {
966 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
967 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
969 /* ____________________This is for debugging purposes only ________________________
970 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
971 debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
972 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
973 debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
974 __________________________________________________________________________________*/
977 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
978 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
980 if (bus->noPFMemRanges) {
981 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
982 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
984 /* __________________________This is for debugging purposes only _______________________
985 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
986 debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
987 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
988 debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
989 ______________________________________________________________________________________*/
991 if (need_pfmem_upper) { /* since can't support n.e.ways */
992 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
993 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
996 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
997 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1000 debug("b4 writing control information\n");
1002 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1003 if ((irq > 0x00) && (irq < 0x05))
1004 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1006 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1007 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1008 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1011 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1012 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1013 for (i = 0; i < 32; i++) {
1014 if (amount_needed->devices[i]) {
1015 debug("device where devices[i] is 1 = %x\n", i);
1016 func->devices[i] = 1;
1019 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1020 func_passed = &func;
1021 debug("func->busno b4 returning is %x\n", func->busno);
1022 debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1023 kfree(amount_needed);
1026 err("Configuring bridge was unsuccessful...\n");
1033 kfree(amount_needed);
1035 ibmphp_remove_resource(pfmem);
1037 ibmphp_remove_resource(io);
1039 ibmphp_remove_resource(mem);
1040 for (i = 0; i < 2; i++) { /* for 2 BARs */
1042 ibmphp_remove_resource(bus_io[i]);
1044 } else if (bus_pfmem[i]) {
1045 ibmphp_remove_resource(bus_pfmem[i]);
1046 func->pfmem[i] = NULL;
1047 } else if (bus_mem[i]) {
1048 ibmphp_remove_resource(bus_mem[i]);
1049 func->mem[i] = NULL;
1055 /*****************************************************************************
1056 * This function adds up the amount of resources needed behind the PPB bridge
1057 * and passes it to the configure_bridge function
1058 * Input: bridge function
1059 * Output: amount of resources needed
1060 *****************************************************************************/
1061 static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1066 u8 device, function;
1068 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1080 struct res_needed *amount;
1082 amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1086 ibmphp_pci_bus->number = busno;
1088 debug("the bus_no behind the bridge is %x\n", busno);
1089 debug("scanning devices behind the bridge...\n");
1090 for (device = 0; device < 32; device++) {
1091 amount->devices[device] = 0;
1092 for (function = 0; function < 8; function++) {
1093 devfn = PCI_DEVFN(device, function);
1095 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1097 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1098 /* found correct device!!! */
1101 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1102 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1104 debug("hdr_type behind the bridge is %x\n", hdr_type);
1105 if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1106 err("embedded bridges not supported for hot-plugging.\n");
1107 amount->not_correct = 1;
1111 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1112 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1113 err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device);
1114 amount->not_correct = 1;
1116 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1117 err("The device %x is not supported for hot plugging. Please choose another device.\n", device);
1118 amount->not_correct = 1;
1122 amount->devices[device] = 1;
1124 for (count = 0; address[count]; count++) {
1127 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1128 if (tmp & 0x01) // IO
1129 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1131 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1133 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1134 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1136 debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1138 if (!bar[count]) /* This BAR is not implemented */
1141 //tmp_bar = bar[count];
1143 debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1145 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1147 len[count] = bar[count] & 0xFFFFFFFC;
1148 len[count] = ~len[count] + 1;
1149 amount->io += len[count];
1151 /* This is Memory */
1152 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1154 len[count] = bar[count] & 0xFFFFFFF0;
1155 len[count] = ~len[count] + 1;
1156 amount->pfmem += len[count];
1157 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1158 /* takes up another dword */
1162 /* regular memory */
1163 len[count] = bar[count] & 0xFFFFFFF0;
1164 len[count] = ~len[count] + 1;
1165 amount->mem += len[count];
1166 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1167 /* takes up another dword */
1173 } /* end if (valid) */
1178 amount->not_correct = 1;
1180 amount->not_correct = 0;
1181 if ((amount->io) && (amount->io < IOBRIDGE))
1182 amount->io = IOBRIDGE;
1183 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1184 amount->mem = MEMBRIDGE;
1185 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1186 amount->pfmem = MEMBRIDGE;
1190 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1191 * upon bootup in the system, since we don't allocate func to such case, we need to read
1192 * the start addresses from pci config space and then find the corresponding entries in
1193 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1194 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1195 * the bootup case), since it should work same way
1197 static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1210 struct resource_node *io;
1211 struct resource_node *mem;
1212 struct resource_node *pfmem;
1213 struct bus_node *bus;
1220 debug("%s - enter\n", __func__);
1222 bus = ibmphp_find_res_bus(busno);
1224 debug("cannot find corresponding bus.\n");
1228 devfn = PCI_DEVFN(device, function);
1229 ibmphp_pci_bus->number = busno;
1230 for (count = 0; address[count]; count++) { /* for 6 BARs */
1231 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1233 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1235 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1236 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1237 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1239 debug("start_address is %x\n", start_address);
1240 debug("busno, device, function %x %x %x\n", busno, device, function);
1242 /* This BAR is not implemented */
1243 debug("is this bar no implemented?, count = %d\n", count);
1246 tmp_address = start_address;
1247 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1249 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1250 size = size & 0xFFFFFFFC;
1252 end_address = start_address + size - 1;
1253 if (ibmphp_find_resource(bus, start_address, &io, IO))
1254 goto report_search_failure;
1256 debug("io->start = %x\n", io->start);
1258 start_address = io->end + 1;
1259 ibmphp_remove_resource(io);
1260 /* This is needed b/c of the old I/O restrictions in the BIOS */
1261 while (temp_end < end_address) {
1262 if (ibmphp_find_resource(bus, start_address,
1264 goto report_search_failure;
1266 debug("io->start = %x\n", io->start);
1268 start_address = io->end + 1;
1269 ibmphp_remove_resource(io);
1272 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1274 /* This is Memory */
1275 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1277 debug("start address of pfmem is %x\n", start_address);
1278 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1280 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1281 err("cannot find corresponding PFMEM resource to remove\n");
1285 debug("pfmem->start = %x\n", pfmem->start);
1287 ibmphp_remove_resource(pfmem);
1290 /* regular memory */
1291 debug("start address of mem is %x\n", start_address);
1292 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1294 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1295 err("cannot find corresponding MEM resource to remove\n");
1299 debug("mem->start = %x\n", mem->start);
1301 ibmphp_remove_resource(mem);
1304 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1305 /* takes up another dword */
1313 report_search_failure:
1314 err("cannot find corresponding IO resource to remove\n");
1318 static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1321 int bus_no, pri_no, sub_no, sec_no = 0;
1322 u32 start_address, tmp_address;
1323 u8 sec_number, sub_number, pri_number;
1324 struct resource_node *io = NULL;
1325 struct resource_node *mem = NULL;
1326 struct resource_node *pfmem = NULL;
1327 struct bus_node *bus;
1335 devfn = PCI_DEVFN(device, function);
1336 ibmphp_pci_bus->number = busno;
1337 bus_no = (int) busno;
1338 debug("busno is %x\n", busno);
1339 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1340 debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1342 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1343 debug("sec_number is %x\n", sec_number);
1344 sec_no = (int) sec_number;
1345 pri_no = (int) pri_number;
1346 if (pri_no != bus_no) {
1347 err("primary numbers in our structures and pci config space don't match.\n");
1351 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1352 sub_no = (int) sub_number;
1353 debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1354 if (sec_no != sub_number) {
1355 err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1359 bus = ibmphp_find_res_bus(sec_number);
1361 err("cannot find Bus structure for the bridged device\n");
1364 debug("bus->busno is %x\n", bus->busno);
1365 debug("sec_number is %x\n", sec_number);
1367 ibmphp_remove_bus(bus, busno);
1369 for (count = 0; address[count]; count++) {
1371 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1373 if (!start_address) {
1374 /* This BAR is not implemented */
1378 tmp_address = start_address;
1380 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1382 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1383 if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1384 err("cannot find corresponding IO resource to remove\n");
1388 debug("io->start = %x\n", io->start);
1390 ibmphp_remove_resource(io);
1392 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1394 /* This is Memory */
1395 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1397 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1398 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1399 err("cannot find corresponding PFMEM resource to remove\n");
1403 debug("pfmem->start = %x\n", pfmem->start);
1405 ibmphp_remove_resource(pfmem);
1408 /* regular memory */
1409 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1410 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1411 err("cannot find corresponding MEM resource to remove\n");
1415 debug("mem->start = %x\n", mem->start);
1417 ibmphp_remove_resource(mem);
1420 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1421 /* takes up another dword */
1426 debug("%s - exiting, returning success\n", __func__);
1430 static int unconfigure_boot_card(struct slot *slot_cur)
1440 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1442 debug("%s - enter\n", __func__);
1444 device = slot_cur->device;
1445 busno = slot_cur->bus;
1447 debug("b4 for loop, device is %x\n", device);
1448 /* For every function on the card */
1449 for (function = 0x0; function < 0x08; function++) {
1450 devfn = PCI_DEVFN(device, function);
1451 ibmphp_pci_bus->number = busno;
1453 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1455 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1456 /* found correct device!!! */
1459 debug("%s - found correct device\n", __func__);
1461 /* header: x x x x x x x x
1462 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1463 * |_=> 0 = single function device, 1 = multi-function device
1466 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1467 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1469 debug("hdr_type %x, class %x\n", hdr_type, class);
1470 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1471 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1472 err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device, function);
1474 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1475 err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device, function);
1480 case PCI_HEADER_TYPE_NORMAL:
1481 rc = unconfigure_boot_device(busno, device, function);
1483 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1484 device, function, busno);
1489 case PCI_HEADER_TYPE_MULTIDEVICE:
1490 rc = unconfigure_boot_device(busno, device, function);
1492 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1493 device, function, busno);
1497 case PCI_HEADER_TYPE_BRIDGE:
1499 if (class != PCI_CLASS_BRIDGE_PCI) {
1500 err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
1503 rc = unconfigure_boot_bridge(busno, device, function);
1505 err("was not able to hot-remove PPB properly.\n");
1511 case PCI_HEADER_TYPE_MULTIBRIDGE:
1513 if (class != PCI_CLASS_BRIDGE_PCI) {
1514 err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
1517 rc = unconfigure_boot_bridge(busno, device, function);
1519 err("was not able to hot-remove PPB properly.\n");
1524 err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1527 } /* end of switch */
1528 } /* end of valid device */
1531 if (!valid_device) {
1532 err("Could not find device to unconfigure. Or could not read the card.\n");
1539 * free the resources of the card (multi, single, or bridged)
1540 * Parameters: slot, flag to say if this is for removing entire module or just
1541 * unconfiguring the device
1542 * TO DO: will probably need to add some code in case there was some resource,
1543 * to remove it... this is from when we have errors in the configure_card...
1544 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1545 * Returns: 0, -1, -ENODEV
1547 int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1552 struct slot *sl = *slot_cur;
1553 struct pci_func *cur_func = NULL;
1554 struct pci_func *temp_func;
1556 debug("%s - enter\n", __func__);
1559 /* Need to unconfigure the card */
1560 rc = unconfigure_boot_card(sl);
1561 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1562 /* In all other cases, will still need to get rid of func structure if it exists */
1568 cur_func = sl->func;
1570 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1571 if (cur_func->bus) {
1572 /* in other words, it's a PPB */
1578 for (i = 0; i < count; i++) {
1579 if (cur_func->io[i]) {
1580 debug("io[%d] exists\n", i);
1582 ibmphp_remove_resource(cur_func->io[i]);
1583 cur_func->io[i] = NULL;
1585 if (cur_func->mem[i]) {
1586 debug("mem[%d] exists\n", i);
1588 ibmphp_remove_resource(cur_func->mem[i]);
1589 cur_func->mem[i] = NULL;
1591 if (cur_func->pfmem[i]) {
1592 debug("pfmem[%d] exists\n", i);
1594 ibmphp_remove_resource(cur_func->pfmem[i]);
1595 cur_func->pfmem[i] = NULL;
1599 temp_func = cur_func->next;
1601 cur_func = temp_func;
1607 debug("%s - exit\n", __func__);
1612 * add a new bus resulting from hot-plugging a PPB bridge with devices
1614 * Input: bus and the amount of resources needed (we know we can assign those,
1615 * since they've been checked already
1616 * Output: bus added to the correct spot
1619 static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1621 struct range_node *io_range = NULL;
1622 struct range_node *mem_range = NULL;
1623 struct range_node *pfmem_range = NULL;
1624 struct bus_node *cur_bus = NULL;
1626 /* Trying to find the parent bus number */
1627 if (parent_busno != 0xFF) {
1628 cur_bus = ibmphp_find_res_bus(parent_busno);
1630 err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1634 list_add(&bus->bus_list, &cur_bus->bus_list);
1637 io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1641 io_range->start = io->start;
1642 io_range->end = io->end;
1643 io_range->rangeno = 1;
1644 bus->noIORanges = 1;
1645 bus->rangeIO = io_range;
1648 mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1652 mem_range->start = mem->start;
1653 mem_range->end = mem->end;
1654 mem_range->rangeno = 1;
1655 bus->noMemRanges = 1;
1656 bus->rangeMem = mem_range;
1659 pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1663 pfmem_range->start = pfmem->start;
1664 pfmem_range->end = pfmem->end;
1665 pfmem_range->rangeno = 1;
1666 bus->noPFMemRanges = 1;
1667 bus->rangePFMem = pfmem_range;
1673 * find the 1st available bus number for PPB to set as its secondary bus
1674 * Parameters: bus_number of the primary bus
1675 * Returns: bus_number of the secondary bus or 0xff in case of failure
1677 static u8 find_sec_number(u8 primary_busno, u8 slotno)
1681 struct bus_info *bus;
1682 struct bus_node *bus_cur;
1684 bus = ibmphp_find_same_bus_num(primary_busno);
1686 err("cannot get slot range of the bus from the BIOS\n");
1689 max = bus->slot_max;
1690 min = bus->slot_min;
1691 if ((slotno > max) || (slotno < min)) {
1692 err("got the wrong range\n");
1695 busno = (u8) (slotno - (u8) min);
1696 busno += primary_busno + 0x01;
1697 bus_cur = ibmphp_find_res_bus(busno);
1698 /* either there is no such bus number, or there are no ranges, which
1699 * can only happen if we removed the bridged device in previous load
1700 * of the driver, and now only have the skeleton bus struct
1702 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))