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
);
159 newfunc
->busno
= cur_func
->busno
;
160 newfunc
->device
= device
;
161 cur_func
->next
= newfunc
;
163 for (j
= 0; j
< 4; j
++)
164 newfunc
->irq
[j
] = cur_func
->irq
[j
];
166 case PCI_HEADER_TYPE_MULTIBRIDGE
:
168 if (class != PCI_CLASS_BRIDGE_PCI
) {
169 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
173 assign_alt_irq(cur_func
, class_code
);
174 rc
= configure_bridge(&cur_func
, slotno
);
176 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
177 err("Bus %x, devfunc %x\n", cur_func
->busno
, cur_func
->device
);
181 /* We need to do this in case some other BARs were properly inserted */
182 err("was not able to hot-add PPB properly.\n");
183 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
188 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
190 for (i
= 0; i
< 32; i
++) {
191 if (func
->devices
[i
]) {
192 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
196 newfunc
->busno
= sec_number
;
197 newfunc
->device
= (u8
) i
;
198 for (j
= 0; j
< 4; j
++)
199 newfunc
->irq
[j
] = cur_func
->irq
[j
];
202 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
) ;
203 prev_func
->next
= newfunc
;
205 cur_func
->next
= newfunc
;
207 rc
= ibmphp_configure_card(newfunc
, slotno
);
208 /* This could only happen if kmalloc failed */
210 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
211 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
219 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
223 newfunc
->busno
= cur_func
->busno
;
224 newfunc
->device
= device
;
225 for (j
= 0; j
< 4; j
++)
226 newfunc
->irq
[j
] = cur_func
->irq
[j
];
227 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
);
228 prev_func
->next
= newfunc
;
231 case PCI_HEADER_TYPE_BRIDGE
:
233 debug("class now is %x\n", class);
234 if (class != PCI_CLASS_BRIDGE_PCI
) {
235 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
240 assign_alt_irq(cur_func
, class_code
);
242 debug("cur_func->busno b4 configure_bridge is %x\n", cur_func
->busno
);
243 rc
= configure_bridge(&cur_func
, slotno
);
245 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
246 err("Bus %x, devfunc %x\n", cur_func
->busno
, cur_func
->device
);
250 /* We need to do this in case some other BARs were properly inserted */
251 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
252 err("was not able to hot-add PPB properly.\n");
256 debug("cur_func->busno = %x, device = %x, function = %x\n",
257 cur_func
->busno
, device
, function
);
258 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
259 debug("after configuring bridge..., sec_number = %x\n", sec_number
);
261 for (i
= 0; i
< 32; i
++) {
262 if (func
->devices
[i
]) {
263 debug("inside for loop, device is %x\n", i
);
264 newfunc
= kzalloc(sizeof(*newfunc
), GFP_KERNEL
);
268 newfunc
->busno
= sec_number
;
269 newfunc
->device
= (u8
) i
;
270 for (j
= 0; j
< 4; j
++)
271 newfunc
->irq
[j
] = cur_func
->irq
[j
];
274 for (prev_func
= cur_func
; prev_func
->next
; prev_func
= prev_func
->next
);
275 prev_func
->next
= newfunc
;
277 cur_func
->next
= newfunc
;
279 rc
= ibmphp_configure_card(newfunc
, slotno
);
281 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
283 /* We need to do this in case some other BARs were properly inserted */
284 func
->bus
= 1; /* To indicate to the unconfigure function that this is a PPB */
295 err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type
);
297 } /* end of switch */
298 } /* end of valid device */
302 err("Cannot find any valid devices on the card. Or unable to read from card.\n");
309 for (i
= 0; i
< cleanup_count
; i
++) {
310 if (cur_func
->io
[i
]) {
311 ibmphp_remove_resource(cur_func
->io
[i
]);
312 cur_func
->io
[i
] = NULL
;
313 } else if (cur_func
->pfmem
[i
]) {
314 ibmphp_remove_resource(cur_func
->pfmem
[i
]);
315 cur_func
->pfmem
[i
] = NULL
;
316 } else if (cur_func
->mem
[i
]) {
317 ibmphp_remove_resource(cur_func
->mem
[i
]);
318 cur_func
->mem
[i
] = NULL
;
325 * This function configures the pci BARs of a single device.
326 * Input: pointer to the pci_func
327 * Output: configured PCI, 0, or error
329 static int configure_device(struct pci_func
*func
)
344 struct resource_node
*io
[6];
345 struct resource_node
*mem
[6];
346 struct resource_node
*mem_tmp
;
347 struct resource_node
*pfmem
[6];
350 debug("%s - inside\n", __func__
);
352 devfn
= PCI_DEVFN(func
->device
, func
->function
);
353 ibmphp_pci_bus
->number
= func
->busno
;
355 for (count
= 0; address
[count
]; count
++) { /* for 6 BARs */
357 /* not sure if i need this. per scott, said maybe need * something like this
358 if devices don't adhere 100% to the spec, so don't want to write
361 pcibios_read_config_byte(cur_func->busno, cur_func->device,
362 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
363 if (tmp & 0x01) // IO
364 pcibios_write_config_dword(cur_func->busno, cur_func->device,
365 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
367 pcibios_write_config_dword(cur_func->busno, cur_func->device,
368 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
370 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
371 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
373 if (!bar
[count
]) /* This BAR is not implemented */
376 debug("Device %x BAR %d wants %x\n", func
->device
, count
, bar
[count
]);
378 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
380 debug("inside IO SPACE\n");
382 len
[count
] = bar
[count
] & 0xFFFFFFFC;
383 len
[count
] = ~len
[count
] + 1;
385 debug("len[count] in IO %x, count %d\n", len
[count
], count
);
387 io
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
392 io
[count
]->type
= IO
;
393 io
[count
]->busno
= func
->busno
;
394 io
[count
]->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
395 io
[count
]->len
= len
[count
];
396 if (ibmphp_check_resource(io
[count
], 0) == 0) {
397 ibmphp_add_resource(io
[count
]);
398 func
->io
[count
] = io
[count
];
400 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
401 func
->busno
, func
->device
, func
->function
, len
[count
]);
405 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->io
[count
]->start
);
407 /* _______________This is for debugging purposes only_____________________ */
408 debug("b4 writing, the IO address is %x\n", func
->io
[count
]->start
);
409 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
410 debug("after writing.... the start address is %x\n", bar
[count
]);
411 /* _________________________________________________________________________*/
415 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
417 debug("PFMEM SPACE\n");
419 len
[count
] = bar
[count
] & 0xFFFFFFF0;
420 len
[count
] = ~len
[count
] + 1;
422 debug("len[count] in PFMEM %x, count %d\n", len
[count
], count
);
424 pfmem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
428 pfmem
[count
]->type
= PFMEM
;
429 pfmem
[count
]->busno
= func
->busno
;
430 pfmem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
432 pfmem
[count
]->len
= len
[count
];
433 pfmem
[count
]->fromMem
= 0;
434 if (ibmphp_check_resource(pfmem
[count
], 0) == 0) {
435 ibmphp_add_resource(pfmem
[count
]);
436 func
->pfmem
[count
] = pfmem
[count
];
438 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
444 mem_tmp
->busno
= pfmem
[count
]->busno
;
445 mem_tmp
->devfunc
= pfmem
[count
]->devfunc
;
446 mem_tmp
->len
= pfmem
[count
]->len
;
447 debug("there's no pfmem... going into mem.\n");
448 if (ibmphp_check_resource(mem_tmp
, 0) == 0) {
449 ibmphp_add_resource(mem_tmp
);
450 pfmem
[count
]->fromMem
= 1;
451 pfmem
[count
]->rangeno
= mem_tmp
->rangeno
;
452 pfmem
[count
]->start
= mem_tmp
->start
;
453 pfmem
[count
]->end
= mem_tmp
->end
;
454 ibmphp_add_pfmem_from_mem(pfmem
[count
]);
455 func
->pfmem
[count
] = pfmem
[count
];
457 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
458 func
->busno
, func
->device
, len
[count
]);
465 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->pfmem
[count
]->start
);
467 /*_______________This is for debugging purposes only______________________________*/
468 debug("b4 writing, start address is %x\n", func
->pfmem
[count
]->start
);
469 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
470 debug("after writing, start address is %x\n", bar
[count
]);
471 /*_________________________________________________________________________________*/
473 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) { /* takes up another dword */
474 debug("inside the mem 64 case, count %d\n", count
);
476 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
477 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
481 debug("REGULAR MEM SPACE\n");
483 len
[count
] = bar
[count
] & 0xFFFFFFF0;
484 len
[count
] = ~len
[count
] + 1;
486 debug("len[count] in Mem %x, count %d\n", len
[count
], count
);
488 mem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
492 mem
[count
]->type
= MEM
;
493 mem
[count
]->busno
= func
->busno
;
494 mem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
496 mem
[count
]->len
= len
[count
];
497 if (ibmphp_check_resource(mem
[count
], 0) == 0) {
498 ibmphp_add_resource(mem
[count
]);
499 func
->mem
[count
] = mem
[count
];
501 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
502 func
->busno
, func
->device
, len
[count
]);
506 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->mem
[count
]->start
);
507 /* _______________________This is for debugging purposes only _______________________*/
508 debug("b4 writing, start address is %x\n", func
->mem
[count
]->start
);
509 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
510 debug("after writing, the address is %x\n", bar
[count
]);
511 /* __________________________________________________________________________________*/
513 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
514 /* takes up another dword */
515 debug("inside mem 64 case, reg. mem, count %d\n", count
);
517 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
518 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
524 func
->bus
= 0; /* To indicate that this is not a PPB */
525 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_PIN
, &irq
);
526 if ((irq
> 0x00) && (irq
< 0x05))
527 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_LINE
, func
->irq
[irq
- 1]);
529 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, CACHE
);
530 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_LATENCY_TIMER
, LATENCY
);
532 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_ROM_ADDRESS
, 0x00L
);
533 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_COMMAND
, DEVICEENABLE
);
538 /******************************************************************************
539 * This routine configures a PCI-2-PCI bridge and the functions behind it
540 * Parameters: pci_func
542 ******************************************************************************/
543 static int configure_bridge(struct pci_func
**func_passed
, u8 slotno
)
556 u8 need_io_upper
= 0;
557 u8 need_pfmem_upper
= 0;
558 struct res_needed
*amount_needed
= NULL
;
559 struct resource_node
*io
= NULL
;
560 struct resource_node
*bus_io
[2] = {NULL
, NULL
};
561 struct resource_node
*mem
= NULL
;
562 struct resource_node
*bus_mem
[2] = {NULL
, NULL
};
563 struct resource_node
*mem_tmp
= NULL
;
564 struct resource_node
*pfmem
= NULL
;
565 struct resource_node
*bus_pfmem
[2] = {NULL
, NULL
};
566 struct bus_node
*bus
;
572 struct pci_func
*func
= *func_passed
;
577 debug("%s - enter\n", __func__
);
579 devfn
= PCI_DEVFN(func
->function
, func
->device
);
580 ibmphp_pci_bus
->number
= func
->busno
;
582 /* Configuring necessary info for the bridge so that we could see the devices
586 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_PRIMARY_BUS
, func
->busno
);
588 /* _____________________For debugging purposes only __________________________
589 pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
590 debug("primary # written into the bridge is %x\n", pri_number);
591 ___________________________________________________________________________*/
593 /* in EBDA, only get allocated 1 additional bus # per slot */
594 sec_number
= find_sec_number(func
->busno
, slotno
);
595 if (sec_number
== 0xff) {
596 err("cannot allocate secondary bus number for the bridged device\n");
600 debug("after find_sec_number, the number we got is %x\n", sec_number
);
601 debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func
->busno
);
603 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, sec_number
);
605 /* __________________For debugging purposes only __________________________________
606 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
607 debug("sec_number after write/read is %x\n", sec_number);
608 ________________________________________________________________________________*/
610 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, sec_number
);
612 /* __________________For debugging purposes only ____________________________________
613 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
614 debug("subordinate number after write/read is %x\n", sec_number);
615 __________________________________________________________________________________*/
617 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_CACHE_LINE_SIZE
, CACHE
);
618 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_LATENCY_TIMER
, LATENCY
);
619 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_SEC_LATENCY_TIMER
, LATENCY
);
621 debug("func->busno is %x\n", func
->busno
);
622 debug("sec_number after writing is %x\n", sec_number
);
625 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
626 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
627 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
630 /* First we need to allocate mem/io for the bridge itself in case it needs it */
631 for (count
= 0; address
[count
]; count
++) { /* for 2 BARs */
632 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
633 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
636 /* This BAR is not implemented */
637 debug("so we come here then, eh?, count = %d\n", count
);
640 // tmp_bar = bar[count];
642 debug("Bar %d wants %x\n", count
, bar
[count
]);
644 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
646 len
[count
] = bar
[count
] & 0xFFFFFFFC;
647 len
[count
] = ~len
[count
] + 1;
649 debug("len[count] in IO = %x\n", len
[count
]);
651 bus_io
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
653 if (!bus_io
[count
]) {
657 bus_io
[count
]->type
= IO
;
658 bus_io
[count
]->busno
= func
->busno
;
659 bus_io
[count
]->devfunc
= PCI_DEVFN(func
->device
,
661 bus_io
[count
]->len
= len
[count
];
662 if (ibmphp_check_resource(bus_io
[count
], 0) == 0) {
663 ibmphp_add_resource(bus_io
[count
]);
664 func
->io
[count
] = bus_io
[count
];
666 err("cannot allocate requested io for bus %x, device %x, len %x\n",
667 func
->busno
, func
->device
, len
[count
]);
668 kfree(bus_io
[count
]);
672 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->io
[count
]->start
);
676 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
678 len
[count
] = bar
[count
] & 0xFFFFFFF0;
679 len
[count
] = ~len
[count
] + 1;
681 debug("len[count] in PFMEM = %x\n", len
[count
]);
683 bus_pfmem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
684 if (!bus_pfmem
[count
]) {
688 bus_pfmem
[count
]->type
= PFMEM
;
689 bus_pfmem
[count
]->busno
= func
->busno
;
690 bus_pfmem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
692 bus_pfmem
[count
]->len
= len
[count
];
693 bus_pfmem
[count
]->fromMem
= 0;
694 if (ibmphp_check_resource(bus_pfmem
[count
], 0) == 0) {
695 ibmphp_add_resource(bus_pfmem
[count
]);
696 func
->pfmem
[count
] = bus_pfmem
[count
];
698 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
704 mem_tmp
->busno
= bus_pfmem
[count
]->busno
;
705 mem_tmp
->devfunc
= bus_pfmem
[count
]->devfunc
;
706 mem_tmp
->len
= bus_pfmem
[count
]->len
;
707 if (ibmphp_check_resource(mem_tmp
, 0) == 0) {
708 ibmphp_add_resource(mem_tmp
);
709 bus_pfmem
[count
]->fromMem
= 1;
710 bus_pfmem
[count
]->rangeno
= mem_tmp
->rangeno
;
711 ibmphp_add_pfmem_from_mem(bus_pfmem
[count
]);
712 func
->pfmem
[count
] = bus_pfmem
[count
];
714 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
715 func
->busno
, func
->device
, len
[count
]);
717 kfree(bus_pfmem
[count
]);
722 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->pfmem
[count
]->start
);
724 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
725 /* takes up another dword */
727 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
728 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
733 len
[count
] = bar
[count
] & 0xFFFFFFF0;
734 len
[count
] = ~len
[count
] + 1;
736 debug("len[count] in Memory is %x\n", len
[count
]);
738 bus_mem
[count
] = kzalloc(sizeof(struct resource_node
), GFP_KERNEL
);
739 if (!bus_mem
[count
]) {
743 bus_mem
[count
]->type
= MEM
;
744 bus_mem
[count
]->busno
= func
->busno
;
745 bus_mem
[count
]->devfunc
= PCI_DEVFN(func
->device
,
747 bus_mem
[count
]->len
= len
[count
];
748 if (ibmphp_check_resource(bus_mem
[count
], 0) == 0) {
749 ibmphp_add_resource(bus_mem
[count
]);
750 func
->mem
[count
] = bus_mem
[count
];
752 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
753 func
->busno
, func
->device
, len
[count
]);
754 kfree(bus_mem
[count
]);
758 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], func
->mem
[count
]->start
);
760 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
761 /* takes up another dword */
763 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
764 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0x00000000);
771 /* Now need to see how much space the devices behind the bridge needed */
772 amount_needed
= scan_behind_bridge(func
, sec_number
);
773 if (amount_needed
== NULL
)
776 ibmphp_pci_bus
->number
= func
->busno
;
777 debug("after coming back from scan_behind_bridge\n");
778 debug("amount_needed->not_correct = %x\n", amount_needed
->not_correct
);
779 debug("amount_needed->io = %x\n", amount_needed
->io
);
780 debug("amount_needed->mem = %x\n", amount_needed
->mem
);
781 debug("amount_needed->pfmem = %x\n", amount_needed
->pfmem
);
783 if (amount_needed
->not_correct
) {
784 debug("amount_needed is not correct\n");
785 for (count
= 0; address
[count
]; count
++) {
788 ibmphp_remove_resource(bus_io
[count
]);
789 func
->io
[count
] = NULL
;
790 } else if (bus_pfmem
[count
]) {
791 ibmphp_remove_resource(bus_pfmem
[count
]);
792 func
->pfmem
[count
] = NULL
;
793 } else if (bus_mem
[count
]) {
794 ibmphp_remove_resource(bus_mem
[count
]);
795 func
->mem
[count
] = NULL
;
798 kfree(amount_needed
);
802 if (!amount_needed
->io
) {
803 debug("it doesn't want IO?\n");
806 debug("it wants %x IO behind the bridge\n", amount_needed
->io
);
807 io
= kzalloc(sizeof(*io
), GFP_KERNEL
);
814 io
->busno
= func
->busno
;
815 io
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
816 io
->len
= amount_needed
->io
;
817 if (ibmphp_check_resource(io
, 1) == 0) {
818 debug("were we able to add io\n");
819 ibmphp_add_resource(io
);
824 if (!amount_needed
->mem
) {
825 debug("it doesn't want n.e.memory?\n");
828 debug("it wants %x memory behind the bridge\n", amount_needed
->mem
);
829 mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
835 mem
->busno
= func
->busno
;
836 mem
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
837 mem
->len
= amount_needed
->mem
;
838 if (ibmphp_check_resource(mem
, 1) == 0) {
839 ibmphp_add_resource(mem
);
841 debug("were we able to add mem\n");
845 if (!amount_needed
->pfmem
) {
846 debug("it doesn't want n.e.pfmem mem?\n");
849 debug("it wants %x pfmemory behind the bridge\n", amount_needed
->pfmem
);
850 pfmem
= kzalloc(sizeof(*pfmem
), GFP_KERNEL
);
856 pfmem
->busno
= func
->busno
;
857 pfmem
->devfunc
= PCI_DEVFN(func
->device
, func
->function
);
858 pfmem
->len
= amount_needed
->pfmem
;
860 if (ibmphp_check_resource(pfmem
, 1) == 0) {
861 ibmphp_add_resource(pfmem
);
864 mem_tmp
= kzalloc(sizeof(*mem_tmp
), GFP_KERNEL
);
870 mem_tmp
->busno
= pfmem
->busno
;
871 mem_tmp
->devfunc
= pfmem
->devfunc
;
872 mem_tmp
->len
= pfmem
->len
;
873 if (ibmphp_check_resource(mem_tmp
, 1) == 0) {
874 ibmphp_add_resource(mem_tmp
);
876 pfmem
->rangeno
= mem_tmp
->rangeno
;
877 ibmphp_add_pfmem_from_mem(pfmem
);
883 debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
884 debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io
, flag_mem
, flag_pfmem
);
886 if (flag_io
&& flag_mem
&& flag_pfmem
) {
887 /* If on bootup, there was a bridged card in this slot,
888 * then card was removed and ibmphp got unloaded and loaded
889 * back again, there's no way for us to remove the bus
890 * struct, so no need to kmalloc, can use existing node
892 bus
= ibmphp_find_res_bus(sec_number
);
894 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
899 bus
->busno
= sec_number
;
900 debug("b4 adding new bus\n");
901 rc
= add_new_bus(bus
, io
, mem
, pfmem
, func
->busno
);
902 } else if (!(bus
->rangeIO
) && !(bus
->rangeMem
) && !(bus
->rangePFMem
))
903 rc
= add_new_bus(bus
, io
, mem
, pfmem
, 0xFF);
905 err("expected bus structure not empty?\n");
911 ibmphp_remove_bus(bus
, func
->busno
);
912 kfree(amount_needed
);
918 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, &io_base
);
919 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, &pfmem_base
);
921 if ((io_base
& PCI_IO_RANGE_TYPE_MASK
) == PCI_IO_RANGE_TYPE_32
) {
925 if ((pfmem_base
& PCI_PREF_RANGE_TYPE_MASK
) == PCI_PREF_RANGE_TYPE_64
) {
927 need_pfmem_upper
= 1;
930 if (bus
->noIORanges
) {
931 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, 0x00 | bus
->rangeIO
->start
>> 8);
932 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT
, 0x00 | bus
->rangeIO
->end
>> 8);
934 /* _______________This is for debugging purposes only ____________________
935 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
936 debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
937 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
938 debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
939 ________________________________________________________________________*/
941 if (need_io_upper
) { /* since can't support n.e.ways */
942 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_IO_BASE_UPPER16
, 0x0000);
943 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT_UPPER16
, 0x0000);
946 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_BASE
, 0x00);
947 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_IO_LIMIT
, 0x00);
950 if (bus
->noMemRanges
) {
951 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_BASE
, 0x0000 | bus
->rangeMem
->start
>> 16);
952 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_LIMIT
, 0x0000 | bus
->rangeMem
->end
>> 16);
954 /* ____________________This is for debugging purposes only ________________________
955 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
956 debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
957 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
958 debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
959 __________________________________________________________________________________*/
962 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_BASE
, 0xffff);
963 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_MEMORY_LIMIT
, 0x0000);
965 if (bus
->noPFMemRanges
) {
966 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, 0x0000 | bus
->rangePFMem
->start
>> 16);
967 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, 0x0000 | bus
->rangePFMem
->end
>> 16);
969 /* __________________________This is for debugging purposes only _______________________
970 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
971 debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
972 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
973 debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
974 ______________________________________________________________________________________*/
976 if (need_pfmem_upper
) { /* since can't support n.e.ways */
977 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_PREF_BASE_UPPER32
, 0x00000000);
978 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, PCI_PREF_LIMIT_UPPER32
, 0x00000000);
981 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_BASE
, 0xffff);
982 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_PREF_MEMORY_LIMIT
, 0x0000);
985 debug("b4 writing control information\n");
987 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_PIN
, &irq
);
988 if ((irq
> 0x00) && (irq
< 0x05))
989 pci_bus_write_config_byte(ibmphp_pci_bus
, devfn
, PCI_INTERRUPT_LINE
, func
->irq
[irq
- 1]);
991 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
992 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
993 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
996 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_COMMAND
, DEVICEENABLE
);
997 pci_bus_write_config_word(ibmphp_pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, 0x07);
998 for (i
= 0; i
< 32; i
++) {
999 if (amount_needed
->devices
[i
]) {
1000 debug("device where devices[i] is 1 = %x\n", i
);
1001 func
->devices
[i
] = 1;
1004 func
->bus
= 1; /* For unconfiguring, to indicate it's PPB */
1005 func_passed
= &func
;
1006 debug("func->busno b4 returning is %x\n", func
->busno
);
1007 debug("func->busno b4 returning in the other structure is %x\n", (*func_passed
)->busno
);
1008 kfree(amount_needed
);
1011 err("Configuring bridge was unsuccessful...\n");
1018 kfree(amount_needed
);
1020 ibmphp_remove_resource(pfmem
);
1022 ibmphp_remove_resource(io
);
1024 ibmphp_remove_resource(mem
);
1025 for (i
= 0; i
< 2; i
++) { /* for 2 BARs */
1027 ibmphp_remove_resource(bus_io
[i
]);
1029 } else if (bus_pfmem
[i
]) {
1030 ibmphp_remove_resource(bus_pfmem
[i
]);
1031 func
->pfmem
[i
] = NULL
;
1032 } else if (bus_mem
[i
]) {
1033 ibmphp_remove_resource(bus_mem
[i
]);
1034 func
->mem
[i
] = NULL
;
1040 /*****************************************************************************
1041 * This function adds up the amount of resources needed behind the PPB bridge
1042 * and passes it to the configure_bridge function
1043 * Input: bridge function
1044 * Output: amount of resources needed
1045 *****************************************************************************/
1046 static struct res_needed
*scan_behind_bridge(struct pci_func
*func
, u8 busno
)
1051 u8 device
, function
;
1053 int howmany
= 0; /*this is to see if there are any devices behind the bridge */
1065 struct res_needed
*amount
;
1067 amount
= kzalloc(sizeof(*amount
), GFP_KERNEL
);
1071 ibmphp_pci_bus
->number
= busno
;
1073 debug("the bus_no behind the bridge is %x\n", busno
);
1074 debug("scanning devices behind the bridge...\n");
1075 for (device
= 0; device
< 32; device
++) {
1076 amount
->devices
[device
] = 0;
1077 for (function
= 0; function
< 8; function
++) {
1078 devfn
= PCI_DEVFN(device
, function
);
1080 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_VENDOR_ID
, &vendor_id
);
1082 if (vendor_id
!= PCI_VENDOR_ID_NOTVALID
) {
1083 /* found correct device!!! */
1086 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_HEADER_TYPE
, &hdr_type
);
1087 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, PCI_CLASS_REVISION
, &class);
1089 debug("hdr_type behind the bridge is %x\n", hdr_type
);
1090 if ((hdr_type
& 0x7f) == PCI_HEADER_TYPE_BRIDGE
) {
1091 err("embedded bridges not supported for hot-plugging.\n");
1092 amount
->not_correct
= 1;
1096 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1097 if (class == PCI_CLASS_NOT_DEFINED_VGA
) {
1098 err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device
);
1099 amount
->not_correct
= 1;
1101 } else if (class == PCI_CLASS_DISPLAY_VGA
) {
1102 err("The device %x is not supported for hot plugging. Please choose another device.\n", device
);
1103 amount
->not_correct
= 1;
1107 amount
->devices
[device
] = 1;
1109 for (count
= 0; address
[count
]; count
++) {
1112 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1113 if (tmp & 0x01) // IO
1114 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1116 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1118 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
1119 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &bar
[count
]);
1121 debug("what is bar[count]? %x, count = %d\n", bar
[count
], count
);
1123 if (!bar
[count
]) /* This BAR is not implemented */
1126 //tmp_bar = bar[count];
1128 debug("count %d device %x function %x wants %x resources\n", count
, device
, function
, bar
[count
]);
1130 if (bar
[count
] & PCI_BASE_ADDRESS_SPACE_IO
) {
1132 len
[count
] = bar
[count
] & 0xFFFFFFFC;
1133 len
[count
] = ~len
[count
] + 1;
1134 amount
->io
+= len
[count
];
1136 /* This is Memory */
1137 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1139 len
[count
] = bar
[count
] & 0xFFFFFFF0;
1140 len
[count
] = ~len
[count
] + 1;
1141 amount
->pfmem
+= len
[count
];
1142 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
)
1143 /* takes up another dword */
1147 /* regular memory */
1148 len
[count
] = bar
[count
] & 0xFFFFFFF0;
1149 len
[count
] = ~len
[count
] + 1;
1150 amount
->mem
+= len
[count
];
1151 if (bar
[count
] & PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1152 /* takes up another dword */
1158 } /* end if (valid) */
1163 amount
->not_correct
= 1;
1165 amount
->not_correct
= 0;
1166 if ((amount
->io
) && (amount
->io
< IOBRIDGE
))
1167 amount
->io
= IOBRIDGE
;
1168 if ((amount
->mem
) && (amount
->mem
< MEMBRIDGE
))
1169 amount
->mem
= MEMBRIDGE
;
1170 if ((amount
->pfmem
) && (amount
->pfmem
< MEMBRIDGE
))
1171 amount
->pfmem
= MEMBRIDGE
;
1175 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1176 * upon bootup in the system, since we don't allocate func to such case, we need to read
1177 * the start addresses from pci config space and then find the corresponding entries in
1178 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1179 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1180 * the bootup case), since it should work same way
1182 static int unconfigure_boot_device(u8 busno
, u8 device
, u8 function
)
1195 struct resource_node
*io
;
1196 struct resource_node
*mem
;
1197 struct resource_node
*pfmem
;
1198 struct bus_node
*bus
;
1205 debug("%s - enter\n", __func__
);
1207 bus
= ibmphp_find_res_bus(busno
);
1209 debug("cannot find corresponding bus.\n");
1213 devfn
= PCI_DEVFN(device
, function
);
1214 ibmphp_pci_bus
->number
= busno
;
1215 for (count
= 0; address
[count
]; count
++) { /* for 6 BARs */
1216 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &start_address
);
1218 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1220 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], 0xFFFFFFFF);
1221 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &size
);
1222 pci_bus_write_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], start_address
);
1224 debug("start_address is %x\n", start_address
);
1225 debug("busno, device, function %x %x %x\n", busno
, device
, function
);
1227 /* This BAR is not implemented */
1228 debug("is this bar no implemented?, count = %d\n", count
);
1231 tmp_address
= start_address
;
1232 if (start_address
& PCI_BASE_ADDRESS_SPACE_IO
) {
1234 start_address
&= PCI_BASE_ADDRESS_IO_MASK
;
1235 size
= size
& 0xFFFFFFFC;
1237 end_address
= start_address
+ size
- 1;
1238 if (ibmphp_find_resource(bus
, start_address
, &io
, IO
))
1239 goto report_search_failure
;
1241 debug("io->start = %x\n", io
->start
);
1243 start_address
= io
->end
+ 1;
1244 ibmphp_remove_resource(io
);
1245 /* This is needed b/c of the old I/O restrictions in the BIOS */
1246 while (temp_end
< end_address
) {
1247 if (ibmphp_find_resource(bus
, start_address
,
1249 goto report_search_failure
;
1251 debug("io->start = %x\n", io
->start
);
1253 start_address
= io
->end
+ 1;
1254 ibmphp_remove_resource(io
);
1257 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1259 /* This is Memory */
1260 if (start_address
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1262 debug("start address of pfmem is %x\n", start_address
);
1263 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1265 if (ibmphp_find_resource(bus
, start_address
, &pfmem
, PFMEM
) < 0) {
1266 err("cannot find corresponding PFMEM resource to remove\n");
1270 debug("pfmem->start = %x\n", pfmem
->start
);
1272 ibmphp_remove_resource(pfmem
);
1275 /* regular memory */
1276 debug("start address of mem is %x\n", start_address
);
1277 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1279 if (ibmphp_find_resource(bus
, start_address
, &mem
, MEM
) < 0) {
1280 err("cannot find corresponding MEM resource to remove\n");
1284 debug("mem->start = %x\n", mem
->start
);
1286 ibmphp_remove_resource(mem
);
1289 if (tmp_address
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1290 /* takes up another dword */
1298 report_search_failure
:
1299 err("cannot find corresponding IO resource to remove\n");
1303 static int unconfigure_boot_bridge(u8 busno
, u8 device
, u8 function
)
1306 int bus_no
, pri_no
, sub_no
, sec_no
= 0;
1307 u32 start_address
, tmp_address
;
1308 u8 sec_number
, sub_number
, pri_number
;
1309 struct resource_node
*io
= NULL
;
1310 struct resource_node
*mem
= NULL
;
1311 struct resource_node
*pfmem
= NULL
;
1312 struct bus_node
*bus
;
1320 devfn
= PCI_DEVFN(device
, function
);
1321 ibmphp_pci_bus
->number
= busno
;
1322 bus_no
= (int) busno
;
1323 debug("busno is %x\n", busno
);
1324 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_PRIMARY_BUS
, &pri_number
);
1325 debug("%s - busno = %x, primary_number = %x\n", __func__
, busno
, pri_number
);
1327 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SECONDARY_BUS
, &sec_number
);
1328 debug("sec_number is %x\n", sec_number
);
1329 sec_no
= (int) sec_number
;
1330 pri_no
= (int) pri_number
;
1331 if (pri_no
!= bus_no
) {
1332 err("primary numbers in our structures and pci config space don't match.\n");
1336 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_SUBORDINATE_BUS
, &sub_number
);
1337 sub_no
= (int) sub_number
;
1338 debug("sub_no is %d, sec_no is %d\n", sub_no
, sec_no
);
1339 if (sec_no
!= sub_number
) {
1340 err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1344 bus
= ibmphp_find_res_bus(sec_number
);
1346 err("cannot find Bus structure for the bridged device\n");
1349 debug("bus->busno is %x\n", bus
->busno
);
1350 debug("sec_number is %x\n", sec_number
);
1352 ibmphp_remove_bus(bus
, busno
);
1354 for (count
= 0; address
[count
]; count
++) {
1356 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, address
[count
], &start_address
);
1358 if (!start_address
) {
1359 /* This BAR is not implemented */
1363 tmp_address
= start_address
;
1365 if (start_address
& PCI_BASE_ADDRESS_SPACE_IO
) {
1367 start_address
&= PCI_BASE_ADDRESS_IO_MASK
;
1368 if (ibmphp_find_resource(bus
, start_address
, &io
, IO
) < 0) {
1369 err("cannot find corresponding IO resource to remove\n");
1373 debug("io->start = %x\n", io
->start
);
1375 ibmphp_remove_resource(io
);
1377 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1379 /* This is Memory */
1380 if (start_address
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
1382 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1383 if (ibmphp_find_resource(bus
, start_address
, &pfmem
, PFMEM
) < 0) {
1384 err("cannot find corresponding PFMEM resource to remove\n");
1388 debug("pfmem->start = %x\n", pfmem
->start
);
1390 ibmphp_remove_resource(pfmem
);
1393 /* regular memory */
1394 start_address
&= PCI_BASE_ADDRESS_MEM_MASK
;
1395 if (ibmphp_find_resource(bus
, start_address
, &mem
, MEM
) < 0) {
1396 err("cannot find corresponding MEM resource to remove\n");
1400 debug("mem->start = %x\n", mem
->start
);
1402 ibmphp_remove_resource(mem
);
1405 if (tmp_address
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
1406 /* takes up another dword */
1411 debug("%s - exiting, returning success\n", __func__
);
1415 static int unconfigure_boot_card(struct slot
*slot_cur
)
1425 u8 valid_device
= 0x00; /* To see if we are ever able to find valid device and read it */
1427 debug("%s - enter\n", __func__
);
1429 device
= slot_cur
->device
;
1430 busno
= slot_cur
->bus
;
1432 debug("b4 for loop, device is %x\n", device
);
1433 /* For every function on the card */
1434 for (function
= 0x0; function
< 0x08; function
++) {
1435 devfn
= PCI_DEVFN(device
, function
);
1436 ibmphp_pci_bus
->number
= busno
;
1438 pci_bus_read_config_word(ibmphp_pci_bus
, devfn
, PCI_VENDOR_ID
, &vendor_id
);
1440 if (vendor_id
!= PCI_VENDOR_ID_NOTVALID
) {
1441 /* found correct device!!! */
1444 debug("%s - found correct device\n", __func__
);
1446 /* header: x x x x x x x x
1447 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1448 * |_=> 0 = single function device, 1 = multi-function device
1451 pci_bus_read_config_byte(ibmphp_pci_bus
, devfn
, PCI_HEADER_TYPE
, &hdr_type
);
1452 pci_bus_read_config_dword(ibmphp_pci_bus
, devfn
, PCI_CLASS_REVISION
, &class);
1454 debug("hdr_type %x, class %x\n", hdr_type
, class);
1455 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1456 if (class == PCI_CLASS_NOT_DEFINED_VGA
) {
1457 err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device
, function
);
1459 } else if (class == PCI_CLASS_DISPLAY_VGA
) {
1460 err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device
, function
);
1465 case PCI_HEADER_TYPE_NORMAL
:
1466 rc
= unconfigure_boot_device(busno
, device
, function
);
1468 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1469 device
, function
, busno
);
1474 case PCI_HEADER_TYPE_MULTIDEVICE
:
1475 rc
= unconfigure_boot_device(busno
, device
, function
);
1477 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1478 device
, function
, busno
);
1482 case PCI_HEADER_TYPE_BRIDGE
:
1484 if (class != PCI_CLASS_BRIDGE_PCI
) {
1485 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
);
1488 rc
= unconfigure_boot_bridge(busno
, device
, function
);
1490 err("was not able to hot-remove PPB properly.\n");
1496 case PCI_HEADER_TYPE_MULTIBRIDGE
:
1498 if (class != PCI_CLASS_BRIDGE_PCI
) {
1499 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
);
1502 rc
= unconfigure_boot_bridge(busno
, device
, function
);
1504 err("was not able to hot-remove PPB properly.\n");
1509 err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1511 } /* end of switch */
1512 } /* end of valid device */
1515 if (!valid_device
) {
1516 err("Could not find device to unconfigure. Or could not read the card.\n");
1523 * free the resources of the card (multi, single, or bridged)
1524 * Parameters: slot, flag to say if this is for removing entire module or just
1525 * unconfiguring the device
1526 * TO DO: will probably need to add some code in case there was some resource,
1527 * to remove it... this is from when we have errors in the configure_card...
1528 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1529 * Returns: 0, -1, -ENODEV
1531 int ibmphp_unconfigure_card(struct slot
**slot_cur
, int the_end
)
1536 struct slot
*sl
= *slot_cur
;
1537 struct pci_func
*cur_func
= NULL
;
1538 struct pci_func
*temp_func
;
1540 debug("%s - enter\n", __func__
);
1543 /* Need to unconfigure the card */
1544 rc
= unconfigure_boot_card(sl
);
1545 if ((rc
== -ENODEV
) || (rc
== -EIO
) || (rc
== -EINVAL
)) {
1546 /* In all other cases, will still need to get rid of func structure if it exists */
1552 cur_func
= sl
->func
;
1554 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1555 if (cur_func
->bus
) {
1556 /* in other words, it's a PPB */
1562 for (i
= 0; i
< count
; i
++) {
1563 if (cur_func
->io
[i
]) {
1564 debug("io[%d] exists\n", i
);
1566 ibmphp_remove_resource(cur_func
->io
[i
]);
1567 cur_func
->io
[i
] = NULL
;
1569 if (cur_func
->mem
[i
]) {
1570 debug("mem[%d] exists\n", i
);
1572 ibmphp_remove_resource(cur_func
->mem
[i
]);
1573 cur_func
->mem
[i
] = NULL
;
1575 if (cur_func
->pfmem
[i
]) {
1576 debug("pfmem[%d] exists\n", i
);
1578 ibmphp_remove_resource(cur_func
->pfmem
[i
]);
1579 cur_func
->pfmem
[i
] = NULL
;
1583 temp_func
= cur_func
->next
;
1585 cur_func
= temp_func
;
1591 debug("%s - exit\n", __func__
);
1596 * add a new bus resulting from hot-plugging a PPB bridge with devices
1598 * Input: bus and the amount of resources needed (we know we can assign those,
1599 * since they've been checked already
1600 * Output: bus added to the correct spot
1603 static int add_new_bus(struct bus_node
*bus
, struct resource_node
*io
, struct resource_node
*mem
, struct resource_node
*pfmem
, u8 parent_busno
)
1605 struct range_node
*io_range
= NULL
;
1606 struct range_node
*mem_range
= NULL
;
1607 struct range_node
*pfmem_range
= NULL
;
1608 struct bus_node
*cur_bus
= NULL
;
1610 /* Trying to find the parent bus number */
1611 if (parent_busno
!= 0xFF) {
1612 cur_bus
= ibmphp_find_res_bus(parent_busno
);
1614 err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1618 list_add(&bus
->bus_list
, &cur_bus
->bus_list
);
1621 io_range
= kzalloc(sizeof(*io_range
), GFP_KERNEL
);
1625 io_range
->start
= io
->start
;
1626 io_range
->end
= io
->end
;
1627 io_range
->rangeno
= 1;
1628 bus
->noIORanges
= 1;
1629 bus
->rangeIO
= io_range
;
1632 mem_range
= kzalloc(sizeof(*mem_range
), GFP_KERNEL
);
1636 mem_range
->start
= mem
->start
;
1637 mem_range
->end
= mem
->end
;
1638 mem_range
->rangeno
= 1;
1639 bus
->noMemRanges
= 1;
1640 bus
->rangeMem
= mem_range
;
1643 pfmem_range
= kzalloc(sizeof(*pfmem_range
), GFP_KERNEL
);
1647 pfmem_range
->start
= pfmem
->start
;
1648 pfmem_range
->end
= pfmem
->end
;
1649 pfmem_range
->rangeno
= 1;
1650 bus
->noPFMemRanges
= 1;
1651 bus
->rangePFMem
= pfmem_range
;
1657 * find the 1st available bus number for PPB to set as its secondary bus
1658 * Parameters: bus_number of the primary bus
1659 * Returns: bus_number of the secondary bus or 0xff in case of failure
1661 static u8
find_sec_number(u8 primary_busno
, u8 slotno
)
1665 struct bus_info
*bus
;
1666 struct bus_node
*bus_cur
;
1668 bus
= ibmphp_find_same_bus_num(primary_busno
);
1670 err("cannot get slot range of the bus from the BIOS\n");
1673 max
= bus
->slot_max
;
1674 min
= bus
->slot_min
;
1675 if ((slotno
> max
) || (slotno
< min
)) {
1676 err("got the wrong range\n");
1679 busno
= (u8
) (slotno
- (u8
) min
);
1680 busno
+= primary_busno
+ 0x01;
1681 bus_cur
= ibmphp_find_res_bus(busno
);
1682 /* either there is no such bus number, or there are no ranges, which
1683 * can only happen if we removed the bridged device in previous load
1684 * of the driver, and now only have the skeleton bus struct
1686 if ((!bus_cur
) || (!(bus_cur
->rangeIO
) && !(bus_cur
->rangeMem
) && !(bus_cur
->rangePFMem
)))