xref: /linux/drivers/pci/hotplug/ibmphp_ebda.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * IBM Hot Plug Controller Driver
4  *
5  * Written By: Tong Yu, IBM Corporation
6  *
7  * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
8  * Copyright (C) 2001-2003 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/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"
24 
25 /*
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.
30  *
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...)
35  */
36 
37 /* Global lists */
38 LIST_HEAD(ibmphp_ebda_pci_rsrc_head);
39 LIST_HEAD(ibmphp_slot_head);
40 
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;
52 
53 /* Local functions */
54 static int ebda_rsrc_controller(void);
55 static int ebda_rsrc_rsrc(void);
56 static int ebda_rio_table(void);
57 
58 static struct ebda_hpc_list * __init alloc_ebda_hpc_list(void)
59 {
60 	return kzalloc_obj(struct ebda_hpc_list, GFP_KERNEL);
61 }
62 
63 static struct controller *alloc_ebda_hpc(u32 slot_count, u32 bus_count)
64 {
65 	struct controller *controller;
66 	struct ebda_hpc_slot *slots;
67 	struct ebda_hpc_bus *buses;
68 
69 	controller = kzalloc_obj(struct controller, GFP_KERNEL);
70 	if (!controller)
71 		goto error;
72 
73 	slots = kzalloc_objs(struct ebda_hpc_slot, slot_count, GFP_KERNEL);
74 	if (!slots)
75 		goto error_contr;
76 	controller->slots = slots;
77 
78 	buses = kzalloc_objs(struct ebda_hpc_bus, bus_count, GFP_KERNEL);
79 	if (!buses)
80 		goto error_slots;
81 	controller->buses = buses;
82 
83 	return controller;
84 error_slots:
85 	kfree(controller->slots);
86 error_contr:
87 	kfree(controller);
88 error:
89 	return NULL;
90 }
91 
92 static void free_ebda_hpc(struct controller *controller)
93 {
94 	kfree(controller->slots);
95 	kfree(controller->buses);
96 	kfree(controller);
97 }
98 
99 static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list(void)
100 {
101 	return kzalloc_obj(struct ebda_rsrc_list, GFP_KERNEL);
102 }
103 
104 static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc(void)
105 {
106 	return kzalloc_obj(struct ebda_pci_rsrc, GFP_KERNEL);
107 }
108 
109 static void __init print_bus_info(void)
110 {
111 	struct bus_info *ptr;
112 
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);
120 
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);
126 
127 	}
128 }
129 
130 static void print_lo_info(void)
131 {
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);
141 
142 	}
143 }
144 
145 static void print_vg_info(void)
146 {
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);
156 
157 	}
158 }
159 
160 static void __init print_ebda_pci_rsrc(void)
161 {
162 	struct ebda_pci_rsrc *ptr;
163 
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);
167 	}
168 }
169 
170 static void __init print_ibm_slot(void)
171 {
172 	struct slot *ptr;
173 
174 	list_for_each_entry(ptr, &ibmphp_slot_head, ibm_slot_list) {
175 		debug("%s - slot_number: %x\n", __func__, ptr->number);
176 	}
177 }
178 
179 static void __init print_opt_vg(void)
180 {
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);
188 	}
189 }
190 
191 static void __init print_ebda_hpc(void)
192 {
193 	struct controller *hpc_ptr;
194 	u16 index;
195 
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);
202 		}
203 
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);
206 
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;
214 
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;
220 
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;
227 		}
228 	}
229 }
230 
231 int __init ibmphp_access_ebda(void)
232 {
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;
236 
237 
238 	rio_complete = 0;
239 	hs_complete = 0;
240 
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);
247 
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;
256 
257 	io_mem = ioremap(ebda_seg<<4, (ebda_sz * 1024));
258 	if (!io_mem)
259 		return -ENOMEM;
260 	next_offset = 0x180;
261 
262 	for (;;) {
263 		offset = next_offset;
264 
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;
269 
270 		next_offset = readw(io_mem + offset);	/* offset of next blk */
271 
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 */
276 
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);
286 
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;
293 
294 			sub_addr = base;
295 			re = readw(io_mem + sub_addr);	/* next sub blk */
296 
297 			sub_addr += 2;
298 			rc_id = readw(io_mem + sub_addr);	/* sub blk id */
299 
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);
305 
306 			sub_addr += 1;
307 			hpc_list_ptr = alloc_ebda_hpc_list();
308 			if (!hpc_list_ptr) {
309 				rc = -ENOMEM;
310 				goto out;
311 			}
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);
319 
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 */
323 
324 			sub_addr += 2;
325 			re_id = readw(io_mem + sub_addr);	/* sub blk id */
326 
327 			sub_addr += 2;
328 			if (re_id != 0x5245)
329 				goto error_nodev;
330 
331 			/* signature of re */
332 			num_entries = readw(io_mem + sub_addr);
333 
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;
339 			}
340 			rsrc_list_ptr->format = format;
341 			rsrc_list_ptr->num_entries = num_entries;
342 			rsrc_list_ptr->phys_addr = sub_addr;
343 
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);
348 
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);
354 
355 			rio_table_ptr = kzalloc_obj(struct rio_table_hdr,
356 						    GFP_KERNEL);
357 			if (!rio_table_ptr) {
358 				rc = -ENOMEM;
359 				goto out;
360 			}
361 			rio_table_ptr->ver_num = readb(io_mem + offset);
362 			rio_table_ptr->scal_count = readb(io_mem + offset + 1);
363 			rio_table_ptr->riodev_count = readb(io_mem + offset + 2);
364 			rio_table_ptr->offset = offset + 3 ;
365 
366 			debug("info about rio table hdr ---\n");
367 			debug("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ",
368 				rio_table_ptr->ver_num, rio_table_ptr->scal_count,
369 				rio_table_ptr->riodev_count, rio_table_ptr->offset);
370 
371 			rio_complete = 1;
372 		}
373 	}
374 
375 	if (!hs_complete && !rio_complete)
376 		goto error_nodev;
377 
378 	if (rio_table_ptr) {
379 		if (rio_complete && rio_table_ptr->ver_num == 3) {
380 			rc = ebda_rio_table();
381 			if (rc)
382 				goto out;
383 		}
384 	}
385 	rc = ebda_rsrc_controller();
386 	if (rc)
387 		goto out;
388 
389 	rc = ebda_rsrc_rsrc();
390 	goto out;
391 error_nodev:
392 	rc = -ENODEV;
393 out:
394 	iounmap(io_mem);
395 	return rc;
396 }
397 
398 /*
399  * map info of scalability details and rio details from physical address
400  */
401 static int __init ebda_rio_table(void)
402 {
403 	u16 offset;
404 	u8 i;
405 	struct rio_detail *rio_detail_ptr;
406 
407 	offset = rio_table_ptr->offset;
408 	offset += 12 * rio_table_ptr->scal_count;
409 
410 	// we do concern about rio details
411 	for (i = 0; i < rio_table_ptr->riodev_count; i++) {
412 		rio_detail_ptr = kzalloc_obj(struct rio_detail, GFP_KERNEL);
413 		if (!rio_detail_ptr)
414 			return -ENOMEM;
415 		rio_detail_ptr->rio_node_id = readb(io_mem + offset);
416 		rio_detail_ptr->bbar = readl(io_mem + offset + 1);
417 		rio_detail_ptr->rio_type = readb(io_mem + offset + 5);
418 		rio_detail_ptr->owner_id = readb(io_mem + offset + 6);
419 		rio_detail_ptr->port0_node_connect = readb(io_mem + offset + 7);
420 		rio_detail_ptr->port0_port_connect = readb(io_mem + offset + 8);
421 		rio_detail_ptr->port1_node_connect = readb(io_mem + offset + 9);
422 		rio_detail_ptr->port1_port_connect = readb(io_mem + offset + 10);
423 		rio_detail_ptr->first_slot_num = readb(io_mem + offset + 11);
424 		rio_detail_ptr->status = readb(io_mem + offset + 12);
425 		rio_detail_ptr->wpindex = readb(io_mem + offset + 13);
426 		rio_detail_ptr->chassis_num = readb(io_mem + offset + 14);
427 //		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);
428 		//create linked list of chassis
429 		if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5)
430 			list_add(&rio_detail_ptr->rio_detail_list, &rio_vg_head);
431 		//create linked list of expansion box
432 		else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7)
433 			list_add(&rio_detail_ptr->rio_detail_list, &rio_lo_head);
434 		else
435 			// not in my concern
436 			kfree(rio_detail_ptr);
437 		offset += 15;
438 	}
439 	print_lo_info();
440 	print_vg_info();
441 	return 0;
442 }
443 
444 /*
445  * reorganizing linked list of chassis
446  */
447 static struct opt_rio *search_opt_vg(u8 chassis_num)
448 {
449 	struct opt_rio *ptr;
450 	list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
451 		if (ptr->chassis_num == chassis_num)
452 			return ptr;
453 	}
454 	return NULL;
455 }
456 
457 static int __init combine_wpg_for_chassis(void)
458 {
459 	struct opt_rio *opt_rio_ptr = NULL;
460 	struct rio_detail *rio_detail_ptr = NULL;
461 
462 	list_for_each_entry(rio_detail_ptr, &rio_vg_head, rio_detail_list) {
463 		opt_rio_ptr = search_opt_vg(rio_detail_ptr->chassis_num);
464 		if (!opt_rio_ptr) {
465 			opt_rio_ptr = kzalloc_obj(struct opt_rio, GFP_KERNEL);
466 			if (!opt_rio_ptr)
467 				return -ENOMEM;
468 			opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
469 			opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
470 			opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
471 			opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
472 			list_add(&opt_rio_ptr->opt_rio_list, &opt_vg_head);
473 		} else {
474 			opt_rio_ptr->first_slot_num = min(opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
475 			opt_rio_ptr->middle_num = max(opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
476 		}
477 	}
478 	print_opt_vg();
479 	return 0;
480 }
481 
482 /*
483  * reorganizing linked list of expansion box
484  */
485 static struct opt_rio_lo *search_opt_lo(u8 chassis_num)
486 {
487 	struct opt_rio_lo *ptr;
488 	list_for_each_entry(ptr, &opt_lo_head, opt_rio_lo_list) {
489 		if (ptr->chassis_num == chassis_num)
490 			return ptr;
491 	}
492 	return NULL;
493 }
494 
495 static int combine_wpg_for_expansion(void)
496 {
497 	struct opt_rio_lo *opt_rio_lo_ptr = NULL;
498 	struct rio_detail *rio_detail_ptr = NULL;
499 
500 	list_for_each_entry(rio_detail_ptr, &rio_lo_head, rio_detail_list) {
501 		opt_rio_lo_ptr = search_opt_lo(rio_detail_ptr->chassis_num);
502 		if (!opt_rio_lo_ptr) {
503 			opt_rio_lo_ptr = kzalloc_obj(struct opt_rio_lo,
504 						     GFP_KERNEL);
505 			if (!opt_rio_lo_ptr)
506 				return -ENOMEM;
507 			opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
508 			opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
509 			opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
510 			opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
511 			opt_rio_lo_ptr->pack_count = 1;
512 
513 			list_add(&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
514 		} else {
515 			opt_rio_lo_ptr->first_slot_num = min(opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
516 			opt_rio_lo_ptr->middle_num = max(opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
517 			opt_rio_lo_ptr->pack_count = 2;
518 		}
519 	}
520 	return 0;
521 }
522 
523 
524 /* Since we don't know the max slot number per each chassis, hence go
525  * through the list of all chassis to find out the range
526  * Arguments: slot_num, 1st slot number of the chassis we think we are on,
527  * var (0 = chassis, 1 = expansion box)
528  */
529 static int first_slot_num(u8 slot_num, u8 first_slot, u8 var)
530 {
531 	struct opt_rio *opt_vg_ptr = NULL;
532 	struct opt_rio_lo *opt_lo_ptr = NULL;
533 	int rc = 0;
534 
535 	if (!var) {
536 		list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
537 			if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) {
538 				rc = -ENODEV;
539 				break;
540 			}
541 		}
542 	} else {
543 		list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
544 			if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
545 				rc = -ENODEV;
546 				break;
547 			}
548 		}
549 	}
550 	return rc;
551 }
552 
553 static struct opt_rio_lo *find_rxe_num(u8 slot_num)
554 {
555 	struct opt_rio_lo *opt_lo_ptr;
556 
557 	list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
558 		//check to see if this slot_num belongs to expansion box
559 		if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_lo_ptr->first_slot_num, 1)))
560 			return opt_lo_ptr;
561 	}
562 	return NULL;
563 }
564 
565 static struct opt_rio *find_chassis_num(u8 slot_num)
566 {
567 	struct opt_rio *opt_vg_ptr;
568 
569 	list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
570 		//check to see if this slot_num belongs to chassis
571 		if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_vg_ptr->first_slot_num, 0)))
572 			return opt_vg_ptr;
573 	}
574 	return NULL;
575 }
576 
577 /* This routine will find out how many slots are in the chassis, so that
578  * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
579  */
580 static u8 calculate_first_slot(u8 slot_num)
581 {
582 	u8 first_slot = 1;
583 	struct slot *slot_cur;
584 
585 	list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
586 		if (slot_cur->ctrl) {
587 			if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num))
588 				first_slot = slot_cur->ctrl->ending_slot_num;
589 		}
590 	}
591 	return first_slot + 1;
592 
593 }
594 
595 #define SLOT_NAME_SIZE 30
596 
597 static char *create_file_name(struct slot *slot_cur)
598 {
599 	struct opt_rio *opt_vg_ptr = NULL;
600 	struct opt_rio_lo *opt_lo_ptr = NULL;
601 	static char str[SLOT_NAME_SIZE];
602 	int which = 0; /* rxe = 1, chassis = 0 */
603 	u8 number = 1; /* either chassis or rxe # */
604 	u8 first_slot = 1;
605 	u8 slot_num;
606 	u8 flag = 0;
607 
608 	if (!slot_cur) {
609 		err("Structure passed is empty\n");
610 		return NULL;
611 	}
612 
613 	slot_num = slot_cur->number;
614 
615 	memset(str, 0, sizeof(str));
616 
617 	if (rio_table_ptr) {
618 		if (rio_table_ptr->ver_num == 3) {
619 			opt_vg_ptr = find_chassis_num(slot_num);
620 			opt_lo_ptr = find_rxe_num(slot_num);
621 		}
622 	}
623 	if (opt_vg_ptr) {
624 		if (opt_lo_ptr) {
625 			if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
626 				number = opt_lo_ptr->chassis_num;
627 				first_slot = opt_lo_ptr->first_slot_num;
628 				which = 1; /* it is RXE */
629 			} else {
630 				first_slot = opt_vg_ptr->first_slot_num;
631 				number = opt_vg_ptr->chassis_num;
632 				which = 0;
633 			}
634 		} else {
635 			first_slot = opt_vg_ptr->first_slot_num;
636 			number = opt_vg_ptr->chassis_num;
637 			which = 0;
638 		}
639 		++flag;
640 	} else if (opt_lo_ptr) {
641 		number = opt_lo_ptr->chassis_num;
642 		first_slot = opt_lo_ptr->first_slot_num;
643 		which = 1;
644 		++flag;
645 	} else if (rio_table_ptr) {
646 		if (rio_table_ptr->ver_num == 3) {
647 			/* if both NULL and we DO have correct RIO table in BIOS */
648 			return NULL;
649 		}
650 	}
651 	if (!flag) {
652 		if (slot_cur->ctrl->ctlr_type == 4) {
653 			first_slot = calculate_first_slot(slot_num);
654 			which = 1;
655 		} else {
656 			which = 0;
657 		}
658 	}
659 
660 	sprintf(str, "%s%dslot%d",
661 		which == 0 ? "chassis" : "rxe",
662 		number, slot_num - first_slot + 1);
663 	return str;
664 }
665 
666 static int fillslotinfo(struct hotplug_slot *hotplug_slot)
667 {
668 	struct slot *slot;
669 	int rc = 0;
670 
671 	slot = to_slot(hotplug_slot);
672 	rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
673 	return rc;
674 }
675 
676 static struct pci_driver ibmphp_driver;
677 
678 /*
679  * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
680  * each hpc from physical address to a list of hot plug controllers based on
681  * hpc descriptors.
682  */
683 static int __init ebda_rsrc_controller(void)
684 {
685 	u16 addr, addr_slot, addr_bus;
686 	u8 ctlr_id, temp, bus_index;
687 	u16 ctlr, slot, bus;
688 	u16 slot_num, bus_num, index;
689 	struct controller *hpc_ptr;
690 	struct ebda_hpc_bus *bus_ptr;
691 	struct ebda_hpc_slot *slot_ptr;
692 	struct bus_info *bus_info_ptr1, *bus_info_ptr2;
693 	int rc;
694 	struct slot *tmp_slot;
695 	char name[SLOT_NAME_SIZE];
696 
697 	addr = hpc_list_ptr->phys_addr;
698 	for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
699 		bus_index = 1;
700 		ctlr_id = readb(io_mem + addr);
701 		addr += 1;
702 		slot_num = readb(io_mem + addr);
703 
704 		addr += 1;
705 		addr_slot = addr;	/* offset of slot structure */
706 		addr += (slot_num * 4);
707 
708 		bus_num = readb(io_mem + addr);
709 
710 		addr += 1;
711 		addr_bus = addr;	/* offset of bus */
712 		addr += (bus_num * 9);	/* offset of ctlr_type */
713 		temp = readb(io_mem + addr);
714 
715 		addr += 1;
716 		/* init hpc structure */
717 		hpc_ptr = alloc_ebda_hpc(slot_num, bus_num);
718 		if (!hpc_ptr) {
719 			return -ENOMEM;
720 		}
721 		hpc_ptr->ctlr_id = ctlr_id;
722 		hpc_ptr->ctlr_relative_id = ctlr;
723 		hpc_ptr->slot_count = slot_num;
724 		hpc_ptr->bus_count = bus_num;
725 		debug("now enter ctlr data structure ---\n");
726 		debug("ctlr id: %x\n", ctlr_id);
727 		debug("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
728 		debug("count of slots controlled by this ctlr: %x\n", slot_num);
729 		debug("count of buses controlled by this ctlr: %x\n", bus_num);
730 
731 		/* init slot structure, fetch slot, bus, cap... */
732 		slot_ptr = hpc_ptr->slots;
733 		for (slot = 0; slot < slot_num; slot++) {
734 			slot_ptr->slot_num = readb(io_mem + addr_slot);
735 			slot_ptr->slot_bus_num = readb(io_mem + addr_slot + slot_num);
736 			slot_ptr->ctl_index = readb(io_mem + addr_slot + 2*slot_num);
737 			slot_ptr->slot_cap = readb(io_mem + addr_slot + 3*slot_num);
738 
739 			// create bus_info lined list --- if only one slot per bus: slot_min = slot_max
740 
741 			bus_info_ptr2 = ibmphp_find_same_bus_num(slot_ptr->slot_bus_num);
742 			if (!bus_info_ptr2) {
743 				bus_info_ptr1 = kzalloc_obj(struct bus_info,
744 							    GFP_KERNEL);
745 				if (!bus_info_ptr1) {
746 					rc = -ENOMEM;
747 					goto error_no_slot;
748 				}
749 				bus_info_ptr1->slot_min = slot_ptr->slot_num;
750 				bus_info_ptr1->slot_max = slot_ptr->slot_num;
751 				bus_info_ptr1->slot_count += 1;
752 				bus_info_ptr1->busno = slot_ptr->slot_bus_num;
753 				bus_info_ptr1->index = bus_index++;
754 				bus_info_ptr1->current_speed = 0xff;
755 				bus_info_ptr1->current_bus_mode = 0xff;
756 
757 				bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
758 
759 				list_add_tail(&bus_info_ptr1->bus_info_list, &bus_info_head);
760 
761 			} else {
762 				bus_info_ptr2->slot_min = min(bus_info_ptr2->slot_min, slot_ptr->slot_num);
763 				bus_info_ptr2->slot_max = max(bus_info_ptr2->slot_max, slot_ptr->slot_num);
764 				bus_info_ptr2->slot_count += 1;
765 
766 			}
767 
768 			// end of creating the bus_info linked list
769 
770 			slot_ptr++;
771 			addr_slot += 1;
772 		}
773 
774 		/* init bus structure */
775 		bus_ptr = hpc_ptr->buses;
776 		for (bus = 0; bus < bus_num; bus++) {
777 			bus_ptr->bus_num = readb(io_mem + addr_bus + bus);
778 			bus_ptr->slots_at_33_conv = readb(io_mem + addr_bus + bus_num + 8 * bus);
779 			bus_ptr->slots_at_66_conv = readb(io_mem + addr_bus + bus_num + 8 * bus + 1);
780 
781 			bus_ptr->slots_at_66_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 2);
782 
783 			bus_ptr->slots_at_100_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 3);
784 
785 			bus_ptr->slots_at_133_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 4);
786 
787 			bus_info_ptr2 = ibmphp_find_same_bus_num(bus_ptr->bus_num);
788 			if (bus_info_ptr2) {
789 				bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
790 				bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
791 				bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
792 				bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
793 				bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix;
794 			}
795 			bus_ptr++;
796 		}
797 
798 		hpc_ptr->ctlr_type = temp;
799 
800 		switch (hpc_ptr->ctlr_type) {
801 			case 1:
802 				hpc_ptr->u.pci_ctlr.bus = readb(io_mem + addr);
803 				hpc_ptr->u.pci_ctlr.dev_fun = readb(io_mem + addr + 1);
804 				hpc_ptr->irq = readb(io_mem + addr + 2);
805 				addr += 3;
806 				debug("ctrl bus = %x, ctlr devfun = %x, irq = %x\n",
807 					hpc_ptr->u.pci_ctlr.bus,
808 					hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
809 				break;
810 
811 			case 0:
812 				hpc_ptr->u.isa_ctlr.io_start = readw(io_mem + addr);
813 				hpc_ptr->u.isa_ctlr.io_end = readw(io_mem + addr + 2);
814 				if (!request_region(hpc_ptr->u.isa_ctlr.io_start,
815 						     (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
816 						     "ibmphp")) {
817 					rc = -ENODEV;
818 					goto error_no_slot;
819 				}
820 				hpc_ptr->irq = readb(io_mem + addr + 4);
821 				addr += 5;
822 				break;
823 
824 			case 2:
825 			case 4:
826 				hpc_ptr->u.wpeg_ctlr.wpegbbar = readl(io_mem + addr);
827 				hpc_ptr->u.wpeg_ctlr.i2c_addr = readb(io_mem + addr + 4);
828 				hpc_ptr->irq = readb(io_mem + addr + 5);
829 				addr += 6;
830 				break;
831 			default:
832 				rc = -ENODEV;
833 				goto error_no_slot;
834 		}
835 
836 		//reorganize chassis' linked list
837 		combine_wpg_for_chassis();
838 		combine_wpg_for_expansion();
839 		hpc_ptr->revision = 0xff;
840 		hpc_ptr->options = 0xff;
841 		hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
842 		hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;
843 
844 		// register slots with hpc core as well as create linked list of ibm slot
845 		for (index = 0; index < hpc_ptr->slot_count; index++) {
846 			tmp_slot = kzalloc_obj(*tmp_slot, GFP_KERNEL);
847 			if (!tmp_slot) {
848 				rc = -ENOMEM;
849 				goto error_no_slot;
850 			}
851 
852 			tmp_slot->flag = 1;
853 
854 			tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
855 			if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
856 				tmp_slot->supported_speed =  3;
857 			else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
858 				tmp_slot->supported_speed =  2;
859 			else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
860 				tmp_slot->supported_speed =  1;
861 
862 			if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
863 				tmp_slot->supported_bus_mode = 1;
864 			else
865 				tmp_slot->supported_bus_mode = 0;
866 
867 
868 			tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;
869 
870 			bus_info_ptr1 = ibmphp_find_same_bus_num(hpc_ptr->slots[index].slot_bus_num);
871 			if (!bus_info_ptr1) {
872 				rc = -ENODEV;
873 				goto error;
874 			}
875 			tmp_slot->bus_on = bus_info_ptr1;
876 			bus_info_ptr1 = NULL;
877 			tmp_slot->ctrl = hpc_ptr;
878 
879 			tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
880 			tmp_slot->number = hpc_ptr->slots[index].slot_num;
881 
882 			rc = fillslotinfo(&tmp_slot->hotplug_slot);
883 			if (rc)
884 				goto error;
885 
886 			rc = ibmphp_init_devno(&tmp_slot);
887 			if (rc)
888 				goto error;
889 			tmp_slot->hotplug_slot.ops = &ibmphp_hotplug_slot_ops;
890 
891 			// end of registering ibm slot with hotplug core
892 
893 			list_add(&tmp_slot->ibm_slot_list, &ibmphp_slot_head);
894 		}
895 
896 		print_bus_info();
897 		list_add(&hpc_ptr->ebda_hpc_list, &ebda_hpc_head);
898 
899 	}			/* each hpc  */
900 
901 	list_for_each_entry(tmp_slot, &ibmphp_slot_head, ibm_slot_list) {
902 		snprintf(name, SLOT_NAME_SIZE, "%s", create_file_name(tmp_slot));
903 		pci_hp_register(&tmp_slot->hotplug_slot,
904 			pci_find_bus(0, tmp_slot->bus), tmp_slot->device, name);
905 	}
906 
907 	print_ebda_hpc();
908 	print_ibm_slot();
909 	return 0;
910 
911 error:
912 	kfree(tmp_slot);
913 error_no_slot:
914 	free_ebda_hpc(hpc_ptr);
915 	return rc;
916 }
917 
918 /*
919  * map info (bus, devfun, start addr, end addr..) of i/o, memory,
920  * pfm from the physical addr to a list of resource.
921  */
922 static int __init ebda_rsrc_rsrc(void)
923 {
924 	u16 addr;
925 	short rsrc;
926 	u8 type, rsrc_type;
927 	struct ebda_pci_rsrc *rsrc_ptr;
928 
929 	addr = rsrc_list_ptr->phys_addr;
930 	debug("now entering rsrc land\n");
931 	debug("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
932 
933 	for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
934 		type = readb(io_mem + addr);
935 
936 		addr += 1;
937 		rsrc_type = type & EBDA_RSRC_TYPE_MASK;
938 
939 		if (rsrc_type == EBDA_IO_RSRC_TYPE) {
940 			rsrc_ptr = alloc_ebda_pci_rsrc();
941 			if (!rsrc_ptr) {
942 				iounmap(io_mem);
943 				return -ENOMEM;
944 			}
945 			rsrc_ptr->rsrc_type = type;
946 
947 			rsrc_ptr->bus_num = readb(io_mem + addr);
948 			rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
949 			rsrc_ptr->start_addr = readw(io_mem + addr + 2);
950 			rsrc_ptr->end_addr = readw(io_mem + addr + 4);
951 			addr += 6;
952 
953 			debug("rsrc from io type ----\n");
954 			debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
955 				rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
956 
957 			list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
958 		}
959 
960 		if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
961 			rsrc_ptr = alloc_ebda_pci_rsrc();
962 			if (!rsrc_ptr) {
963 				iounmap(io_mem);
964 				return -ENOMEM;
965 			}
966 			rsrc_ptr->rsrc_type = type;
967 
968 			rsrc_ptr->bus_num = readb(io_mem + addr);
969 			rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
970 			rsrc_ptr->start_addr = readl(io_mem + addr + 2);
971 			rsrc_ptr->end_addr = readl(io_mem + addr + 6);
972 			addr += 10;
973 
974 			debug("rsrc from mem or pfm ---\n");
975 			debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
976 				rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
977 
978 			list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
979 		}
980 	}
981 	kfree(rsrc_list_ptr);
982 	rsrc_list_ptr = NULL;
983 	print_ebda_pci_rsrc();
984 	return 0;
985 }
986 
987 u16 ibmphp_get_total_controllers(void)
988 {
989 	return hpc_list_ptr->num_ctlrs;
990 }
991 
992 struct slot *ibmphp_get_slot_from_physical_num(u8 physical_num)
993 {
994 	struct slot *slot;
995 
996 	list_for_each_entry(slot, &ibmphp_slot_head, ibm_slot_list) {
997 		if (slot->number == physical_num)
998 			return slot;
999 	}
1000 	return NULL;
1001 }
1002 
1003 /* To find:
1004  *	- the smallest slot number
1005  *	- the largest slot number
1006  *	- the total number of the slots based on each bus
1007  *	  (if only one slot per bus slot_min = slot_max )
1008  */
1009 struct bus_info *ibmphp_find_same_bus_num(u32 num)
1010 {
1011 	struct bus_info *ptr;
1012 
1013 	list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1014 		if (ptr->busno == num)
1015 			 return ptr;
1016 	}
1017 	return NULL;
1018 }
1019 
1020 /*  Finding relative bus number, in order to map corresponding
1021  *  bus register
1022  */
1023 int ibmphp_get_bus_index(u8 num)
1024 {
1025 	struct bus_info *ptr;
1026 
1027 	list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1028 		if (ptr->busno == num)
1029 			return ptr->index;
1030 	}
1031 	return -ENODEV;
1032 }
1033 
1034 void ibmphp_free_bus_info_queue(void)
1035 {
1036 	struct bus_info *bus_info, *next;
1037 
1038 	list_for_each_entry_safe(bus_info, next, &bus_info_head,
1039 				 bus_info_list) {
1040 		kfree (bus_info);
1041 	}
1042 }
1043 
1044 void ibmphp_free_ebda_hpc_queue(void)
1045 {
1046 	struct controller *controller = NULL, *next;
1047 	int pci_flag = 0;
1048 
1049 	list_for_each_entry_safe(controller, next, &ebda_hpc_head,
1050 				 ebda_hpc_list) {
1051 		if (controller->ctlr_type == 0)
1052 			release_region(controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
1053 		else if ((controller->ctlr_type == 1) && (!pci_flag)) {
1054 			++pci_flag;
1055 			pci_unregister_driver(&ibmphp_driver);
1056 		}
1057 		free_ebda_hpc(controller);
1058 	}
1059 }
1060 
1061 void ibmphp_free_ebda_pci_rsrc_queue(void)
1062 {
1063 	struct ebda_pci_rsrc *resource, *next;
1064 
1065 	list_for_each_entry_safe(resource, next, &ibmphp_ebda_pci_rsrc_head,
1066 				 ebda_pci_rsrc_list) {
1067 		kfree (resource);
1068 		resource = NULL;
1069 	}
1070 }
1071 
1072 static const struct pci_device_id id_table[] = {
1073 	{
1074 		.vendor		= PCI_VENDOR_ID_IBM,
1075 		.device		= HPC_DEVICE_ID,
1076 		.subvendor	= PCI_VENDOR_ID_IBM,
1077 		.subdevice	= HPC_SUBSYSTEM_ID,
1078 		.class		= ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1079 	}, {}
1080 };
1081 
1082 MODULE_DEVICE_TABLE(pci, id_table);
1083 
1084 static int ibmphp_probe(struct pci_dev *, const struct pci_device_id *);
1085 static struct pci_driver ibmphp_driver = {
1086 	.name		= "ibmphp",
1087 	.id_table	= id_table,
1088 	.probe		= ibmphp_probe,
1089 };
1090 
1091 int ibmphp_register_pci(void)
1092 {
1093 	struct controller *ctrl;
1094 	int rc = 0;
1095 
1096 	list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
1097 		if (ctrl->ctlr_type == 1) {
1098 			rc = pci_register_driver(&ibmphp_driver);
1099 			break;
1100 		}
1101 	}
1102 	return rc;
1103 }
1104 static int ibmphp_probe(struct pci_dev *dev, const struct pci_device_id *ids)
1105 {
1106 	struct controller *ctrl;
1107 
1108 	debug("inside ibmphp_probe\n");
1109 
1110 	list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
1111 		if (ctrl->ctlr_type == 1) {
1112 			if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
1113 				ctrl->ctrl_dev = dev;
1114 				debug("found device!!!\n");
1115 				debug("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
1116 				return 0;
1117 			}
1118 		}
1119 	}
1120 	return -ENODEV;
1121 }
1122