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 cur_func->function = function;
109 debug ("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->funcion = %x\n", cur_func->busno, device, function);
111 pci_read_config_word_nodev (ibmphp_pci_root_ops, cur_func->busno, device, function, PCI_VENDOR_ID, &vendor_id);
113 debug ("vendor_id is %x\n", vendor_id);
114 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
115 /* found correct device!!! */
116 debug ("found valid device, vendor_id = %x\n", vendor_id);
120 /* header: x x x x x x x x
121 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
122 * |_=> 0 = single function device, 1 = multi-function device
125 pci_read_config_byte_nodev (ibmphp_pci_root_ops, cur_func->busno, device, function, PCI_HEADER_TYPE, &hdr_type);
126 pci_read_config_dword_nodev (ibmphp_pci_root_ops, cur_func->busno, device, function, PCI_CLASS_REVISION, &class);
128 class_code = class >> 24;
129 debug ("hrd_type = %x, class = %x, class_code %x \n", hdr_type, class, class_code);
130 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
131 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
132 err ("The device %x is VGA compatible and as is not supported for hot plugging. "
133 "Please choose another device.\n", cur_func->device);
135 } else if (class == PCI_CLASS_DISPLAY_VGA) {
136 err ("The device %x is not supported for hot plugging. "
137 "Please choose another device.\n", cur_func->device);
141 case PCI_HEADER_TYPE_NORMAL:
142 debug ("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
143 assign_alt_irq (cur_func, class_code);
144 if ((rc = configure_device (cur_func)) < 0) {
145 /* We need to do this in case some other BARs were properly inserted */
146 err ("was not able to configure devfunc %x on bus %x. \n",
147 cur_func->device, cur_func->busno);
151 cur_func->next = NULL;
154 case PCI_HEADER_TYPE_MULTIDEVICE:
155 assign_alt_irq (cur_func, class_code);
156 if ((rc = configure_device (cur_func)) < 0) {
157 /* We need to do this in case some other BARs were properly inserted */
158 err ("was not able to configure devfunc %x on bus %x...bailing out\n",
159 cur_func->device, cur_func->busno);
163 newfunc = (struct pci_func *) kmalloc (sizeof (struct pci_func), GFP_KERNEL);
165 err ("out of system memory \n");
168 memset (newfunc, 0, sizeof (struct pci_func));
169 newfunc->busno = cur_func->busno;
170 newfunc->device = device;
171 cur_func->next = newfunc;
173 for (j = 0; j < 4; j++)
174 newfunc->irq[j] = cur_func->irq[j];
176 case PCI_HEADER_TYPE_MULTIBRIDGE:
178 if (class != PCI_CLASS_BRIDGE_PCI) {
179 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
180 "Please insert another card.\n", cur_func->device);
183 assign_alt_irq (cur_func, class_code);
184 rc = configure_bridge (&cur_func, slotno);
186 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
187 err ("Bus %x, devfunc %x \n", cur_func->busno, cur_func->device);
191 /* We need to do this in case some other BARs were properly inserted */
192 err ("was not able to hot-add PPB properly.\n");
193 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
198 pci_read_config_byte_nodev (ibmphp_pci_root_ops, cur_func->busno, device, function, PCI_SECONDARY_BUS, &sec_number);
200 for (i = 0; i < 32; i++) {
201 if (func->devices[i]) {
202 newfunc = (struct pci_func *) kmalloc (sizeof (struct pci_func), GFP_KERNEL);
204 err ("out of system memory \n");
207 memset (newfunc, 0, sizeof (struct pci_func));
208 newfunc->busno = sec_number;
209 newfunc->device = (u8) i;
210 for (j = 0; j < 4; j++)
211 newfunc->irq[j] = cur_func->irq[j];
214 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
215 prev_func->next = newfunc;
217 cur_func->next = newfunc;
219 rc = ibmphp_configure_card (newfunc, slotno);
220 /* This could only happen if kmalloc failed */
222 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
223 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
231 newfunc = (struct pci_func *) kmalloc (sizeof (struct pci_func), GFP_KERNEL);
233 err ("out of system memory \n");
236 memset (newfunc, 0, sizeof (struct pci_func));
237 newfunc->busno = cur_func->busno;
238 newfunc->device = device;
239 for (j = 0; j < 4; j++)
240 newfunc->irq[j] = cur_func->irq[j];
241 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
242 prev_func->next = newfunc;
245 case PCI_HEADER_TYPE_BRIDGE:
247 debug ("class now is %x\n", class);
248 if (class != PCI_CLASS_BRIDGE_PCI) {
249 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. "
250 "Please insert another card.\n", cur_func->device);
254 assign_alt_irq (cur_func, class_code);
256 debug ("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
257 rc = configure_bridge (&cur_func, slotno);
259 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
260 err ("Bus %x, devfunc %x \n", cur_func->busno, cur_func->device);
264 /* We need to do this in case some other BARs were properly inserted */
265 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
266 err ("was not able to hot-add PPB properly.\n");
270 debug ("cur_func->busno = %x, device = %x, function = %x\n", cur_func->busno, device, function);
271 pci_read_config_byte_nodev (ibmphp_pci_root_ops, cur_func->busno, device, function, PCI_SECONDARY_BUS, &sec_number);
272 debug ("after configuring bridge..., sec_number = %x\n", sec_number);
274 for (i = 0; i < 32; i++) {
275 if (func->devices[i]) {
276 debug ("inside for loop, device is %x\n", i);
277 newfunc = (struct pci_func *) kmalloc (sizeof (struct pci_func), GFP_KERNEL);
279 err (" out of system memory \n");
282 memset (newfunc, 0, sizeof (struct pci_func));
283 newfunc->busno = sec_number;
284 newfunc->device = (u8) i;
285 for (j = 0; j < 4; j++)
286 newfunc->irq[j] = cur_func->irq[j];
289 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
290 prev_func->next = newfunc;
292 cur_func->next = newfunc;
294 rc = ibmphp_configure_card (newfunc, slotno);
296 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
298 /* We need to do this in case some other BARs were properly inserted */
299 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
310 err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
313 } /* end of switch */
314 } /* end of valid device */
318 err ("Cannot find any valid devices on the card. Or unable to read from card.\n");
325 for (i = 0; i < cleanup_count; i++) {
326 if (cur_func->io[i]) {
327 ibmphp_remove_resource (cur_func->io[i]);
328 cur_func->io[i] = NULL;
329 } else if (cur_func->pfmem[i]) {
330 ibmphp_remove_resource (cur_func->pfmem[i]);
331 cur_func->pfmem[i] = NULL;
332 } else if (cur_func->mem[i]) {
333 ibmphp_remove_resource (cur_func->mem[i]);
334 cur_func->mem[i] = NULL;
341 * This function configures the pci BARs of a single device.
342 * Input: pointer to the pci_func
343 * Output: configured PCI, 0, or error
345 static int configure_device (struct pci_func *func)
360 struct resource_node *io[6];
361 struct resource_node *mem[6];
362 struct resource_node *mem_tmp;
363 struct resource_node *pfmem[6];
367 debug ("%s - inside\n", __FUNCTION__);
369 device = func->device;
370 function = func->function;
372 for (count = 0; address[count]; count++) { /* for 6 BARs */
374 /* not sure if i need this. per scott, said maybe need smth like this
375 if devices don't adhere 100% to the spec, so don't want to write
378 pcibios_read_config_byte(cur_func->busno, cur_func->device,
379 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
380 if (tmp & 0x01) // IO
381 pcibios_write_config_dword(cur_func->busno, cur_func->device,
382 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
384 pcibios_write_config_dword(cur_func->busno, cur_func->device,
385 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
387 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0xFFFFFFFF);
388 pci_read_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], &bar[count]);
390 if (!bar[count]) /* This BAR is not implemented */
393 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
395 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
397 debug ("inside IO SPACE\n");
399 len[count] = bar[count] & 0xFFFFFFFC;
400 len[count] = ~len[count] + 1;
402 debug ("len[count] in IO %x, count %d\n", len[count], count);
404 io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
407 err ("out of system memory \n");
410 memset (io[count], 0, sizeof (struct resource_node));
411 io[count]->type = IO;
412 io[count]->busno = func->busno;
413 io[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
414 io[count]->len = len[count];
415 if (ibmphp_check_resource(io[count], 0) == 0) {
416 ibmphp_add_resource (io[count]);
417 func->io[count] = io[count];
419 err ("cannot allocate requested io for bus %x device %x function %x len %x\n",
420 func->busno, func->device, func->function, len[count]);
424 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->io[count]->start);
426 /* _______________This is for debugging purposes only_____________________ */
427 debug ("b4 writing, the IO address is %x\n", func->io[count]->start);
428 pci_read_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], &bar[count]);
429 debug ("after writing.... the start address is %x\n", bar[count]);
430 /* _________________________________________________________________________*/
434 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
436 debug ("PFMEM SPACE\n");
438 len[count] = bar[count] & 0xFFFFFFF0;
439 len[count] = ~len[count] + 1;
441 debug ("len[count] in PFMEM %x, count %d\n", len[count], count);
443 pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
445 err ("out of system memory \n");
448 memset (pfmem[count], 0, sizeof (struct resource_node));
449 pfmem[count]->type = PFMEM;
450 pfmem[count]->busno = func->busno;
451 pfmem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
452 pfmem[count]->len = len[count];
453 pfmem[count]->fromMem = FALSE;
454 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
455 ibmphp_add_resource (pfmem[count]);
456 func->pfmem[count] = pfmem[count];
458 mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
460 err ("out of system memory \n");
461 kfree (pfmem[count]);
464 memset (mem_tmp, 0, sizeof (struct resource_node));
466 mem_tmp->busno = pfmem[count]->busno;
467 mem_tmp->devfunc = pfmem[count]->devfunc;
468 mem_tmp->len = pfmem[count]->len;
469 debug ("there's no pfmem... going into mem.\n");
470 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
471 ibmphp_add_resource (mem_tmp);
472 pfmem[count]->fromMem = TRUE;
473 pfmem[count]->rangeno = mem_tmp->rangeno;
474 pfmem[count]->start = mem_tmp->start;
475 pfmem[count]->end = mem_tmp->end;
476 ibmphp_add_pfmem_from_mem (pfmem[count]);
477 func->pfmem[count] = pfmem[count];
479 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
480 func->busno, func->device, len[count]);
482 kfree (pfmem[count]);
487 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->pfmem[count]->start);
489 /*_______________This if for debugging purposes only______________________________*/
490 debug ("b4 writing, start addres is %x\n", func->pfmem[count]->start);
491 pci_read_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], &bar[count]);
492 debug ("after writing, start address is %x\n", bar[count]);
493 /*_________________________________________________________________________________*/
495 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
496 debug ("inside the mem 64 case, count %d\n", count);
498 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
499 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0x00000000);
503 debug ("REGULAR MEM SPACE\n");
505 len[count] = bar[count] & 0xFFFFFFF0;
506 len[count] = ~len[count] + 1;
508 debug ("len[count] in Mem %x, count %d\n", len[count], count);
510 mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
512 err ("out of system memory \n");
515 memset (mem[count], 0, sizeof (struct resource_node));
516 mem[count]->type = MEM;
517 mem[count]->busno = func->busno;
518 mem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
519 mem[count]->len = len[count];
520 if (ibmphp_check_resource (mem[count], 0) == 0) {
521 ibmphp_add_resource (mem[count]);
522 func->mem[count] = mem[count];
524 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
525 func->busno, func->device, len[count]);
529 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->mem[count]->start);
530 /* _______________________This is for debugging purposes only _______________________*/
531 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
532 pci_read_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], &bar[count]);
533 debug ("after writing, the address is %x\n", bar[count]);
534 /* __________________________________________________________________________________*/
536 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
537 /* takes up another dword */
538 debug ("inside mem 64 case, reg. mem, count %d\n", count);
540 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
541 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0x00000000);
547 func->bus = 0; /* To indicate that this is not a PPB */
548 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_INTERRUPT_PIN, &irq);
549 if ((irq > 0x00) && (irq < 0x05))
550 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
552 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_CACHE_LINE_SIZE, CACHE);
553 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_LATENCY_TIMER, LATENCY);
555 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_ROM_ADDRESS, 0x00L);
556 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_COMMAND, DEVICEENABLE);
561 /******************************************************************************
562 * This routine configures a PCI-2-PCI bridge and the functions behind it
563 * Parameters: pci_func
565 ******************************************************************************/
566 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
578 u8 flag_pfmem = FALSE;
579 u8 need_io_upper = FALSE;
580 u8 need_pfmem_upper = FALSE;
581 struct res_needed *amount_needed = NULL;
582 struct resource_node *io = NULL;
583 struct resource_node *bus_io[2] = {NULL, NULL};
584 struct resource_node *mem = NULL;
585 struct resource_node *bus_mem[2] = {NULL, NULL};
586 struct resource_node *mem_tmp = NULL;
587 struct resource_node *pfmem = NULL;
588 struct resource_node *bus_pfmem[2] = {NULL, NULL};
589 struct bus_node *bus;
595 struct pci_func *func = *func_passed;
601 debug ("%s - enter\n", __FUNCTION__);
603 function = func->function;
604 device = func->device;
606 /* Configuring necessary info for the bridge so that we could see the devices
610 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PRIMARY_BUS, func->busno);
612 /* _____________________For debugging purposes only __________________________
613 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PRIMARY_BUS, &pri_number);
614 debug ("primary # written into the bridge is %x\n", pri_number);
615 ___________________________________________________________________________*/
617 /* in EBDA, only get allocated 1 additional bus # per slot */
618 sec_number = find_sec_number (func->busno, slotno);
619 if (sec_number == 0xff) {
620 err ("cannot allocate secondary bus number for the bridged device \n");
624 debug ("after find_sec_number, the number we got is %x\n", sec_number);
625 debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
627 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_SECONDARY_BUS, sec_number);
629 /* __________________For debugging purposes only __________________________________
630 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_SECONDARY_BUS, &sec_number);
631 debug ("sec_number after write/read is %x\n", sec_number);
632 ________________________________________________________________________________*/
634 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_SUBORDINATE_BUS, sec_number);
636 /* __________________For debugging purposes only ____________________________________
637 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_SUBORDINATE_BUS, &sec_number);
638 debug ("subordinate number after write/read is %x\n", sec_number);
639 __________________________________________________________________________________*/
641 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_CACHE_LINE_SIZE, CACHE);
642 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_LATENCY_TIMER, LATENCY);
643 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_SEC_LATENCY_TIMER, LATENCY);
645 debug ("func->busno is %x\n", func->busno);
646 debug ("sec_number after writing is %x\n", sec_number);
649 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
650 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
651 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
654 /* First we need to allocate mem/io for the bridge itself in case it needs it */
655 for (count = 0; address[count]; count++) { /* for 2 BARs */
656 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0xFFFFFFFF);
657 pci_read_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], &bar[count]);
660 /* This BAR is not implemented */
661 debug ("so we come here then, eh?, count = %d\n", count);
664 // tmp_bar = bar[count];
666 debug ("Bar %d wants %x\n", count, bar[count]);
668 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
670 len[count] = bar[count] & 0xFFFFFFFC;
671 len[count] = ~len[count] + 1;
673 debug ("len[count] in IO = %x\n", len[count]);
675 bus_io[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
677 if (!bus_io[count]) {
678 err ("out of system memory \n");
682 memset (bus_io[count], 0, sizeof (struct resource_node));
683 bus_io[count]->type = IO;
684 bus_io[count]->busno = func->busno;
685 bus_io[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
686 bus_io[count]->len = len[count];
687 if (ibmphp_check_resource (bus_io[count], 0) == 0) {
688 ibmphp_add_resource (bus_io[count]);
689 func->io[count] = bus_io[count];
691 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
692 func->busno, func->device, len[count]);
693 kfree (bus_io[count]);
697 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->io[count]->start);
701 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
703 len[count] = bar[count] & 0xFFFFFFF0;
704 len[count] = ~len[count] + 1;
706 debug ("len[count] in PFMEM = %x\n", len[count]);
708 bus_pfmem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
709 if (!bus_pfmem[count]) {
710 err ("out of system memory \n");
714 memset (bus_pfmem[count], 0, sizeof (struct resource_node));
715 bus_pfmem[count]->type = PFMEM;
716 bus_pfmem[count]->busno = func->busno;
717 bus_pfmem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
718 bus_pfmem[count]->len = len[count];
719 bus_pfmem[count]->fromMem = FALSE;
720 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
721 ibmphp_add_resource (bus_pfmem[count]);
722 func->pfmem[count] = bus_pfmem[count];
724 mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
726 err ("out of system memory \n");
730 memset (mem_tmp, 0, sizeof (struct resource_node));
732 mem_tmp->busno = bus_pfmem[count]->busno;
733 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
734 mem_tmp->len = bus_pfmem[count]->len;
735 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
736 ibmphp_add_resource (mem_tmp);
737 bus_pfmem[count]->fromMem = TRUE;
738 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
739 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
740 func->pfmem[count] = bus_pfmem[count];
742 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
743 func->busno, func->device, len[count]);
745 kfree (bus_pfmem[count]);
750 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->pfmem[count]->start);
752 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
753 /* takes up another dword */
755 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
756 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0x00000000);
761 len[count] = bar[count] & 0xFFFFFFF0;
762 len[count] = ~len[count] + 1;
764 debug ("len[count] in Memory is %x\n", len[count]);
766 bus_mem[count] = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
767 if (!bus_mem[count]) {
768 err ("out of system memory \n");
772 memset (bus_mem[count], 0, sizeof (struct resource_node));
773 bus_mem[count]->type = MEM;
774 bus_mem[count]->busno = func->busno;
775 bus_mem[count]->devfunc = ((func->device << 3) | (func->function & 0x7));
776 bus_mem[count]->len = len[count];
777 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
778 ibmphp_add_resource (bus_mem[count]);
779 func->mem[count] = bus_mem[count];
781 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
782 func->busno, func->device, len[count]);
783 kfree (bus_mem[count]);
787 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], func->mem[count]->start);
789 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
790 /* takes up another dword */
792 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
793 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, address[count], 0x00000000);
800 /* Now need to see how much space the devices behind the bridge needed */
801 amount_needed = scan_behind_bridge (func, sec_number);
802 if (amount_needed == NULL)
805 debug ("after coming back from scan_behind_bridge\n");
806 debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
807 debug ("amount_needed->io = %x\n", amount_needed->io);
808 debug ("amount_needed->mem = %x\n", amount_needed->mem);
809 debug ("amount_needed->pfmem = %x\n", amount_needed->pfmem);
811 if (amount_needed->not_correct) {
812 debug ("amount_needed is not correct \n");
813 for (count = 0; address[count]; count++) {
816 ibmphp_remove_resource (bus_io[count]);
817 func->io[count] = NULL;
818 } else if (bus_pfmem[count]) {
819 ibmphp_remove_resource (bus_pfmem[count]);
820 func->pfmem[count] = NULL;
821 } else if (bus_mem[count]) {
822 ibmphp_remove_resource (bus_mem[count]);
823 func->mem[count] = NULL;
826 kfree (amount_needed);
830 if (!amount_needed->io) {
831 debug ("it doesn't want IO?\n");
834 debug ("it wants %x IO behind the bridge \n", amount_needed->io);
835 io = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
838 err ("out of system memory \n");
842 memset (io, 0, sizeof (struct resource_node));
844 io->busno = func->busno;
845 io->devfunc = ((func->device << 3) | (func->function & 0x7));
846 io->len = amount_needed->io;
847 if (ibmphp_check_resource (io, 1) == 0) {
848 debug ("were we able to add io\n");
849 ibmphp_add_resource (io);
854 if (!amount_needed->mem) {
855 debug ("it doesn't want n.e.memory?\n");
858 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
859 mem = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
861 err ("out of system memory \n");
865 memset (mem, 0, sizeof (struct resource_node));
867 mem->busno = func->busno;
868 mem->devfunc = ((func->device << 3) | (func->function & 0x7));
869 mem->len = amount_needed->mem;
870 if (ibmphp_check_resource (mem, 1) == 0) {
871 ibmphp_add_resource (mem);
873 debug ("were we able to add mem\n");
877 if (!amount_needed->pfmem) {
878 debug ("it doesn't want n.e.pfmem mem?\n");
881 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
882 pfmem = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
884 err ("out of system memory \n");
888 memset (pfmem, 0, sizeof (struct resource_node));
890 pfmem->busno = func->busno;
891 pfmem->devfunc = ((func->device << 3) | (func->function & 0x7));
892 pfmem->len = amount_needed->pfmem;
893 pfmem->fromMem = FALSE;
894 if (ibmphp_check_resource (pfmem, 1) == 0) {
895 ibmphp_add_resource (pfmem);
898 mem_tmp = kmalloc (sizeof (struct resource_node), GFP_KERNEL);
900 err ("out of system memory \n");
904 memset (mem_tmp, 0, sizeof (struct resource_node));
906 mem_tmp->busno = pfmem->busno;
907 mem_tmp->devfunc = pfmem->devfunc;
908 mem_tmp->len = pfmem->len;
909 if (ibmphp_check_resource (mem_tmp, 1) == 0) {
910 ibmphp_add_resource (mem_tmp);
911 pfmem->fromMem = TRUE;
912 pfmem->rangeno = mem_tmp->rangeno;
913 ibmphp_add_pfmem_from_mem (pfmem);
919 debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
920 debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
922 if (flag_io && flag_mem && flag_pfmem) {
923 /* If on bootup, there was a bridged card in this slot,
924 * then card was removed and ibmphp got unloaded and loaded
925 * back again, there's no way for us to remove the bus
926 * struct, so no need to kmalloc, can use existing node
928 bus = ibmphp_find_res_bus (sec_number);
930 bus = kmalloc (sizeof (struct bus_node), GFP_KERNEL);
932 err ("out of system memory \n");
936 memset (bus, 0, sizeof (struct bus_node));
937 bus->busno = sec_number;
938 debug ("b4 adding new bus\n");
939 rc = add_new_bus (bus, io, mem, pfmem, func->busno);
940 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
941 rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
943 err ("expected bus structure not empty? \n");
949 ibmphp_remove_bus (bus, func->busno);
955 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_BASE, &io_base);
956 pci_read_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_BASE, &pfmem_base);
958 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
960 need_io_upper = TRUE;
962 if ((io_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
963 debug ("pfmem 64\n");
964 need_pfmem_upper = TRUE;
967 if (bus->noIORanges) {
968 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
969 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
971 /* _______________This is for debugging purposes only ____________________
972 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_BASE, &temp);
973 debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
974 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_LIMIT, &temp);
975 debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
976 ________________________________________________________________________*/
978 if (need_io_upper) { /* since can't support n.e.ways */
979 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_BASE_UPPER16, 0x0000);
980 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_LIMIT_UPPER16, 0x0000);
983 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_BASE, 0x00);
984 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_IO_LIMIT, 0x00);
987 if (bus->noMemRanges) {
988 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
989 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
991 /* ____________________This is for debugging purposes only ________________________
992 pci_read_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_BASE, &temp);
993 debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
994 pci_read_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_LIMIT, &temp);
995 debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
996 __________________________________________________________________________________*/
999 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_BASE, 0xffff);
1000 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_MEMORY_LIMIT, 0x0000);
1002 if (bus->noPFMemRanges) {
1003 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
1004 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
1006 /* __________________________This is for debugging purposes only _______________________
1007 pci_read_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_BASE, &temp);
1008 debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1009 pci_read_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_LIMIT, &temp);
1010 debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1011 ______________________________________________________________________________________*/
1013 if (need_pfmem_upper) { /* since can't support n.e.ways */
1014 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_BASE_UPPER32, 0x00000000);
1015 pci_write_config_dword_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1018 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_BASE, 0xffff);
1019 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_PREF_MEMORY_LIMIT, 0x0000);
1022 debug ("b4 writing control information\n");
1024 pci_read_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_INTERRUPT_PIN, &irq);
1025 if ((irq > 0x00) && (irq < 0x05))
1026 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1028 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_BRIDGE_CONTROL, ctrl);
1029 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1030 pci_write_config_byte_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1033 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_COMMAND, DEVICEENABLE);
1034 pci_write_config_word_nodev (ibmphp_pci_root_ops, func->busno, device, function, PCI_BRIDGE_CONTROL, 0x07);
1035 for (i = 0; i < 32; i++) {
1036 if (amount_needed->devices[i]) {
1037 debug ("device where devices[i] is 1 = %x\n", i);
1038 func->devices[i] = 1;
1041 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1042 func_passed = &func;
1043 debug ("func->busno b4 returning is %x\n", func->busno);
1044 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1045 kfree (amount_needed);
1048 err ("Configuring bridge was unsuccessful... \n");
1056 kfree (amount_needed);
1058 ibmphp_remove_resource (pfmem);
1060 ibmphp_remove_resource (io);
1062 ibmphp_remove_resource (mem);
1063 for (i = 0; i < 2; i++) { /* for 2 BARs */
1065 ibmphp_remove_resource (bus_io[i]);
1067 } else if (bus_pfmem[i]) {
1068 ibmphp_remove_resource (bus_pfmem[i]);
1069 func->pfmem[i] = NULL;
1070 } else if (bus_mem[i]) {
1071 ibmphp_remove_resource (bus_mem[i]);
1072 func->mem[i] = NULL;
1078 /*****************************************************************************
1079 * This function adds up the amount of resources needed behind the PPB bridge
1080 * and passes it to the configure_bridge function
1081 * Input: bridge function
1082 * Ouput: amount of resources needed
1083 *****************************************************************************/
1084 static struct res_needed *scan_behind_bridge (struct pci_func * func, u8 busno)
1089 u8 device, function;
1090 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1102 struct res_needed *amount;
1104 amount = kmalloc (sizeof (struct res_needed), GFP_KERNEL);
1107 memset (amount, 0, sizeof (struct res_needed));
1109 debug ("the bus_no behind the bridge is %x\n", busno);
1110 debug ("scanning devices behind the bridge...\n");
1111 for (device = 0; device < 32; device++) {
1112 amount->devices[device] = 0;
1113 for (function = 0; function < 8; function++) {
1115 pci_read_config_word_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_VENDOR_ID, &vendor_id);
1117 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1118 /* found correct device!!! */
1121 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_HEADER_TYPE, &hdr_type);
1122 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_CLASS_REVISION, &class);
1124 debug ("hdr_type behind the bridge is %x\n", hdr_type);
1125 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
1126 err ("embedded bridges not supported for hot-plugging.\n");
1127 amount->not_correct = TRUE;
1131 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1132 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1133 err ("The device %x is VGA compatible and as is not supported for hot plugging. "
1134 "Please choose another device.\n", device);
1135 amount->not_correct = TRUE;
1137 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1138 err ("The device %x is not supported for hot plugging. "
1139 "Please choose another device.\n", device);
1140 amount->not_correct = TRUE;
1144 amount->devices[device] = 1;
1146 for (count = 0; address[count]; count++) {
1149 pci_read_config_byte_nodev(ibmphp_pci_root_ops, busno, device, function, address[count], &tmp);
1150 if (tmp & 0x01) // IO
1151 pci_write_config_dword_nodev(ibmphp_pci_root_ops, busno, device, function, address[count], 0xFFFFFFFD);
1153 pci_write_config_dword_nodev(ibmphp_pci_root_ops, busno, device, function, address[count], 0xFFFFFFFF);
1155 pci_write_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], 0xFFFFFFFF);
1156 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], &bar[count]);
1158 debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
1160 if (!bar[count]) /* This BAR is not implemented */
1163 //tmp_bar = bar[count];
1165 debug ("count %d device %x function %x wants %x resources \n", count, device, function, bar[count]);
1167 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1169 len[count] = bar[count] & 0xFFFFFFFC;
1170 len[count] = ~len[count] + 1;
1171 amount->io += len[count];
1173 /* This is Memory */
1174 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1176 len[count] = bar[count] & 0xFFFFFFF0;
1177 len[count] = ~len[count] + 1;
1178 amount->pfmem += len[count];
1179 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1180 /* takes up another dword */
1184 /* regular memory */
1185 len[count] = bar[count] & 0xFFFFFFF0;
1186 len[count] = ~len[count] + 1;
1187 amount->mem += len[count];
1188 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1189 /* takes up another dword */
1195 } /* end if (valid) */
1200 amount->not_correct = TRUE;
1202 amount->not_correct = FALSE;
1203 if ((amount->io) && (amount->io < IOBRIDGE))
1204 amount->io = IOBRIDGE;
1205 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1206 amount->mem = MEMBRIDGE;
1207 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1208 amount->pfmem = MEMBRIDGE;
1212 /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1213 * upon bootup in the system, since we don't allocate func to such case, we need to read
1214 * the start addresses from pci config space and then find the corresponding entries in
1215 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1216 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1217 * the bootup case), since it should work same way
1219 static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
1232 struct resource_node *io;
1233 struct resource_node *mem;
1234 struct resource_node *pfmem;
1235 struct bus_node *bus;
1241 debug ("%s - enter\n", __FUNCTION__);
1243 bus = ibmphp_find_res_bus (busno);
1245 debug ("cannot find corresponding bus.\n");
1249 for (count = 0; address[count]; count++) { /* for 6 BARs */
1250 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], &start_address);
1252 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1254 pci_write_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], 0xFFFFFFFF);
1255 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], &size);
1256 pci_write_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], start_address);
1258 debug ("start_address is %x\n", start_address);
1259 debug ("busno, device, function %x %x %x\n", busno, device, function);
1261 /* This BAR is not implemented */
1262 debug ("is this bar no implemented?, count = %d\n", count);
1265 tmp_address = start_address;
1266 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1268 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1269 size = size & 0xFFFFFFFC;
1271 end_address = start_address + size - 1;
1272 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1273 err ("cannot find corresponding IO resource to remove\n");
1276 debug ("io->start = %x\n", io->start);
1278 start_address = io->end + 1;
1279 ibmphp_remove_resource (io);
1280 /* This is needed b/c of the old I/O restrictions in the BIOS */
1281 while (temp_end < end_address) {
1282 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1283 err ("cannot find corresponding IO resource to remove\n");
1286 debug ("io->start = %x\n", io->start);
1288 start_address = io->end + 1;
1289 ibmphp_remove_resource (io);
1292 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1294 /* This is Memory */
1295 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1297 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1298 debug ("start address of pfmem is %x\n", start_address);
1300 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1301 err ("cannot find corresponding PFMEM resource to remove\n");
1305 debug ("pfmem->start = %x\n", pfmem->start);
1307 ibmphp_remove_resource (pfmem);
1309 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1310 /* takes up another dword */
1315 /* regular memory */
1316 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1317 debug ("start address of mem is %x\n", start_address);
1318 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1319 err ("cannot find corresponding MEM resource to remove\n");
1323 debug ("mem->start = %x\n", mem->start);
1325 ibmphp_remove_resource (mem);
1327 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1328 /* takes up another dword */
1338 static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
1341 int bus_no, pri_no, sub_no, sec_no = 0;
1342 u32 start_address, tmp_address;
1343 u8 sec_number, sub_number, pri_number;
1344 struct resource_node *io = NULL;
1345 struct resource_node *mem = NULL;
1346 struct resource_node *pfmem = NULL;
1347 struct bus_node *bus;
1354 bus_no = (int) busno;
1355 debug ("busno is %x\n", busno);
1356 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_PRIMARY_BUS, &pri_number);
1357 debug ("%s - busno = %x, primary_number = %x\n", __FUNCTION__, busno, pri_number);
1359 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_SECONDARY_BUS, &sec_number);
1360 debug ("sec_number is %x\n", sec_number);
1361 sec_no = (int) sec_number;
1362 pri_no = (int) pri_number;
1363 if (pri_no != bus_no) {
1364 err ("primary numbers in our structures and pci config space don't match.\n");
1368 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_SECONDARY_BUS, &sec_number);
1369 sec_no = (int) sec_no;
1371 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_SUBORDINATE_BUS, &sub_number);
1372 sub_no = (int) sub_number;
1373 debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1374 if (sec_no != sub_number) {
1375 err ("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1379 bus = ibmphp_find_res_bus (sec_number);
1380 debug ("bus->busno is %x\n", bus->busno);
1381 debug ("sec_number is %x\n", sec_number);
1383 err ("cannot find Bus structure for the bridged device\n");
1387 ibmphp_remove_bus (bus, busno);
1389 for (count = 0; address[count]; count++) {
1391 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, address[count], &start_address);
1393 if (!start_address) {
1394 /* This BAR is not implemented */
1398 tmp_address = start_address;
1400 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1402 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1403 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1404 err ("cannot find corresponding IO resource to remove\n");
1408 debug ("io->start = %x\n", io->start);
1410 ibmphp_remove_resource (io);
1412 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1414 /* This is Memory */
1415 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1417 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1418 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1419 err ("cannot find corresponding PFMEM resource to remove\n");
1423 debug ("pfmem->start = %x\n", pfmem->start);
1425 ibmphp_remove_resource (pfmem);
1427 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1428 /* takes up another dword */
1433 /* regular memory */
1434 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1435 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1436 err ("cannot find corresponding MEM resource to remove\n");
1440 debug ("mem->start = %x\n", mem->start);
1442 ibmphp_remove_resource (mem);
1444 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1445 /* takes up another dword */
1451 debug ("%s - exiting, returning success\n", __FUNCTION__);
1455 static int unconfigure_boot_card (struct slot *slot_cur)
1464 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1466 debug ("%s - enter\n", __FUNCTION__);
1468 device = slot_cur->device;
1469 busno = slot_cur->bus;
1471 debug ("b4 for loop, device is %x\n", device);
1472 /* For every function on the card */
1473 for (function = 0x0; function < 0x08; function++) {
1475 pci_read_config_word_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_VENDOR_ID, &vendor_id);
1477 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1478 /* found correct device!!! */
1481 debug ("%s - found correct device\n", __FUNCTION__);
1483 /* header: x x x x x x x x
1484 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1485 * |_=> 0 = single function device, 1 = multi-function device
1488 pci_read_config_byte_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_HEADER_TYPE, &hdr_type);
1489 pci_read_config_dword_nodev (ibmphp_pci_root_ops, busno, device, function, PCI_CLASS_REVISION, &class);
1491 debug ("hdr_type %x, class %x\n", hdr_type, class);
1492 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1493 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1494 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
1495 "Please choose another device.\n", device, function);
1497 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1498 err ("The device %x function %x is not supported for hot removing. "
1499 "Please choose another device.\n", device, function);
1504 case PCI_HEADER_TYPE_NORMAL:
1505 rc = unconfigure_boot_device (busno, device, function);
1507 err ("was not able to unconfigure device %x func %x on bus %x. bailing out... \n",
1508 device, function, busno);
1513 case PCI_HEADER_TYPE_MULTIDEVICE:
1514 rc = unconfigure_boot_device (busno, device, function);
1516 err ("was not able to unconfigure device %x func %x on bus %x. bailing out... \n",
1517 device, function, busno);
1521 case PCI_HEADER_TYPE_BRIDGE:
1523 if (class != PCI_CLASS_BRIDGE_PCI) {
1524 err ("This device %x function %x is not PCI-to-PCI bridge, "
1525 "and is not supported for hot-removing. "
1526 "Please try another card.\n", device, function);
1529 rc = unconfigure_boot_bridge (busno, device, function);
1531 err ("was not able to hot-remove PPB properly.\n");
1537 case PCI_HEADER_TYPE_MULTIBRIDGE:
1539 if (class != PCI_CLASS_BRIDGE_PCI) {
1540 err ("This device %x function %x is not PCI-to-PCI bridge, "
1541 "and is not supported for hot-removing. "
1542 "Please try another card.\n", device, function);
1545 rc = unconfigure_boot_bridge (busno, device, function);
1547 err ("was not able to hot-remove PPB properly.\n");
1552 err ("MAJOR PROBLEM!!!! Cannot read device's header \n");
1555 } /* end of switch */
1556 } /* end of valid device */
1559 if (!valid_device) {
1560 err ("Could not find device to unconfigure. Or could not read the card. \n");
1567 * free the resources of the card (multi, single, or bridged)
1568 * Parameters: slot, flag to say if this is for removing entire module or just
1569 * unconfiguring the device
1570 * TO DO: will probably need to add some code in case there was some resource,
1571 * to remove it... this is from when we have errors in the configure_card...
1572 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1573 * Returns: 0, -1, -ENODEV
1575 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1580 struct slot *sl = *slot_cur;
1581 struct pci_func *cur_func = NULL;
1582 struct pci_func *temp_func;
1584 debug ("%s - enter\n", __FUNCTION__);
1587 /* Need to unconfigure the card */
1588 rc = unconfigure_boot_card (sl);
1589 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1590 /* In all other cases, will still need to get rid of func structure if it exists */
1596 cur_func = sl->func;
1598 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1599 if (cur_func->bus) {
1600 /* in other words, it's a PPB */
1606 for (i = 0; i < count; i++) {
1607 if (cur_func->io[count]) {
1608 debug ("io[%d] exists \n", count);
1610 ibmphp_remove_resource (cur_func->io[count]);
1611 cur_func->io[count] = NULL;
1613 if (cur_func->mem[count]) {
1614 debug ("mem[%d] exists \n", count);
1616 ibmphp_remove_resource (cur_func->mem[count]);
1617 cur_func->mem[count] = NULL;
1619 if (cur_func->pfmem[count]) {
1620 debug ("pfmem[%d] exists \n", count);
1622 ibmphp_remove_resource (cur_func->pfmem[count]);
1623 cur_func->pfmem[count] = NULL;
1627 temp_func = cur_func->next;
1629 cur_func = temp_func;
1635 debug ("%s - exit\n", __FUNCTION__);
1640 * add a new bus resulting from hot-plugging a PPB bridge with devices
1642 * Input: bus and the amount of resources needed (we know we can assign those,
1643 * since they've been checked already
1644 * Output: bus added to the correct spot
1647 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1649 struct range_node *io_range = NULL;
1650 struct range_node *mem_range = NULL;
1651 struct range_node *pfmem_range = NULL;
1652 struct bus_node *cur_bus = NULL;
1654 /* Trying to find the parent bus number */
1655 if (parent_busno != 0xFF) {
1656 cur_bus = ibmphp_find_res_bus (parent_busno);
1658 err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1662 list_add (&bus->bus_list, &cur_bus->bus_list);
1665 io_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1667 err ("out of system memory \n");
1670 memset (io_range, 0, sizeof (struct range_node));
1671 io_range->start = io->start;
1672 io_range->end = io->end;
1673 io_range->rangeno = 1;
1674 bus->noIORanges = 1;
1675 bus->rangeIO = io_range;
1678 mem_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1680 err ("out of system memory \n");
1683 memset (mem_range, 0, sizeof (struct range_node));
1684 mem_range->start = mem->start;
1685 mem_range->end = mem->end;
1686 mem_range->rangeno = 1;
1687 bus->noMemRanges = 1;
1688 bus->rangeMem = mem_range;
1691 pfmem_range = kmalloc (sizeof (struct range_node), GFP_KERNEL);
1693 err ("out of system memory \n");
1696 memset (pfmem_range, 0, sizeof (struct range_node));
1697 pfmem_range->start = pfmem->start;
1698 pfmem_range->end = pfmem->end;
1699 pfmem_range->rangeno = 1;
1700 bus->noPFMemRanges = 1;
1701 bus->rangePFMem = pfmem_range;
1707 * find the 1st available bus number for PPB to set as its secondary bus
1708 * Parameters: bus_number of the primary bus
1709 * Returns: bus_number of the secondary bus or 0xff in case of failure
1711 static u8 find_sec_number (u8 primary_busno, u8 slotno)
1715 struct bus_info *bus;
1716 struct bus_node *bus_cur;
1718 bus = ibmphp_find_same_bus_num (primary_busno);
1720 err ("cannot get slot range of the bus from the BIOS\n");
1723 max = bus->slot_max;
1724 min = bus->slot_min;
1725 if ((slotno > max) || (slotno < min)) {
1726 err ("got the wrong range\n");
1729 busno = (u8) (slotno - (u8) min);
1730 busno += primary_busno + 0x01;
1731 bus_cur = ibmphp_find_res_bus (busno);
1732 /* either there is no such bus number, or there are no ranges, which
1733 * can only happen if we removed the bridged device in previous load
1734 * of the driver, and now only have the skeleton bus struct
1736 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))