blob: 8467d028732514277a098815f3597ebfed1591ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM Hot Plug Controller Driver
3 *
4 * Written By: Tong Yu, IBM Corporation
5 *
6 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001-2003 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/mm.h>
33#include <linux/slab.h>
34#include <linux/pci.h>
35#include <linux/list.h>
36#include <linux/init.h>
37#include "ibmphp.h"
38
39/*
40 * POST builds data blocks(in this data block definition, a char-1
41 * byte, short(or word)-2 byte, long(dword)-4 byte) in the Extended
42 * BIOS Data Area which describe the configuration of the hot-plug
43 * controllers and resources used by the PCI Hot-Plug devices.
44 *
45 * This file walks EBDA, maps data block from physical addr,
46 * reconstruct linked lists about all system resource(MEM, PFM, IO)
47 * already assigned by POST, as well as linked lists about hot plug
48 * controllers (ctlr#, slot#, bus&slot features...)
49 */
50
51/* Global lists */
52LIST_HEAD (ibmphp_ebda_pci_rsrc_head);
53LIST_HEAD (ibmphp_slot_head);
54
55/* Local variables */
56static struct ebda_hpc_list *hpc_list_ptr;
57static struct ebda_rsrc_list *rsrc_list_ptr;
58static struct rio_table_hdr *rio_table_ptr = NULL;
59static LIST_HEAD (ebda_hpc_head);
60static LIST_HEAD (bus_info_head);
61static LIST_HEAD (rio_vg_head);
62static LIST_HEAD (rio_lo_head);
63static LIST_HEAD (opt_vg_head);
64static LIST_HEAD (opt_lo_head);
65static void __iomem *io_mem;
66
67/* Local functions */
68static int ebda_rsrc_controller (void);
69static int ebda_rsrc_rsrc (void);
70static int ebda_rio_table (void);
71
72static struct ebda_hpc_list * __init alloc_ebda_hpc_list (void)
73{
Eric Sesterhennf5afe802006-02-28 15:34:49 +010074 return kzalloc(sizeof(struct ebda_hpc_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static struct controller *alloc_ebda_hpc (u32 slot_count, u32 bus_count)
78{
79 struct controller *controller;
80 struct ebda_hpc_slot *slots;
81 struct ebda_hpc_bus *buses;
82
Eric Sesterhennf5afe802006-02-28 15:34:49 +010083 controller = kzalloc(sizeof(struct controller), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!controller)
85 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Sesterhennf5afe802006-02-28 15:34:49 +010087 slots = kcalloc(slot_count, sizeof(struct ebda_hpc_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!slots)
89 goto error_contr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 controller->slots = slots;
91
Eric Sesterhennf5afe802006-02-28 15:34:49 +010092 buses = kcalloc(bus_count, sizeof(struct ebda_hpc_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!buses)
94 goto error_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 controller->buses = buses;
96
97 return controller;
98error_slots:
99 kfree(controller->slots);
100error_contr:
101 kfree(controller);
102error:
103 return NULL;
104}
105
106static void free_ebda_hpc (struct controller *controller)
107{
108 kfree (controller->slots);
109 kfree (controller->buses);
110 kfree (controller);
111}
112
113static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list (void)
114{
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100115 return kzalloc(sizeof(struct ebda_rsrc_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc (void)
119{
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100120 return kzalloc(sizeof(struct ebda_pci_rsrc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void __init print_bus_info (void)
124{
125 struct bus_info *ptr;
126 struct list_head *ptr1;
127
128 list_for_each (ptr1, &bus_info_head) {
129 ptr = list_entry (ptr1, struct bus_info, bus_info_list);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800130 debug ("%s - slot_min = %x\n", __func__, ptr->slot_min);
131 debug ("%s - slot_max = %x\n", __func__, ptr->slot_max);
132 debug ("%s - slot_count = %x\n", __func__, ptr->slot_count);
133 debug ("%s - bus# = %x\n", __func__, ptr->busno);
134 debug ("%s - current_speed = %x\n", __func__, ptr->current_speed);
135 debug ("%s - controller_id = %x\n", __func__, ptr->controller_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800137 debug ("%s - slots_at_33_conv = %x\n", __func__, ptr->slots_at_33_conv);
138 debug ("%s - slots_at_66_conv = %x\n", __func__, ptr->slots_at_66_conv);
139 debug ("%s - slots_at_66_pcix = %x\n", __func__, ptr->slots_at_66_pcix);
140 debug ("%s - slots_at_100_pcix = %x\n", __func__, ptr->slots_at_100_pcix);
141 debug ("%s - slots_at_133_pcix = %x\n", __func__, ptr->slots_at_133_pcix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 }
144}
145
146static void print_lo_info (void)
147{
148 struct rio_detail *ptr;
149 struct list_head *ptr1;
150 debug ("print_lo_info ----\n");
151 list_for_each (ptr1, &rio_lo_head) {
152 ptr = list_entry (ptr1, struct rio_detail, rio_detail_list);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800153 debug ("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
154 debug ("%s - rio_type = %x\n", __func__, ptr->rio_type);
155 debug ("%s - owner_id = %x\n", __func__, ptr->owner_id);
156 debug ("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
157 debug ("%s - wpindex = %x\n", __func__, ptr->wpindex);
158 debug ("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 }
161}
162
163static void print_vg_info (void)
164{
165 struct rio_detail *ptr;
166 struct list_head *ptr1;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800167 debug ("%s ---\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 list_for_each (ptr1, &rio_vg_head) {
169 ptr = list_entry (ptr1, struct rio_detail, rio_detail_list);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800170 debug ("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
171 debug ("%s - rio_type = %x\n", __func__, ptr->rio_type);
172 debug ("%s - owner_id = %x\n", __func__, ptr->owner_id);
173 debug ("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
174 debug ("%s - wpindex = %x\n", __func__, ptr->wpindex);
175 debug ("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 }
178}
179
180static void __init print_ebda_pci_rsrc (void)
181{
182 struct ebda_pci_rsrc *ptr;
183 struct list_head *ptr1;
184
185 list_for_each (ptr1, &ibmphp_ebda_pci_rsrc_head) {
186 ptr = list_entry (ptr1, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
187 debug ("%s - rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800188 __func__, ptr->rsrc_type ,ptr->bus_num, ptr->dev_fun,ptr->start_addr, ptr->end_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190}
191
192static void __init print_ibm_slot (void)
193{
194 struct slot *ptr;
195 struct list_head *ptr1;
196
197 list_for_each (ptr1, &ibmphp_slot_head) {
198 ptr = list_entry (ptr1, struct slot, ibm_slot_list);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800199 debug ("%s - slot_number: %x\n", __func__, ptr->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201}
202
203static void __init print_opt_vg (void)
204{
205 struct opt_rio *ptr;
206 struct list_head *ptr1;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800207 debug ("%s ---\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 list_for_each (ptr1, &opt_vg_head) {
209 ptr = list_entry (ptr1, struct opt_rio, opt_rio_list);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800210 debug ("%s - rio_type %x\n", __func__, ptr->rio_type);
211 debug ("%s - chassis_num: %x\n", __func__, ptr->chassis_num);
212 debug ("%s - first_slot_num: %x\n", __func__, ptr->first_slot_num);
213 debug ("%s - middle_num: %x\n", __func__, ptr->middle_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
217static void __init print_ebda_hpc (void)
218{
219 struct controller *hpc_ptr;
220 struct list_head *ptr1;
221 u16 index;
222
223 list_for_each (ptr1, &ebda_hpc_head) {
224
225 hpc_ptr = list_entry (ptr1, struct controller, ebda_hpc_list);
226
227 for (index = 0; index < hpc_ptr->slot_count; index++) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800228 debug ("%s - physical slot#: %x\n", __func__, hpc_ptr->slots[index].slot_num);
229 debug ("%s - pci bus# of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_bus_num);
230 debug ("%s - index into ctlr addr: %x\n", __func__, hpc_ptr->slots[index].ctl_index);
231 debug ("%s - cap of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234 for (index = 0; index < hpc_ptr->bus_count; index++) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800235 debug ("%s - bus# of each bus controlled by this ctlr: %x\n", __func__, hpc_ptr->buses[index].bus_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800238 debug ("%s - type of hpc: %x\n", __func__, hpc_ptr->ctlr_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 switch (hpc_ptr->ctlr_type) {
240 case 1:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800241 debug ("%s - bus: %x\n", __func__, hpc_ptr->u.pci_ctlr.bus);
242 debug ("%s - dev_fun: %x\n", __func__, hpc_ptr->u.pci_ctlr.dev_fun);
243 debug ("%s - irq: %x\n", __func__, hpc_ptr->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245
246 case 0:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800247 debug ("%s - io_start: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_start);
248 debug ("%s - io_end: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_end);
249 debug ("%s - irq: %x\n", __func__, hpc_ptr->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 case 2:
253 case 4:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800254 debug ("%s - wpegbbar: %lx\n", __func__, hpc_ptr->u.wpeg_ctlr.wpegbbar);
255 debug ("%s - i2c_addr: %x\n", __func__, hpc_ptr->u.wpeg_ctlr.i2c_addr);
256 debug ("%s - irq: %x\n", __func__, hpc_ptr->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258 }
259 }
260}
261
262int __init ibmphp_access_ebda (void)
263{
264 u8 format, num_ctlrs, rio_complete, hs_complete;
265 u16 ebda_seg, num_entries, next_offset, offset, blk_id, sub_addr, re, rc_id, re_id, base;
266 int rc = 0;
267
268
269 rio_complete = 0;
270 hs_complete = 0;
271
272 io_mem = ioremap ((0x40 << 4) + 0x0e, 2);
273 if (!io_mem )
274 return -ENOMEM;
275 ebda_seg = readw (io_mem);
276 iounmap (io_mem);
277 debug ("returned ebda segment: %x\n", ebda_seg);
278
279 io_mem = ioremap (ebda_seg<<4, 65000);
280 if (!io_mem )
281 return -ENOMEM;
282 next_offset = 0x180;
283
284 for (;;) {
285 offset = next_offset;
286 next_offset = readw (io_mem + offset); /* offset of next blk */
287
288 offset += 2;
289 if (next_offset == 0) /* 0 indicate it's last blk */
290 break;
291 blk_id = readw (io_mem + offset); /* this blk id */
292
293 offset += 2;
294 /* check if it is hot swap block or rio block */
295 if (blk_id != 0x4853 && blk_id != 0x4752)
296 continue;
297 /* found hs table */
298 if (blk_id == 0x4853) {
299 debug ("now enter hot swap block---\n");
300 debug ("hot blk id: %x\n", blk_id);
301 format = readb (io_mem + offset);
302
303 offset += 1;
304 if (format != 4)
305 goto error_nodev;
306 debug ("hot blk format: %x\n", format);
307 /* hot swap sub blk */
308 base = offset;
309
310 sub_addr = base;
311 re = readw (io_mem + sub_addr); /* next sub blk */
312
313 sub_addr += 2;
314 rc_id = readw (io_mem + sub_addr); /* sub blk id */
315
316 sub_addr += 2;
317 if (rc_id != 0x5243)
318 goto error_nodev;
319 /* rc sub blk signature */
320 num_ctlrs = readb (io_mem + sub_addr);
321
322 sub_addr += 1;
323 hpc_list_ptr = alloc_ebda_hpc_list ();
324 if (!hpc_list_ptr) {
325 rc = -ENOMEM;
326 goto out;
327 }
328 hpc_list_ptr->format = format;
329 hpc_list_ptr->num_ctlrs = num_ctlrs;
330 hpc_list_ptr->phys_addr = sub_addr; /* offset of RSRC_CONTROLLER blk */
331 debug ("info about hpc descriptor---\n");
332 debug ("hot blk format: %x\n", format);
333 debug ("num of controller: %x\n", num_ctlrs);
334 debug ("offset of hpc data structure enteries: %x\n ", sub_addr);
335
336 sub_addr = base + re; /* re sub blk */
337 /* FIXME: rc is never used/checked */
338 rc = readw (io_mem + sub_addr); /* next sub blk */
339
340 sub_addr += 2;
341 re_id = readw (io_mem + sub_addr); /* sub blk id */
342
343 sub_addr += 2;
344 if (re_id != 0x5245)
345 goto error_nodev;
346
347 /* signature of re */
348 num_entries = readw (io_mem + sub_addr);
349
350 sub_addr += 2; /* offset of RSRC_ENTRIES blk */
351 rsrc_list_ptr = alloc_ebda_rsrc_list ();
352 if (!rsrc_list_ptr ) {
353 rc = -ENOMEM;
354 goto out;
355 }
356 rsrc_list_ptr->format = format;
357 rsrc_list_ptr->num_entries = num_entries;
358 rsrc_list_ptr->phys_addr = sub_addr;
359
360 debug ("info about rsrc descriptor---\n");
361 debug ("format: %x\n", format);
362 debug ("num of rsrc: %x\n", num_entries);
363 debug ("offset of rsrc data structure enteries: %x\n ", sub_addr);
364
365 hs_complete = 1;
366 } else {
367 /* found rio table, blk_id == 0x4752 */
368 debug ("now enter io table ---\n");
369 debug ("rio blk id: %x\n", blk_id);
370
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100371 rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!rio_table_ptr)
373 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rio_table_ptr->ver_num = readb (io_mem + offset);
375 rio_table_ptr->scal_count = readb (io_mem + offset + 1);
376 rio_table_ptr->riodev_count = readb (io_mem + offset + 2);
377 rio_table_ptr->offset = offset +3 ;
378
379 debug("info about rio table hdr ---\n");
380 debug("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ",
381 rio_table_ptr->ver_num, rio_table_ptr->scal_count,
382 rio_table_ptr->riodev_count, rio_table_ptr->offset);
383
384 rio_complete = 1;
385 }
386 }
387
388 if (!hs_complete && !rio_complete)
389 goto error_nodev;
390
391 if (rio_table_ptr) {
392 if (rio_complete && rio_table_ptr->ver_num == 3) {
393 rc = ebda_rio_table ();
394 if (rc)
395 goto out;
396 }
397 }
398 rc = ebda_rsrc_controller ();
399 if (rc)
400 goto out;
401
402 rc = ebda_rsrc_rsrc ();
403 goto out;
404error_nodev:
405 rc = -ENODEV;
406out:
407 iounmap (io_mem);
408 return rc;
409}
410
411/*
412 * map info of scalability details and rio details from physical address
413 */
414static int __init ebda_rio_table (void)
415{
416 u16 offset;
417 u8 i;
418 struct rio_detail *rio_detail_ptr;
419
420 offset = rio_table_ptr->offset;
421 offset += 12 * rio_table_ptr->scal_count;
422
423 // we do concern about rio details
424 for (i = 0; i < rio_table_ptr->riodev_count; i++) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100425 rio_detail_ptr = kzalloc(sizeof(struct rio_detail), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!rio_detail_ptr)
427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 rio_detail_ptr->rio_node_id = readb (io_mem + offset);
429 rio_detail_ptr->bbar = readl (io_mem + offset + 1);
430 rio_detail_ptr->rio_type = readb (io_mem + offset + 5);
431 rio_detail_ptr->owner_id = readb (io_mem + offset + 6);
432 rio_detail_ptr->port0_node_connect = readb (io_mem + offset + 7);
433 rio_detail_ptr->port0_port_connect = readb (io_mem + offset + 8);
434 rio_detail_ptr->port1_node_connect = readb (io_mem + offset + 9);
435 rio_detail_ptr->port1_port_connect = readb (io_mem + offset + 10);
436 rio_detail_ptr->first_slot_num = readb (io_mem + offset + 11);
437 rio_detail_ptr->status = readb (io_mem + offset + 12);
438 rio_detail_ptr->wpindex = readb (io_mem + offset + 13);
439 rio_detail_ptr->chassis_num = readb (io_mem + offset + 14);
440// debug ("rio_node_id: %x\nbbar: %x\nrio_type: %x\nowner_id: %x\nport0_node: %x\nport0_port: %x\nport1_node: %x\nport1_port: %x\nfirst_slot_num: %x\nstatus: %x\n", rio_detail_ptr->rio_node_id, rio_detail_ptr->bbar, rio_detail_ptr->rio_type, rio_detail_ptr->owner_id, rio_detail_ptr->port0_node_connect, rio_detail_ptr->port0_port_connect, rio_detail_ptr->port1_node_connect, rio_detail_ptr->port1_port_connect, rio_detail_ptr->first_slot_num, rio_detail_ptr->status);
441 //create linked list of chassis
442 if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5)
443 list_add (&rio_detail_ptr->rio_detail_list, &rio_vg_head);
444 //create linked list of expansion box
445 else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7)
446 list_add (&rio_detail_ptr->rio_detail_list, &rio_lo_head);
447 else
448 // not in my concern
449 kfree (rio_detail_ptr);
450 offset += 15;
451 }
452 print_lo_info ();
453 print_vg_info ();
454 return 0;
455}
456
457/*
458 * reorganizing linked list of chassis
459 */
460static struct opt_rio *search_opt_vg (u8 chassis_num)
461{
462 struct opt_rio *ptr;
463 struct list_head *ptr1;
464 list_for_each (ptr1, &opt_vg_head) {
465 ptr = list_entry (ptr1, struct opt_rio, opt_rio_list);
466 if (ptr->chassis_num == chassis_num)
467 return ptr;
468 }
469 return NULL;
470}
471
472static int __init combine_wpg_for_chassis (void)
473{
474 struct opt_rio *opt_rio_ptr = NULL;
475 struct rio_detail *rio_detail_ptr = NULL;
476 struct list_head *list_head_ptr = NULL;
477
478 list_for_each (list_head_ptr, &rio_vg_head) {
479 rio_detail_ptr = list_entry (list_head_ptr, struct rio_detail, rio_detail_list);
480 opt_rio_ptr = search_opt_vg (rio_detail_ptr->chassis_num);
481 if (!opt_rio_ptr) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100482 opt_rio_ptr = kzalloc(sizeof(struct opt_rio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!opt_rio_ptr)
484 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
486 opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
487 opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
488 opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
489 list_add (&opt_rio_ptr->opt_rio_list, &opt_vg_head);
490 } else {
491 opt_rio_ptr->first_slot_num = min (opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
492 opt_rio_ptr->middle_num = max (opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
493 }
494 }
495 print_opt_vg ();
496 return 0;
497}
498
499/*
500 * reorgnizing linked list of expansion box
501 */
502static struct opt_rio_lo *search_opt_lo (u8 chassis_num)
503{
504 struct opt_rio_lo *ptr;
505 struct list_head *ptr1;
506 list_for_each (ptr1, &opt_lo_head) {
507 ptr = list_entry (ptr1, struct opt_rio_lo, opt_rio_lo_list);
508 if (ptr->chassis_num == chassis_num)
509 return ptr;
510 }
511 return NULL;
512}
513
514static int combine_wpg_for_expansion (void)
515{
516 struct opt_rio_lo *opt_rio_lo_ptr = NULL;
517 struct rio_detail *rio_detail_ptr = NULL;
518 struct list_head *list_head_ptr = NULL;
519
520 list_for_each (list_head_ptr, &rio_lo_head) {
521 rio_detail_ptr = list_entry (list_head_ptr, struct rio_detail, rio_detail_list);
522 opt_rio_lo_ptr = search_opt_lo (rio_detail_ptr->chassis_num);
523 if (!opt_rio_lo_ptr) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100524 opt_rio_lo_ptr = kzalloc(sizeof(struct opt_rio_lo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!opt_rio_lo_ptr)
526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
528 opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
529 opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
530 opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
531 opt_rio_lo_ptr->pack_count = 1;
532
533 list_add (&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
534 } else {
535 opt_rio_lo_ptr->first_slot_num = min (opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
536 opt_rio_lo_ptr->middle_num = max (opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
537 opt_rio_lo_ptr->pack_count = 2;
538 }
539 }
540 return 0;
541}
542
543
544/* Since we don't know the max slot number per each chassis, hence go
545 * through the list of all chassis to find out the range
546 * Arguments: slot_num, 1st slot number of the chassis we think we are on,
547 * var (0 = chassis, 1 = expansion box)
548 */
549static int first_slot_num (u8 slot_num, u8 first_slot, u8 var)
550{
551 struct opt_rio *opt_vg_ptr = NULL;
552 struct opt_rio_lo *opt_lo_ptr = NULL;
553 struct list_head *ptr = NULL;
554 int rc = 0;
555
556 if (!var) {
557 list_for_each (ptr, &opt_vg_head) {
558 opt_vg_ptr = list_entry (ptr, struct opt_rio, opt_rio_list);
559 if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) {
560 rc = -ENODEV;
561 break;
562 }
563 }
564 } else {
565 list_for_each (ptr, &opt_lo_head) {
566 opt_lo_ptr = list_entry (ptr, struct opt_rio_lo, opt_rio_lo_list);
567 if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
568 rc = -ENODEV;
569 break;
570 }
571 }
572 }
573 return rc;
574}
575
576static struct opt_rio_lo * find_rxe_num (u8 slot_num)
577{
578 struct opt_rio_lo *opt_lo_ptr;
579 struct list_head *ptr;
580
581 list_for_each (ptr, &opt_lo_head) {
582 opt_lo_ptr = list_entry (ptr, struct opt_rio_lo, opt_rio_lo_list);
583 //check to see if this slot_num belongs to expansion box
584 if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_lo_ptr->first_slot_num, 1)))
585 return opt_lo_ptr;
586 }
587 return NULL;
588}
589
590static struct opt_rio * find_chassis_num (u8 slot_num)
591{
592 struct opt_rio *opt_vg_ptr;
593 struct list_head *ptr;
594
595 list_for_each (ptr, &opt_vg_head) {
596 opt_vg_ptr = list_entry (ptr, struct opt_rio, opt_rio_list);
597 //check to see if this slot_num belongs to chassis
598 if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_vg_ptr->first_slot_num, 0)))
599 return opt_vg_ptr;
600 }
601 return NULL;
602}
603
604/* This routine will find out how many slots are in the chassis, so that
605 * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
606 */
607static u8 calculate_first_slot (u8 slot_num)
608{
609 u8 first_slot = 1;
610 struct list_head * list;
611 struct slot * slot_cur;
612
613 list_for_each (list, &ibmphp_slot_head) {
614 slot_cur = list_entry (list, struct slot, ibm_slot_list);
615 if (slot_cur->ctrl) {
616 if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num))
617 first_slot = slot_cur->ctrl->ending_slot_num;
618 }
619 }
620 return first_slot + 1;
621
622}
623static char *create_file_name (struct slot * slot_cur)
624{
625 struct opt_rio *opt_vg_ptr = NULL;
626 struct opt_rio_lo *opt_lo_ptr = NULL;
627 static char str[30];
628 int which = 0; /* rxe = 1, chassis = 0 */
629 u8 number = 1; /* either chassis or rxe # */
630 u8 first_slot = 1;
631 u8 slot_num;
632 u8 flag = 0;
633
634 if (!slot_cur) {
635 err ("Structure passed is empty\n");
636 return NULL;
637 }
638
639 slot_num = slot_cur->number;
640
641 memset (str, 0, sizeof(str));
642
643 if (rio_table_ptr) {
644 if (rio_table_ptr->ver_num == 3) {
645 opt_vg_ptr = find_chassis_num (slot_num);
646 opt_lo_ptr = find_rxe_num (slot_num);
647 }
648 }
649 if (opt_vg_ptr) {
650 if (opt_lo_ptr) {
651 if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
652 number = opt_lo_ptr->chassis_num;
653 first_slot = opt_lo_ptr->first_slot_num;
654 which = 1; /* it is RXE */
655 } else {
656 first_slot = opt_vg_ptr->first_slot_num;
657 number = opt_vg_ptr->chassis_num;
658 which = 0;
659 }
660 } else {
661 first_slot = opt_vg_ptr->first_slot_num;
662 number = opt_vg_ptr->chassis_num;
663 which = 0;
664 }
665 ++flag;
666 } else if (opt_lo_ptr) {
667 number = opt_lo_ptr->chassis_num;
668 first_slot = opt_lo_ptr->first_slot_num;
669 which = 1;
670 ++flag;
671 } else if (rio_table_ptr) {
672 if (rio_table_ptr->ver_num == 3) {
673 /* if both NULL and we DO have correct RIO table in BIOS */
674 return NULL;
675 }
676 }
677 if (!flag) {
678 if (slot_cur->ctrl->ctlr_type == 4) {
679 first_slot = calculate_first_slot (slot_num);
680 which = 1;
681 } else {
682 which = 0;
683 }
684 }
685
686 sprintf(str, "%s%dslot%d",
687 which == 0 ? "chassis" : "rxe",
688 number, slot_num - first_slot + 1);
689 return str;
690}
691
692static int fillslotinfo(struct hotplug_slot *hotplug_slot)
693{
694 struct slot *slot;
695 int rc = 0;
696
697 if (!hotplug_slot || !hotplug_slot->private)
698 return -EINVAL;
699
700 slot = hotplug_slot->private;
701 rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
702 if (rc)
703 return rc;
704
705 // power - enabled:1 not:0
706 hotplug_slot->info->power_status = SLOT_POWER(slot->status);
707
708 // attention - off:0, on:1, blinking:2
709 hotplug_slot->info->attention_status = SLOT_ATTN(slot->status, slot->ext_status);
710
711 // latch - open:1 closed:0
712 hotplug_slot->info->latch_status = SLOT_LATCH(slot->status);
713
714 // pci board - present:1 not:0
715 if (SLOT_PRESENT (slot->status))
716 hotplug_slot->info->adapter_status = 1;
717 else
718 hotplug_slot->info->adapter_status = 0;
719/*
720 if (slot->bus_on->supported_bus_mode
721 && (slot->bus_on->supported_speed == BUS_SPEED_66))
722 hotplug_slot->info->max_bus_speed_status = BUS_SPEED_66PCIX;
723 else
724 hotplug_slot->info->max_bus_speed_status = slot->bus_on->supported_speed;
725*/
726
727 return rc;
728}
729
730static void release_slot(struct hotplug_slot *hotplug_slot)
731{
732 struct slot *slot;
733
734 if (!hotplug_slot || !hotplug_slot->private)
735 return;
736
737 slot = hotplug_slot->private;
738 kfree(slot->hotplug_slot->info);
739 kfree(slot->hotplug_slot->name);
740 kfree(slot->hotplug_slot);
741 slot->ctrl = NULL;
742 slot->bus_on = NULL;
743
744 /* we don't want to actually remove the resources, since free_resources will do just that */
745 ibmphp_unconfigure_card(&slot, -1);
746
747 kfree (slot);
748}
749
750static struct pci_driver ibmphp_driver;
751
752/*
753 * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
754 * each hpc from physical address to a list of hot plug controllers based on
755 * hpc descriptors.
756 */
757static int __init ebda_rsrc_controller (void)
758{
759 u16 addr, addr_slot, addr_bus;
760 u8 ctlr_id, temp, bus_index;
761 u16 ctlr, slot, bus;
762 u16 slot_num, bus_num, index;
763 struct hotplug_slot *hp_slot_ptr;
764 struct controller *hpc_ptr;
765 struct ebda_hpc_bus *bus_ptr;
766 struct ebda_hpc_slot *slot_ptr;
767 struct bus_info *bus_info_ptr1, *bus_info_ptr2;
768 int rc;
769 struct slot *tmp_slot;
770 struct list_head *list;
771
772 addr = hpc_list_ptr->phys_addr;
773 for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
774 bus_index = 1;
775 ctlr_id = readb (io_mem + addr);
776 addr += 1;
777 slot_num = readb (io_mem + addr);
778
779 addr += 1;
780 addr_slot = addr; /* offset of slot structure */
781 addr += (slot_num * 4);
782
783 bus_num = readb (io_mem + addr);
784
785 addr += 1;
786 addr_bus = addr; /* offset of bus */
787 addr += (bus_num * 9); /* offset of ctlr_type */
788 temp = readb (io_mem + addr);
789
790 addr += 1;
791 /* init hpc structure */
792 hpc_ptr = alloc_ebda_hpc (slot_num, bus_num);
793 if (!hpc_ptr ) {
794 rc = -ENOMEM;
795 goto error_no_hpc;
796 }
797 hpc_ptr->ctlr_id = ctlr_id;
798 hpc_ptr->ctlr_relative_id = ctlr;
799 hpc_ptr->slot_count = slot_num;
800 hpc_ptr->bus_count = bus_num;
801 debug ("now enter ctlr data struture ---\n");
802 debug ("ctlr id: %x\n", ctlr_id);
803 debug ("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
804 debug ("count of slots controlled by this ctlr: %x\n", slot_num);
805 debug ("count of buses controlled by this ctlr: %x\n", bus_num);
806
807 /* init slot structure, fetch slot, bus, cap... */
808 slot_ptr = hpc_ptr->slots;
809 for (slot = 0; slot < slot_num; slot++) {
810 slot_ptr->slot_num = readb (io_mem + addr_slot);
811 slot_ptr->slot_bus_num = readb (io_mem + addr_slot + slot_num);
812 slot_ptr->ctl_index = readb (io_mem + addr_slot + 2*slot_num);
813 slot_ptr->slot_cap = readb (io_mem + addr_slot + 3*slot_num);
814
815 // create bus_info lined list --- if only one slot per bus: slot_min = slot_max
816
817 bus_info_ptr2 = ibmphp_find_same_bus_num (slot_ptr->slot_bus_num);
818 if (!bus_info_ptr2) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100819 bus_info_ptr1 = kzalloc(sizeof(struct bus_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!bus_info_ptr1) {
821 rc = -ENOMEM;
822 goto error_no_hp_slot;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 bus_info_ptr1->slot_min = slot_ptr->slot_num;
825 bus_info_ptr1->slot_max = slot_ptr->slot_num;
826 bus_info_ptr1->slot_count += 1;
827 bus_info_ptr1->busno = slot_ptr->slot_bus_num;
828 bus_info_ptr1->index = bus_index++;
829 bus_info_ptr1->current_speed = 0xff;
830 bus_info_ptr1->current_bus_mode = 0xff;
831
832 bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
833
834 list_add_tail (&bus_info_ptr1->bus_info_list, &bus_info_head);
835
836 } else {
837 bus_info_ptr2->slot_min = min (bus_info_ptr2->slot_min, slot_ptr->slot_num);
838 bus_info_ptr2->slot_max = max (bus_info_ptr2->slot_max, slot_ptr->slot_num);
839 bus_info_ptr2->slot_count += 1;
840
841 }
842
843 // end of creating the bus_info linked list
844
845 slot_ptr++;
846 addr_slot += 1;
847 }
848
849 /* init bus structure */
850 bus_ptr = hpc_ptr->buses;
851 for (bus = 0; bus < bus_num; bus++) {
852 bus_ptr->bus_num = readb (io_mem + addr_bus + bus);
853 bus_ptr->slots_at_33_conv = readb (io_mem + addr_bus + bus_num + 8 * bus);
854 bus_ptr->slots_at_66_conv = readb (io_mem + addr_bus + bus_num + 8 * bus + 1);
855
856 bus_ptr->slots_at_66_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 2);
857
858 bus_ptr->slots_at_100_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 3);
859
860 bus_ptr->slots_at_133_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 4);
861
862 bus_info_ptr2 = ibmphp_find_same_bus_num (bus_ptr->bus_num);
863 if (bus_info_ptr2) {
864 bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
865 bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
866 bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
867 bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
868 bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix;
869 }
870 bus_ptr++;
871 }
872
873 hpc_ptr->ctlr_type = temp;
874
875 switch (hpc_ptr->ctlr_type) {
876 case 1:
877 hpc_ptr->u.pci_ctlr.bus = readb (io_mem + addr);
878 hpc_ptr->u.pci_ctlr.dev_fun = readb (io_mem + addr + 1);
879 hpc_ptr->irq = readb (io_mem + addr + 2);
880 addr += 3;
881 debug ("ctrl bus = %x, ctlr devfun = %x, irq = %x\n",
882 hpc_ptr->u.pci_ctlr.bus,
883 hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
884 break;
885
886 case 0:
887 hpc_ptr->u.isa_ctlr.io_start = readw (io_mem + addr);
888 hpc_ptr->u.isa_ctlr.io_end = readw (io_mem + addr + 2);
889 if (!request_region (hpc_ptr->u.isa_ctlr.io_start,
890 (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
891 "ibmphp")) {
892 rc = -ENODEV;
893 goto error_no_hp_slot;
894 }
895 hpc_ptr->irq = readb (io_mem + addr + 4);
896 addr += 5;
897 break;
898
899 case 2:
900 case 4:
901 hpc_ptr->u.wpeg_ctlr.wpegbbar = readl (io_mem + addr);
902 hpc_ptr->u.wpeg_ctlr.i2c_addr = readb (io_mem + addr + 4);
903 hpc_ptr->irq = readb (io_mem + addr + 5);
904 addr += 6;
905 break;
906 default:
907 rc = -ENODEV;
908 goto error_no_hp_slot;
909 }
910
911 //reorganize chassis' linked list
912 combine_wpg_for_chassis ();
913 combine_wpg_for_expansion ();
914 hpc_ptr->revision = 0xff;
915 hpc_ptr->options = 0xff;
916 hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
917 hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;
918
919 // register slots with hpc core as well as create linked list of ibm slot
920 for (index = 0; index < hpc_ptr->slot_count; index++) {
921
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100922 hp_slot_ptr = kzalloc(sizeof(*hp_slot_ptr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!hp_slot_ptr) {
924 rc = -ENOMEM;
925 goto error_no_hp_slot;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100928 hp_slot_ptr->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!hp_slot_ptr->info) {
930 rc = -ENOMEM;
931 goto error_no_hp_info;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 hp_slot_ptr->name = kmalloc(30, GFP_KERNEL);
935 if (!hp_slot_ptr->name) {
936 rc = -ENOMEM;
937 goto error_no_hp_name;
938 }
939
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100940 tmp_slot = kzalloc(sizeof(*tmp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!tmp_slot) {
942 rc = -ENOMEM;
943 goto error_no_slot;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Kristen Accardidc6712d2006-03-14 16:24:47 -0800946 tmp_slot->flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
949 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
950 tmp_slot->supported_speed = 3;
951 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
952 tmp_slot->supported_speed = 2;
953 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
954 tmp_slot->supported_speed = 1;
955
956 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
957 tmp_slot->supported_bus_mode = 1;
958 else
959 tmp_slot->supported_bus_mode = 0;
960
961
962 tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;
963
964 bus_info_ptr1 = ibmphp_find_same_bus_num (hpc_ptr->slots[index].slot_bus_num);
965 if (!bus_info_ptr1) {
Jesper Juhlb91aac22008-03-08 02:16:07 +0100966 kfree(tmp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 rc = -ENODEV;
968 goto error;
969 }
970 tmp_slot->bus_on = bus_info_ptr1;
971 bus_info_ptr1 = NULL;
972 tmp_slot->ctrl = hpc_ptr;
973
974 tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
975 tmp_slot->number = hpc_ptr->slots[index].slot_num;
976 tmp_slot->hotplug_slot = hp_slot_ptr;
977
978 hp_slot_ptr->private = tmp_slot;
979 hp_slot_ptr->release = release_slot;
980
981 rc = fillslotinfo(hp_slot_ptr);
982 if (rc)
983 goto error;
984
985 rc = ibmphp_init_devno ((struct slot **) &hp_slot_ptr->private);
986 if (rc)
987 goto error;
988 hp_slot_ptr->ops = &ibmphp_hotplug_slot_ops;
989
990 // end of registering ibm slot with hotplug core
991
992 list_add (& ((struct slot *)(hp_slot_ptr->private))->ibm_slot_list, &ibmphp_slot_head);
993 }
994
995 print_bus_info ();
996 list_add (&hpc_ptr->ebda_hpc_list, &ebda_hpc_head );
997
998 } /* each hpc */
999
1000 list_for_each (list, &ibmphp_slot_head) {
1001 tmp_slot = list_entry (list, struct slot, ibm_slot_list);
1002
1003 snprintf (tmp_slot->hotplug_slot->name, 30, "%s", create_file_name (tmp_slot));
Alex Chiangf46753c2008-06-10 15:28:50 -06001004 pci_hp_register(tmp_slot->hotplug_slot,
1005 pci_find_bus(0, tmp_slot->bus), tmp_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
1008 print_ebda_hpc ();
1009 print_ibm_slot ();
1010 return 0;
1011
1012error:
1013 kfree (hp_slot_ptr->private);
1014error_no_slot:
1015 kfree (hp_slot_ptr->name);
1016error_no_hp_name:
1017 kfree (hp_slot_ptr->info);
1018error_no_hp_info:
1019 kfree (hp_slot_ptr);
1020error_no_hp_slot:
1021 free_ebda_hpc (hpc_ptr);
1022error_no_hpc:
1023 iounmap (io_mem);
1024 return rc;
1025}
1026
1027/*
1028 * map info (bus, devfun, start addr, end addr..) of i/o, memory,
1029 * pfm from the physical addr to a list of resource.
1030 */
1031static int __init ebda_rsrc_rsrc (void)
1032{
1033 u16 addr;
1034 short rsrc;
1035 u8 type, rsrc_type;
1036 struct ebda_pci_rsrc *rsrc_ptr;
1037
1038 addr = rsrc_list_ptr->phys_addr;
1039 debug ("now entering rsrc land\n");
1040 debug ("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
1041
1042 for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
1043 type = readb (io_mem + addr);
1044
1045 addr += 1;
1046 rsrc_type = type & EBDA_RSRC_TYPE_MASK;
1047
1048 if (rsrc_type == EBDA_IO_RSRC_TYPE) {
1049 rsrc_ptr = alloc_ebda_pci_rsrc ();
1050 if (!rsrc_ptr) {
1051 iounmap (io_mem);
1052 return -ENOMEM;
1053 }
1054 rsrc_ptr->rsrc_type = type;
1055
1056 rsrc_ptr->bus_num = readb (io_mem + addr);
1057 rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1058 rsrc_ptr->start_addr = readw (io_mem + addr + 2);
1059 rsrc_ptr->end_addr = readw (io_mem + addr + 4);
1060 addr += 6;
1061
1062 debug ("rsrc from io type ----\n");
1063 debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1064 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1065
1066 list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1067 }
1068
1069 if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
1070 rsrc_ptr = alloc_ebda_pci_rsrc ();
1071 if (!rsrc_ptr ) {
1072 iounmap (io_mem);
1073 return -ENOMEM;
1074 }
1075 rsrc_ptr->rsrc_type = type;
1076
1077 rsrc_ptr->bus_num = readb (io_mem + addr);
1078 rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1079 rsrc_ptr->start_addr = readl (io_mem + addr + 2);
1080 rsrc_ptr->end_addr = readl (io_mem + addr + 6);
1081 addr += 10;
1082
1083 debug ("rsrc from mem or pfm ---\n");
1084 debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1085 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1086
1087 list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1088 }
1089 }
1090 kfree (rsrc_list_ptr);
1091 rsrc_list_ptr = NULL;
1092 print_ebda_pci_rsrc ();
1093 return 0;
1094}
1095
1096u16 ibmphp_get_total_controllers (void)
1097{
1098 return hpc_list_ptr->num_ctlrs;
1099}
1100
1101struct slot *ibmphp_get_slot_from_physical_num (u8 physical_num)
1102{
1103 struct slot *slot;
1104 struct list_head *list;
1105
1106 list_for_each (list, &ibmphp_slot_head) {
1107 slot = list_entry (list, struct slot, ibm_slot_list);
1108 if (slot->number == physical_num)
1109 return slot;
1110 }
1111 return NULL;
1112}
1113
1114/* To find:
1115 * - the smallest slot number
1116 * - the largest slot number
1117 * - the total number of the slots based on each bus
1118 * (if only one slot per bus slot_min = slot_max )
1119 */
1120struct bus_info *ibmphp_find_same_bus_num (u32 num)
1121{
1122 struct bus_info *ptr;
1123 struct list_head *ptr1;
1124
1125 list_for_each (ptr1, &bus_info_head) {
1126 ptr = list_entry (ptr1, struct bus_info, bus_info_list);
1127 if (ptr->busno == num)
1128 return ptr;
1129 }
1130 return NULL;
1131}
1132
1133/* Finding relative bus number, in order to map corresponding
1134 * bus register
1135 */
1136int ibmphp_get_bus_index (u8 num)
1137{
1138 struct bus_info *ptr;
1139 struct list_head *ptr1;
1140
1141 list_for_each (ptr1, &bus_info_head) {
1142 ptr = list_entry (ptr1, struct bus_info, bus_info_list);
1143 if (ptr->busno == num)
1144 return ptr->index;
1145 }
1146 return -ENODEV;
1147}
1148
1149void ibmphp_free_bus_info_queue (void)
1150{
1151 struct bus_info *bus_info;
1152 struct list_head *list;
1153 struct list_head *next;
1154
1155 list_for_each_safe (list, next, &bus_info_head ) {
1156 bus_info = list_entry (list, struct bus_info, bus_info_list);
1157 kfree (bus_info);
1158 }
1159}
1160
1161void ibmphp_free_ebda_hpc_queue (void)
1162{
1163 struct controller *controller = NULL;
1164 struct list_head *list;
1165 struct list_head *next;
1166 int pci_flag = 0;
1167
1168 list_for_each_safe (list, next, &ebda_hpc_head) {
1169 controller = list_entry (list, struct controller, ebda_hpc_list);
1170 if (controller->ctlr_type == 0)
1171 release_region (controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
1172 else if ((controller->ctlr_type == 1) && (!pci_flag)) {
1173 ++pci_flag;
1174 pci_unregister_driver (&ibmphp_driver);
1175 }
1176 free_ebda_hpc (controller);
1177 }
1178}
1179
1180void ibmphp_free_ebda_pci_rsrc_queue (void)
1181{
1182 struct ebda_pci_rsrc *resource;
1183 struct list_head *list;
1184 struct list_head *next;
1185
1186 list_for_each_safe (list, next, &ibmphp_ebda_pci_rsrc_head) {
1187 resource = list_entry (list, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
1188 kfree (resource);
1189 resource = NULL;
1190 }
1191}
1192
1193static struct pci_device_id id_table[] = {
1194 {
1195 .vendor = PCI_VENDOR_ID_IBM,
1196 .device = HPC_DEVICE_ID,
1197 .subvendor = PCI_VENDOR_ID_IBM,
1198 .subdevice = HPC_SUBSYSTEM_ID,
1199 .class = ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1200 }, {}
1201};
1202
1203MODULE_DEVICE_TABLE(pci, id_table);
1204
1205static int ibmphp_probe (struct pci_dev *, const struct pci_device_id *);
1206static struct pci_driver ibmphp_driver = {
1207 .name = "ibmphp",
1208 .id_table = id_table,
1209 .probe = ibmphp_probe,
1210};
1211
1212int ibmphp_register_pci (void)
1213{
1214 struct controller *ctrl;
1215 struct list_head *tmp;
1216 int rc = 0;
1217
1218 list_for_each (tmp, &ebda_hpc_head) {
1219 ctrl = list_entry (tmp, struct controller, ebda_hpc_list);
1220 if (ctrl->ctlr_type == 1) {
1221 rc = pci_register_driver(&ibmphp_driver);
1222 break;
1223 }
1224 }
1225 return rc;
1226}
1227static int ibmphp_probe (struct pci_dev * dev, const struct pci_device_id *ids)
1228{
1229 struct controller *ctrl;
1230 struct list_head *tmp;
1231
1232 debug ("inside ibmphp_probe\n");
1233
1234 list_for_each (tmp, &ebda_hpc_head) {
1235 ctrl = list_entry (tmp, struct controller, ebda_hpc_list);
1236 if (ctrl->ctlr_type == 1) {
1237 if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
1238 ctrl->ctrl_dev = dev;
1239 debug ("found device!!!\n");
1240 debug ("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
1241 return 0;
1242 }
1243 }
1244 }
1245 return -ENODEV;
1246}
1247