2 * IBM Hot Plug Controller Driver
4 * Written By: Irene Zubarev, IBM Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Send feedback to <gregkh@us.ibm.com>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/list.h>
37 static int configure_device(struct pci_func *);
38 static int configure_bridge(struct pci_func **, u8);
39 static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
40 static int add_new_bus (struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
41 static u8 find_sec_number (u8 primary_busno, u8 slotno);
44 * NOTE..... If BIOS doesn't provide default routing, we assign:
45 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
46 * If adapter is bridged, then we assign 11 to it and devices behind it.
47 * We also assign the same irq numbers for multi function devices.
48 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
50 static void assign_alt_irq (struct pci_func * cur_func, u8 class_code)
53 for (j = 0; j < 4; j++) {
54 if (cur_func->irq[j] == 0xff) {
56 case PCI_BASE_CLASS_STORAGE:
57 cur_func->irq[j] = SCSI_IRQ;
59 case PCI_BASE_CLASS_NETWORK:
60 cur_func->irq[j] = LAN_IRQ;
63 cur_func->irq[j] = OTHER_IRQ;
71 * Configures the device to be added (will allocate needed resources if it
72 * can), the device can be a bridge or a regular pci device, can also be
75 * Input: function to be added
77 * TO DO: The error case with Multifunction device or multi function bridge,
78 * if there is an error, will need to go through all previous functions and
79 * unconfigure....or can add some code into unconfigure_card....
81 int ibmphp_configure_card (struct pci_func *func, u8 slotno)
86 u8 hdr_type, device, sec_number;
88 struct pci_func *newfunc; /* for multi devices */
89 struct pci_func *cur_func, *prev_func;
93 u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
95 debug ("inside configure_card, func->busno = %x\n", func->busno);
97 device = func->device;
100 /* We only get bus and device from IRQ routing table. So at this point,
101 * func->busno is correct, and func->device contains only device (at the 5
105 /* For every function on the card */
106 for (function = 0x00; function < 0x08; function++) {
107 unsigned int devfn = PCI_DEVFN(device, function);
108 ibmphp_pci_bus->number = cur_func->busno;
110 cur_func->function = function;
112 debug ("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->funcion = %x\n",
113 cur_func->busno, cur_func->device, cur_func->function);
115 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
117 debug ("vendor_id is %x\n", vendor_id);
118 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
119 /* found correct device!!! */
120 debug ("found valid device, vendor_id = %x\n", vendor_id);
124 /* header: x x x x x x x x
125 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
126 * |_=> 0 = single function device, 1 = multi-function device
129 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
130 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
132 class_code = class >> 24;
133 debug ("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
134 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
135 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
136 err ("The device %x is VGA compatible and as is not supported for hot plugging. "
137 "Please choose another device.\n", cur_func->device);
139 } else if (class == PCI_CLASS_DISPLAY_VGA) {
140 err ("The device %x is not supported for hot plugging. "
141 "Please choose another device.\n", cur_func->device);
145 case PCI_HEADER_TYPE_NORMAL:
146 debug ("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
147 assign_alt_irq (cur_func, class_code);
148 if ((rc = configure_device (cur_func)) < 0) {
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.\n",
151 cur_func->device, cur_func->busno);
155 cur_func->next = NULL;
158 case PCI_HEADER_TYPE_MULTIDEVICE:
159 assign_alt_irq (cur_func, class_code);
160 if ((rc = configure_device (cur_func)) < 0) {
161 /* We need to do this in case some other BARs were properly inserted */
162 err ("was not able to configure devfunc %x on bus %x...bailing out\n",
163 cur_func->device, cur_func->busno);
167 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
169 err ("out of system memory\n");
172 memset (newfunc, 0, sizeof (struct pci_func));
173 newfunc->busno = cur_func->busno;
174 newfunc->device = device;
175 cur_func->next = newfunc;
177 for (j = 0; j < 4; j++)
178 newfunc->irq[j] = cur_func->irq[j];
180 case PCI_HEADER_TYPE_MULTIBRIDGE:
182 if (class != PCI_CLASS_BRIDGE_PCI) {
183 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
184 "Please insert another card.\n", cur_func->device);
187 assign_alt_irq (cur_func, class_code);
188 rc = configure_bridge (&cur_func, slotno);
190 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
191 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
195 /* We need to do this in case some other BARs were properly inserted */
196 err ("was not able to hot-add PPB properly.\n");
197 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
202 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
204 for (i = 0; i < 32; i++) {
205 if (func->devices[i]) {
206 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
208 err ("out of system memory\n");
211 memset (newfunc, 0, sizeof (struct pci_func));
212 newfunc->busno = sec_number;
213 newfunc->device = (u8) i;
214 for (j = 0; j < 4; j++)
215 newfunc->irq[j] = cur_func->irq[j];
218 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
219 prev_func->next = newfunc;
221 cur_func->next = newfunc;
223 rc = ibmphp_configure_card (newfunc, slotno);
224 /* This could only happen if kmalloc failed */
226 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
227 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
235 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
237 err ("out of system memory\n");
240 memset (newfunc, 0, sizeof (struct pci_func));
241 newfunc->busno = cur_func->busno;
242 newfunc->device = device;
243 for (j = 0; j < 4; j++)
244 newfunc->irq[j] = cur_func->irq[j];
245 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
246 prev_func->next = newfunc;
249 case PCI_HEADER_TYPE_BRIDGE:
251 debug ("class now is %x\n", class);
252 if (class != PCI_CLASS_BRIDGE_PCI) {
253 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
254 "Please insert another card.\n", cur_func->device);
258 assign_alt_irq (cur_func, class_code);
260 debug ("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
261 rc = configure_bridge (&cur_func, slotno);
263 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
264 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
268 /* We need to do this in case some other BARs were properly inserted */
269 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
270 err ("was not able to hot-add PPB properly.\n");
274 debug ("cur_func->busno = %x, device = %x, function = %x\n",
275 cur_func->busno, device, function);
276 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
277 debug ("after configuring bridge..., sec_number = %x\n", sec_number);
279 for (i = 0; i < 32; i++) {
280 if (func->devices[i]) {
281 debug ("inside for loop, device is %x\n", i);
282 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
284 err (" out of system memory\n");
287 memset (newfunc, 0, sizeof (struct pci_func));
288 newfunc->busno = sec_number;
289 newfunc->device = (u8) i;
290 for (j = 0; j < 4; j++)
291 newfunc->irq[j] = cur_func->irq[j];
294 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
295 prev_func->next = newfunc;
297 cur_func->next = newfunc;
299 rc = ibmphp_configure_card (newfunc, slotno);
301 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
303 /* We need to do this in case some other BARs were properly inserted */
304 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
315 err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
318 } /* end of switch */
319 } /* end of valid device */
323 err ("Cannot find any valid devices on the card. Or unable to read from card.\n");
330 for (i = 0; i < cleanup_count; i++) {
331 if (cur_func->io[i]) {
332 ibmphp_remove_resource (cur_func->io[i]);
333 cur_func->io[i] = NULL;
334 } else if (cur_func->pfmem[i]) {
335 ibmphp_remove_resource (cur_func->pfmem[i]);
336 cur_func->pfmem[i] = NULL;
337 } else if (cur_func->mem[i]) {
338 ibmphp_remove_resource (cur_func->mem[i]);
339 cur_func->mem[i] = NULL;
346 * This function configures the pci BARs of a single device.
347 * Input: pointer to the pci_func
348 * Output: configured PCI, 0, or error
350 static int configure_device (struct pci_func *func)
365 struct resource_node *io[6];
366 struct resource_node *mem[6];
367 struct resource_node *mem_tmp;
368 struct resource_node *pfmem[6];
371 debug ("%s - inside\n", __FUNCTION__);
373 devfn = PCI_DEVFN(func->device, func->function);
374 ibmphp_pci_bus->number = func->busno;
376 for (count = 0; address[count]; count++) { /* for 6 BARs */
378 /* not sure if i need this. per scott, said maybe need smth like this
379 if devices don't adhere 100% to the spec, so don't want to write
382 pcibios_read_config_byte(cur_func->busno, cur_func->device,
383 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
384 if (tmp & 0x01) // IO
385 pcibios_write_config_dword(cur_func->busno, cur_func->device,
386 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
388 pcibios_write_config_dword(cur_func->busno, cur_func->device,
389 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
391 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
392 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
394 if (!bar[count]) /* This BAR is not implemented */
397 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
399 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
401 debug ("inside IO SPACE\n");
403 len[count] = bar[count] & 0xFFFFFFFC;
404 len[count] = ~len[count] + 1;
406 debug ("len[count] in IO %x, count %d\n", len[count], count);
408 io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
411 err ("out of system memory\n");
414 memset (io[count], 0, sizeof (struct resource_node));
415 io[count]->type = IO;
416 io[count]->busno = func->busno;
417 io[count]->devfunc = PCI_DEVFN(func->device, func->function);
418 io[count]->len = len[count];
419 if (ibmphp_check_resource(io[count], 0) == 0) {
420 ibmphp_add_resource (io[count]);
421 func->io[count] = io[count];
423 err ("cannot allocate requested io for bus %x device %x function %x len %x\n",
424 func->busno, func->device, func->function, len[count]);
428 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
430 /* _______________This is for debugging purposes only_____________________ */
431 debug ("b4 writing, the IO address is %x\n", func->io[count]->start);
432 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
433 debug ("after writing.... the start address is %x\n", bar[count]);
434 /* _________________________________________________________________________*/
438 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
440 debug ("PFMEM SPACE\n");
442 len[count] = bar[count] & 0xFFFFFFF0;
443 len[count] = ~len[count] + 1;
445 debug ("len[count] in PFMEM %x, count %d\n", len[count], count);
447 pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
449 err ("out of system memory\n");
452 memset (pfmem[count], 0, sizeof (struct resource_node));
453 pfmem[count]->type = PFMEM;
454 pfmem[count]->busno = func->busno;
455 pfmem[count]->devfunc = PCI_DEVFN(func->device,
457 pfmem[count]->len = len[count];
458 pfmem[count]->fromMem = FALSE;
459 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
460 ibmphp_add_resource (pfmem[count]);
461 func->pfmem[count] = pfmem[count];
463 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
465 err ("out of system memory\n");
466 kfree (pfmem[count]);
469 memset (mem_tmp, 0, sizeof (struct resource_node));
471 mem_tmp->busno = pfmem[count]->busno;
472 mem_tmp->devfunc = pfmem[count]->devfunc;
473 mem_tmp->len = pfmem[count]->len;
474 debug ("there's no pfmem... going into mem.\n");
475 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
476 ibmphp_add_resource (mem_tmp);
477 pfmem[count]->fromMem = TRUE;
478 pfmem[count]->rangeno = mem_tmp->rangeno;
479 pfmem[count]->start = mem_tmp->start;
480 pfmem[count]->end = mem_tmp->end;
481 ibmphp_add_pfmem_from_mem (pfmem[count]);
482 func->pfmem[count] = pfmem[count];
484 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
485 func->busno, func->device, len[count]);
487 kfree (pfmem[count]);
492 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
494 /*_______________This is for debugging purposes only______________________________*/
495 debug ("b4 writing, start address is %x\n", func->pfmem[count]->start);
496 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
497 debug ("after writing, start address is %x\n", bar[count]);
498 /*_________________________________________________________________________________*/
500 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
501 debug ("inside the mem 64 case, count %d\n", count);
503 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
504 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
508 debug ("REGULAR MEM SPACE\n");
510 len[count] = bar[count] & 0xFFFFFFF0;
511 len[count] = ~len[count] + 1;
513 debug ("len[count] in Mem %x, count %d\n", len[count], count);
515 mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
517 err ("out of system memory\n");
520 memset (mem[count], 0, sizeof (struct resource_node));
521 mem[count]->type = MEM;
522 mem[count]->busno = func->busno;
523 mem[count]->devfunc = PCI_DEVFN(func->device,
525 mem[count]->len = len[count];
526 if (ibmphp_check_resource (mem[count], 0) == 0) {
527 ibmphp_add_resource (mem[count]);
528 func->mem[count] = mem[count];
530 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
531 func->busno, func->device, len[count]);
535 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
536 /* _______________________This is for debugging purposes only _______________________*/
537 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
538 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
539 debug ("after writing, the address is %x\n", bar[count]);
540 /* __________________________________________________________________________________*/
542 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
543 /* takes up another dword */
544 debug ("inside mem 64 case, reg. mem, count %d\n", count);
546 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
547 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
553 func->bus = 0; /* To indicate that this is not a PPB */
554 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
555 if ((irq > 0x00) && (irq < 0x05))
556 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
558 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
559 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
561 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
562 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
567 /******************************************************************************
568 * This routine configures a PCI-2-PCI bridge and the functions behind it
569 * Parameters: pci_func
571 ******************************************************************************/
572 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
584 u8 flag_pfmem = FALSE;
585 u8 need_io_upper = FALSE;
586 u8 need_pfmem_upper = FALSE;
587 struct res_needed *amount_needed = NULL;
588 struct resource_node *io = NULL;
589 struct resource_node *bus_io[2] = {NULL, NULL};
590 struct resource_node *mem = NULL;
591 struct resource_node *bus_mem[2] = {NULL, NULL};
592 struct resource_node *mem_tmp = NULL;
593 struct resource_node *pfmem = NULL;
594 struct resource_node *bus_pfmem[2] = {NULL, NULL};
595 struct bus_node *bus;
601 struct pci_func *func = *func_passed;
606 debug ("%s - enter\n", __FUNCTION__);
608 devfn = PCI_DEVFN(func->function, func->device);
609 ibmphp_pci_bus->number = func->busno;
611 /* Configuring necessary info for the bridge so that we could see the devices
615 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
617 /* _____________________For debugging purposes only __________________________
618 pci_bus_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
619 debug ("primary # written into the bridge is %x\n", pri_number);
620 ___________________________________________________________________________*/
622 /* in EBDA, only get allocated 1 additional bus # per slot */
623 sec_number = find_sec_number (func->busno, slotno);
624 if (sec_number == 0xff) {
625 err ("cannot allocate secondary bus number for the bridged device\n");
629 debug ("after find_sec_number, the number we got is %x\n", sec_number);
630 debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
632 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
634 /* __________________For debugging purposes only __________________________________
635 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
636 debug ("sec_number after write/read is %x\n", sec_number);
637 ________________________________________________________________________________*/
639 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
641 /* __________________For debugging purposes only ____________________________________
642 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
643 debug ("subordinate number after write/read is %x\n", sec_number);
644 __________________________________________________________________________________*/
646 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
647 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
648 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
650 debug ("func->busno is %x\n", func->busno);
651 debug ("sec_number after writing is %x\n", sec_number);
654 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
655 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
656 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
659 /* First we need to allocate mem/io for the bridge itself in case it needs it */
660 for (count = 0; address[count]; count++) { /* for 2 BARs */
661 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
662 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
665 /* This BAR is not implemented */
666 debug ("so we come here then, eh?, count = %d\n", count);
669 // tmp_bar = bar[count];
671 debug ("Bar %d wants %x\n", count, bar[count]);
673 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
675 len[count] = bar[count] & 0xFFFFFFFC;
676 len[count] = ~len[count] + 1;
678 debug ("len[count] in IO = %x\n", len[count]);
680 bus_io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
682 if (!bus_io[count]) {
683 err ("out of system memory\n");
687 memset (bus_io[count], 0, sizeof (struct resource_node));
688 bus_io[count]->type = IO;
689 bus_io[count]->busno = func->busno;
690 bus_io[count]->devfunc = PCI_DEVFN(func->device,
692 bus_io[count]->len = len[count];
693 if (ibmphp_check_resource (bus_io[count], 0) == 0) {
694 ibmphp_add_resource (bus_io[count]);
695 func->io[count] = bus_io[count];
697 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
698 func->busno, func->device, len[count]);
699 kfree (bus_io[count]);
703 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
707 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
709 len[count] = bar[count] & 0xFFFFFFF0;
710 len[count] = ~len[count] + 1;
712 debug ("len[count] in PFMEM = %x\n", len[count]);
714 bus_pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
715 if (!bus_pfmem[count]) {
716 err ("out of system memory\n");
720 memset (bus_pfmem[count], 0, sizeof (struct resource_node));
721 bus_pfmem[count]->type = PFMEM;
722 bus_pfmem[count]->busno = func->busno;
723 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
725 bus_pfmem[count]->len = len[count];
726 bus_pfmem[count]->fromMem = FALSE;
727 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
728 ibmphp_add_resource (bus_pfmem[count]);
729 func->pfmem[count] = bus_pfmem[count];
731 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
733 err ("out of system memory\n");
737 memset (mem_tmp, 0, sizeof (struct resource_node));
739 mem_tmp->busno = bus_pfmem[count]->busno;
740 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
741 mem_tmp->len = bus_pfmem[count]->len;
742 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
743 ibmphp_add_resource (mem_tmp);
744 bus_pfmem[count]->fromMem = TRUE;
745 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
746 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
747 func->pfmem[count] = bus_pfmem[count];
749 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
750 func->busno, func->device, len[count]);
752 kfree (bus_pfmem[count]);
757 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
759 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
760 /* takes up another dword */
762 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
763 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
768 len[count] = bar[count] & 0xFFFFFFF0;
769 len[count] = ~len[count] + 1;
771 debug ("len[count] in Memory is %x\n", len[count]);
773 bus_mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
774 if (!bus_mem[count]) {
775 err ("out of system memory\n");
779 memset (bus_mem[count], 0, sizeof (struct resource_node));
780 bus_mem[count]->type = MEM;
781 bus_mem[count]->busno = func->busno;
782 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
784 bus_mem[count]->len = len[count];
785 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
786 ibmphp_add_resource (bus_mem[count]);
787 func->mem[count] = bus_mem[count];
789 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
790 func->busno, func->device, len[count]);
791 kfree (bus_mem[count]);
795 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
797 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
798 /* takes up another dword */
800 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
801 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
808 /* Now need to see how much space the devices behind the bridge needed */
809 amount_needed = scan_behind_bridge (func, sec_number);
810 if (amount_needed == NULL)
813 ibmphp_pci_bus->number = func->busno;
814 debug ("after coming back from scan_behind_bridge\n");
815 debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
816 debug ("amount_needed->io = %x\n", amount_needed->io);
817 debug ("amount_needed->mem = %x\n", amount_needed->mem);
818 debug ("amount_needed->pfmem = %x\n", amount_needed->pfmem);
820 if (amount_needed->not_correct) {
821 debug ("amount_needed is not correct\n");
822 for (count = 0; address[count]; count++) {
825 ibmphp_remove_resource (bus_io[count]);
826 func->io[count] = NULL;
827 } else if (bus_pfmem[count]) {
828 ibmphp_remove_resource (bus_pfmem[count]);
829 func->pfmem[count] = NULL;
830 } else if (bus_mem[count]) {
831 ibmphp_remove_resource (bus_mem[count]);
832 func->mem[count] = NULL;
835 kfree (amount_needed);
839 if (!amount_needed->io) {
840 debug ("it doesn't want IO?\n");
843 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
844 io = kmalloc(sizeof(*io), GFP_KERNEL);
847 err ("out of system memory\n");
851 memset (io, 0, sizeof (struct resource_node));
853 io->busno = func->busno;
854 io->devfunc = PCI_DEVFN(func->device, func->function);
855 io->len = amount_needed->io;
856 if (ibmphp_check_resource (io, 1) == 0) {
857 debug ("were we able to add io\n");
858 ibmphp_add_resource (io);
863 if (!amount_needed->mem) {
864 debug ("it doesn't want n.e.memory?\n");
867 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
868 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
870 err ("out of system memory\n");
874 memset (mem, 0, sizeof (struct resource_node));
876 mem->busno = func->busno;
877 mem->devfunc = PCI_DEVFN(func->device, func->function);
878 mem->len = amount_needed->mem;
879 if (ibmphp_check_resource (mem, 1) == 0) {
880 ibmphp_add_resource (mem);
882 debug ("were we able to add mem\n");
886 if (!amount_needed->pfmem) {
887 debug ("it doesn't want n.e.pfmem mem?\n");
890 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
891 pfmem = kmalloc(sizeof(*pfmem), GFP_KERNEL);
893 err ("out of system memory\n");
897 memset (pfmem, 0, sizeof (struct resource_node));
899 pfmem->busno = func->busno;
900 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
901 pfmem->len = amount_needed->pfmem;
902 pfmem->fromMem = FALSE;
903 if (ibmphp_check_resource (pfmem, 1) == 0) {
904 ibmphp_add_resource (pfmem);
907 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
909 err ("out of system memory\n");
913 memset (mem_tmp, 0, sizeof (struct resource_node));
915 mem_tmp->busno = pfmem->busno;
916 mem_tmp->devfunc = pfmem->devfunc;
917 mem_tmp->len = pfmem->len;
918 if (ibmphp_check_resource (mem_tmp, 1) == 0) {
919 ibmphp_add_resource (mem_tmp);
920 pfmem->fromMem = TRUE;
921 pfmem->rangeno = mem_tmp->rangeno;
922 ibmphp_add_pfmem_from_mem (pfmem);
928 debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
929 debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
931 if (flag_io && flag_mem && flag_pfmem) {
932 /* If on bootup, there was a bridged card in this slot,
933 * then card was removed and ibmphp got unloaded and loaded
934 * back again, there's no way for us to remove the bus
935 * struct, so no need to kmalloc, can use existing node
937 bus = ibmphp_find_res_bus (sec_number);
939 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
941 err ("out of system memory\n");
945 memset (bus, 0, sizeof (struct bus_node));
946 bus->busno = sec_number;
947 debug ("b4 adding new bus\n");
948 rc = add_new_bus (bus, io, mem, pfmem, func->busno);
949 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
950 rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
952 err ("expected bus structure not empty?\n");
958 ibmphp_remove_bus (bus, func->busno);
959 kfree (amount_needed);
965 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
966 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
968 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
970 need_io_upper = TRUE;
972 if ((io_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
973 debug ("pfmem 64\n");
974 need_pfmem_upper = TRUE;
977 if (bus->noIORanges) {
978 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
979 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
981 /* _______________This is for debugging purposes only ____________________
982 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
983 debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
984 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
985 debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
986 ________________________________________________________________________*/
988 if (need_io_upper) { /* since can't support n.e.ways */
989 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
990 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
993 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
994 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
997 if (bus->noMemRanges) {
998 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
999 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
1001 /* ____________________This is for debugging purposes only ________________________
1002 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
1003 debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1004 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
1005 debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1006 __________________________________________________________________________________*/
1009 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
1010 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
1012 if (bus->noPFMemRanges) {
1013 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
1014 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
1016 /* __________________________This is for debugging purposes only _______________________
1017 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1018 debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1019 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1020 debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1021 ______________________________________________________________________________________*/
1023 if (need_pfmem_upper) { /* since can't support n.e.ways */
1024 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1025 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1028 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1029 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1032 debug ("b4 writing control information\n");
1034 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1035 if ((irq > 0x00) && (irq < 0x05))
1036 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1038 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1039 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1040 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1043 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1044 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1045 for (i = 0; i < 32; i++) {
1046 if (amount_needed->devices[i]) {
1047 debug ("device where devices[i] is 1 = %x\n", i);
1048 func->devices[i] = 1;
1051 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1052 func_passed = &func;
1053 debug ("func->busno b4 returning is %x\n", func->busno);
1054 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1055 kfree (amount_needed);
1058 err ("Configuring bridge was unsuccessful...\n");
1065 kfree(amount_needed);
1067 ibmphp_remove_resource (pfmem);
1069 ibmphp_remove_resource (io);
1071 ibmphp_remove_resource (mem);
1072 for (i = 0; i < 2; i++) { /* for 2 BARs */
1074 ibmphp_remove_resource (bus_io[i]);
1076 } else if (bus_pfmem[i]) {
1077 ibmphp_remove_resource (bus_pfmem[i]);
1078 func->pfmem[i] = NULL;
1079 } else if (bus_mem[i]) {
1080 ibmphp_remove_resource (bus_mem[i]);
1081 func->mem[i] = NULL;
1087 /*****************************************************************************
1088 * This function adds up the amount of resources needed behind the PPB bridge
1089 * and passes it to the configure_bridge function
1090 * Input: bridge function
1091 * Ouput: amount of resources needed
1092 *****************************************************************************/
1093 static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno)
1098 u8 device, function;
1100 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1112 struct res_needed *amount;
1114 amount = kmalloc(sizeof(*amount), GFP_KERNEL);
1117 memset (amount, 0, sizeof (struct res_needed));
1119 ibmphp_pci_bus->number = busno;
1121 debug ("the bus_no behind the bridge is %x\n", busno);
1122 debug ("scanning devices behind the bridge...\n");
1123 for (device = 0; device < 32; device++) {
1124 amount->devices[device] = 0;
1125 for (function = 0; function < 8; function++) {
1126 devfn = PCI_DEVFN(device, function);
1128 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1130 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1131 /* found correct device!!! */
1134 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1135 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1137 debug ("hdr_type behind the bridge is %x\n", hdr_type);
1138 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
1139 err ("embedded bridges not supported for hot-plugging.\n");
1140 amount->not_correct = TRUE;
1144 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1145 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1146 err ("The device %x is VGA compatible and as is not supported for hot plugging. "
1147 "Please choose another device.\n", device);
1148 amount->not_correct = TRUE;
1150 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1151 err ("The device %x is not supported for hot plugging. "
1152 "Please choose another device.\n", device);
1153 amount->not_correct = TRUE;
1157 amount->devices[device] = 1;
1159 for (count = 0; address[count]; count++) {
1162 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp);
1163 if (tmp & 0x01) // IO
1164 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1166 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1168 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1169 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
1171 debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
1173 if (!bar[count]) /* This BAR is not implemented */
1176 //tmp_bar = bar[count];
1178 debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1180 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1182 len[count] = bar[count] & 0xFFFFFFFC;
1183 len[count] = ~len[count] + 1;
1184 amount->io += len[count];
1186 /* This is Memory */
1187 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1189 len[count] = bar[count] & 0xFFFFFFF0;
1190 len[count] = ~len[count] + 1;
1191 amount->pfmem += len[count];
1192 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1193 /* takes up another dword */
1197 /* regular memory */
1198 len[count] = bar[count] & 0xFFFFFFF0;
1199 len[count] = ~len[count] + 1;
1200 amount->mem += len[count];
1201 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1202 /* takes up another dword */
1208 } /* end if (valid) */
1213 amount->not_correct = TRUE;
1215 amount->not_correct = FALSE;
1216 if ((amount->io) && (amount->io < IOBRIDGE))
1217 amount->io = IOBRIDGE;
1218 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1219 amount->mem = MEMBRIDGE;
1220 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1221 amount->pfmem = MEMBRIDGE;
1225 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1226 * upon bootup in the system, since we don't allocate func to such case, we need to read
1227 * the start addresses from pci config space and then find the corresponding entries in
1228 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1229 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1230 * the bootup case), since it should work same way
1232 static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
1245 struct resource_node *io;
1246 struct resource_node *mem;
1247 struct resource_node *pfmem;
1248 struct bus_node *bus;
1255 debug ("%s - enter\n", __FUNCTION__);
1257 bus = ibmphp_find_res_bus (busno);
1259 debug ("cannot find corresponding bus.\n");
1263 devfn = PCI_DEVFN(device, function);
1264 ibmphp_pci_bus->number = busno;
1265 for (count = 0; address[count]; count++) { /* for 6 BARs */
1266 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1268 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1270 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1271 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size);
1272 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address);
1274 debug ("start_address is %x\n", start_address);
1275 debug ("busno, device, function %x %x %x\n", busno, device, function);
1277 /* This BAR is not implemented */
1278 debug ("is this bar no implemented?, count = %d\n", count);
1281 tmp_address = start_address;
1282 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1284 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1285 size = size & 0xFFFFFFFC;
1287 end_address = start_address + size - 1;
1288 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1289 err ("cannot find corresponding IO resource to remove\n");
1292 debug ("io->start = %x\n", io->start);
1294 start_address = io->end + 1;
1295 ibmphp_remove_resource (io);
1296 /* This is needed b/c of the old I/O restrictions in the BIOS */
1297 while (temp_end < end_address) {
1298 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1299 err ("cannot find corresponding IO resource to remove\n");
1302 debug ("io->start = %x\n", io->start);
1304 start_address = io->end + 1;
1305 ibmphp_remove_resource (io);
1308 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1310 /* This is Memory */
1311 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1313 debug ("start address of pfmem is %x\n", start_address);
1314 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1316 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1317 err ("cannot find corresponding PFMEM resource to remove\n");
1321 debug ("pfmem->start = %x\n", pfmem->start);
1323 ibmphp_remove_resource(pfmem);
1326 /* regular memory */
1327 debug ("start address of mem is %x\n", start_address);
1328 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1330 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1331 err ("cannot find corresponding MEM resource to remove\n");
1335 debug ("mem->start = %x\n", mem->start);
1337 ibmphp_remove_resource(mem);
1340 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1341 /* takes up another dword */
1350 static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
1353 int bus_no, pri_no, sub_no, sec_no = 0;
1354 u32 start_address, tmp_address;
1355 u8 sec_number, sub_number, pri_number;
1356 struct resource_node *io = NULL;
1357 struct resource_node *mem = NULL;
1358 struct resource_node *pfmem = NULL;
1359 struct bus_node *bus;
1367 devfn = PCI_DEVFN(device, function);
1368 ibmphp_pci_bus->number = busno;
1369 bus_no = (int) busno;
1370 debug ("busno is %x\n", busno);
1371 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1372 debug ("%s - busno = %x, primary_number = %x\n", __FUNCTION__, busno, pri_number);
1374 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1375 debug ("sec_number is %x\n", sec_number);
1376 sec_no = (int) sec_number;
1377 pri_no = (int) pri_number;
1378 if (pri_no != bus_no) {
1379 err ("primary numbers in our structures and pci config space don't match.\n");
1383 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1384 sub_no = (int) sub_number;
1385 debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1386 if (sec_no != sub_number) {
1387 err ("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1391 bus = ibmphp_find_res_bus (sec_number);
1392 debug ("bus->busno is %x\n", bus->busno);
1393 debug ("sec_number is %x\n", sec_number);
1395 err ("cannot find Bus structure for the bridged device\n");
1399 ibmphp_remove_bus (bus, busno);
1401 for (count = 0; address[count]; count++) {
1403 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1405 if (!start_address) {
1406 /* This BAR is not implemented */
1410 tmp_address = start_address;
1412 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1414 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1415 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1416 err ("cannot find corresponding IO resource to remove\n");
1420 debug ("io->start = %x\n", io->start);
1422 ibmphp_remove_resource (io);
1424 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1426 /* This is Memory */
1427 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1429 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1430 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1431 err ("cannot find corresponding PFMEM resource to remove\n");
1435 debug ("pfmem->start = %x\n", pfmem->start);
1437 ibmphp_remove_resource(pfmem);
1440 /* regular memory */
1441 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1442 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1443 err ("cannot find corresponding MEM resource to remove\n");
1447 debug ("mem->start = %x\n", mem->start);
1449 ibmphp_remove_resource(mem);
1452 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1453 /* takes up another dword */
1458 debug ("%s - exiting, returning success\n", __FUNCTION__);
1462 static int unconfigure_boot_card (struct slot *slot_cur)
1472 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1474 debug ("%s - enter\n", __FUNCTION__);
1476 device = slot_cur->device;
1477 busno = slot_cur->bus;
1479 debug ("b4 for loop, device is %x\n", device);
1480 /* For every function on the card */
1481 for (function = 0x0; function < 0x08; function++) {
1482 devfn = PCI_DEVFN(device, function);
1483 ibmphp_pci_bus->number = busno;
1485 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1487 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1488 /* found correct device!!! */
1491 debug ("%s - found correct device\n", __FUNCTION__);
1493 /* header: x x x x x x x x
1494 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1495 * |_=> 0 = single function device, 1 = multi-function device
1498 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1499 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1501 debug ("hdr_type %x, class %x\n", hdr_type, class);
1502 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1503 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1504 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
1505 "Please choose another device.\n", device, function);
1507 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1508 err ("The device %x function %x is not supported for hot removing. "
1509 "Please choose another device.\n", device, function);
1514 case PCI_HEADER_TYPE_NORMAL:
1515 rc = unconfigure_boot_device (busno, device, function);
1517 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1518 device, function, busno);
1523 case PCI_HEADER_TYPE_MULTIDEVICE:
1524 rc = unconfigure_boot_device (busno, device, function);
1526 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1527 device, function, busno);
1531 case PCI_HEADER_TYPE_BRIDGE:
1533 if (class != PCI_CLASS_BRIDGE_PCI) {
1534 err ("This device %x function %x is not PCI-to-PCI bridge, "
1535 "and is not supported for hot-removing. "
1536 "Please try another card.\n", device, function);
1539 rc = unconfigure_boot_bridge (busno, device, function);
1541 err ("was not able to hot-remove PPB properly.\n");
1547 case PCI_HEADER_TYPE_MULTIBRIDGE:
1549 if (class != PCI_CLASS_BRIDGE_PCI) {
1550 err ("This device %x function %x is not PCI-to-PCI bridge, "
1551 "and is not supported for hot-removing. "
1552 "Please try another card.\n", device, function);
1555 rc = unconfigure_boot_bridge (busno, device, function);
1557 err ("was not able to hot-remove PPB properly.\n");
1562 err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
1565 } /* end of switch */
1566 } /* end of valid device */
1569 if (!valid_device) {
1570 err ("Could not find device to unconfigure. Or could not read the card.\n");
1577 * free the resources of the card (multi, single, or bridged)
1578 * Parameters: slot, flag to say if this is for removing entire module or just
1579 * unconfiguring the device
1580 * TO DO: will probably need to add some code in case there was some resource,
1581 * to remove it... this is from when we have errors in the configure_card...
1582 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1583 * Returns: 0, -1, -ENODEV
1585 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1590 struct slot *sl = *slot_cur;
1591 struct pci_func *cur_func = NULL;
1592 struct pci_func *temp_func;
1594 debug ("%s - enter\n", __FUNCTION__);
1597 /* Need to unconfigure the card */
1598 rc = unconfigure_boot_card (sl);
1599 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1600 /* In all other cases, will still need to get rid of func structure if it exists */
1606 cur_func = sl->func;
1608 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1609 if (cur_func->bus) {
1610 /* in other words, it's a PPB */
1616 for (i = 0; i < count; i++) {
1617 if (cur_func->io[i]) {
1618 debug ("io[%d] exists\n", i);
1620 ibmphp_remove_resource (cur_func->io[i]);
1621 cur_func->io[i] = NULL;
1623 if (cur_func->mem[i]) {
1624 debug ("mem[%d] exists\n", i);
1626 ibmphp_remove_resource (cur_func->mem[i]);
1627 cur_func->mem[i] = NULL;
1629 if (cur_func->pfmem[i]) {
1630 debug ("pfmem[%d] exists\n", i);
1632 ibmphp_remove_resource (cur_func->pfmem[i]);
1633 cur_func->pfmem[i] = NULL;
1637 temp_func = cur_func->next;
1639 cur_func = temp_func;
1645 debug ("%s - exit\n", __FUNCTION__);
1650 * add a new bus resulting from hot-plugging a PPB bridge with devices
1652 * Input: bus and the amount of resources needed (we know we can assign those,
1653 * since they've been checked already
1654 * Output: bus added to the correct spot
1657 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1659 struct range_node *io_range = NULL;
1660 struct range_node *mem_range = NULL;
1661 struct range_node *pfmem_range = NULL;
1662 struct bus_node *cur_bus = NULL;
1664 /* Trying to find the parent bus number */
1665 if (parent_busno != 0xFF) {
1666 cur_bus = ibmphp_find_res_bus (parent_busno);
1668 err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1672 list_add (&bus->bus_list, &cur_bus->bus_list);
1675 io_range = kmalloc(sizeof(*io_range), GFP_KERNEL);
1677 err ("out of system memory\n");
1680 memset (io_range, 0, sizeof (struct range_node));
1681 io_range->start = io->start;
1682 io_range->end = io->end;
1683 io_range->rangeno = 1;
1684 bus->noIORanges = 1;
1685 bus->rangeIO = io_range;
1688 mem_range = kmalloc(sizeof(*mem_range), GFP_KERNEL);
1690 err ("out of system memory\n");
1693 memset (mem_range, 0, sizeof (struct range_node));
1694 mem_range->start = mem->start;
1695 mem_range->end = mem->end;
1696 mem_range->rangeno = 1;
1697 bus->noMemRanges = 1;
1698 bus->rangeMem = mem_range;
1701 pfmem_range = kmalloc(sizeof(*pfmem_range), GFP_KERNEL);
1703 err ("out of system memory\n");
1706 memset (pfmem_range, 0, sizeof (struct range_node));
1707 pfmem_range->start = pfmem->start;
1708 pfmem_range->end = pfmem->end;
1709 pfmem_range->rangeno = 1;
1710 bus->noPFMemRanges = 1;
1711 bus->rangePFMem = pfmem_range;
1717 * find the 1st available bus number for PPB to set as its secondary bus
1718 * Parameters: bus_number of the primary bus
1719 * Returns: bus_number of the secondary bus or 0xff in case of failure
1721 static u8 find_sec_number (u8 primary_busno, u8 slotno)
1725 struct bus_info *bus;
1726 struct bus_node *bus_cur;
1728 bus = ibmphp_find_same_bus_num (primary_busno);
1730 err ("cannot get slot range of the bus from the BIOS\n");
1733 max = bus->slot_max;
1734 min = bus->slot_min;
1735 if ((slotno > max) || (slotno < min)) {
1736 err ("got the wrong range\n");
1739 busno = (u8) (slotno - (u8) min);
1740 busno += primary_busno + 0x01;
1741 bus_cur = ibmphp_find_res_bus (busno);
1742 /* either there is no such bus number, or there are no ranges, which
1743 * can only happen if we removed the bridged device in previous load
1744 * of the driver, and now only have the skeleton bus struct
1746 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))