staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / pci / hotplug / ibmphp_ebda.c
blob6f8e90e3ec08ce4d41c1477dd92b7583ea35ecf6
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * IBM Hot Plug Controller Driver
5 * Written By: Tong Yu, IBM Corporation
7 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
8 * Copyright (C) 2001-2003 IBM Corp.
10 * All rights reserved.
12 * Send feedback to <gregkh@us.ibm.com>
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/mm.h>
19 #include <linux/slab.h>
20 #include <linux/pci.h>
21 #include <linux/list.h>
22 #include <linux/init.h>
23 #include "ibmphp.h"
26 * POST builds data blocks(in this data block definition, a char-1
27 * byte, short(or word)-2 byte, long(dword)-4 byte) in the Extended
28 * BIOS Data Area which describe the configuration of the hot-plug
29 * controllers and resources used by the PCI Hot-Plug devices.
31 * This file walks EBDA, maps data block from physical addr,
32 * reconstruct linked lists about all system resource(MEM, PFM, IO)
33 * already assigned by POST, as well as linked lists about hot plug
34 * controllers (ctlr#, slot#, bus&slot features...)
37 /* Global lists */
38 LIST_HEAD(ibmphp_ebda_pci_rsrc_head);
39 LIST_HEAD(ibmphp_slot_head);
41 /* Local variables */
42 static struct ebda_hpc_list *hpc_list_ptr;
43 static struct ebda_rsrc_list *rsrc_list_ptr;
44 static struct rio_table_hdr *rio_table_ptr = NULL;
45 static LIST_HEAD(ebda_hpc_head);
46 static LIST_HEAD(bus_info_head);
47 static LIST_HEAD(rio_vg_head);
48 static LIST_HEAD(rio_lo_head);
49 static LIST_HEAD(opt_vg_head);
50 static LIST_HEAD(opt_lo_head);
51 static void __iomem *io_mem;
53 /* Local functions */
54 static int ebda_rsrc_controller(void);
55 static int ebda_rsrc_rsrc(void);
56 static int ebda_rio_table(void);
58 static struct ebda_hpc_list * __init alloc_ebda_hpc_list(void)
60 return kzalloc(sizeof(struct ebda_hpc_list), GFP_KERNEL);
63 static struct controller *alloc_ebda_hpc(u32 slot_count, u32 bus_count)
65 struct controller *controller;
66 struct ebda_hpc_slot *slots;
67 struct ebda_hpc_bus *buses;
69 controller = kzalloc(sizeof(struct controller), GFP_KERNEL);
70 if (!controller)
71 goto error;
73 slots = kcalloc(slot_count, sizeof(struct ebda_hpc_slot), GFP_KERNEL);
74 if (!slots)
75 goto error_contr;
76 controller->slots = slots;
78 buses = kcalloc(bus_count, sizeof(struct ebda_hpc_bus), GFP_KERNEL);
79 if (!buses)
80 goto error_slots;
81 controller->buses = buses;
83 return controller;
84 error_slots:
85 kfree(controller->slots);
86 error_contr:
87 kfree(controller);
88 error:
89 return NULL;
92 static void free_ebda_hpc(struct controller *controller)
94 kfree(controller->slots);
95 kfree(controller->buses);
96 kfree(controller);
99 static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list(void)
101 return kzalloc(sizeof(struct ebda_rsrc_list), GFP_KERNEL);
104 static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc(void)
106 return kzalloc(sizeof(struct ebda_pci_rsrc), GFP_KERNEL);
109 static void __init print_bus_info(void)
111 struct bus_info *ptr;
113 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
114 debug("%s - slot_min = %x\n", __func__, ptr->slot_min);
115 debug("%s - slot_max = %x\n", __func__, ptr->slot_max);
116 debug("%s - slot_count = %x\n", __func__, ptr->slot_count);
117 debug("%s - bus# = %x\n", __func__, ptr->busno);
118 debug("%s - current_speed = %x\n", __func__, ptr->current_speed);
119 debug("%s - controller_id = %x\n", __func__, ptr->controller_id);
121 debug("%s - slots_at_33_conv = %x\n", __func__, ptr->slots_at_33_conv);
122 debug("%s - slots_at_66_conv = %x\n", __func__, ptr->slots_at_66_conv);
123 debug("%s - slots_at_66_pcix = %x\n", __func__, ptr->slots_at_66_pcix);
124 debug("%s - slots_at_100_pcix = %x\n", __func__, ptr->slots_at_100_pcix);
125 debug("%s - slots_at_133_pcix = %x\n", __func__, ptr->slots_at_133_pcix);
130 static void print_lo_info(void)
132 struct rio_detail *ptr;
133 debug("print_lo_info ----\n");
134 list_for_each_entry(ptr, &rio_lo_head, rio_detail_list) {
135 debug("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
136 debug("%s - rio_type = %x\n", __func__, ptr->rio_type);
137 debug("%s - owner_id = %x\n", __func__, ptr->owner_id);
138 debug("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
139 debug("%s - wpindex = %x\n", __func__, ptr->wpindex);
140 debug("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
145 static void print_vg_info(void)
147 struct rio_detail *ptr;
148 debug("%s ---\n", __func__);
149 list_for_each_entry(ptr, &rio_vg_head, rio_detail_list) {
150 debug("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
151 debug("%s - rio_type = %x\n", __func__, ptr->rio_type);
152 debug("%s - owner_id = %x\n", __func__, ptr->owner_id);
153 debug("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
154 debug("%s - wpindex = %x\n", __func__, ptr->wpindex);
155 debug("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
160 static void __init print_ebda_pci_rsrc(void)
162 struct ebda_pci_rsrc *ptr;
164 list_for_each_entry(ptr, &ibmphp_ebda_pci_rsrc_head, ebda_pci_rsrc_list) {
165 debug("%s - rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
166 __func__, ptr->rsrc_type, ptr->bus_num, ptr->dev_fun, ptr->start_addr, ptr->end_addr);
170 static void __init print_ibm_slot(void)
172 struct slot *ptr;
174 list_for_each_entry(ptr, &ibmphp_slot_head, ibm_slot_list) {
175 debug("%s - slot_number: %x\n", __func__, ptr->number);
179 static void __init print_opt_vg(void)
181 struct opt_rio *ptr;
182 debug("%s ---\n", __func__);
183 list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
184 debug("%s - rio_type %x\n", __func__, ptr->rio_type);
185 debug("%s - chassis_num: %x\n", __func__, ptr->chassis_num);
186 debug("%s - first_slot_num: %x\n", __func__, ptr->first_slot_num);
187 debug("%s - middle_num: %x\n", __func__, ptr->middle_num);
191 static void __init print_ebda_hpc(void)
193 struct controller *hpc_ptr;
194 u16 index;
196 list_for_each_entry(hpc_ptr, &ebda_hpc_head, ebda_hpc_list) {
197 for (index = 0; index < hpc_ptr->slot_count; index++) {
198 debug("%s - physical slot#: %x\n", __func__, hpc_ptr->slots[index].slot_num);
199 debug("%s - pci bus# of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_bus_num);
200 debug("%s - index into ctlr addr: %x\n", __func__, hpc_ptr->slots[index].ctl_index);
201 debug("%s - cap of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_cap);
204 for (index = 0; index < hpc_ptr->bus_count; index++)
205 debug("%s - bus# of each bus controlled by this ctlr: %x\n", __func__, hpc_ptr->buses[index].bus_num);
207 debug("%s - type of hpc: %x\n", __func__, hpc_ptr->ctlr_type);
208 switch (hpc_ptr->ctlr_type) {
209 case 1:
210 debug("%s - bus: %x\n", __func__, hpc_ptr->u.pci_ctlr.bus);
211 debug("%s - dev_fun: %x\n", __func__, hpc_ptr->u.pci_ctlr.dev_fun);
212 debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
213 break;
215 case 0:
216 debug("%s - io_start: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_start);
217 debug("%s - io_end: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_end);
218 debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
219 break;
221 case 2:
222 case 4:
223 debug("%s - wpegbbar: %lx\n", __func__, hpc_ptr->u.wpeg_ctlr.wpegbbar);
224 debug("%s - i2c_addr: %x\n", __func__, hpc_ptr->u.wpeg_ctlr.i2c_addr);
225 debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
226 break;
231 int __init ibmphp_access_ebda(void)
233 u8 format, num_ctlrs, rio_complete, hs_complete, ebda_sz;
234 u16 ebda_seg, num_entries, next_offset, offset, blk_id, sub_addr, re, rc_id, re_id, base;
235 int rc = 0;
238 rio_complete = 0;
239 hs_complete = 0;
241 io_mem = ioremap((0x40 << 4) + 0x0e, 2);
242 if (!io_mem)
243 return -ENOMEM;
244 ebda_seg = readw(io_mem);
245 iounmap(io_mem);
246 debug("returned ebda segment: %x\n", ebda_seg);
248 io_mem = ioremap(ebda_seg<<4, 1);
249 if (!io_mem)
250 return -ENOMEM;
251 ebda_sz = readb(io_mem);
252 iounmap(io_mem);
253 debug("ebda size: %d(KiB)\n", ebda_sz);
254 if (ebda_sz == 0)
255 return -ENOMEM;
257 io_mem = ioremap(ebda_seg<<4, (ebda_sz * 1024));
258 if (!io_mem)
259 return -ENOMEM;
260 next_offset = 0x180;
262 for (;;) {
263 offset = next_offset;
265 /* Make sure what we read is still in the mapped section */
266 if (WARN(offset > (ebda_sz * 1024 - 4),
267 "ibmphp_ebda: next read is beyond ebda_sz\n"))
268 break;
270 next_offset = readw(io_mem + offset); /* offset of next blk */
272 offset += 2;
273 if (next_offset == 0) /* 0 indicate it's last blk */
274 break;
275 blk_id = readw(io_mem + offset); /* this blk id */
277 offset += 2;
278 /* check if it is hot swap block or rio block */
279 if (blk_id != 0x4853 && blk_id != 0x4752)
280 continue;
281 /* found hs table */
282 if (blk_id == 0x4853) {
283 debug("now enter hot swap block---\n");
284 debug("hot blk id: %x\n", blk_id);
285 format = readb(io_mem + offset);
287 offset += 1;
288 if (format != 4)
289 goto error_nodev;
290 debug("hot blk format: %x\n", format);
291 /* hot swap sub blk */
292 base = offset;
294 sub_addr = base;
295 re = readw(io_mem + sub_addr); /* next sub blk */
297 sub_addr += 2;
298 rc_id = readw(io_mem + sub_addr); /* sub blk id */
300 sub_addr += 2;
301 if (rc_id != 0x5243)
302 goto error_nodev;
303 /* rc sub blk signature */
304 num_ctlrs = readb(io_mem + sub_addr);
306 sub_addr += 1;
307 hpc_list_ptr = alloc_ebda_hpc_list();
308 if (!hpc_list_ptr) {
309 rc = -ENOMEM;
310 goto out;
312 hpc_list_ptr->format = format;
313 hpc_list_ptr->num_ctlrs = num_ctlrs;
314 hpc_list_ptr->phys_addr = sub_addr; /* offset of RSRC_CONTROLLER blk */
315 debug("info about hpc descriptor---\n");
316 debug("hot blk format: %x\n", format);
317 debug("num of controller: %x\n", num_ctlrs);
318 debug("offset of hpc data structure entries: %x\n ", sub_addr);
320 sub_addr = base + re; /* re sub blk */
321 /* FIXME: rc is never used/checked */
322 rc = readw(io_mem + sub_addr); /* next sub blk */
324 sub_addr += 2;
325 re_id = readw(io_mem + sub_addr); /* sub blk id */
327 sub_addr += 2;
328 if (re_id != 0x5245)
329 goto error_nodev;
331 /* signature of re */
332 num_entries = readw(io_mem + sub_addr);
334 sub_addr += 2; /* offset of RSRC_ENTRIES blk */
335 rsrc_list_ptr = alloc_ebda_rsrc_list();
336 if (!rsrc_list_ptr) {
337 rc = -ENOMEM;
338 goto out;
340 rsrc_list_ptr->format = format;
341 rsrc_list_ptr->num_entries = num_entries;
342 rsrc_list_ptr->phys_addr = sub_addr;
344 debug("info about rsrc descriptor---\n");
345 debug("format: %x\n", format);
346 debug("num of rsrc: %x\n", num_entries);
347 debug("offset of rsrc data structure entries: %x\n ", sub_addr);
349 hs_complete = 1;
350 } else {
351 /* found rio table, blk_id == 0x4752 */
352 debug("now enter io table ---\n");
353 debug("rio blk id: %x\n", blk_id);
355 rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL);
356 if (!rio_table_ptr) {
357 rc = -ENOMEM;
358 goto out;
360 rio_table_ptr->ver_num = readb(io_mem + offset);
361 rio_table_ptr->scal_count = readb(io_mem + offset + 1);
362 rio_table_ptr->riodev_count = readb(io_mem + offset + 2);
363 rio_table_ptr->offset = offset + 3 ;
365 debug("info about rio table hdr ---\n");
366 debug("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ",
367 rio_table_ptr->ver_num, rio_table_ptr->scal_count,
368 rio_table_ptr->riodev_count, rio_table_ptr->offset);
370 rio_complete = 1;
374 if (!hs_complete && !rio_complete)
375 goto error_nodev;
377 if (rio_table_ptr) {
378 if (rio_complete && rio_table_ptr->ver_num == 3) {
379 rc = ebda_rio_table();
380 if (rc)
381 goto out;
384 rc = ebda_rsrc_controller();
385 if (rc)
386 goto out;
388 rc = ebda_rsrc_rsrc();
389 goto out;
390 error_nodev:
391 rc = -ENODEV;
392 out:
393 iounmap(io_mem);
394 return rc;
398 * map info of scalability details and rio details from physical address
400 static int __init ebda_rio_table(void)
402 u16 offset;
403 u8 i;
404 struct rio_detail *rio_detail_ptr;
406 offset = rio_table_ptr->offset;
407 offset += 12 * rio_table_ptr->scal_count;
409 // we do concern about rio details
410 for (i = 0; i < rio_table_ptr->riodev_count; i++) {
411 rio_detail_ptr = kzalloc(sizeof(struct rio_detail), GFP_KERNEL);
412 if (!rio_detail_ptr)
413 return -ENOMEM;
414 rio_detail_ptr->rio_node_id = readb(io_mem + offset);
415 rio_detail_ptr->bbar = readl(io_mem + offset + 1);
416 rio_detail_ptr->rio_type = readb(io_mem + offset + 5);
417 rio_detail_ptr->owner_id = readb(io_mem + offset + 6);
418 rio_detail_ptr->port0_node_connect = readb(io_mem + offset + 7);
419 rio_detail_ptr->port0_port_connect = readb(io_mem + offset + 8);
420 rio_detail_ptr->port1_node_connect = readb(io_mem + offset + 9);
421 rio_detail_ptr->port1_port_connect = readb(io_mem + offset + 10);
422 rio_detail_ptr->first_slot_num = readb(io_mem + offset + 11);
423 rio_detail_ptr->status = readb(io_mem + offset + 12);
424 rio_detail_ptr->wpindex = readb(io_mem + offset + 13);
425 rio_detail_ptr->chassis_num = readb(io_mem + offset + 14);
426 // 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);
427 //create linked list of chassis
428 if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5)
429 list_add(&rio_detail_ptr->rio_detail_list, &rio_vg_head);
430 //create linked list of expansion box
431 else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7)
432 list_add(&rio_detail_ptr->rio_detail_list, &rio_lo_head);
433 else
434 // not in my concern
435 kfree(rio_detail_ptr);
436 offset += 15;
438 print_lo_info();
439 print_vg_info();
440 return 0;
444 * reorganizing linked list of chassis
446 static struct opt_rio *search_opt_vg(u8 chassis_num)
448 struct opt_rio *ptr;
449 list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
450 if (ptr->chassis_num == chassis_num)
451 return ptr;
453 return NULL;
456 static int __init combine_wpg_for_chassis(void)
458 struct opt_rio *opt_rio_ptr = NULL;
459 struct rio_detail *rio_detail_ptr = NULL;
461 list_for_each_entry(rio_detail_ptr, &rio_vg_head, rio_detail_list) {
462 opt_rio_ptr = search_opt_vg(rio_detail_ptr->chassis_num);
463 if (!opt_rio_ptr) {
464 opt_rio_ptr = kzalloc(sizeof(struct opt_rio), GFP_KERNEL);
465 if (!opt_rio_ptr)
466 return -ENOMEM;
467 opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
468 opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
469 opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
470 opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
471 list_add(&opt_rio_ptr->opt_rio_list, &opt_vg_head);
472 } else {
473 opt_rio_ptr->first_slot_num = min(opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
474 opt_rio_ptr->middle_num = max(opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
477 print_opt_vg();
478 return 0;
482 * reorganizing linked list of expansion box
484 static struct opt_rio_lo *search_opt_lo(u8 chassis_num)
486 struct opt_rio_lo *ptr;
487 list_for_each_entry(ptr, &opt_lo_head, opt_rio_lo_list) {
488 if (ptr->chassis_num == chassis_num)
489 return ptr;
491 return NULL;
494 static int combine_wpg_for_expansion(void)
496 struct opt_rio_lo *opt_rio_lo_ptr = NULL;
497 struct rio_detail *rio_detail_ptr = NULL;
499 list_for_each_entry(rio_detail_ptr, &rio_lo_head, rio_detail_list) {
500 opt_rio_lo_ptr = search_opt_lo(rio_detail_ptr->chassis_num);
501 if (!opt_rio_lo_ptr) {
502 opt_rio_lo_ptr = kzalloc(sizeof(struct opt_rio_lo), GFP_KERNEL);
503 if (!opt_rio_lo_ptr)
504 return -ENOMEM;
505 opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
506 opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
507 opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
508 opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
509 opt_rio_lo_ptr->pack_count = 1;
511 list_add(&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
512 } else {
513 opt_rio_lo_ptr->first_slot_num = min(opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
514 opt_rio_lo_ptr->middle_num = max(opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
515 opt_rio_lo_ptr->pack_count = 2;
518 return 0;
522 /* Since we don't know the max slot number per each chassis, hence go
523 * through the list of all chassis to find out the range
524 * Arguments: slot_num, 1st slot number of the chassis we think we are on,
525 * var (0 = chassis, 1 = expansion box)
527 static int first_slot_num(u8 slot_num, u8 first_slot, u8 var)
529 struct opt_rio *opt_vg_ptr = NULL;
530 struct opt_rio_lo *opt_lo_ptr = NULL;
531 int rc = 0;
533 if (!var) {
534 list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
535 if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) {
536 rc = -ENODEV;
537 break;
540 } else {
541 list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
542 if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
543 rc = -ENODEV;
544 break;
548 return rc;
551 static struct opt_rio_lo *find_rxe_num(u8 slot_num)
553 struct opt_rio_lo *opt_lo_ptr;
555 list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
556 //check to see if this slot_num belongs to expansion box
557 if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_lo_ptr->first_slot_num, 1)))
558 return opt_lo_ptr;
560 return NULL;
563 static struct opt_rio *find_chassis_num(u8 slot_num)
565 struct opt_rio *opt_vg_ptr;
567 list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
568 //check to see if this slot_num belongs to chassis
569 if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_vg_ptr->first_slot_num, 0)))
570 return opt_vg_ptr;
572 return NULL;
575 /* This routine will find out how many slots are in the chassis, so that
576 * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
578 static u8 calculate_first_slot(u8 slot_num)
580 u8 first_slot = 1;
581 struct slot *slot_cur;
583 list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
584 if (slot_cur->ctrl) {
585 if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num))
586 first_slot = slot_cur->ctrl->ending_slot_num;
589 return first_slot + 1;
593 #define SLOT_NAME_SIZE 30
595 static char *create_file_name(struct slot *slot_cur)
597 struct opt_rio *opt_vg_ptr = NULL;
598 struct opt_rio_lo *opt_lo_ptr = NULL;
599 static char str[SLOT_NAME_SIZE];
600 int which = 0; /* rxe = 1, chassis = 0 */
601 u8 number = 1; /* either chassis or rxe # */
602 u8 first_slot = 1;
603 u8 slot_num;
604 u8 flag = 0;
606 if (!slot_cur) {
607 err("Structure passed is empty\n");
608 return NULL;
611 slot_num = slot_cur->number;
613 memset(str, 0, sizeof(str));
615 if (rio_table_ptr) {
616 if (rio_table_ptr->ver_num == 3) {
617 opt_vg_ptr = find_chassis_num(slot_num);
618 opt_lo_ptr = find_rxe_num(slot_num);
621 if (opt_vg_ptr) {
622 if (opt_lo_ptr) {
623 if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
624 number = opt_lo_ptr->chassis_num;
625 first_slot = opt_lo_ptr->first_slot_num;
626 which = 1; /* it is RXE */
627 } else {
628 first_slot = opt_vg_ptr->first_slot_num;
629 number = opt_vg_ptr->chassis_num;
630 which = 0;
632 } else {
633 first_slot = opt_vg_ptr->first_slot_num;
634 number = opt_vg_ptr->chassis_num;
635 which = 0;
637 ++flag;
638 } else if (opt_lo_ptr) {
639 number = opt_lo_ptr->chassis_num;
640 first_slot = opt_lo_ptr->first_slot_num;
641 which = 1;
642 ++flag;
643 } else if (rio_table_ptr) {
644 if (rio_table_ptr->ver_num == 3) {
645 /* if both NULL and we DO have correct RIO table in BIOS */
646 return NULL;
649 if (!flag) {
650 if (slot_cur->ctrl->ctlr_type == 4) {
651 first_slot = calculate_first_slot(slot_num);
652 which = 1;
653 } else {
654 which = 0;
658 sprintf(str, "%s%dslot%d",
659 which == 0 ? "chassis" : "rxe",
660 number, slot_num - first_slot + 1);
661 return str;
664 static int fillslotinfo(struct hotplug_slot *hotplug_slot)
666 struct slot *slot;
667 int rc = 0;
669 if (!hotplug_slot || !hotplug_slot->private)
670 return -EINVAL;
672 slot = hotplug_slot->private;
673 rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
674 if (rc)
675 return rc;
677 // power - enabled:1 not:0
678 hotplug_slot->info->power_status = SLOT_POWER(slot->status);
680 // attention - off:0, on:1, blinking:2
681 hotplug_slot->info->attention_status = SLOT_ATTN(slot->status, slot->ext_status);
683 // latch - open:1 closed:0
684 hotplug_slot->info->latch_status = SLOT_LATCH(slot->status);
686 // pci board - present:1 not:0
687 if (SLOT_PRESENT(slot->status))
688 hotplug_slot->info->adapter_status = 1;
689 else
690 hotplug_slot->info->adapter_status = 0;
692 if (slot->bus_on->supported_bus_mode
693 && (slot->bus_on->supported_speed == BUS_SPEED_66))
694 hotplug_slot->info->max_bus_speed_status = BUS_SPEED_66PCIX;
695 else
696 hotplug_slot->info->max_bus_speed_status = slot->bus_on->supported_speed;
699 return rc;
702 static struct pci_driver ibmphp_driver;
705 * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
706 * each hpc from physical address to a list of hot plug controllers based on
707 * hpc descriptors.
709 static int __init ebda_rsrc_controller(void)
711 u16 addr, addr_slot, addr_bus;
712 u8 ctlr_id, temp, bus_index;
713 u16 ctlr, slot, bus;
714 u16 slot_num, bus_num, index;
715 struct hotplug_slot *hp_slot_ptr;
716 struct controller *hpc_ptr;
717 struct ebda_hpc_bus *bus_ptr;
718 struct ebda_hpc_slot *slot_ptr;
719 struct bus_info *bus_info_ptr1, *bus_info_ptr2;
720 int rc;
721 struct slot *tmp_slot;
722 char name[SLOT_NAME_SIZE];
724 addr = hpc_list_ptr->phys_addr;
725 for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
726 bus_index = 1;
727 ctlr_id = readb(io_mem + addr);
728 addr += 1;
729 slot_num = readb(io_mem + addr);
731 addr += 1;
732 addr_slot = addr; /* offset of slot structure */
733 addr += (slot_num * 4);
735 bus_num = readb(io_mem + addr);
737 addr += 1;
738 addr_bus = addr; /* offset of bus */
739 addr += (bus_num * 9); /* offset of ctlr_type */
740 temp = readb(io_mem + addr);
742 addr += 1;
743 /* init hpc structure */
744 hpc_ptr = alloc_ebda_hpc(slot_num, bus_num);
745 if (!hpc_ptr) {
746 rc = -ENOMEM;
747 goto error_no_hpc;
749 hpc_ptr->ctlr_id = ctlr_id;
750 hpc_ptr->ctlr_relative_id = ctlr;
751 hpc_ptr->slot_count = slot_num;
752 hpc_ptr->bus_count = bus_num;
753 debug("now enter ctlr data structure ---\n");
754 debug("ctlr id: %x\n", ctlr_id);
755 debug("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
756 debug("count of slots controlled by this ctlr: %x\n", slot_num);
757 debug("count of buses controlled by this ctlr: %x\n", bus_num);
759 /* init slot structure, fetch slot, bus, cap... */
760 slot_ptr = hpc_ptr->slots;
761 for (slot = 0; slot < slot_num; slot++) {
762 slot_ptr->slot_num = readb(io_mem + addr_slot);
763 slot_ptr->slot_bus_num = readb(io_mem + addr_slot + slot_num);
764 slot_ptr->ctl_index = readb(io_mem + addr_slot + 2*slot_num);
765 slot_ptr->slot_cap = readb(io_mem + addr_slot + 3*slot_num);
767 // create bus_info lined list --- if only one slot per bus: slot_min = slot_max
769 bus_info_ptr2 = ibmphp_find_same_bus_num(slot_ptr->slot_bus_num);
770 if (!bus_info_ptr2) {
771 bus_info_ptr1 = kzalloc(sizeof(struct bus_info), GFP_KERNEL);
772 if (!bus_info_ptr1) {
773 rc = -ENOMEM;
774 goto error_no_hp_slot;
776 bus_info_ptr1->slot_min = slot_ptr->slot_num;
777 bus_info_ptr1->slot_max = slot_ptr->slot_num;
778 bus_info_ptr1->slot_count += 1;
779 bus_info_ptr1->busno = slot_ptr->slot_bus_num;
780 bus_info_ptr1->index = bus_index++;
781 bus_info_ptr1->current_speed = 0xff;
782 bus_info_ptr1->current_bus_mode = 0xff;
784 bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
786 list_add_tail(&bus_info_ptr1->bus_info_list, &bus_info_head);
788 } else {
789 bus_info_ptr2->slot_min = min(bus_info_ptr2->slot_min, slot_ptr->slot_num);
790 bus_info_ptr2->slot_max = max(bus_info_ptr2->slot_max, slot_ptr->slot_num);
791 bus_info_ptr2->slot_count += 1;
795 // end of creating the bus_info linked list
797 slot_ptr++;
798 addr_slot += 1;
801 /* init bus structure */
802 bus_ptr = hpc_ptr->buses;
803 for (bus = 0; bus < bus_num; bus++) {
804 bus_ptr->bus_num = readb(io_mem + addr_bus + bus);
805 bus_ptr->slots_at_33_conv = readb(io_mem + addr_bus + bus_num + 8 * bus);
806 bus_ptr->slots_at_66_conv = readb(io_mem + addr_bus + bus_num + 8 * bus + 1);
808 bus_ptr->slots_at_66_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 2);
810 bus_ptr->slots_at_100_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 3);
812 bus_ptr->slots_at_133_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 4);
814 bus_info_ptr2 = ibmphp_find_same_bus_num(bus_ptr->bus_num);
815 if (bus_info_ptr2) {
816 bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
817 bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
818 bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
819 bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
820 bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix;
822 bus_ptr++;
825 hpc_ptr->ctlr_type = temp;
827 switch (hpc_ptr->ctlr_type) {
828 case 1:
829 hpc_ptr->u.pci_ctlr.bus = readb(io_mem + addr);
830 hpc_ptr->u.pci_ctlr.dev_fun = readb(io_mem + addr + 1);
831 hpc_ptr->irq = readb(io_mem + addr + 2);
832 addr += 3;
833 debug("ctrl bus = %x, ctlr devfun = %x, irq = %x\n",
834 hpc_ptr->u.pci_ctlr.bus,
835 hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
836 break;
838 case 0:
839 hpc_ptr->u.isa_ctlr.io_start = readw(io_mem + addr);
840 hpc_ptr->u.isa_ctlr.io_end = readw(io_mem + addr + 2);
841 if (!request_region(hpc_ptr->u.isa_ctlr.io_start,
842 (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
843 "ibmphp")) {
844 rc = -ENODEV;
845 goto error_no_hp_slot;
847 hpc_ptr->irq = readb(io_mem + addr + 4);
848 addr += 5;
849 break;
851 case 2:
852 case 4:
853 hpc_ptr->u.wpeg_ctlr.wpegbbar = readl(io_mem + addr);
854 hpc_ptr->u.wpeg_ctlr.i2c_addr = readb(io_mem + addr + 4);
855 hpc_ptr->irq = readb(io_mem + addr + 5);
856 addr += 6;
857 break;
858 default:
859 rc = -ENODEV;
860 goto error_no_hp_slot;
863 //reorganize chassis' linked list
864 combine_wpg_for_chassis();
865 combine_wpg_for_expansion();
866 hpc_ptr->revision = 0xff;
867 hpc_ptr->options = 0xff;
868 hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
869 hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;
871 // register slots with hpc core as well as create linked list of ibm slot
872 for (index = 0; index < hpc_ptr->slot_count; index++) {
874 hp_slot_ptr = kzalloc(sizeof(*hp_slot_ptr), GFP_KERNEL);
875 if (!hp_slot_ptr) {
876 rc = -ENOMEM;
877 goto error_no_hp_slot;
880 hp_slot_ptr->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
881 if (!hp_slot_ptr->info) {
882 rc = -ENOMEM;
883 goto error_no_hp_info;
886 tmp_slot = kzalloc(sizeof(*tmp_slot), GFP_KERNEL);
887 if (!tmp_slot) {
888 rc = -ENOMEM;
889 goto error_no_slot;
892 tmp_slot->flag = 1;
894 tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
895 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
896 tmp_slot->supported_speed = 3;
897 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
898 tmp_slot->supported_speed = 2;
899 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
900 tmp_slot->supported_speed = 1;
902 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
903 tmp_slot->supported_bus_mode = 1;
904 else
905 tmp_slot->supported_bus_mode = 0;
908 tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;
910 bus_info_ptr1 = ibmphp_find_same_bus_num(hpc_ptr->slots[index].slot_bus_num);
911 if (!bus_info_ptr1) {
912 kfree(tmp_slot);
913 rc = -ENODEV;
914 goto error;
916 tmp_slot->bus_on = bus_info_ptr1;
917 bus_info_ptr1 = NULL;
918 tmp_slot->ctrl = hpc_ptr;
920 tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
921 tmp_slot->number = hpc_ptr->slots[index].slot_num;
922 tmp_slot->hotplug_slot = hp_slot_ptr;
924 hp_slot_ptr->private = tmp_slot;
926 rc = fillslotinfo(hp_slot_ptr);
927 if (rc)
928 goto error;
930 rc = ibmphp_init_devno((struct slot **) &hp_slot_ptr->private);
931 if (rc)
932 goto error;
933 hp_slot_ptr->ops = &ibmphp_hotplug_slot_ops;
935 // end of registering ibm slot with hotplug core
937 list_add(&((struct slot *)(hp_slot_ptr->private))->ibm_slot_list, &ibmphp_slot_head);
940 print_bus_info();
941 list_add(&hpc_ptr->ebda_hpc_list, &ebda_hpc_head);
943 } /* each hpc */
945 list_for_each_entry(tmp_slot, &ibmphp_slot_head, ibm_slot_list) {
946 snprintf(name, SLOT_NAME_SIZE, "%s", create_file_name(tmp_slot));
947 pci_hp_register(tmp_slot->hotplug_slot,
948 pci_find_bus(0, tmp_slot->bus), tmp_slot->device, name);
951 print_ebda_hpc();
952 print_ibm_slot();
953 return 0;
955 error:
956 kfree(hp_slot_ptr->private);
957 error_no_slot:
958 kfree(hp_slot_ptr->info);
959 error_no_hp_info:
960 kfree(hp_slot_ptr);
961 error_no_hp_slot:
962 free_ebda_hpc(hpc_ptr);
963 error_no_hpc:
964 iounmap(io_mem);
965 return rc;
969 * map info (bus, devfun, start addr, end addr..) of i/o, memory,
970 * pfm from the physical addr to a list of resource.
972 static int __init ebda_rsrc_rsrc(void)
974 u16 addr;
975 short rsrc;
976 u8 type, rsrc_type;
977 struct ebda_pci_rsrc *rsrc_ptr;
979 addr = rsrc_list_ptr->phys_addr;
980 debug("now entering rsrc land\n");
981 debug("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
983 for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
984 type = readb(io_mem + addr);
986 addr += 1;
987 rsrc_type = type & EBDA_RSRC_TYPE_MASK;
989 if (rsrc_type == EBDA_IO_RSRC_TYPE) {
990 rsrc_ptr = alloc_ebda_pci_rsrc();
991 if (!rsrc_ptr) {
992 iounmap(io_mem);
993 return -ENOMEM;
995 rsrc_ptr->rsrc_type = type;
997 rsrc_ptr->bus_num = readb(io_mem + addr);
998 rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
999 rsrc_ptr->start_addr = readw(io_mem + addr + 2);
1000 rsrc_ptr->end_addr = readw(io_mem + addr + 4);
1001 addr += 6;
1003 debug("rsrc from io type ----\n");
1004 debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1005 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1007 list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1010 if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
1011 rsrc_ptr = alloc_ebda_pci_rsrc();
1012 if (!rsrc_ptr) {
1013 iounmap(io_mem);
1014 return -ENOMEM;
1016 rsrc_ptr->rsrc_type = type;
1018 rsrc_ptr->bus_num = readb(io_mem + addr);
1019 rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
1020 rsrc_ptr->start_addr = readl(io_mem + addr + 2);
1021 rsrc_ptr->end_addr = readl(io_mem + addr + 6);
1022 addr += 10;
1024 debug("rsrc from mem or pfm ---\n");
1025 debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1026 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1028 list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1031 kfree(rsrc_list_ptr);
1032 rsrc_list_ptr = NULL;
1033 print_ebda_pci_rsrc();
1034 return 0;
1037 u16 ibmphp_get_total_controllers(void)
1039 return hpc_list_ptr->num_ctlrs;
1042 struct slot *ibmphp_get_slot_from_physical_num(u8 physical_num)
1044 struct slot *slot;
1046 list_for_each_entry(slot, &ibmphp_slot_head, ibm_slot_list) {
1047 if (slot->number == physical_num)
1048 return slot;
1050 return NULL;
1053 /* To find:
1054 * - the smallest slot number
1055 * - the largest slot number
1056 * - the total number of the slots based on each bus
1057 * (if only one slot per bus slot_min = slot_max )
1059 struct bus_info *ibmphp_find_same_bus_num(u32 num)
1061 struct bus_info *ptr;
1063 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1064 if (ptr->busno == num)
1065 return ptr;
1067 return NULL;
1070 /* Finding relative bus number, in order to map corresponding
1071 * bus register
1073 int ibmphp_get_bus_index(u8 num)
1075 struct bus_info *ptr;
1077 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1078 if (ptr->busno == num)
1079 return ptr->index;
1081 return -ENODEV;
1084 void ibmphp_free_bus_info_queue(void)
1086 struct bus_info *bus_info, *next;
1088 list_for_each_entry_safe(bus_info, next, &bus_info_head,
1089 bus_info_list) {
1090 kfree (bus_info);
1094 void ibmphp_free_ebda_hpc_queue(void)
1096 struct controller *controller = NULL, *next;
1097 int pci_flag = 0;
1099 list_for_each_entry_safe(controller, next, &ebda_hpc_head,
1100 ebda_hpc_list) {
1101 if (controller->ctlr_type == 0)
1102 release_region(controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
1103 else if ((controller->ctlr_type == 1) && (!pci_flag)) {
1104 ++pci_flag;
1105 pci_unregister_driver(&ibmphp_driver);
1107 free_ebda_hpc(controller);
1111 void ibmphp_free_ebda_pci_rsrc_queue(void)
1113 struct ebda_pci_rsrc *resource, *next;
1115 list_for_each_entry_safe(resource, next, &ibmphp_ebda_pci_rsrc_head,
1116 ebda_pci_rsrc_list) {
1117 kfree (resource);
1118 resource = NULL;
1122 static const struct pci_device_id id_table[] = {
1124 .vendor = PCI_VENDOR_ID_IBM,
1125 .device = HPC_DEVICE_ID,
1126 .subvendor = PCI_VENDOR_ID_IBM,
1127 .subdevice = HPC_SUBSYSTEM_ID,
1128 .class = ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1129 }, {}
1132 MODULE_DEVICE_TABLE(pci, id_table);
1134 static int ibmphp_probe(struct pci_dev *, const struct pci_device_id *);
1135 static struct pci_driver ibmphp_driver = {
1136 .name = "ibmphp",
1137 .id_table = id_table,
1138 .probe = ibmphp_probe,
1141 int ibmphp_register_pci(void)
1143 struct controller *ctrl;
1144 int rc = 0;
1146 list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
1147 if (ctrl->ctlr_type == 1) {
1148 rc = pci_register_driver(&ibmphp_driver);
1149 break;
1152 return rc;
1154 static int ibmphp_probe(struct pci_dev *dev, const struct pci_device_id *ids)
1156 struct controller *ctrl;
1158 debug("inside ibmphp_probe\n");
1160 list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
1161 if (ctrl->ctlr_type == 1) {
1162 if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
1163 ctrl->ctrl_dev = dev;
1164 debug("found device!!!\n");
1165 debug("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
1166 return 0;
1170 return -ENODEV;