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