1 // SPDX-License-Identifier: GPL-2.0+
3 * IBM Hot Plug Controller Driver
5 * Written By: Irene Zubarev, IBM Corporation
7 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
8 * Copyright (C) 2001,2002 IBM Corp.
10 * All rights reserved.
12 * Send feedback to <gregkh@us.ibm.com>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19 #include <linux/list.h>
23 static int configure_device(struct pci_func *);
24 static int configure_bridge(struct pci_func **, u8);
25 static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
26 static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
27 static u8 find_sec_number(u8 primary_busno, u8 slotno);
30 * NOTE..... If BIOS doesn't provide default routing, we assign:
31 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
32 * If adapter is bridged, then we assign 11 to it and devices behind it.
33 * We also assign the same irq numbers for multi function devices.
34 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
36 static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
39 for (j = 0; j < 4; j++) {
40 if (cur_func->irq[j] == 0xff) {
42 case PCI_BASE_CLASS_STORAGE:
43 cur_func->irq[j] = SCSI_IRQ;
45 case PCI_BASE_CLASS_NETWORK:
46 cur_func->irq[j] = LAN_IRQ;
49 cur_func->irq[j] = OTHER_IRQ;
57 * Configures the device to be added (will allocate needed resources if it
58 * can), the device can be a bridge or a regular pci device, can also be
61 * Input: function to be added
63 * TO DO: The error case with Multifunction device or multi function bridge,
64 * if there is an error, will need to go through all previous functions and
65 * unconfigure....or can add some code into unconfigure_card....
67 int ibmphp_configure_card(struct pci_func *func, u8 slotno)
72 u8 hdr_type, device, sec_number;
74 struct pci_func *newfunc; /* for multi devices */
75 struct pci_func *cur_func, *prev_func;
79 u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
81 debug("inside configure_card, func->busno = %x\n", func->busno);
83 device = func->device;
86 /* We only get bus and device from IRQ routing table. So at this point,
87 * func->busno is correct, and func->device contains only device (at the 5
91 /* For every function on the card */
92 for (function = 0x00; function < 0x08; function++) {
93 unsigned int devfn = PCI_DEVFN(device, function);
94 ibmphp_pci_bus->number = cur_func->busno;
96 cur_func->function = function;
98 debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
99 cur_func->busno, cur_func->device, cur_func->function);
101 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
103 debug("vendor_id is %x\n", vendor_id);
104 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
105 /* found correct device!!! */
106 debug("found valid device, vendor_id = %x\n", vendor_id);
110 /* header: x x x x x x x x
111 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
112 * |_=> 0 = single function device, 1 = multi-function device
115 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
116 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
118 class_code = class >> 24;
119 debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
120 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
121 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
122 err("The device %x is VGA compatible and as is not supported for hot plugging. "
123 "Please choose another device.\n", cur_func->device);
125 } else if (class == PCI_CLASS_DISPLAY_VGA) {
126 err("The device %x is not supported for hot plugging. Please choose another device.\n",
131 case PCI_HEADER_TYPE_NORMAL:
132 debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
133 assign_alt_irq(cur_func, class_code);
134 rc = configure_device(cur_func);
136 /* We need to do this in case some other BARs were properly inserted */
137 err("was not able to configure devfunc %x on bus %x.\n",
138 cur_func->device, cur_func->busno);
142 cur_func->next = NULL;
145 case PCI_HEADER_TYPE_MULTIDEVICE:
146 assign_alt_irq(cur_func, class_code);
147 rc = configure_device(cur_func);
149 /* We need to do this in case some other BARs were properly inserted */
150 err("was not able to configure devfunc %x on bus %x...bailing out\n",
151 cur_func->device, cur_func->busno);
155 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
157 err("out of system memory\n");
160 newfunc->busno = cur_func->busno;
161 newfunc->device = device;
162 cur_func->next = newfunc;
164 for (j = 0; j < 4; j++)
165 newfunc->irq[j] = cur_func->irq[j];
167 case PCI_HEADER_TYPE_MULTIBRIDGE:
169 if (class != PCI_CLASS_BRIDGE_PCI) {
170 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
174 assign_alt_irq(cur_func, class_code);
175 rc = configure_bridge(&cur_func, slotno);
177 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
178 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
182 /* We need to do this in case some other BARs were properly inserted */
183 err("was not able to hot-add PPB properly.\n");
184 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
189 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
191 for (i = 0; i < 32; i++) {
192 if (func->devices[i]) {
193 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
195 err("out of system memory\n");
198 newfunc->busno = sec_number;
199 newfunc->device = (u8) i;
200 for (j = 0; j < 4; j++)
201 newfunc->irq[j] = cur_func->irq[j];
204 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
205 prev_func->next = newfunc;
207 cur_func->next = newfunc;
209 rc = ibmphp_configure_card(newfunc, slotno);
210 /* This could only happen if kmalloc failed */
212 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
213 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
221 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
223 err("out of system memory\n");
226 newfunc->busno = cur_func->busno;
227 newfunc->device = device;
228 for (j = 0; j < 4; j++)
229 newfunc->irq[j] = cur_func->irq[j];
230 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
231 prev_func->next = newfunc;
234 case PCI_HEADER_TYPE_BRIDGE:
236 debug("class now is %x\n", class);
237 if (class != PCI_CLASS_BRIDGE_PCI) {
238 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
243 assign_alt_irq(cur_func, class_code);
245 debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
246 rc = configure_bridge(&cur_func, slotno);
248 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
249 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
253 /* We need to do this in case some other BARs were properly inserted */
254 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
255 err("was not able to hot-add PPB properly.\n");
259 debug("cur_func->busno = %x, device = %x, function = %x\n",
260 cur_func->busno, device, function);
261 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
262 debug("after configuring bridge..., sec_number = %x\n", sec_number);
264 for (i = 0; i < 32; i++) {
265 if (func->devices[i]) {
266 debug("inside for loop, device is %x\n", i);
267 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
269 err(" out of system memory\n");
272 newfunc->busno = sec_number;
273 newfunc->device = (u8) i;
274 for (j = 0; j < 4; j++)
275 newfunc->irq[j] = cur_func->irq[j];
278 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
279 prev_func->next = newfunc;
281 cur_func->next = newfunc;
283 rc = ibmphp_configure_card(newfunc, slotno);
285 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
287 /* We need to do this in case some other BARs were properly inserted */
288 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
299 err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
302 } /* end of switch */
303 } /* end of valid device */
307 err("Cannot find any valid devices on the card. Or unable to read from card.\n");
314 for (i = 0; i < cleanup_count; i++) {
315 if (cur_func->io[i]) {
316 ibmphp_remove_resource(cur_func->io[i]);
317 cur_func->io[i] = NULL;
318 } else if (cur_func->pfmem[i]) {
319 ibmphp_remove_resource(cur_func->pfmem[i]);
320 cur_func->pfmem[i] = NULL;
321 } else if (cur_func->mem[i]) {
322 ibmphp_remove_resource(cur_func->mem[i]);
323 cur_func->mem[i] = NULL;
330 * This function configures the pci BARs of a single device.
331 * Input: pointer to the pci_func
332 * Output: configured PCI, 0, or error
334 static int configure_device(struct pci_func *func)
349 struct resource_node *io[6];
350 struct resource_node *mem[6];
351 struct resource_node *mem_tmp;
352 struct resource_node *pfmem[6];
355 debug("%s - inside\n", __func__);
357 devfn = PCI_DEVFN(func->device, func->function);
358 ibmphp_pci_bus->number = func->busno;
360 for (count = 0; address[count]; count++) { /* for 6 BARs */
362 /* not sure if i need this. per scott, said maybe need * something like this
363 if devices don't adhere 100% to the spec, so don't want to write
366 pcibios_read_config_byte(cur_func->busno, cur_func->device,
367 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
368 if (tmp & 0x01) // IO
369 pcibios_write_config_dword(cur_func->busno, cur_func->device,
370 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
372 pcibios_write_config_dword(cur_func->busno, cur_func->device,
373 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
375 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
376 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
378 if (!bar[count]) /* This BAR is not implemented */
381 debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
383 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
385 debug("inside IO SPACE\n");
387 len[count] = bar[count] & 0xFFFFFFFC;
388 len[count] = ~len[count] + 1;
390 debug("len[count] in IO %x, count %d\n", len[count], count);
392 io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
395 err("out of system memory\n");
398 io[count]->type = IO;
399 io[count]->busno = func->busno;
400 io[count]->devfunc = PCI_DEVFN(func->device, func->function);
401 io[count]->len = len[count];
402 if (ibmphp_check_resource(io[count], 0) == 0) {
403 ibmphp_add_resource(io[count]);
404 func->io[count] = io[count];
406 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
407 func->busno, func->device, func->function, len[count]);
411 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
413 /* _______________This is for debugging purposes only_____________________ */
414 debug("b4 writing, the IO address is %x\n", func->io[count]->start);
415 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
416 debug("after writing.... the start address is %x\n", bar[count]);
417 /* _________________________________________________________________________*/
421 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
423 debug("PFMEM SPACE\n");
425 len[count] = bar[count] & 0xFFFFFFF0;
426 len[count] = ~len[count] + 1;
428 debug("len[count] in PFMEM %x, count %d\n", len[count], count);
430 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
432 err("out of system memory\n");
435 pfmem[count]->type = PFMEM;
436 pfmem[count]->busno = func->busno;
437 pfmem[count]->devfunc = PCI_DEVFN(func->device,
439 pfmem[count]->len = len[count];
440 pfmem[count]->fromMem = 0;
441 if (ibmphp_check_resource(pfmem[count], 0) == 0) {
442 ibmphp_add_resource(pfmem[count]);
443 func->pfmem[count] = pfmem[count];
445 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
447 err("out of system memory\n");
452 mem_tmp->busno = pfmem[count]->busno;
453 mem_tmp->devfunc = pfmem[count]->devfunc;
454 mem_tmp->len = pfmem[count]->len;
455 debug("there's no pfmem... going into mem.\n");
456 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
457 ibmphp_add_resource(mem_tmp);
458 pfmem[count]->fromMem = 1;
459 pfmem[count]->rangeno = mem_tmp->rangeno;
460 pfmem[count]->start = mem_tmp->start;
461 pfmem[count]->end = mem_tmp->end;
462 ibmphp_add_pfmem_from_mem(pfmem[count]);
463 func->pfmem[count] = pfmem[count];
465 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
466 func->busno, func->device, len[count]);
473 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
475 /*_______________This is for debugging purposes only______________________________*/
476 debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
477 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
478 debug("after writing, start address is %x\n", bar[count]);
479 /*_________________________________________________________________________________*/
481 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
482 debug("inside the mem 64 case, count %d\n", count);
484 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
485 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
489 debug("REGULAR MEM SPACE\n");
491 len[count] = bar[count] & 0xFFFFFFF0;
492 len[count] = ~len[count] + 1;
494 debug("len[count] in Mem %x, count %d\n", len[count], count);
496 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
498 err("out of system memory\n");
501 mem[count]->type = MEM;
502 mem[count]->busno = func->busno;
503 mem[count]->devfunc = PCI_DEVFN(func->device,
505 mem[count]->len = len[count];
506 if (ibmphp_check_resource(mem[count], 0) == 0) {
507 ibmphp_add_resource(mem[count]);
508 func->mem[count] = mem[count];
510 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
511 func->busno, func->device, len[count]);
515 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
516 /* _______________________This is for debugging purposes only _______________________*/
517 debug("b4 writing, start address is %x\n", func->mem[count]->start);
518 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
519 debug("after writing, the address is %x\n", bar[count]);
520 /* __________________________________________________________________________________*/
522 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
523 /* takes up another dword */
524 debug("inside mem 64 case, reg. mem, count %d\n", count);
526 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
527 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
533 func->bus = 0; /* To indicate that this is not a PPB */
534 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
535 if ((irq > 0x00) && (irq < 0x05))
536 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
538 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
539 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
541 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
542 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
547 /******************************************************************************
548 * This routine configures a PCI-2-PCI bridge and the functions behind it
549 * Parameters: pci_func
551 ******************************************************************************/
552 static int configure_bridge(struct pci_func **func_passed, u8 slotno)
565 u8 need_io_upper = 0;
566 u8 need_pfmem_upper = 0;
567 struct res_needed *amount_needed = NULL;
568 struct resource_node *io = NULL;
569 struct resource_node *bus_io[2] = {NULL, NULL};
570 struct resource_node *mem = NULL;
571 struct resource_node *bus_mem[2] = {NULL, NULL};
572 struct resource_node *mem_tmp = NULL;
573 struct resource_node *pfmem = NULL;
574 struct resource_node *bus_pfmem[2] = {NULL, NULL};
575 struct bus_node *bus;
581 struct pci_func *func = *func_passed;
586 debug("%s - enter\n", __func__);
588 devfn = PCI_DEVFN(func->function, func->device);
589 ibmphp_pci_bus->number = func->busno;
591 /* Configuring necessary info for the bridge so that we could see the devices
595 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
597 /* _____________________For debugging purposes only __________________________
598 pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
599 debug("primary # written into the bridge is %x\n", pri_number);
600 ___________________________________________________________________________*/
602 /* in EBDA, only get allocated 1 additional bus # per slot */
603 sec_number = find_sec_number(func->busno, slotno);
604 if (sec_number == 0xff) {
605 err("cannot allocate secondary bus number for the bridged device\n");
609 debug("after find_sec_number, the number we got is %x\n", sec_number);
610 debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
612 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
614 /* __________________For debugging purposes only __________________________________
615 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
616 debug("sec_number after write/read is %x\n", sec_number);
617 ________________________________________________________________________________*/
619 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
621 /* __________________For debugging purposes only ____________________________________
622 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
623 debug("subordinate number after write/read is %x\n", sec_number);
624 __________________________________________________________________________________*/
626 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
627 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
628 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
630 debug("func->busno is %x\n", func->busno);
631 debug("sec_number after writing is %x\n", sec_number);
634 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
635 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
636 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
639 /* First we need to allocate mem/io for the bridge itself in case it needs it */
640 for (count = 0; address[count]; count++) { /* for 2 BARs */
641 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
642 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
645 /* This BAR is not implemented */
646 debug("so we come here then, eh?, count = %d\n", count);
649 // tmp_bar = bar[count];
651 debug("Bar %d wants %x\n", count, bar[count]);
653 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
655 len[count] = bar[count] & 0xFFFFFFFC;
656 len[count] = ~len[count] + 1;
658 debug("len[count] in IO = %x\n", len[count]);
660 bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
662 if (!bus_io[count]) {
663 err("out of system memory\n");
667 bus_io[count]->type = IO;
668 bus_io[count]->busno = func->busno;
669 bus_io[count]->devfunc = PCI_DEVFN(func->device,
671 bus_io[count]->len = len[count];
672 if (ibmphp_check_resource(bus_io[count], 0) == 0) {
673 ibmphp_add_resource(bus_io[count]);
674 func->io[count] = bus_io[count];
676 err("cannot allocate requested io for bus %x, device %x, len %x\n",
677 func->busno, func->device, len[count]);
678 kfree(bus_io[count]);
682 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
686 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
688 len[count] = bar[count] & 0xFFFFFFF0;
689 len[count] = ~len[count] + 1;
691 debug("len[count] in PFMEM = %x\n", len[count]);
693 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
694 if (!bus_pfmem[count]) {
695 err("out of system memory\n");
699 bus_pfmem[count]->type = PFMEM;
700 bus_pfmem[count]->busno = func->busno;
701 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
703 bus_pfmem[count]->len = len[count];
704 bus_pfmem[count]->fromMem = 0;
705 if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
706 ibmphp_add_resource(bus_pfmem[count]);
707 func->pfmem[count] = bus_pfmem[count];
709 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
711 err("out of system memory\n");
716 mem_tmp->busno = bus_pfmem[count]->busno;
717 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
718 mem_tmp->len = bus_pfmem[count]->len;
719 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
720 ibmphp_add_resource(mem_tmp);
721 bus_pfmem[count]->fromMem = 1;
722 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
723 ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
724 func->pfmem[count] = bus_pfmem[count];
726 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
727 func->busno, func->device, len[count]);
729 kfree(bus_pfmem[count]);
734 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
736 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
737 /* takes up another dword */
739 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
740 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
745 len[count] = bar[count] & 0xFFFFFFF0;
746 len[count] = ~len[count] + 1;
748 debug("len[count] in Memory is %x\n", len[count]);
750 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
751 if (!bus_mem[count]) {
752 err("out of system memory\n");
756 bus_mem[count]->type = MEM;
757 bus_mem[count]->busno = func->busno;
758 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
760 bus_mem[count]->len = len[count];
761 if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
762 ibmphp_add_resource(bus_mem[count]);
763 func->mem[count] = bus_mem[count];
765 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
766 func->busno, func->device, len[count]);
767 kfree(bus_mem[count]);
771 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
773 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
774 /* takes up another dword */
776 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
777 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
784 /* Now need to see how much space the devices behind the bridge needed */
785 amount_needed = scan_behind_bridge(func, sec_number);
786 if (amount_needed == NULL)
789 ibmphp_pci_bus->number = func->busno;
790 debug("after coming back from scan_behind_bridge\n");
791 debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
792 debug("amount_needed->io = %x\n", amount_needed->io);
793 debug("amount_needed->mem = %x\n", amount_needed->mem);
794 debug("amount_needed->pfmem = %x\n", amount_needed->pfmem);
796 if (amount_needed->not_correct) {
797 debug("amount_needed is not correct\n");
798 for (count = 0; address[count]; count++) {
801 ibmphp_remove_resource(bus_io[count]);
802 func->io[count] = NULL;
803 } else if (bus_pfmem[count]) {
804 ibmphp_remove_resource(bus_pfmem[count]);
805 func->pfmem[count] = NULL;
806 } else if (bus_mem[count]) {
807 ibmphp_remove_resource(bus_mem[count]);
808 func->mem[count] = NULL;
811 kfree(amount_needed);
815 if (!amount_needed->io) {
816 debug("it doesn't want IO?\n");
819 debug("it wants %x IO behind the bridge\n", amount_needed->io);
820 io = kzalloc(sizeof(*io), GFP_KERNEL);
823 err("out of system memory\n");
828 io->busno = func->busno;
829 io->devfunc = PCI_DEVFN(func->device, func->function);
830 io->len = amount_needed->io;
831 if (ibmphp_check_resource(io, 1) == 0) {
832 debug("were we able to add io\n");
833 ibmphp_add_resource(io);
838 if (!amount_needed->mem) {
839 debug("it doesn't want n.e.memory?\n");
842 debug("it wants %x memory behind the bridge\n", amount_needed->mem);
843 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
845 err("out of system memory\n");
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);
867 err("out of system memory\n");
872 pfmem->busno = func->busno;
873 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
874 pfmem->len = amount_needed->pfmem;
876 if (ibmphp_check_resource(pfmem, 1) == 0) {
877 ibmphp_add_resource(pfmem);
880 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
882 err("out of system memory\n");
887 mem_tmp->busno = pfmem->busno;
888 mem_tmp->devfunc = pfmem->devfunc;
889 mem_tmp->len = pfmem->len;
890 if (ibmphp_check_resource(mem_tmp, 1) == 0) {
891 ibmphp_add_resource(mem_tmp);
893 pfmem->rangeno = mem_tmp->rangeno;
894 ibmphp_add_pfmem_from_mem(pfmem);
900 debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
901 debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
903 if (flag_io && flag_mem && flag_pfmem) {
904 /* If on bootup, there was a bridged card in this slot,
905 * then card was removed and ibmphp got unloaded and loaded
906 * back again, there's no way for us to remove the bus
907 * struct, so no need to kmalloc, can use existing node
909 bus = ibmphp_find_res_bus(sec_number);
911 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
913 err("out of system memory\n");
917 bus->busno = sec_number;
918 debug("b4 adding new bus\n");
919 rc = add_new_bus(bus, io, mem, pfmem, func->busno);
920 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
921 rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
923 err("expected bus structure not empty?\n");
929 ibmphp_remove_bus(bus, func->busno);
930 kfree(amount_needed);
936 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
937 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
939 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
943 if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
945 need_pfmem_upper = 1;
948 if (bus->noIORanges) {
949 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
950 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
952 /* _______________This is for debugging purposes only ____________________
953 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
954 debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
955 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
956 debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
957 ________________________________________________________________________*/
959 if (need_io_upper) { /* since can't support n.e.ways */
960 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
961 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
964 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
965 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
968 if (bus->noMemRanges) {
969 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
970 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
972 /* ____________________This is for debugging purposes only ________________________
973 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
974 debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
975 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
976 debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
977 __________________________________________________________________________________*/
980 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
981 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
983 if (bus->noPFMemRanges) {
984 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
985 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
987 /* __________________________This is for debugging purposes only _______________________
988 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
989 debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
990 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
991 debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
992 ______________________________________________________________________________________*/
994 if (need_pfmem_upper) { /* since can't support n.e.ways */
995 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
996 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
999 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1000 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1003 debug("b4 writing control information\n");
1005 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1006 if ((irq > 0x00) && (irq < 0x05))
1007 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1009 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1010 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1011 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1014 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1015 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1016 for (i = 0; i < 32; i++) {
1017 if (amount_needed->devices[i]) {
1018 debug("device where devices[i] is 1 = %x\n", i);
1019 func->devices[i] = 1;
1022 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1023 func_passed = &func;
1024 debug("func->busno b4 returning is %x\n", func->busno);
1025 debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1026 kfree(amount_needed);
1029 err("Configuring bridge was unsuccessful...\n");
1036 kfree(amount_needed);
1038 ibmphp_remove_resource(pfmem);
1040 ibmphp_remove_resource(io);
1042 ibmphp_remove_resource(mem);
1043 for (i = 0; i < 2; i++) { /* for 2 BARs */
1045 ibmphp_remove_resource(bus_io[i]);
1047 } else if (bus_pfmem[i]) {
1048 ibmphp_remove_resource(bus_pfmem[i]);
1049 func->pfmem[i] = NULL;
1050 } else if (bus_mem[i]) {
1051 ibmphp_remove_resource(bus_mem[i]);
1052 func->mem[i] = NULL;
1058 /*****************************************************************************
1059 * This function adds up the amount of resources needed behind the PPB bridge
1060 * and passes it to the configure_bridge function
1061 * Input: bridge function
1062 * Output: amount of resources needed
1063 *****************************************************************************/
1064 static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1069 u8 device, function;
1071 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1083 struct res_needed *amount;
1085 amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1089 ibmphp_pci_bus->number = busno;
1091 debug("the bus_no behind the bridge is %x\n", busno);
1092 debug("scanning devices behind the bridge...\n");
1093 for (device = 0; device < 32; device++) {
1094 amount->devices[device] = 0;
1095 for (function = 0; function < 8; function++) {
1096 devfn = PCI_DEVFN(device, function);
1098 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1100 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1101 /* found correct device!!! */
1104 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1105 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1107 debug("hdr_type behind the bridge is %x\n", hdr_type);
1108 if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1109 err("embedded bridges not supported for hot-plugging.\n");
1110 amount->not_correct = 1;
1114 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1115 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1116 err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device);
1117 amount->not_correct = 1;
1119 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1120 err("The device %x is not supported for hot plugging. Please choose another device.\n", device);
1121 amount->not_correct = 1;
1125 amount->devices[device] = 1;
1127 for (count = 0; address[count]; count++) {
1130 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1131 if (tmp & 0x01) // IO
1132 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1134 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1136 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1137 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1139 debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1141 if (!bar[count]) /* This BAR is not implemented */
1144 //tmp_bar = bar[count];
1146 debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1148 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1150 len[count] = bar[count] & 0xFFFFFFFC;
1151 len[count] = ~len[count] + 1;
1152 amount->io += len[count];
1154 /* This is Memory */
1155 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1157 len[count] = bar[count] & 0xFFFFFFF0;
1158 len[count] = ~len[count] + 1;
1159 amount->pfmem += len[count];
1160 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1161 /* takes up another dword */
1165 /* regular memory */
1166 len[count] = bar[count] & 0xFFFFFFF0;
1167 len[count] = ~len[count] + 1;
1168 amount->mem += len[count];
1169 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1170 /* takes up another dword */
1176 } /* end if (valid) */
1181 amount->not_correct = 1;
1183 amount->not_correct = 0;
1184 if ((amount->io) && (amount->io < IOBRIDGE))
1185 amount->io = IOBRIDGE;
1186 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1187 amount->mem = MEMBRIDGE;
1188 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1189 amount->pfmem = MEMBRIDGE;
1193 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1194 * upon bootup in the system, since we don't allocate func to such case, we need to read
1195 * the start addresses from pci config space and then find the corresponding entries in
1196 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1197 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1198 * the bootup case), since it should work same way
1200 static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1213 struct resource_node *io;
1214 struct resource_node *mem;
1215 struct resource_node *pfmem;
1216 struct bus_node *bus;
1223 debug("%s - enter\n", __func__);
1225 bus = ibmphp_find_res_bus(busno);
1227 debug("cannot find corresponding bus.\n");
1231 devfn = PCI_DEVFN(device, function);
1232 ibmphp_pci_bus->number = busno;
1233 for (count = 0; address[count]; count++) { /* for 6 BARs */
1234 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1236 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1238 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1239 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1240 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1242 debug("start_address is %x\n", start_address);
1243 debug("busno, device, function %x %x %x\n", busno, device, function);
1245 /* This BAR is not implemented */
1246 debug("is this bar no implemented?, count = %d\n", count);
1249 tmp_address = start_address;
1250 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1252 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1253 size = size & 0xFFFFFFFC;
1255 end_address = start_address + size - 1;
1256 if (ibmphp_find_resource(bus, start_address, &io, IO))
1257 goto report_search_failure;
1259 debug("io->start = %x\n", io->start);
1261 start_address = io->end + 1;
1262 ibmphp_remove_resource(io);
1263 /* This is needed b/c of the old I/O restrictions in the BIOS */
1264 while (temp_end < end_address) {
1265 if (ibmphp_find_resource(bus, start_address,
1267 goto report_search_failure;
1269 debug("io->start = %x\n", io->start);
1271 start_address = io->end + 1;
1272 ibmphp_remove_resource(io);
1275 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1277 /* This is Memory */
1278 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1280 debug("start address of pfmem is %x\n", start_address);
1281 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1283 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1284 err("cannot find corresponding PFMEM resource to remove\n");
1288 debug("pfmem->start = %x\n", pfmem->start);
1290 ibmphp_remove_resource(pfmem);
1293 /* regular memory */
1294 debug("start address of mem is %x\n", start_address);
1295 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1297 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1298 err("cannot find corresponding MEM resource to remove\n");
1302 debug("mem->start = %x\n", mem->start);
1304 ibmphp_remove_resource(mem);
1307 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1308 /* takes up another dword */
1316 report_search_failure:
1317 err("cannot find corresponding IO resource to remove\n");
1321 static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1324 int bus_no, pri_no, sub_no, sec_no = 0;
1325 u32 start_address, tmp_address;
1326 u8 sec_number, sub_number, pri_number;
1327 struct resource_node *io = NULL;
1328 struct resource_node *mem = NULL;
1329 struct resource_node *pfmem = NULL;
1330 struct bus_node *bus;
1338 devfn = PCI_DEVFN(device, function);
1339 ibmphp_pci_bus->number = busno;
1340 bus_no = (int) busno;
1341 debug("busno is %x\n", busno);
1342 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1343 debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1345 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1346 debug("sec_number is %x\n", sec_number);
1347 sec_no = (int) sec_number;
1348 pri_no = (int) pri_number;
1349 if (pri_no != bus_no) {
1350 err("primary numbers in our structures and pci config space don't match.\n");
1354 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1355 sub_no = (int) sub_number;
1356 debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1357 if (sec_no != sub_number) {
1358 err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1362 bus = ibmphp_find_res_bus(sec_number);
1364 err("cannot find Bus structure for the bridged device\n");
1367 debug("bus->busno is %x\n", bus->busno);
1368 debug("sec_number is %x\n", sec_number);
1370 ibmphp_remove_bus(bus, busno);
1372 for (count = 0; address[count]; count++) {
1374 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1376 if (!start_address) {
1377 /* This BAR is not implemented */
1381 tmp_address = start_address;
1383 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1385 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1386 if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1387 err("cannot find corresponding IO resource to remove\n");
1391 debug("io->start = %x\n", io->start);
1393 ibmphp_remove_resource(io);
1395 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1397 /* This is Memory */
1398 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1400 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1401 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1402 err("cannot find corresponding PFMEM resource to remove\n");
1406 debug("pfmem->start = %x\n", pfmem->start);
1408 ibmphp_remove_resource(pfmem);
1411 /* regular memory */
1412 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1413 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1414 err("cannot find corresponding MEM resource to remove\n");
1418 debug("mem->start = %x\n", mem->start);
1420 ibmphp_remove_resource(mem);
1423 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1424 /* takes up another dword */
1429 debug("%s - exiting, returning success\n", __func__);
1433 static int unconfigure_boot_card(struct slot *slot_cur)
1443 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1445 debug("%s - enter\n", __func__);
1447 device = slot_cur->device;
1448 busno = slot_cur->bus;
1450 debug("b4 for loop, device is %x\n", device);
1451 /* For every function on the card */
1452 for (function = 0x0; function < 0x08; function++) {
1453 devfn = PCI_DEVFN(device, function);
1454 ibmphp_pci_bus->number = busno;
1456 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1458 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1459 /* found correct device!!! */
1462 debug("%s - found correct device\n", __func__);
1464 /* header: x x x x x x x x
1465 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1466 * |_=> 0 = single function device, 1 = multi-function device
1469 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1470 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1472 debug("hdr_type %x, class %x\n", hdr_type, class);
1473 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1474 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1475 err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device, function);
1477 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1478 err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device, function);
1483 case PCI_HEADER_TYPE_NORMAL:
1484 rc = unconfigure_boot_device(busno, device, function);
1486 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1487 device, function, busno);
1492 case PCI_HEADER_TYPE_MULTIDEVICE:
1493 rc = unconfigure_boot_device(busno, device, function);
1495 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1496 device, function, busno);
1500 case PCI_HEADER_TYPE_BRIDGE:
1502 if (class != PCI_CLASS_BRIDGE_PCI) {
1503 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);
1506 rc = unconfigure_boot_bridge(busno, device, function);
1508 err("was not able to hot-remove PPB properly.\n");
1514 case PCI_HEADER_TYPE_MULTIBRIDGE:
1516 if (class != PCI_CLASS_BRIDGE_PCI) {
1517 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);
1520 rc = unconfigure_boot_bridge(busno, device, function);
1522 err("was not able to hot-remove PPB properly.\n");
1527 err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1530 } /* end of switch */
1531 } /* end of valid device */
1534 if (!valid_device) {
1535 err("Could not find device to unconfigure. Or could not read the card.\n");
1542 * free the resources of the card (multi, single, or bridged)
1543 * Parameters: slot, flag to say if this is for removing entire module or just
1544 * unconfiguring the device
1545 * TO DO: will probably need to add some code in case there was some resource,
1546 * to remove it... this is from when we have errors in the configure_card...
1547 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1548 * Returns: 0, -1, -ENODEV
1550 int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1555 struct slot *sl = *slot_cur;
1556 struct pci_func *cur_func = NULL;
1557 struct pci_func *temp_func;
1559 debug("%s - enter\n", __func__);
1562 /* Need to unconfigure the card */
1563 rc = unconfigure_boot_card(sl);
1564 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1565 /* In all other cases, will still need to get rid of func structure if it exists */
1571 cur_func = sl->func;
1573 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1574 if (cur_func->bus) {
1575 /* in other words, it's a PPB */
1581 for (i = 0; i < count; i++) {
1582 if (cur_func->io[i]) {
1583 debug("io[%d] exists\n", i);
1585 ibmphp_remove_resource(cur_func->io[i]);
1586 cur_func->io[i] = NULL;
1588 if (cur_func->mem[i]) {
1589 debug("mem[%d] exists\n", i);
1591 ibmphp_remove_resource(cur_func->mem[i]);
1592 cur_func->mem[i] = NULL;
1594 if (cur_func->pfmem[i]) {
1595 debug("pfmem[%d] exists\n", i);
1597 ibmphp_remove_resource(cur_func->pfmem[i]);
1598 cur_func->pfmem[i] = NULL;
1602 temp_func = cur_func->next;
1604 cur_func = temp_func;
1610 debug("%s - exit\n", __func__);
1615 * add a new bus resulting from hot-plugging a PPB bridge with devices
1617 * Input: bus and the amount of resources needed (we know we can assign those,
1618 * since they've been checked already
1619 * Output: bus added to the correct spot
1622 static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1624 struct range_node *io_range = NULL;
1625 struct range_node *mem_range = NULL;
1626 struct range_node *pfmem_range = NULL;
1627 struct bus_node *cur_bus = NULL;
1629 /* Trying to find the parent bus number */
1630 if (parent_busno != 0xFF) {
1631 cur_bus = ibmphp_find_res_bus(parent_busno);
1633 err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1637 list_add(&bus->bus_list, &cur_bus->bus_list);
1640 io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1642 err("out of system memory\n");
1645 io_range->start = io->start;
1646 io_range->end = io->end;
1647 io_range->rangeno = 1;
1648 bus->noIORanges = 1;
1649 bus->rangeIO = io_range;
1652 mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1654 err("out of system memory\n");
1657 mem_range->start = mem->start;
1658 mem_range->end = mem->end;
1659 mem_range->rangeno = 1;
1660 bus->noMemRanges = 1;
1661 bus->rangeMem = mem_range;
1664 pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1666 err("out of system memory\n");
1669 pfmem_range->start = pfmem->start;
1670 pfmem_range->end = pfmem->end;
1671 pfmem_range->rangeno = 1;
1672 bus->noPFMemRanges = 1;
1673 bus->rangePFMem = pfmem_range;
1679 * find the 1st available bus number for PPB to set as its secondary bus
1680 * Parameters: bus_number of the primary bus
1681 * Returns: bus_number of the secondary bus or 0xff in case of failure
1683 static u8 find_sec_number(u8 primary_busno, u8 slotno)
1687 struct bus_info *bus;
1688 struct bus_node *bus_cur;
1690 bus = ibmphp_find_same_bus_num(primary_busno);
1692 err("cannot get slot range of the bus from the BIOS\n");
1695 max = bus->slot_max;
1696 min = bus->slot_min;
1697 if ((slotno > max) || (slotno < min)) {
1698 err("got the wrong range\n");
1701 busno = (u8) (slotno - (u8) min);
1702 busno += primary_busno + 0x01;
1703 bus_cur = ibmphp_find_res_bus(busno);
1704 /* either there is no such bus number, or there are no ranges, which
1705 * can only happen if we removed the bridged device in previous load
1706 * of the driver, and now only have the skeleton bus struct
1708 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))