PCI: Merge multi-line quoted strings
[deliverable/linux.git] / drivers / pci / hotplug / ibmphp_pci.c
CommitLineData
1da177e4
LT
1/*
2 * IBM Hot Plug Controller Driver
f7625980 3 *
1da177e4 4 * Written By: Irene Zubarev, IBM Corporation
f7625980 5 *
1da177e4
LT
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
8 *
9 * All rights reserved.
10 *
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.
15 *
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.
21 *
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.
25 *
26 * Send feedback to <gregkh@us.ibm.com>
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/pci.h>
33#include <linux/list.h>
34#include "ibmphp.h"
35
36
37static int configure_device(struct pci_func *);
38static int configure_bridge(struct pci_func **, u8);
39static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
40static int add_new_bus (struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
41static u8 find_sec_number (u8 primary_busno, u8 slotno);
42
43/*
44 * NOTE..... If BIOS doesn't provide default routing, we assign:
f7625980 45 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
1da177e4
LT
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)
49 */
3c78bc61 50static void assign_alt_irq (struct pci_func *cur_func, u8 class_code)
1da177e4
LT
51{
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;
65 }
66 }
67 }
68}
69
70/*
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
f7625980 74 *
1da177e4 75 * Input: function to be added
f7625980 76 *
1da177e4 77 * TO DO: The error case with Multifunction device or multi function bridge,
f7625980 78 * if there is an error, will need to go through all previous functions and
1da177e4
LT
79 * unconfigure....or can add some code into unconfigure_card....
80 */
81int ibmphp_configure_card (struct pci_func *func, u8 slotno)
82{
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 */
94
95 debug ("inside configure_card, func->busno = %x\n", func->busno);
96
97 device = func->device;
98 cur_func = func;
99
100 /* We only get bus and device from IRQ routing table. So at this point,
f7625980 101 * func->busno is correct, and func->device contains only device (at the 5
1da177e4
LT
102 * highest bits)
103 */
104
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;
109
110 cur_func->function = function;
111
367fa982 112 debug ("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
1da177e4
LT
113 cur_func->busno, cur_func->device, cur_func->function);
114
115 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
116
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);
121
122 ++valid_device;
123
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
127 */
128
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);
131
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) {
227f0647
RD
140 err ("The device %x is not supported for hot plugging. Please choose another device.\n",
141 cur_func->device);
1da177e4
LT
142 return -ENODEV;
143 }
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;
f7625980 154 }
1da177e4
LT
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;
166 }
f5afe806 167 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
1da177e4
LT
168 if (!newfunc) {
169 err ("out of system memory\n");
170 return -ENOMEM;
171 }
1da177e4
LT
172 newfunc->busno = cur_func->busno;
173 newfunc->device = device;
174 cur_func->next = newfunc;
175 cur_func = newfunc;
176 for (j = 0; j < 4; j++)
177 newfunc->irq[j] = cur_func->irq[j];
178 break;
179 case PCI_HEADER_TYPE_MULTIBRIDGE:
180 class >>= 8;
181 if (class != PCI_CLASS_BRIDGE_PCI) {
227f0647
RD
182 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
183 cur_func->device);
1da177e4
LT
184 return -ENODEV;
185 }
186 assign_alt_irq (cur_func, class_code);
187 rc = configure_bridge (&cur_func, slotno);
188 if (rc == -ENODEV) {
189 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
190 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
191 return rc;
192 }
193 if (rc) {
194 /* We need to do this in case some other BARs were properly inserted */
195 err ("was not able to hot-add PPB properly.\n");
196 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
197 cleanup_count = 2;
198 goto error;
199 }
200
201 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
dc6712d1 202 flag = 0;
1da177e4
LT
203 for (i = 0; i < 32; i++) {
204 if (func->devices[i]) {
f5afe806 205 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
1da177e4
LT
206 if (!newfunc) {
207 err ("out of system memory\n");
208 return -ENOMEM;
209 }
1da177e4
LT
210 newfunc->busno = sec_number;
211 newfunc->device = (u8) i;
212 for (j = 0; j < 4; j++)
213 newfunc->irq[j] = cur_func->irq[j];
214
215 if (flag) {
216 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
217 prev_func->next = newfunc;
218 } else
219 cur_func->next = newfunc;
220
221 rc = ibmphp_configure_card (newfunc, slotno);
222 /* This could only happen if kmalloc failed */
223 if (rc) {
224 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
225 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
226 cleanup_count = 2;
227 goto error;
228 }
dc6712d1 229 flag = 1;
1da177e4
LT
230 }
231 }
232
f5afe806 233 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
1da177e4
LT
234 if (!newfunc) {
235 err ("out of system memory\n");
236 return -ENOMEM;
237 }
1da177e4
LT
238 newfunc->busno = cur_func->busno;
239 newfunc->device = device;
240 for (j = 0; j < 4; j++)
241 newfunc->irq[j] = cur_func->irq[j];
242 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
243 prev_func->next = newfunc;
244 cur_func = newfunc;
245 break;
246 case PCI_HEADER_TYPE_BRIDGE:
247 class >>= 8;
248 debug ("class now is %x\n", class);
249 if (class != PCI_CLASS_BRIDGE_PCI) {
227f0647
RD
250 err ("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
251 cur_func->device);
1da177e4
LT
252 return -ENODEV;
253 }
254
255 assign_alt_irq (cur_func, class_code);
256
257 debug ("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
258 rc = configure_bridge (&cur_func, slotno);
259 if (rc == -ENODEV) {
260 err ("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
261 err ("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
262 return rc;
263 }
264 if (rc) {
265 /* We need to do this in case some other BARs were properly inserted */
266 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
267 err ("was not able to hot-add PPB properly.\n");
268 cleanup_count = 2;
269 goto error;
270 }
271 debug ("cur_func->busno = %x, device = %x, function = %x\n",
272 cur_func->busno, device, function);
273 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
274 debug ("after configuring bridge..., sec_number = %x\n", sec_number);
dc6712d1 275 flag = 0;
1da177e4
LT
276 for (i = 0; i < 32; i++) {
277 if (func->devices[i]) {
278 debug ("inside for loop, device is %x\n", i);
f5afe806 279 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
1da177e4
LT
280 if (!newfunc) {
281 err (" out of system memory\n");
282 return -ENOMEM;
283 }
1da177e4
LT
284 newfunc->busno = sec_number;
285 newfunc->device = (u8) i;
286 for (j = 0; j < 4; j++)
287 newfunc->irq[j] = cur_func->irq[j];
288
289 if (flag) {
290 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
291 prev_func->next = newfunc;
292 } else
293 cur_func->next = newfunc;
294
295 rc = ibmphp_configure_card (newfunc, slotno);
296
297 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
298 if (rc) {
299 /* We need to do this in case some other BARs were properly inserted */
300 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
301 cleanup_count = 2;
302 goto error;
303 }
dc6712d1 304 flag = 1;
1da177e4
LT
305 }
306 }
307
308 function = 0x8;
309 break;
310 default:
311 err ("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
312 return -ENXIO;
313 break;
314 } /* end of switch */
315 } /* end of valid device */
316 } /* end of for */
317
318 if (!valid_device) {
319 err ("Cannot find any valid devices on the card. Or unable to read from card.\n");
320 return -ENODEV;
321 }
322
323 return 0;
324
325error:
326 for (i = 0; i < cleanup_count; i++) {
327 if (cur_func->io[i]) {
328 ibmphp_remove_resource (cur_func->io[i]);
329 cur_func->io[i] = NULL;
330 } else if (cur_func->pfmem[i]) {
331 ibmphp_remove_resource (cur_func->pfmem[i]);
332 cur_func->pfmem[i] = NULL;
333 } else if (cur_func->mem[i]) {
334 ibmphp_remove_resource (cur_func->mem[i]);
335 cur_func->mem[i] = NULL;
336 }
337 }
338 return rc;
339}
340
341/*
f7625980 342 * This function configures the pci BARs of a single device.
1da177e4
LT
343 * Input: pointer to the pci_func
344 * Output: configured PCI, 0, or error
345 */
346static int configure_device (struct pci_func *func)
347{
348 u32 bar[6];
349 u32 address[] = {
350 PCI_BASE_ADDRESS_0,
351 PCI_BASE_ADDRESS_1,
352 PCI_BASE_ADDRESS_2,
353 PCI_BASE_ADDRESS_3,
354 PCI_BASE_ADDRESS_4,
355 PCI_BASE_ADDRESS_5,
356 0
357 };
358 u8 irq;
359 int count;
360 int len[6];
361 struct resource_node *io[6];
362 struct resource_node *mem[6];
363 struct resource_node *mem_tmp;
364 struct resource_node *pfmem[6];
365 unsigned int devfn;
366
66bef8c0 367 debug ("%s - inside\n", __func__);
1da177e4
LT
368
369 devfn = PCI_DEVFN(func->device, func->function);
370 ibmphp_pci_bus->number = func->busno;
371
372 for (count = 0; address[count]; count++) { /* for 6 BARs */
373
f7625980 374 /* not sure if i need this. per scott, said maybe need * something like this
1da177e4
LT
375 if devices don't adhere 100% to the spec, so don't want to write
376 to the reserved bits
377
f7625980 378 pcibios_read_config_byte(cur_func->busno, cur_func->device,
1da177e4
LT
379 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
380 if (tmp & 0x01) // IO
f7625980 381 pcibios_write_config_dword(cur_func->busno, cur_func->device,
1da177e4
LT
382 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
383 else // Memory
f7625980 384 pcibios_write_config_dword(cur_func->busno, cur_func->device,
1da177e4
LT
385 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
386 */
387 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
388 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
389
390 if (!bar[count]) /* This BAR is not implemented */
391 continue;
392
393 debug ("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
394
395 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
396 /* This is IO */
397 debug ("inside IO SPACE\n");
398
399 len[count] = bar[count] & 0xFFFFFFFC;
400 len[count] = ~len[count] + 1;
401
402 debug ("len[count] in IO %x, count %d\n", len[count], count);
403
f5afe806 404 io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1da177e4
LT
405
406 if (!io[count]) {
407 err ("out of system memory\n");
408 return -ENOMEM;
409 }
1da177e4
LT
410 io[count]->type = IO;
411 io[count]->busno = func->busno;
412 io[count]->devfunc = PCI_DEVFN(func->device, func->function);
413 io[count]->len = len[count];
414 if (ibmphp_check_resource(io[count], 0) == 0) {
415 ibmphp_add_resource (io[count]);
416 func->io[count] = io[count];
417 } else {
418 err ("cannot allocate requested io for bus %x device %x function %x len %x\n",
419 func->busno, func->device, func->function, len[count]);
420 kfree (io[count]);
421 return -EIO;
422 }
423 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
f7625980
BH
424
425 /* _______________This is for debugging purposes only_____________________ */
1da177e4
LT
426 debug ("b4 writing, the IO address is %x\n", func->io[count]->start);
427 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
428 debug ("after writing.... the start address is %x\n", bar[count]);
429 /* _________________________________________________________________________*/
430
431 } else {
432 /* This is Memory */
433 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
434 /* pfmem */
435 debug ("PFMEM SPACE\n");
436
437 len[count] = bar[count] & 0xFFFFFFF0;
438 len[count] = ~len[count] + 1;
439
440 debug ("len[count] in PFMEM %x, count %d\n", len[count], count);
441
f5afe806 442 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1da177e4
LT
443 if (!pfmem[count]) {
444 err ("out of system memory\n");
445 return -ENOMEM;
446 }
1da177e4
LT
447 pfmem[count]->type = PFMEM;
448 pfmem[count]->busno = func->busno;
449 pfmem[count]->devfunc = PCI_DEVFN(func->device,
450 func->function);
451 pfmem[count]->len = len[count];
dc6712d1 452 pfmem[count]->fromMem = 0;
1da177e4
LT
453 if (ibmphp_check_resource (pfmem[count], 0) == 0) {
454 ibmphp_add_resource (pfmem[count]);
455 func->pfmem[count] = pfmem[count];
456 } else {
f5afe806 457 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
1da177e4
LT
458 if (!mem_tmp) {
459 err ("out of system memory\n");
460 kfree (pfmem[count]);
461 return -ENOMEM;
462 }
1da177e4
LT
463 mem_tmp->type = MEM;
464 mem_tmp->busno = pfmem[count]->busno;
465 mem_tmp->devfunc = pfmem[count]->devfunc;
466 mem_tmp->len = pfmem[count]->len;
467 debug ("there's no pfmem... going into mem.\n");
468 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
469 ibmphp_add_resource (mem_tmp);
dc6712d1 470 pfmem[count]->fromMem = 1;
1da177e4
LT
471 pfmem[count]->rangeno = mem_tmp->rangeno;
472 pfmem[count]->start = mem_tmp->start;
473 pfmem[count]->end = mem_tmp->end;
474 ibmphp_add_pfmem_from_mem (pfmem[count]);
475 func->pfmem[count] = pfmem[count];
476 } else {
477 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
478 func->busno, func->device, len[count]);
479 kfree (mem_tmp);
480 kfree (pfmem[count]);
481 return -EIO;
482 }
483 }
484
485 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
486
f7625980 487 /*_______________This is for debugging purposes only______________________________*/
1da177e4
LT
488 debug ("b4 writing, start address is %x\n", func->pfmem[count]->start);
489 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
490 debug ("after writing, start address is %x\n", bar[count]);
491 /*_________________________________________________________________________________*/
492
493 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
494 debug ("inside the mem 64 case, count %d\n", count);
495 count += 1;
496 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
497 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
498 }
499 } else {
500 /* regular memory */
501 debug ("REGULAR MEM SPACE\n");
502
503 len[count] = bar[count] & 0xFFFFFFF0;
504 len[count] = ~len[count] + 1;
505
506 debug ("len[count] in Mem %x, count %d\n", len[count], count);
507
f5afe806 508 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1da177e4
LT
509 if (!mem[count]) {
510 err ("out of system memory\n");
511 return -ENOMEM;
512 }
1da177e4
LT
513 mem[count]->type = MEM;
514 mem[count]->busno = func->busno;
515 mem[count]->devfunc = PCI_DEVFN(func->device,
516 func->function);
517 mem[count]->len = len[count];
518 if (ibmphp_check_resource (mem[count], 0) == 0) {
519 ibmphp_add_resource (mem[count]);
520 func->mem[count] = mem[count];
521 } else {
522 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
523 func->busno, func->device, len[count]);
524 kfree (mem[count]);
525 return -EIO;
526 }
527 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
528 /* _______________________This is for debugging purposes only _______________________*/
529 debug ("b4 writing, start address is %x\n", func->mem[count]->start);
530 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
531 debug ("after writing, the address is %x\n", bar[count]);
532 /* __________________________________________________________________________________*/
533
534 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
535 /* takes up another dword */
536 debug ("inside mem 64 case, reg. mem, count %d\n", count);
537 count += 1;
538 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
539 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
540 }
541 }
542 } /* end of mem */
543 } /* end of for */
544
545 func->bus = 0; /* To indicate that this is not a PPB */
546 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
547 if ((irq > 0x00) && (irq < 0x05))
548 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
549
550 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
551 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
552
d648daca 553 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
1da177e4
LT
554 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
555
556 return 0;
557}
558
559/******************************************************************************
560 * This routine configures a PCI-2-PCI bridge and the functions behind it
561 * Parameters: pci_func
f7625980 562 * Returns:
1da177e4
LT
563 ******************************************************************************/
564static int configure_bridge (struct pci_func **func_passed, u8 slotno)
565{
566 int count;
567 int i;
568 int rc;
569 u8 sec_number;
570 u8 io_base;
571 u16 pfmem_base;
572 u32 bar[2];
573 u32 len[2];
dc6712d1
KA
574 u8 flag_io = 0;
575 u8 flag_mem = 0;
576 u8 flag_pfmem = 0;
577 u8 need_io_upper = 0;
578 u8 need_pfmem_upper = 0;
1da177e4
LT
579 struct res_needed *amount_needed = NULL;
580 struct resource_node *io = NULL;
581 struct resource_node *bus_io[2] = {NULL, NULL};
582 struct resource_node *mem = NULL;
583 struct resource_node *bus_mem[2] = {NULL, NULL};
584 struct resource_node *mem_tmp = NULL;
585 struct resource_node *pfmem = NULL;
586 struct resource_node *bus_pfmem[2] = {NULL, NULL};
587 struct bus_node *bus;
588 u32 address[] = {
589 PCI_BASE_ADDRESS_0,
590 PCI_BASE_ADDRESS_1,
591 0
592 };
593 struct pci_func *func = *func_passed;
594 unsigned int devfn;
595 u8 irq;
596 int retval;
597
66bef8c0 598 debug ("%s - enter\n", __func__);
1da177e4
LT
599
600 devfn = PCI_DEVFN(func->function, func->device);
601 ibmphp_pci_bus->number = func->busno;
602
603 /* Configuring necessary info for the bridge so that we could see the devices
604 * behind it
605 */
606
607 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
608
609 /* _____________________For debugging purposes only __________________________
610 pci_bus_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
611 debug ("primary # written into the bridge is %x\n", pri_number);
612 ___________________________________________________________________________*/
613
614 /* in EBDA, only get allocated 1 additional bus # per slot */
615 sec_number = find_sec_number (func->busno, slotno);
616 if (sec_number == 0xff) {
617 err ("cannot allocate secondary bus number for the bridged device\n");
618 return -EINVAL;
619 }
620
621 debug ("after find_sec_number, the number we got is %x\n", sec_number);
622 debug ("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
623
624 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
f7625980 625
1da177e4
LT
626 /* __________________For debugging purposes only __________________________________
627 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
628 debug ("sec_number after write/read is %x\n", sec_number);
629 ________________________________________________________________________________*/
630
631 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
632
633 /* __________________For debugging purposes only ____________________________________
634 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
635 debug ("subordinate number after write/read is %x\n", sec_number);
636 __________________________________________________________________________________*/
637
638 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
639 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
640 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
641
642 debug ("func->busno is %x\n", func->busno);
643 debug ("sec_number after writing is %x\n", sec_number);
644
645
646 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
f7625980 647 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
1da177e4
LT
648 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
649
650
651 /* First we need to allocate mem/io for the bridge itself in case it needs it */
652 for (count = 0; address[count]; count++) { /* for 2 BARs */
653 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
654 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
655
656 if (!bar[count]) {
657 /* This BAR is not implemented */
658 debug ("so we come here then, eh?, count = %d\n", count);
659 continue;
660 }
661 // tmp_bar = bar[count];
662
663 debug ("Bar %d wants %x\n", count, bar[count]);
664
665 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
666 /* This is IO */
667 len[count] = bar[count] & 0xFFFFFFFC;
668 len[count] = ~len[count] + 1;
669
670 debug ("len[count] in IO = %x\n", len[count]);
671
f5afe806 672 bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
f7625980 673
1da177e4
LT
674 if (!bus_io[count]) {
675 err ("out of system memory\n");
676 retval = -ENOMEM;
677 goto error;
678 }
1da177e4
LT
679 bus_io[count]->type = IO;
680 bus_io[count]->busno = func->busno;
681 bus_io[count]->devfunc = PCI_DEVFN(func->device,
682 func->function);
683 bus_io[count]->len = len[count];
684 if (ibmphp_check_resource (bus_io[count], 0) == 0) {
685 ibmphp_add_resource (bus_io[count]);
686 func->io[count] = bus_io[count];
687 } else {
688 err ("cannot allocate requested io for bus %x, device %x, len %x\n",
689 func->busno, func->device, len[count]);
690 kfree (bus_io[count]);
691 return -EIO;
692 }
693
694 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
695
696 } else {
697 /* This is Memory */
698 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
699 /* pfmem */
700 len[count] = bar[count] & 0xFFFFFFF0;
701 len[count] = ~len[count] + 1;
702
703 debug ("len[count] in PFMEM = %x\n", len[count]);
704
f5afe806 705 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1da177e4
LT
706 if (!bus_pfmem[count]) {
707 err ("out of system memory\n");
708 retval = -ENOMEM;
709 goto error;
710 }
1da177e4
LT
711 bus_pfmem[count]->type = PFMEM;
712 bus_pfmem[count]->busno = func->busno;
713 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
714 func->function);
715 bus_pfmem[count]->len = len[count];
dc6712d1 716 bus_pfmem[count]->fromMem = 0;
1da177e4
LT
717 if (ibmphp_check_resource (bus_pfmem[count], 0) == 0) {
718 ibmphp_add_resource (bus_pfmem[count]);
719 func->pfmem[count] = bus_pfmem[count];
720 } else {
f5afe806 721 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
1da177e4
LT
722 if (!mem_tmp) {
723 err ("out of system memory\n");
724 retval = -ENOMEM;
725 goto error;
726 }
1da177e4
LT
727 mem_tmp->type = MEM;
728 mem_tmp->busno = bus_pfmem[count]->busno;
729 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
730 mem_tmp->len = bus_pfmem[count]->len;
731 if (ibmphp_check_resource (mem_tmp, 0) == 0) {
732 ibmphp_add_resource (mem_tmp);
dc6712d1 733 bus_pfmem[count]->fromMem = 1;
1da177e4
LT
734 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
735 ibmphp_add_pfmem_from_mem (bus_pfmem[count]);
736 func->pfmem[count] = bus_pfmem[count];
737 } else {
f7625980 738 err ("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
1da177e4
LT
739 func->busno, func->device, len[count]);
740 kfree (mem_tmp);
741 kfree (bus_pfmem[count]);
742 return -EIO;
743 }
744 }
745
746 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
747
748 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
749 /* takes up another dword */
750 count += 1;
751 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
752 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
753
754 }
755 } else {
756 /* regular memory */
757 len[count] = bar[count] & 0xFFFFFFF0;
758 len[count] = ~len[count] + 1;
759
760 debug ("len[count] in Memory is %x\n", len[count]);
761
f5afe806 762 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
1da177e4
LT
763 if (!bus_mem[count]) {
764 err ("out of system memory\n");
765 retval = -ENOMEM;
766 goto error;
767 }
1da177e4
LT
768 bus_mem[count]->type = MEM;
769 bus_mem[count]->busno = func->busno;
770 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
771 func->function);
772 bus_mem[count]->len = len[count];
773 if (ibmphp_check_resource (bus_mem[count], 0) == 0) {
774 ibmphp_add_resource (bus_mem[count]);
775 func->mem[count] = bus_mem[count];
776 } else {
777 err ("cannot allocate requested mem for bus %x, device %x, len %x\n",
778 func->busno, func->device, len[count]);
779 kfree (bus_mem[count]);
780 return -EIO;
781 }
782
783 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
784
785 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
786 /* takes up another dword */
787 count += 1;
788 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
789 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0x00000000);
790
791 }
792 }
793 } /* end of mem */
794 } /* end of for */
795
796 /* Now need to see how much space the devices behind the bridge needed */
797 amount_needed = scan_behind_bridge (func, sec_number);
798 if (amount_needed == NULL)
799 return -ENOMEM;
800
801 ibmphp_pci_bus->number = func->busno;
802 debug ("after coming back from scan_behind_bridge\n");
803 debug ("amount_needed->not_correct = %x\n", amount_needed->not_correct);
804 debug ("amount_needed->io = %x\n", amount_needed->io);
805 debug ("amount_needed->mem = %x\n", amount_needed->mem);
806 debug ("amount_needed->pfmem = %x\n", amount_needed->pfmem);
807
f7625980 808 if (amount_needed->not_correct) {
1da177e4
LT
809 debug ("amount_needed is not correct\n");
810 for (count = 0; address[count]; count++) {
811 /* for 2 BARs */
812 if (bus_io[count]) {
813 ibmphp_remove_resource (bus_io[count]);
814 func->io[count] = NULL;
815 } else if (bus_pfmem[count]) {
816 ibmphp_remove_resource (bus_pfmem[count]);
817 func->pfmem[count] = NULL;
818 } else if (bus_mem[count]) {
819 ibmphp_remove_resource (bus_mem[count]);
820 func->mem[count] = NULL;
821 }
822 }
823 kfree (amount_needed);
824 return -ENODEV;
825 }
826
827 if (!amount_needed->io) {
828 debug ("it doesn't want IO?\n");
dc6712d1 829 flag_io = 1;
1da177e4
LT
830 } else {
831 debug ("it wants %x IO behind the bridge\n", amount_needed->io);
f5afe806 832 io = kzalloc(sizeof(*io), GFP_KERNEL);
f7625980 833
1da177e4
LT
834 if (!io) {
835 err ("out of system memory\n");
836 retval = -ENOMEM;
837 goto error;
838 }
1da177e4
LT
839 io->type = IO;
840 io->busno = func->busno;
841 io->devfunc = PCI_DEVFN(func->device, func->function);
842 io->len = amount_needed->io;
843 if (ibmphp_check_resource (io, 1) == 0) {
844 debug ("were we able to add io\n");
845 ibmphp_add_resource (io);
dc6712d1 846 flag_io = 1;
1da177e4
LT
847 }
848 }
849
850 if (!amount_needed->mem) {
851 debug ("it doesn't want n.e.memory?\n");
dc6712d1 852 flag_mem = 1;
1da177e4
LT
853 } else {
854 debug ("it wants %x memory behind the bridge\n", amount_needed->mem);
f5afe806 855 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
1da177e4
LT
856 if (!mem) {
857 err ("out of system memory\n");
858 retval = -ENOMEM;
859 goto error;
860 }
1da177e4
LT
861 mem->type = MEM;
862 mem->busno = func->busno;
863 mem->devfunc = PCI_DEVFN(func->device, func->function);
864 mem->len = amount_needed->mem;
865 if (ibmphp_check_resource (mem, 1) == 0) {
866 ibmphp_add_resource (mem);
dc6712d1 867 flag_mem = 1;
1da177e4
LT
868 debug ("were we able to add mem\n");
869 }
870 }
871
872 if (!amount_needed->pfmem) {
873 debug ("it doesn't want n.e.pfmem mem?\n");
dc6712d1 874 flag_pfmem = 1;
1da177e4
LT
875 } else {
876 debug ("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
f5afe806 877 pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
1da177e4
LT
878 if (!pfmem) {
879 err ("out of system memory\n");
880 retval = -ENOMEM;
881 goto error;
882 }
1da177e4
LT
883 pfmem->type = PFMEM;
884 pfmem->busno = func->busno;
885 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
886 pfmem->len = amount_needed->pfmem;
dc6712d1 887 pfmem->fromMem = 0;
1da177e4
LT
888 if (ibmphp_check_resource (pfmem, 1) == 0) {
889 ibmphp_add_resource (pfmem);
dc6712d1 890 flag_pfmem = 1;
1da177e4 891 } else {
f5afe806 892 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
1da177e4
LT
893 if (!mem_tmp) {
894 err ("out of system memory\n");
895 retval = -ENOMEM;
896 goto error;
897 }
1da177e4
LT
898 mem_tmp->type = MEM;
899 mem_tmp->busno = pfmem->busno;
900 mem_tmp->devfunc = pfmem->devfunc;
901 mem_tmp->len = pfmem->len;
902 if (ibmphp_check_resource (mem_tmp, 1) == 0) {
903 ibmphp_add_resource (mem_tmp);
dc6712d1 904 pfmem->fromMem = 1;
1da177e4
LT
905 pfmem->rangeno = mem_tmp->rangeno;
906 ibmphp_add_pfmem_from_mem (pfmem);
dc6712d1 907 flag_pfmem = 1;
1da177e4
LT
908 }
909 }
910 }
911
912 debug ("b4 if (flag_io && flag_mem && flag_pfmem)\n");
913 debug ("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
914
915 if (flag_io && flag_mem && flag_pfmem) {
916 /* If on bootup, there was a bridged card in this slot,
917 * then card was removed and ibmphp got unloaded and loaded
918 * back again, there's no way for us to remove the bus
919 * struct, so no need to kmalloc, can use existing node
920 */
921 bus = ibmphp_find_res_bus (sec_number);
922 if (!bus) {
f5afe806 923 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
924 if (!bus) {
925 err ("out of system memory\n");
926 retval = -ENOMEM;
927 goto error;
928 }
1da177e4
LT
929 bus->busno = sec_number;
930 debug ("b4 adding new bus\n");
931 rc = add_new_bus (bus, io, mem, pfmem, func->busno);
932 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
933 rc = add_new_bus (bus, io, mem, pfmem, 0xFF);
934 else {
935 err ("expected bus structure not empty?\n");
936 retval = -EIO;
937 goto error;
938 }
939 if (rc) {
940 if (rc == -ENOMEM) {
941 ibmphp_remove_bus (bus, func->busno);
942 kfree (amount_needed);
943 return rc;
944 }
945 retval = rc;
946 goto error;
947 }
948 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
949 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
950
951 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
952 debug ("io 32\n");
dc6712d1 953 need_io_upper = 1;
1da177e4 954 }
88d69a1d 955 if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
1da177e4 956 debug ("pfmem 64\n");
dc6712d1 957 need_pfmem_upper = 1;
1da177e4
LT
958 }
959
960 if (bus->noIORanges) {
961 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
f7625980 962 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
1da177e4
LT
963
964 /* _______________This is for debugging purposes only ____________________
965 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
966 debug ("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
967 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
968 debug ("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
969 ________________________________________________________________________*/
970
971 if (need_io_upper) { /* since can't support n.e.ways */
972 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
973 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
974 }
975 } else {
976 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
977 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
978 }
979
980 if (bus->noMemRanges) {
981 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
982 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
f7625980 983
1da177e4
LT
984 /* ____________________This is for debugging purposes only ________________________
985 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
986 debug ("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
987 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
988 debug ("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
989 __________________________________________________________________________________*/
990
991 } else {
992 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
993 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
994 }
995 if (bus->noPFMemRanges) {
996 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
997 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
998
999 /* __________________________This is for debugging purposes only _______________________
1000 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1001 debug ("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1002 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1003 debug ("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1004 ______________________________________________________________________________________*/
1005
1006 if (need_pfmem_upper) { /* since can't support n.e.ways */
1007 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1008 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1009 }
1010 } else {
1011 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1012 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1013 }
1014
1015 debug ("b4 writing control information\n");
1016
1017 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1018 if ((irq > 0x00) && (irq < 0x05))
1019 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
f7625980 1020 /*
1da177e4
LT
1021 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1022 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1023 pci_bus_write_config_byte (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1024 */
1025
1026 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1027 pci_bus_write_config_word (ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1028 for (i = 0; i < 32; i++) {
1029 if (amount_needed->devices[i]) {
1030 debug ("device where devices[i] is 1 = %x\n", i);
1031 func->devices[i] = 1;
1032 }
1033 }
1034 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1035 func_passed = &func;
1036 debug ("func->busno b4 returning is %x\n", func->busno);
1037 debug ("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1038 kfree (amount_needed);
1039 return 0;
1040 } else {
1041 err ("Configuring bridge was unsuccessful...\n");
1042 mem_tmp = NULL;
1043 retval = -EIO;
1044 goto error;
1045 }
1046
1047error:
1048 kfree(amount_needed);
1049 if (pfmem)
1050 ibmphp_remove_resource (pfmem);
1051 if (io)
1052 ibmphp_remove_resource (io);
1053 if (mem)
1054 ibmphp_remove_resource (mem);
1055 for (i = 0; i < 2; i++) { /* for 2 BARs */
1056 if (bus_io[i]) {
1057 ibmphp_remove_resource (bus_io[i]);
1058 func->io[i] = NULL;
1059 } else if (bus_pfmem[i]) {
1060 ibmphp_remove_resource (bus_pfmem[i]);
1061 func->pfmem[i] = NULL;
1062 } else if (bus_mem[i]) {
1063 ibmphp_remove_resource (bus_mem[i]);
1064 func->mem[i] = NULL;
1065 }
1066 }
1067 return retval;
1068}
1069
1070/*****************************************************************************
1071 * This function adds up the amount of resources needed behind the PPB bridge
1072 * and passes it to the configure_bridge function
1073 * Input: bridge function
f7625980 1074 * Output: amount of resources needed
1da177e4 1075 *****************************************************************************/
3c78bc61 1076static struct res_needed *scan_behind_bridge (struct pci_func *func, u8 busno)
1da177e4
LT
1077{
1078 int count, len[6];
1079 u16 vendor_id;
1080 u8 hdr_type;
1081 u8 device, function;
1082 unsigned int devfn;
1083 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1084
1085 u32 bar[6], class;
1086 u32 address[] = {
1087 PCI_BASE_ADDRESS_0,
1088 PCI_BASE_ADDRESS_1,
1089 PCI_BASE_ADDRESS_2,
1090 PCI_BASE_ADDRESS_3,
1091 PCI_BASE_ADDRESS_4,
1092 PCI_BASE_ADDRESS_5,
1093 0
1094 };
1095 struct res_needed *amount;
1096
f5afe806 1097 amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1da177e4
LT
1098 if (amount == NULL)
1099 return NULL;
1da177e4
LT
1100
1101 ibmphp_pci_bus->number = busno;
1102
1103 debug ("the bus_no behind the bridge is %x\n", busno);
1104 debug ("scanning devices behind the bridge...\n");
1105 for (device = 0; device < 32; device++) {
1106 amount->devices[device] = 0;
1107 for (function = 0; function < 8; function++) {
1108 devfn = PCI_DEVFN(device, function);
1109
1110 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1111
1112 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1113 /* found correct device!!! */
1114 howmany++;
1115
1116 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1117 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1118
1119 debug ("hdr_type behind the bridge is %x\n", hdr_type);
1120 if (hdr_type & PCI_HEADER_TYPE_BRIDGE) {
1121 err ("embedded bridges not supported for hot-plugging.\n");
dc6712d1 1122 amount->not_correct = 1;
1da177e4
LT
1123 return amount;
1124 }
1125
1126 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1127 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
227f0647 1128 err ("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device);
dc6712d1 1129 amount->not_correct = 1;
1da177e4
LT
1130 return amount;
1131 } else if (class == PCI_CLASS_DISPLAY_VGA) {
227f0647 1132 err ("The device %x is not supported for hot plugging. Please choose another device.\n", device);
dc6712d1 1133 amount->not_correct = 1;
1da177e4
LT
1134 return amount;
1135 }
1136
1137 amount->devices[device] = 1;
1138
1139 for (count = 0; address[count]; count++) {
1140 /* for 6 BARs */
1141 /*
1142 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, address[count], &tmp);
1143 if (tmp & 0x01) // IO
1144 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1145 else // MEMORY
1146 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1147 */
1148 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1149 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &bar[count]);
1150
1151 debug ("what is bar[count]? %x, count = %d\n", bar[count], count);
1152
1153 if (!bar[count]) /* This BAR is not implemented */
1154 continue;
1155
1156 //tmp_bar = bar[count];
1157
1158 debug ("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1159
1160 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1161 /* This is IO */
1162 len[count] = bar[count] & 0xFFFFFFFC;
1163 len[count] = ~len[count] + 1;
1164 amount->io += len[count];
1165 } else {
1166 /* This is Memory */
1167 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1168 /* pfmem */
1169 len[count] = bar[count] & 0xFFFFFFF0;
1170 len[count] = ~len[count] + 1;
1171 amount->pfmem += len[count];
1172 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1173 /* takes up another dword */
1174 count += 1;
1175
1176 } else {
1177 /* regular memory */
1178 len[count] = bar[count] & 0xFFFFFFF0;
1179 len[count] = ~len[count] + 1;
1180 amount->mem += len[count];
1181 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1182 /* takes up another dword */
1183 count += 1;
1184 }
1185 }
1186 }
1187 } /* end for */
1188 } /* end if (valid) */
1189 } /* end for */
1190 } /* end for */
1191
1192 if (!howmany)
dc6712d1 1193 amount->not_correct = 1;
1da177e4 1194 else
dc6712d1 1195 amount->not_correct = 0;
1da177e4
LT
1196 if ((amount->io) && (amount->io < IOBRIDGE))
1197 amount->io = IOBRIDGE;
1198 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1199 amount->mem = MEMBRIDGE;
1200 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1201 amount->pfmem = MEMBRIDGE;
1202 return amount;
1203}
1204
f7625980
BH
1205/* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1206 * upon bootup in the system, since we don't allocate func to such case, we need to read
1207 * the start addresses from pci config space and then find the corresponding entries in
1da177e4
LT
1208 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1209 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1210 * the bootup case), since it should work same way
1211 */
1212static int unconfigure_boot_device (u8 busno, u8 device, u8 function)
1213{
1214 u32 start_address;
1215 u32 address[] = {
1216 PCI_BASE_ADDRESS_0,
1217 PCI_BASE_ADDRESS_1,
1218 PCI_BASE_ADDRESS_2,
1219 PCI_BASE_ADDRESS_3,
1220 PCI_BASE_ADDRESS_4,
1221 PCI_BASE_ADDRESS_5,
1222 0
1223 };
1224 int count;
1225 struct resource_node *io;
1226 struct resource_node *mem;
1227 struct resource_node *pfmem;
1228 struct bus_node *bus;
1229 u32 end_address;
1230 u32 temp_end;
1231 u32 size;
1232 u32 tmp_address;
1233 unsigned int devfn;
1234
66bef8c0 1235 debug ("%s - enter\n", __func__);
1da177e4
LT
1236
1237 bus = ibmphp_find_res_bus (busno);
1238 if (!bus) {
1239 debug ("cannot find corresponding bus.\n");
1240 return -EINVAL;
1241 }
1242
1243 devfn = PCI_DEVFN(device, function);
1244 ibmphp_pci_bus->number = busno;
1245 for (count = 0; address[count]; count++) { /* for 6 BARs */
1246 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1247
1248 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1249
1250 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1251 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &size);
1252 pci_bus_write_config_dword (ibmphp_pci_bus, devfn, address[count], start_address);
1253
1254 debug ("start_address is %x\n", start_address);
1255 debug ("busno, device, function %x %x %x\n", busno, device, function);
1256 if (!size) {
1257 /* This BAR is not implemented */
1258 debug ("is this bar no implemented?, count = %d\n", count);
1259 continue;
1260 }
1261 tmp_address = start_address;
1262 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1263 /* This is IO */
1264 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1265 size = size & 0xFFFFFFFC;
1266 size = ~size + 1;
1267 end_address = start_address + size - 1;
1268 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1269 err ("cannot find corresponding IO resource to remove\n");
1270 return -EIO;
1271 }
1272 debug ("io->start = %x\n", io->start);
1273 temp_end = io->end;
1274 start_address = io->end + 1;
1275 ibmphp_remove_resource (io);
1276 /* This is needed b/c of the old I/O restrictions in the BIOS */
1277 while (temp_end < end_address) {
1278 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1279 err ("cannot find corresponding IO resource to remove\n");
1280 return -EIO;
1281 }
1282 debug ("io->start = %x\n", io->start);
1283 temp_end = io->end;
1284 start_address = io->end + 1;
1285 ibmphp_remove_resource (io);
1286 }
1287
1288 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1289 } else {
1290 /* This is Memory */
1da177e4
LT
1291 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1292 /* pfmem */
1293 debug ("start address of pfmem is %x\n", start_address);
034ecc72 1294 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1da177e4
LT
1295
1296 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1297 err ("cannot find corresponding PFMEM resource to remove\n");
1298 return -EIO;
1299 }
1300 if (pfmem) {
1301 debug ("pfmem->start = %x\n", pfmem->start);
1302
1303 ibmphp_remove_resource(pfmem);
1304 }
1305 } else {
1306 /* regular memory */
1307 debug ("start address of mem is %x\n", start_address);
034ecc72
REB
1308 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1309
1da177e4
LT
1310 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1311 err ("cannot find corresponding MEM resource to remove\n");
1312 return -EIO;
1313 }
1314 if (mem) {
1315 debug ("mem->start = %x\n", mem->start);
1316
1317 ibmphp_remove_resource(mem);
1318 }
1319 }
1320 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1321 /* takes up another dword */
1322 count += 1;
1323 }
1324 } /* end of mem */
1325 } /* end of for */
1326
1327 return 0;
1328}
1329
1330static int unconfigure_boot_bridge (u8 busno, u8 device, u8 function)
1331{
1332 int count;
1333 int bus_no, pri_no, sub_no, sec_no = 0;
1334 u32 start_address, tmp_address;
1335 u8 sec_number, sub_number, pri_number;
1336 struct resource_node *io = NULL;
1337 struct resource_node *mem = NULL;
1338 struct resource_node *pfmem = NULL;
1339 struct bus_node *bus;
1340 u32 address[] = {
1341 PCI_BASE_ADDRESS_0,
1342 PCI_BASE_ADDRESS_1,
1343 0
1344 };
1345 unsigned int devfn;
1346
1347 devfn = PCI_DEVFN(device, function);
1348 ibmphp_pci_bus->number = busno;
1349 bus_no = (int) busno;
1350 debug ("busno is %x\n", busno);
1351 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
66bef8c0 1352 debug ("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1da177e4
LT
1353
1354 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1355 debug ("sec_number is %x\n", sec_number);
1356 sec_no = (int) sec_number;
1357 pri_no = (int) pri_number;
1358 if (pri_no != bus_no) {
1359 err ("primary numbers in our structures and pci config space don't match.\n");
1360 return -EINVAL;
1361 }
1362
1363 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1364 sub_no = (int) sub_number;
1365 debug ("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1366 if (sec_no != sub_number) {
1367 err ("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
1368 return -ENODEV;
1369 }
1370
1371 bus = ibmphp_find_res_bus (sec_number);
1da177e4
LT
1372 if (!bus) {
1373 err ("cannot find Bus structure for the bridged device\n");
1374 return -EINVAL;
1375 }
b0d974e9
AB
1376 debug("bus->busno is %x\n", bus->busno);
1377 debug("sec_number is %x\n", sec_number);
1da177e4
LT
1378
1379 ibmphp_remove_bus (bus, busno);
1380
1381 for (count = 0; address[count]; count++) {
1382 /* for 2 BARs */
1383 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, address[count], &start_address);
1384
1385 if (!start_address) {
1386 /* This BAR is not implemented */
1387 continue;
1388 }
1389
1390 tmp_address = start_address;
1391
1392 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1393 /* This is IO */
1394 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1395 if (ibmphp_find_resource (bus, start_address, &io, IO) < 0) {
1396 err ("cannot find corresponding IO resource to remove\n");
1397 return -EIO;
1398 }
1399 if (io)
1400 debug ("io->start = %x\n", io->start);
1401
1402 ibmphp_remove_resource (io);
1403
1404 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1405 } else {
1406 /* This is Memory */
1da177e4
LT
1407 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1408 /* pfmem */
034ecc72 1409 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1da177e4
LT
1410 if (ibmphp_find_resource (bus, start_address, &pfmem, PFMEM) < 0) {
1411 err ("cannot find corresponding PFMEM resource to remove\n");
1412 return -EINVAL;
1413 }
1414 if (pfmem) {
1415 debug ("pfmem->start = %x\n", pfmem->start);
1416
1417 ibmphp_remove_resource(pfmem);
1418 }
1419 } else {
1420 /* regular memory */
034ecc72 1421 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1da177e4
LT
1422 if (ibmphp_find_resource (bus, start_address, &mem, MEM) < 0) {
1423 err ("cannot find corresponding MEM resource to remove\n");
1424 return -EINVAL;
1425 }
1426 if (mem) {
1427 debug ("mem->start = %x\n", mem->start);
1428
1429 ibmphp_remove_resource(mem);
1430 }
1431 }
1432 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1433 /* takes up another dword */
1434 count += 1;
1435 }
1436 } /* end of mem */
1437 } /* end of for */
66bef8c0 1438 debug ("%s - exiting, returning success\n", __func__);
1da177e4
LT
1439 return 0;
1440}
1441
1442static int unconfigure_boot_card (struct slot *slot_cur)
1443{
1444 u16 vendor_id;
1445 u32 class;
1446 u8 hdr_type;
1447 u8 device;
1448 u8 busno;
1449 u8 function;
1450 int rc;
1451 unsigned int devfn;
1452 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1453
66bef8c0 1454 debug ("%s - enter\n", __func__);
1da177e4
LT
1455
1456 device = slot_cur->device;
1457 busno = slot_cur->bus;
1458
1459 debug ("b4 for loop, device is %x\n", device);
1460 /* For every function on the card */
1461 for (function = 0x0; function < 0x08; function++) {
1462 devfn = PCI_DEVFN(device, function);
1463 ibmphp_pci_bus->number = busno;
1464
1465 pci_bus_read_config_word (ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1466
1467 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1468 /* found correct device!!! */
1469 ++valid_device;
1470
66bef8c0 1471 debug ("%s - found correct device\n", __func__);
1da177e4
LT
1472
1473 /* header: x x x x x x x x
1474 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1475 * |_=> 0 = single function device, 1 = multi-function device
1476 */
1477
1478 pci_bus_read_config_byte (ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1479 pci_bus_read_config_dword (ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1480
1481 debug ("hdr_type %x, class %x\n", hdr_type, class);
1482 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1483 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
227f0647 1484 err ("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device, function);
1da177e4
LT
1485 return -ENODEV;
1486 } else if (class == PCI_CLASS_DISPLAY_VGA) {
227f0647 1487 err ("The device %x function %x is not supported for hot removing. Please choose another device.\n", device, function);
1da177e4
LT
1488 return -ENODEV;
1489 }
1490
1491 switch (hdr_type) {
1492 case PCI_HEADER_TYPE_NORMAL:
1493 rc = unconfigure_boot_device (busno, device, function);
1494 if (rc) {
1495 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1496 device, function, busno);
1497 return rc;
1498 }
1499 function = 0x8;
1500 break;
1501 case PCI_HEADER_TYPE_MULTIDEVICE:
1502 rc = unconfigure_boot_device (busno, device, function);
1503 if (rc) {
1504 err ("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1505 device, function, busno);
1506 return rc;
1507 }
1508 break;
1509 case PCI_HEADER_TYPE_BRIDGE:
1510 class >>= 8;
1511 if (class != PCI_CLASS_BRIDGE_PCI) {
227f0647 1512 err ("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
1da177e4
LT
1513 return -ENODEV;
1514 }
1515 rc = unconfigure_boot_bridge (busno, device, function);
1516 if (rc != 0) {
1517 err ("was not able to hot-remove PPB properly.\n");
1518 return rc;
1519 }
1520
1521 function = 0x8;
1522 break;
1523 case PCI_HEADER_TYPE_MULTIBRIDGE:
1524 class >>= 8;
1525 if (class != PCI_CLASS_BRIDGE_PCI) {
227f0647 1526 err ("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
1da177e4
LT
1527 return -ENODEV;
1528 }
1529 rc = unconfigure_boot_bridge (busno, device, function);
1530 if (rc != 0) {
1531 err ("was not able to hot-remove PPB properly.\n");
1532 return rc;
1533 }
1534 break;
1535 default:
1536 err ("MAJOR PROBLEM!!!! Cannot read device's header\n");
1537 return -1;
1538 break;
1539 } /* end of switch */
1540 } /* end of valid device */
1541 } /* end of for */
1542
1543 if (!valid_device) {
1544 err ("Could not find device to unconfigure. Or could not read the card.\n");
1545 return -1;
1546 }
1547 return 0;
1548}
1549
1550/*
1551 * free the resources of the card (multi, single, or bridged)
1552 * Parameters: slot, flag to say if this is for removing entire module or just
1553 * unconfiguring the device
1554 * TO DO: will probably need to add some code in case there was some resource,
1555 * to remove it... this is from when we have errors in the configure_card...
f7625980
BH
1556 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1557 * Returns: 0, -1, -ENODEV
1da177e4
LT
1558 */
1559int ibmphp_unconfigure_card (struct slot **slot_cur, int the_end)
1560{
1561 int i;
1562 int count;
1563 int rc;
1564 struct slot *sl = *slot_cur;
1565 struct pci_func *cur_func = NULL;
1566 struct pci_func *temp_func;
1567
66bef8c0 1568 debug ("%s - enter\n", __func__);
1da177e4
LT
1569
1570 if (!the_end) {
1571 /* Need to unconfigure the card */
1572 rc = unconfigure_boot_card (sl);
1573 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1574 /* In all other cases, will still need to get rid of func structure if it exists */
1575 return rc;
1576 }
1577 }
1578
1579 if (sl->func) {
1580 cur_func = sl->func;
1581 while (cur_func) {
1582 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1583 if (cur_func->bus) {
1584 /* in other words, it's a PPB */
1585 count = 2;
1586 } else {
1587 count = 6;
1588 }
1589
1590 for (i = 0; i < count; i++) {
1591 if (cur_func->io[i]) {
1592 debug ("io[%d] exists\n", i);
1593 if (the_end > 0)
1594 ibmphp_remove_resource (cur_func->io[i]);
1595 cur_func->io[i] = NULL;
1596 }
1597 if (cur_func->mem[i]) {
1598 debug ("mem[%d] exists\n", i);
1599 if (the_end > 0)
1600 ibmphp_remove_resource (cur_func->mem[i]);
1601 cur_func->mem[i] = NULL;
1602 }
1603 if (cur_func->pfmem[i]) {
1604 debug ("pfmem[%d] exists\n", i);
1605 if (the_end > 0)
1606 ibmphp_remove_resource (cur_func->pfmem[i]);
1607 cur_func->pfmem[i] = NULL;
1608 }
1609 }
1610
1611 temp_func = cur_func->next;
1612 kfree (cur_func);
1613 cur_func = temp_func;
1614 }
1615 }
1616
1617 sl->func = NULL;
1618 *slot_cur = sl;
66bef8c0 1619 debug ("%s - exit\n", __func__);
1da177e4
LT
1620 return 0;
1621}
1622
1623/*
1624 * add a new bus resulting from hot-plugging a PPB bridge with devices
1625 *
1626 * Input: bus and the amount of resources needed (we know we can assign those,
1627 * since they've been checked already
1628 * Output: bus added to the correct spot
f7625980 1629 * 0, -1, error
1da177e4
LT
1630 */
1631static int add_new_bus (struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1632{
1633 struct range_node *io_range = NULL;
1634 struct range_node *mem_range = NULL;
1635 struct range_node *pfmem_range = NULL;
1636 struct bus_node *cur_bus = NULL;
1637
1638 /* Trying to find the parent bus number */
1639 if (parent_busno != 0xFF) {
1640 cur_bus = ibmphp_find_res_bus (parent_busno);
1641 if (!cur_bus) {
1642 err ("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1643 return -ENODEV;
1644 }
f7625980 1645
1da177e4
LT
1646 list_add (&bus->bus_list, &cur_bus->bus_list);
1647 }
1648 if (io) {
f5afe806 1649 io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1da177e4
LT
1650 if (!io_range) {
1651 err ("out of system memory\n");
1652 return -ENOMEM;
1653 }
1da177e4
LT
1654 io_range->start = io->start;
1655 io_range->end = io->end;
1656 io_range->rangeno = 1;
1657 bus->noIORanges = 1;
1658 bus->rangeIO = io_range;
1659 }
1660 if (mem) {
f5afe806 1661 mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1da177e4
LT
1662 if (!mem_range) {
1663 err ("out of system memory\n");
1664 return -ENOMEM;
1665 }
1da177e4
LT
1666 mem_range->start = mem->start;
1667 mem_range->end = mem->end;
1668 mem_range->rangeno = 1;
1669 bus->noMemRanges = 1;
1670 bus->rangeMem = mem_range;
1671 }
1672 if (pfmem) {
f5afe806 1673 pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
f7625980 1674 if (!pfmem_range) {
1da177e4
LT
1675 err ("out of system memory\n");
1676 return -ENOMEM;
1677 }
1da177e4
LT
1678 pfmem_range->start = pfmem->start;
1679 pfmem_range->end = pfmem->end;
1680 pfmem_range->rangeno = 1;
1681 bus->noPFMemRanges = 1;
1682 bus->rangePFMem = pfmem_range;
1683 }
1684 return 0;
1685}
1686
1687/*
1688 * find the 1st available bus number for PPB to set as its secondary bus
1689 * Parameters: bus_number of the primary bus
1690 * Returns: bus_number of the secondary bus or 0xff in case of failure
1691 */
1692static u8 find_sec_number (u8 primary_busno, u8 slotno)
1693{
1694 int min, max;
1695 u8 busno;
1696 struct bus_info *bus;
1697 struct bus_node *bus_cur;
1698
1699 bus = ibmphp_find_same_bus_num (primary_busno);
1700 if (!bus) {
1701 err ("cannot get slot range of the bus from the BIOS\n");
1702 return 0xff;
1703 }
1704 max = bus->slot_max;
1705 min = bus->slot_min;
1706 if ((slotno > max) || (slotno < min)) {
1707 err ("got the wrong range\n");
1708 return 0xff;
1709 }
1710 busno = (u8) (slotno - (u8) min);
1711 busno += primary_busno + 0x01;
1712 bus_cur = ibmphp_find_res_bus (busno);
1713 /* either there is no such bus number, or there are no ranges, which
1714 * can only happen if we removed the bridged device in previous load
1715 * of the driver, and now only have the skeleton bus struct
1716 */
1717 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1718 return busno;
1719 return 0xff;
1720}
This page took 0.834743 seconds and 5 git commands to generate.