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