Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / pci / hotplug / ibmphp_pci.c
blob2335fac65fb4eee0456837e97d9cd99e5adec341
1 /*
2 * IBM Hot Plug Controller Driver
3 *
4 * Written By: Irene Zubarev, IBM Corporation
5 *
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
9 * All rights reserved.
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
20 * details.
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>
34 #include "ibmphp.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)
52 int j;
53 for (j = 0; j < 4; j++) {
54 if (cur_func->irq[j] == 0xff) {
55 switch (class_code) {
56 case PCI_BASE_CLASS_STORAGE:
57 cur_func->irq[j] = SCSI_IRQ;
58 break;
59 case PCI_BASE_CLASS_NETWORK:
60 cur_func->irq[j] = LAN_IRQ;
61 break;
62 default:
63 cur_func->irq[j] = OTHER_IRQ;
64 break;
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
73 * multi-functional
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)
83 u16 vendor_id;
84 u32 class;
85 u8 class_code;
86 u8 hdr_type, device, sec_number;
87 u8 function;
88 struct pci_func *newfunc; /* for multi devices */
89 struct pci_func *cur_func, *prev_func;
90 int rc, i, j;
91 int cleanup_count;
92 u8 flag;
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;
98 cur_func = func;
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
102 * highest bits)
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);
122 ++valid_device;
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);
138 return -ENODEV;
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);
142 return -ENODEV;
144 switch (hdr_type) {
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);
152 cleanup_count = 6;
153 goto error;
155 cur_func->next = NULL;
156 function = 0x8;
157 break;
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);
164 cleanup_count = 6;
165 goto error;
167 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
168 if (!newfunc) {
169 err ("out of system memory\n");
170 return -ENOMEM;
172 memset (newfunc, 0, sizeof (struct pci_func));
173 newfunc->busno = cur_func->busno;
174 newfunc->device = device;
175 cur_func->next = newfunc;
176 cur_func = newfunc;
177 for (j = 0; j < 4; j++)
178 newfunc->irq[j] = cur_func->irq[j];
179 break;
180 case PCI_HEADER_TYPE_MULTIBRIDGE:
181 class >>= 8;
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);
185 return -ENODEV;
187 assign_alt_irq (cur_func, class_code);
188 rc = configure_bridge (&cur_func, slotno);
189 if (rc == -ENODEV) {
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);
192 return rc;
194 if (rc) {
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 */
198 cleanup_count = 2;
199 goto error;
202 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
203 flag = FALSE;
204 for (i = 0; i < 32; i++) {
205 if (func->devices[i]) {
206 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
207 if (!newfunc) {
208 err ("out of system memory\n");
209 return -ENOMEM;
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];
217 if (flag) {
218 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
219 prev_func->next = newfunc;
220 } else
221 cur_func->next = newfunc;
223 rc = ibmphp_configure_card (newfunc, slotno);
224 /* This could only happen if kmalloc failed */
225 if (rc) {
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 */
228 cleanup_count = 2;
229 goto error;
231 flag = TRUE;
235 newfunc = kmalloc(sizeof(*newfunc), GFP_KERNEL);
236 if (!newfunc) {
237 err ("out of system memory\n");
238 return -ENOMEM;
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;
247 cur_func = newfunc;
248 break;
249 case PCI_HEADER_TYPE_BRIDGE:
250 class >>= 8;
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);
255 return -ENODEV;
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);
262 if (rc == -ENODEV) {
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);
265 return rc;
267 if (rc) {
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");
271 cleanup_count = 2;
272 goto error;
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);
278 flag = FALSE;
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);
283 if (!newfunc) {
284 err (" out of system memory\n");
285 return -ENOMEM;
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];
293 if (flag) {
294 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
295 prev_func->next = newfunc;
296 } else
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 */
302 if (rc) {
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 */
305 cleanup_count = 2;
306 goto error;
308 flag = TRUE;
312 function = 0x8;
313 break;
314 default:
315 err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
316 return -ENXIO;
317 break;
318 } /* end of switch */
319 } /* end of valid device */
320 } /* end of for */
322 if (!valid_device) {
323 err ("Cannot find any valid devices on the card. Or unable to read from card.\n");
324 return -ENODEV;
327 return 0;
329 error:
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;
342 return rc;
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)
352 u32 bar[6];
353 u32 address[] = {
354 PCI_BASE_ADDRESS_0,
355 PCI_BASE_ADDRESS_1,
356 PCI_BASE_ADDRESS_2,
357 PCI_BASE_ADDRESS_3,
358 PCI_BASE_ADDRESS_4,
359 PCI_BASE_ADDRESS_5,
362 u8 irq;
363 int count;
364 int len[6];
365 struct resource_node *io[6];
366 struct resource_node *mem[6];
367 struct resource_node *mem_tmp;
368 struct resource_node *pfmem[6];
369 unsigned int devfn;
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
380 to the reserved bits
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);
387 else // Memory
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 */
395 continue;
397 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
399 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
400 /* This is 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);
410 if (!io[count]) {
411 err ("out of system memory\n");
412 return -ENOMEM;
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];
422 } else {
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]);
425 kfree (io[count]);
426 return -EIO;
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 /* _________________________________________________________________________*/
436 } else {
437 /* This is Memory */
438 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
439 /* pfmem */
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);
448 if (!pfmem[count]) {
449 err ("out of system memory\n");
450 return -ENOMEM;
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,
456 func->function);
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];
462 } else {
463 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
464 if (!mem_tmp) {
465 err ("out of system memory\n");
466 kfree (pfmem[count]);
467 return -ENOMEM;
469 memset (mem_tmp, 0, sizeof (struct resource_node));
470 mem_tmp->type = MEM;
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];
483 } else {
484 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
485 func->busno, func->device, len[count]);
486 kfree (mem_tmp);
487 kfree (pfmem[count]);
488 return -EIO;
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);
502 count += 1;
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);
506 } else {
507 /* regular memory */
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);
516 if (!mem[count]) {
517 err ("out of system memory\n");
518 return -ENOMEM;
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,
524 func->function);
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];
529 } else {
530 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
531 func->busno, func->device, len[count]);
532 kfree (mem[count]);
533 return -EIO;
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);
545 count += 1;
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);
550 } /* end of mem */
551 } /* end of for */
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);
564 return 0;
567 /******************************************************************************
568 * This routine configures a PCI-2-PCI bridge and the functions behind it
569 * Parameters: pci_func
570 * Returns:
571 ******************************************************************************/
572 static int configure_bridge (struct pci_func **func_passed, u8 slotno)
574 int count;
575 int i;
576 int rc;
577 u8 sec_number;
578 u8 io_base;
579 u16 pfmem_base;
580 u32 bar[2];
581 u32 len[2];
582 u8 flag_io = FALSE;
583 u8 flag_mem = FALSE;
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;
596 u32 address[] = {
597 PCI_BASE_ADDRESS_0,
598 PCI_BASE_ADDRESS_1,
601 struct pci_func *func = *func_passed;
602 unsigned int devfn;
603 u8 irq;
604 int retval;
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
612 * behind it
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");
626 return -EINVAL;
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]);
664 if (!bar[count]) {
665 /* This BAR is not implemented */
666 debug ("so we come here then, eh?, count = %d\n", count);
667 continue;
669 // tmp_bar = bar[count];
671 debug ("Bar %d wants %x\n", count, bar[count]);
673 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
674 /* This is 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");
684 retval = -ENOMEM;
685 goto error;
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,
691 func->function);
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];
696 } else {
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]);
700 return -EIO;
703 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
705 } else {
706 /* This is Memory */
707 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
708 /* pfmem */
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");
717 retval = -ENOMEM;
718 goto error;
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,
724 func->function);
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];
730 } else {
731 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
732 if (!mem_tmp) {
733 err ("out of system memory\n");
734 retval = -ENOMEM;
735 goto error;
737 memset (mem_tmp, 0, sizeof (struct resource_node));
738 mem_tmp->type = MEM;
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];
748 } else {
749 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
750 func->busno, func->device, len[count]);
751 kfree (mem_tmp);
752 kfree (bus_pfmem[count]);
753 return -EIO;
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 */
761 count += 1;
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);
766 } else {
767 /* regular memory */
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");
776 retval = -ENOMEM;
777 goto error;
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,
783 func->function);
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];
788 } else {
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]);
792 return -EIO;
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 */
799 count += 1;
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);
805 } /* end of mem */
806 } /* end of for */
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)
811 return -ENOMEM;
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++) {
823 /* for 2 BARs */
824 if (bus_io[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);
836 return -ENODEV;
839 if (!amount_needed->io) {
840 debug ("it doesn't want IO?\n");
841 flag_io = TRUE;
842 } else {
843 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
844 io = kmalloc(sizeof(*io), GFP_KERNEL);
846 if (!io) {
847 err ("out of system memory\n");
848 retval = -ENOMEM;
849 goto error;
851 memset (io, 0, sizeof (struct resource_node));
852 io->type = IO;
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);
859 flag_io = TRUE;
863 if (!amount_needed->mem) {
864 debug ("it doesn't want n.e.memory?\n");
865 flag_mem = TRUE;
866 } else {
867 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
868 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
869 if (!mem) {
870 err ("out of system memory\n");
871 retval = -ENOMEM;
872 goto error;
874 memset (mem, 0, sizeof (struct resource_node));
875 mem->type = MEM;
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);
881 flag_mem = TRUE;
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");
888 flag_pfmem = TRUE;
889 } else {
890 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
891 pfmem = kmalloc(sizeof(*pfmem), GFP_KERNEL);
892 if (!pfmem) {
893 err ("out of system memory\n");
894 retval = -ENOMEM;
895 goto error;
897 memset (pfmem, 0, sizeof (struct resource_node));
898 pfmem->type = PFMEM;
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);
905 flag_pfmem = TRUE;
906 } else {
907 mem_tmp = kmalloc(sizeof(*mem_tmp), GFP_KERNEL);
908 if (!mem_tmp) {
909 err ("out of system memory\n");
910 retval = -ENOMEM;
911 goto error;
913 memset (mem_tmp, 0, sizeof (struct resource_node));
914 mem_tmp->type = MEM;
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);
923 flag_pfmem = TRUE;
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);
938 if (!bus) {
939 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
940 if (!bus) {
941 err ("out of system memory\n");
942 retval = -ENOMEM;
943 goto error;
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);
951 else {
952 err ("expected bus structure not empty?\n");
953 retval = -EIO;
954 goto error;
956 if (rc) {
957 if (rc == -ENOMEM) {
958 ibmphp_remove_bus (bus, func->busno);
959 kfree (amount_needed);
960 return rc;
962 retval = rc;
963 goto error;
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) {
969 debug ("io 32\n");
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);
992 } else {
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 __________________________________________________________________________________*/
1008 } else {
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);
1027 } else {
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);
1056 return 0;
1057 } else {
1058 err ("Configuring bridge was unsuccessful...\n");
1059 mem_tmp = NULL;
1060 retval = -EIO;
1061 goto error;
1064 error:
1065 kfree(amount_needed);
1066 if (pfmem)
1067 ibmphp_remove_resource (pfmem);
1068 if (io)
1069 ibmphp_remove_resource (io);
1070 if (mem)
1071 ibmphp_remove_resource (mem);
1072 for (i = 0; i < 2; i++) { /* for 2 BARs */
1073 if (bus_io[i]) {
1074 ibmphp_remove_resource (bus_io[i]);
1075 func->io[i] = NULL;
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;
1084 return retval;
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)
1095 int count, len[6];
1096 u16 vendor_id;
1097 u8 hdr_type;
1098 u8 device, function;
1099 unsigned int devfn;
1100 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1102 u32 bar[6], class;
1103 u32 address[] = {
1104 PCI_BASE_ADDRESS_0,
1105 PCI_BASE_ADDRESS_1,
1106 PCI_BASE_ADDRESS_2,
1107 PCI_BASE_ADDRESS_3,
1108 PCI_BASE_ADDRESS_4,
1109 PCI_BASE_ADDRESS_5,
1112 struct res_needed *amount;
1114 amount = kmalloc(sizeof(*amount), GFP_KERNEL);
1115 if (amount == NULL)
1116 return NULL;
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!!! */
1132 howmany++;
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;
1141 return amount;
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;
1149 return amount;
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;
1154 return amount;
1157 amount->devices[device] = 1;
1159 for (count = 0; address[count]; count++) {
1160 /* for 6 BARs */
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);
1165 else // MEMORY
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 */
1174 continue;
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) {
1181 /* This is IO */
1182 len[count] = bar[count] & 0xFFFFFFFC;
1183 len[count] = ~len[count] + 1;
1184 amount->io += len[count];
1185 } else {
1186 /* This is Memory */
1187 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1188 /* pfmem */
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 */
1194 count += 1;
1196 } else {
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 */
1203 count += 1;
1207 } /* end for */
1208 } /* end if (valid) */
1209 } /* end for */
1210 } /* end for */
1212 if (!howmany)
1213 amount->not_correct = TRUE;
1214 else
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;
1222 return amount;
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)
1234 u32 start_address;
1235 u32 address[] = {
1236 PCI_BASE_ADDRESS_0,
1237 PCI_BASE_ADDRESS_1,
1238 PCI_BASE_ADDRESS_2,
1239 PCI_BASE_ADDRESS_3,
1240 PCI_BASE_ADDRESS_4,
1241 PCI_BASE_ADDRESS_5,
1244 int count;
1245 struct resource_node *io;
1246 struct resource_node *mem;
1247 struct resource_node *pfmem;
1248 struct bus_node *bus;
1249 u32 end_address;
1250 u32 temp_end;
1251 u32 size;
1252 u32 tmp_address;
1253 unsigned int devfn;
1255 debug ("%s - enter\n", __FUNCTION__);
1257 bus = ibmphp_find_res_bus (busno);
1258 if (!bus) {
1259 debug ("cannot find corresponding bus.\n");
1260 return -EINVAL;
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);
1276 if (!size) {
1277 /* This BAR is not implemented */
1278 debug ("is this bar no implemented?, count = %d\n", count);
1279 continue;
1281 tmp_address = start_address;
1282 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1283 /* This is IO */
1284 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1285 size = size & 0xFFFFFFFC;
1286 size = ~size + 1;
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");
1290 return -EIO;
1292 debug ("io->start = %x\n", io->start);
1293 temp_end = io->end;
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");
1300 return -EIO;
1302 debug ("io->start = %x\n", io->start);
1303 temp_end = io->end;
1304 start_address = io->end + 1;
1305 ibmphp_remove_resource (io);
1308 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1309 } else {
1310 /* This is Memory */
1311 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1312 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1313 /* pfmem */
1314 debug ("start address of pfmem is %x\n", start_address);
1316 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1317 err ("cannot find corresponding PFMEM resource to remove\n");
1318 return -EIO;
1320 if (pfmem) {
1321 debug ("pfmem->start = %x\n", pfmem->start);
1323 ibmphp_remove_resource(pfmem);
1325 } else {
1326 /* regular memory */
1327 debug ("start address of mem is %x\n", start_address);
1328 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1329 err ("cannot find corresponding MEM resource to remove\n");
1330 return -EIO;
1332 if (mem) {
1333 debug ("mem->start = %x\n", mem->start);
1335 ibmphp_remove_resource(mem);
1338 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1339 /* takes up another dword */
1340 count += 1;
1342 } /* end of mem */
1343 } /* end of for */
1345 return 0;
1348 static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
1350 int count;
1351 int bus_no, pri_no, sub_no, sec_no = 0;
1352 u32 start_address, tmp_address;
1353 u8 sec_number, sub_number, pri_number;
1354 struct resource_node *io = NULL;
1355 struct resource_node *mem = NULL;
1356 struct resource_node *pfmem = NULL;
1357 struct bus_node *bus;
1358 u32 address[] = {
1359 PCI_BASE_ADDRESS_0,
1360 PCI_BASE_ADDRESS_1,
1363 unsigned int devfn;
1365 devfn = PCI_DEVFN(device, function);
1366 ibmphp_pci_bus->number = busno;
1367 bus_no = (int) busno;
1368 debug ("busno is %x\n", busno);
1369 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1370 debug ("%s - busno = %x, primary_number = %x\n", __FUNCTION__, busno, pri_number);
1372 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1373 debug ("sec_number is %x\n", sec_number);
1374 sec_no = (int) sec_number;
1375 pri_no = (int) pri_number;
1376 if (pri_no != bus_no) {
1377 err ("primary numbers in our structures and pci config space don't match.\n");
1378 return -EINVAL;
1381 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1382 sub_no = (int) sub_number;
1383 debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1384 if (sec_no != sub_number) {
1385 err ("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1386 return -ENODEV;
1389 bus = ibmphp_find_res_bus (sec_number);
1390 debug ("bus->busno is %x\n", bus->busno);
1391 debug ("sec_number is %x\n", sec_number);
1392 if (!bus) {
1393 err ("cannot find Bus structure for the bridged device\n");
1394 return -EINVAL;
1397 ibmphp_remove_bus (bus, busno);
1399 for (count = 0; address[count]; count++) {
1400 /* for 2 BARs */
1401 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1403 if (!start_address) {
1404 /* This BAR is not implemented */
1405 continue;
1408 tmp_address = start_address;
1410 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1411 /* This is IO */
1412 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1413 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1414 err ("cannot find corresponding IO resource to remove\n");
1415 return -EIO;
1417 if (io)
1418 debug ("io->start = %x\n", io->start);
1420 ibmphp_remove_resource (io);
1422 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1423 } else {
1424 /* This is Memory */
1425 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1426 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1427 /* pfmem */
1428 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1429 err ("cannot find corresponding PFMEM resource to remove\n");
1430 return -EINVAL;
1432 if (pfmem) {
1433 debug ("pfmem->start = %x\n", pfmem->start);
1435 ibmphp_remove_resource(pfmem);
1437 } else {
1438 /* regular memory */
1439 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1440 err ("cannot find corresponding MEM resource to remove\n");
1441 return -EINVAL;
1443 if (mem) {
1444 debug ("mem->start = %x\n", mem->start);
1446 ibmphp_remove_resource(mem);
1449 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1450 /* takes up another dword */
1451 count += 1;
1453 } /* end of mem */
1454 } /* end of for */
1455 debug ("%s - exiting, returning success\n", __FUNCTION__);
1456 return 0;
1459 static int unconfigure_boot_card (struct slot *slot_cur)
1461 u16 vendor_id;
1462 u32 class;
1463 u8 hdr_type;
1464 u8 device;
1465 u8 busno;
1466 u8 function;
1467 int rc;
1468 unsigned int devfn;
1469 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1471 debug ("%s - enter\n", __FUNCTION__);
1473 device = slot_cur->device;
1474 busno = slot_cur->bus;
1476 debug ("b4 for loop, device is %x\n", device);
1477 /* For every function on the card */
1478 for (function = 0x0; function < 0x08; function++) {
1479 devfn = PCI_DEVFN(device, function);
1480 ibmphp_pci_bus->number = busno;
1482 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1484 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1485 /* found correct device!!! */
1486 ++valid_device;
1488 debug ("%s - found correct device\n", __FUNCTION__);
1490 /* header: x x x x x x x x
1491 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1492 * |_=> 0 = single function device, 1 = multi-function device
1495 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1496 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1498 debug ("hdr_type %x, class %x\n", hdr_type, class);
1499 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1500 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1501 err ("The device %x function %x is VGA compatible and is not supported for hot removing. "
1502 "Please choose another device.\n", device, function);
1503 return -ENODEV;
1504 } else if (class == PCI_CLASS_DISPLAY_VGA) {
1505 err ("The device %x function %x is not supported for hot removing. "
1506 "Please choose another device.\n", device, function);
1507 return -ENODEV;
1510 switch (hdr_type) {
1511 case PCI_HEADER_TYPE_NORMAL:
1512 rc = unconfigure_boot_device (busno, device, function);
1513 if (rc) {
1514 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1515 device, function, busno);
1516 return rc;
1518 function = 0x8;
1519 break;
1520 case PCI_HEADER_TYPE_MULTIDEVICE:
1521 rc = unconfigure_boot_device (busno, device, function);
1522 if (rc) {
1523 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1524 device, function, busno);
1525 return rc;
1527 break;
1528 case PCI_HEADER_TYPE_BRIDGE:
1529 class >>= 8;
1530 if (class != PCI_CLASS_BRIDGE_PCI) {
1531 err ("This device %x function %x is not PCI-to-PCI bridge, "
1532 "and is not supported for hot-removing. "
1533 "Please try another card.\n", device, function);
1534 return -ENODEV;
1536 rc = unconfigure_boot_bridge (busno, device, function);
1537 if (rc != 0) {
1538 err ("was not able to hot-remove PPB properly.\n");
1539 return rc;
1542 function = 0x8;
1543 break;
1544 case PCI_HEADER_TYPE_MULTIBRIDGE:
1545 class >>= 8;
1546 if (class != PCI_CLASS_BRIDGE_PCI) {
1547 err ("This device %x function %x is not PCI-to-PCI bridge, "
1548 "and is not supported for hot-removing. "
1549 "Please try another card.\n", device, function);
1550 return -ENODEV;
1552 rc = unconfigure_boot_bridge (busno, device, function);
1553 if (rc != 0) {
1554 err ("was not able to hot-remove PPB properly.\n");
1555 return rc;
1557 break;
1558 default:
1559 err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
1560 return -1;
1561 break;
1562 } /* end of switch */
1563 } /* end of valid device */
1564 } /* end of for */
1566 if (!valid_device) {
1567 err ("Could not find device to unconfigure. Or could not read the card.\n");
1568 return -1;
1570 return 0;
1574 * free the resources of the card (multi, single, or bridged)
1575 * Parameters: slot, flag to say if this is for removing entire module or just
1576 * unconfiguring the device
1577 * TO DO: will probably need to add some code in case there was some resource,
1578 * to remove it... this is from when we have errors in the configure_card...
1579 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1580 * Returns: 0, -1, -ENODEV
1582 int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1584 int i;
1585 int count;
1586 int rc;
1587 struct slot *sl = *slot_cur;
1588 struct pci_func *cur_func = NULL;
1589 struct pci_func *temp_func;
1591 debug ("%s - enter\n", __FUNCTION__);
1593 if (!the_end) {
1594 /* Need to unconfigure the card */
1595 rc = unconfigure_boot_card (sl);
1596 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1597 /* In all other cases, will still need to get rid of func structure if it exists */
1598 return rc;
1602 if (sl->func) {
1603 cur_func = sl->func;
1604 while (cur_func) {
1605 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1606 if (cur_func->bus) {
1607 /* in other words, it's a PPB */
1608 count = 2;
1609 } else {
1610 count = 6;
1613 for (i = 0; i < count; i++) {
1614 if (cur_func->io[i]) {
1615 debug ("io[%d] exists\n", i);
1616 if (the_end > 0)
1617 ibmphp_remove_resource (cur_func->io[i]);
1618 cur_func->io[i] = NULL;
1620 if (cur_func->mem[i]) {
1621 debug ("mem[%d] exists\n", i);
1622 if (the_end > 0)
1623 ibmphp_remove_resource (cur_func->mem[i]);
1624 cur_func->mem[i] = NULL;
1626 if (cur_func->pfmem[i]) {
1627 debug ("pfmem[%d] exists\n", i);
1628 if (the_end > 0)
1629 ibmphp_remove_resource (cur_func->pfmem[i]);
1630 cur_func->pfmem[i] = NULL;
1634 temp_func = cur_func->next;
1635 kfree (cur_func);
1636 cur_func = temp_func;
1640 sl->func = NULL;
1641 *slot_cur = sl;
1642 debug ("%s - exit\n", __FUNCTION__);
1643 return 0;
1647 * add a new bus resulting from hot-plugging a PPB bridge with devices
1649 * Input: bus and the amount of resources needed (we know we can assign those,
1650 * since they've been checked already
1651 * Output: bus added to the correct spot
1652 * 0, -1, error
1654 static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1656 struct range_node *io_range = NULL;
1657 struct range_node *mem_range = NULL;
1658 struct range_node *pfmem_range = NULL;
1659 struct bus_node *cur_bus = NULL;
1661 /* Trying to find the parent bus number */
1662 if (parent_busno != 0xFF) {
1663 cur_bus = ibmphp_find_res_bus (parent_busno);
1664 if (!cur_bus) {
1665 err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1666 return -ENODEV;
1669 list_add (&bus->bus_list, &cur_bus->bus_list);
1671 if (io) {
1672 io_range = kmalloc(sizeof(*io_range), GFP_KERNEL);
1673 if (!io_range) {
1674 err ("out of system memory\n");
1675 return -ENOMEM;
1677 memset (io_range, 0, sizeof (struct range_node));
1678 io_range->start = io->start;
1679 io_range->end = io->end;
1680 io_range->rangeno = 1;
1681 bus->noIORanges = 1;
1682 bus->rangeIO = io_range;
1684 if (mem) {
1685 mem_range = kmalloc(sizeof(*mem_range), GFP_KERNEL);
1686 if (!mem_range) {
1687 err ("out of system memory\n");
1688 return -ENOMEM;
1690 memset (mem_range, 0, sizeof (struct range_node));
1691 mem_range->start = mem->start;
1692 mem_range->end = mem->end;
1693 mem_range->rangeno = 1;
1694 bus->noMemRanges = 1;
1695 bus->rangeMem = mem_range;
1697 if (pfmem) {
1698 pfmem_range = kmalloc(sizeof(*pfmem_range), GFP_KERNEL);
1699 if (!pfmem_range) {
1700 err ("out of system memory\n");
1701 return -ENOMEM;
1703 memset (pfmem_range, 0, sizeof (struct range_node));
1704 pfmem_range->start = pfmem->start;
1705 pfmem_range->end = pfmem->end;
1706 pfmem_range->rangeno = 1;
1707 bus->noPFMemRanges = 1;
1708 bus->rangePFMem = pfmem_range;
1710 return 0;
1714 * find the 1st available bus number for PPB to set as its secondary bus
1715 * Parameters: bus_number of the primary bus
1716 * Returns: bus_number of the secondary bus or 0xff in case of failure
1718 static u8 find_sec_number (u8 primary_busno, u8 slotno)
1720 int min, max;
1721 u8 busno;
1722 struct bus_info *bus;
1723 struct bus_node *bus_cur;
1725 bus = ibmphp_find_same_bus_num (primary_busno);
1726 if (!bus) {
1727 err ("cannot get slot range of the bus from the BIOS\n");
1728 return 0xff;
1730 max = bus->slot_max;
1731 min = bus->slot_min;
1732 if ((slotno > max) || (slotno < min)) {
1733 err ("got the wrong range\n");
1734 return 0xff;
1736 busno = (u8) (slotno - (u8) min);
1737 busno += primary_busno + 0x01;
1738 bus_cur = ibmphp_find_res_bus (busno);
1739 /* either there is no such bus number, or there are no ranges, which
1740 * can only happen if we removed the bridged device in previous load
1741 * of the driver, and now only have the skeleton bus struct
1743 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1744 return busno;
1745 return 0xff;