blob: 0f65ac5554344ce4a3c1bc474af73d12e0833dda [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM Hot Plug Controller Driver
3 *
4 * Written By: Tong Yu, IBM Corporation
5 *
6 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001-2003 IBM Corp.
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
20 * details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * Send feedback to <gregkh@us.ibm.com>
27 *
28 */
29
30#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/mm.h>
33#include <linux/slab.h>
34#include <linux/pci.h>
35#include <linux/list.h>
36#include <linux/init.h>
37#include "ibmphp.h"
38
39/*
40 * POST builds data blocks(in this data block definition, a char-1
41 * byte, short(or word)-2 byte, long(dword)-4 byte) in the Extended
42 * BIOS Data Area which describe the configuration of the hot-plug
43 * controllers and resources used by the PCI Hot-Plug devices.
44 *
45 * This file walks EBDA, maps data block from physical addr,
46 * reconstruct linked lists about all system resource(MEM, PFM, IO)
47 * already assigned by POST, as well as linked lists about hot plug
48 * controllers (ctlr#, slot#, bus&slot features...)
49 */
50
51/* Global lists */
52LIST_HEAD (ibmphp_ebda_pci_rsrc_head);
53LIST_HEAD (ibmphp_slot_head);
54
55/* Local variables */
56static struct ebda_hpc_list *hpc_list_ptr;
57static struct ebda_rsrc_list *rsrc_list_ptr;
58static struct rio_table_hdr *rio_table_ptr = NULL;
59static LIST_HEAD (ebda_hpc_head);
60static LIST_HEAD (bus_info_head);
61static LIST_HEAD (rio_vg_head);
62static LIST_HEAD (rio_lo_head);
63static LIST_HEAD (opt_vg_head);
64static LIST_HEAD (opt_lo_head);
65static void __iomem *io_mem;
66
67/* Local functions */
68static int ebda_rsrc_controller (void);
69static int ebda_rsrc_rsrc (void);
70static int ebda_rio_table (void);
71
72static struct ebda_hpc_list * __init alloc_ebda_hpc_list (void)
73{
Eric Sesterhennf5afe802006-02-28 15:34:49 +010074 return kzalloc(sizeof(struct ebda_hpc_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static struct controller *alloc_ebda_hpc (u32 slot_count, u32 bus_count)
78{
79 struct controller *controller;
80 struct ebda_hpc_slot *slots;
81 struct ebda_hpc_bus *buses;
82
Eric Sesterhennf5afe802006-02-28 15:34:49 +010083 controller = kzalloc(sizeof(struct controller), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!controller)
85 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Sesterhennf5afe802006-02-28 15:34:49 +010087 slots = kcalloc(slot_count, sizeof(struct ebda_hpc_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!slots)
89 goto error_contr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 controller->slots = slots;
91
Eric Sesterhennf5afe802006-02-28 15:34:49 +010092 buses = kcalloc(bus_count, sizeof(struct ebda_hpc_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!buses)
94 goto error_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 controller->buses = buses;
96
97 return controller;
98error_slots:
99 kfree(controller->slots);
100error_contr:
101 kfree(controller);
102error:
103 return NULL;
104}
105
106static void free_ebda_hpc (struct controller *controller)
107{
108 kfree (controller->slots);
109 kfree (controller->buses);
110 kfree (controller);
111}
112
113static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list (void)
114{
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100115 return kzalloc(sizeof(struct ebda_rsrc_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc (void)
119{
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100120 return kzalloc(sizeof(struct ebda_pci_rsrc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void __init print_bus_info (void)
124{
125 struct bus_info *ptr;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700126
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700127 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800128 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);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700134
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800135 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 }
142}
143
144static void print_lo_info (void)
145{
146 struct rio_detail *ptr;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700147 debug ("print_lo_info ----\n");
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700148 list_for_each_entry(ptr, &rio_lo_head, rio_detail_list) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800149 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 }
157}
158
159static void print_vg_info (void)
160{
161 struct rio_detail *ptr;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800162 debug ("%s ---\n", __func__);
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700163 list_for_each_entry(ptr, &rio_vg_head, rio_detail_list) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800164 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 }
172}
173
174static void __init print_ebda_pci_rsrc (void)
175{
176 struct ebda_pci_rsrc *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700178 list_for_each_entry(ptr, &ibmphp_ebda_pci_rsrc_head, ebda_pci_rsrc_list) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700179 debug ("%s - rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800180 __func__, ptr->rsrc_type ,ptr->bus_num, ptr->dev_fun,ptr->start_addr, ptr->end_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
184static void __init print_ibm_slot (void)
185{
186 struct slot *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700188 list_for_each_entry(ptr, &ibmphp_slot_head, ibm_slot_list) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800189 debug ("%s - slot_number: %x\n", __func__, ptr->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191}
192
193static void __init print_opt_vg (void)
194{
195 struct opt_rio *ptr;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800196 debug ("%s ---\n", __func__);
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700197 list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800198 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203}
204
205static void __init print_ebda_hpc (void)
206{
207 struct controller *hpc_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 u16 index;
209
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700210 list_for_each_entry(hpc_ptr, &ebda_hpc_head, ebda_hpc_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 for (index = 0; index < hpc_ptr->slot_count; index++) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800212 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 for (index = 0; index < hpc_ptr->bus_count; index++) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800219 debug ("%s - bus# of each bus controlled by this ctlr: %x\n", __func__, hpc_ptr->buses[index].bus_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800222 debug ("%s - type of hpc: %x\n", __func__, hpc_ptr->ctlr_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 switch (hpc_ptr->ctlr_type) {
224 case 1:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800225 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229
230 case 0:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800231 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235
236 case 2:
237 case 4:
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800238 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 }
243 }
244}
245
246int __init ibmphp_access_ebda (void)
247{
Chandrub0fc8892010-01-11 11:49:21 +0530248 u8 format, num_ctlrs, rio_complete, hs_complete, ebda_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700262
Chandrub0fc8892010-01-11 11:49:21 +0530263 io_mem = ioremap(ebda_seg<<4, 1);
Andrew Mortonba02b242010-02-02 14:45:54 -0800264 if (!io_mem)
265 return -ENOMEM;
Chandrub0fc8892010-01-11 11:49:21 +0530266 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!io_mem )
274 return -ENOMEM;
275 next_offset = 0x180;
276
277 for (;;) {
278 offset = next_offset;
Steven Rostedtac3abf22010-11-08 23:20:27 -0500279
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 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;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700313 rc_id = readw (io_mem + sub_addr); /* sub blk id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
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);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700333 debug ("offset of hpc data structure entries: %x\n ", sub_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
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);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700362 debug ("offset of rsrc data structure entries: %x\n ", sub_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
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
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100370 rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL);
Julia Lawall8f0cddd2012-01-12 10:55:16 +0100371 if (!rio_table_ptr) {
372 rc = -ENOMEM;
373 goto out;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rio_table_ptr->ver_num = readb (io_mem + offset);
376 rio_table_ptr->scal_count = readb (io_mem + offset + 1);
377 rio_table_ptr->riodev_count = readb (io_mem + offset + 2);
378 rio_table_ptr->offset = offset +3 ;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 debug("info about rio table hdr ---\n");
381 debug("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ",
382 rio_table_ptr->ver_num, rio_table_ptr->scal_count,
383 rio_table_ptr->riodev_count, rio_table_ptr->offset);
384
385 rio_complete = 1;
386 }
387 }
388
389 if (!hs_complete && !rio_complete)
390 goto error_nodev;
391
392 if (rio_table_ptr) {
393 if (rio_complete && rio_table_ptr->ver_num == 3) {
394 rc = ebda_rio_table ();
395 if (rc)
396 goto out;
397 }
398 }
399 rc = ebda_rsrc_controller ();
400 if (rc)
401 goto out;
402
403 rc = ebda_rsrc_rsrc ();
404 goto out;
405error_nodev:
406 rc = -ENODEV;
407out:
408 iounmap (io_mem);
409 return rc;
410}
411
412/*
413 * map info of scalability details and rio details from physical address
414 */
415static int __init ebda_rio_table (void)
416{
417 u16 offset;
418 u8 i;
419 struct rio_detail *rio_detail_ptr;
420
421 offset = rio_table_ptr->offset;
422 offset += 12 * rio_table_ptr->scal_count;
423
424 // we do concern about rio details
425 for (i = 0; i < rio_table_ptr->riodev_count; i++) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100426 rio_detail_ptr = kzalloc(sizeof(struct rio_detail), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!rio_detail_ptr)
428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 rio_detail_ptr->rio_node_id = readb (io_mem + offset);
430 rio_detail_ptr->bbar = readl (io_mem + offset + 1);
431 rio_detail_ptr->rio_type = readb (io_mem + offset + 5);
432 rio_detail_ptr->owner_id = readb (io_mem + offset + 6);
433 rio_detail_ptr->port0_node_connect = readb (io_mem + offset + 7);
434 rio_detail_ptr->port0_port_connect = readb (io_mem + offset + 8);
435 rio_detail_ptr->port1_node_connect = readb (io_mem + offset + 9);
436 rio_detail_ptr->port1_port_connect = readb (io_mem + offset + 10);
437 rio_detail_ptr->first_slot_num = readb (io_mem + offset + 11);
438 rio_detail_ptr->status = readb (io_mem + offset + 12);
439 rio_detail_ptr->wpindex = readb (io_mem + offset + 13);
440 rio_detail_ptr->chassis_num = readb (io_mem + offset + 14);
441// 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);
442 //create linked list of chassis
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700443 if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 list_add (&rio_detail_ptr->rio_detail_list, &rio_vg_head);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700445 //create linked list of expansion box
446 else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 list_add (&rio_detail_ptr->rio_detail_list, &rio_lo_head);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700448 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 // not in my concern
450 kfree (rio_detail_ptr);
451 offset += 15;
452 }
453 print_lo_info ();
454 print_vg_info ();
455 return 0;
456}
457
458/*
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700459 * reorganizing linked list of chassis
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
461static struct opt_rio *search_opt_vg (u8 chassis_num)
462{
463 struct opt_rio *ptr;
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700464 list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (ptr->chassis_num == chassis_num)
466 return ptr;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return NULL;
469}
470
471static int __init combine_wpg_for_chassis (void)
472{
473 struct opt_rio *opt_rio_ptr = NULL;
474 struct rio_detail *rio_detail_ptr = NULL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700475
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700476 list_for_each_entry(rio_detail_ptr, &rio_vg_head, rio_detail_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 opt_rio_ptr = search_opt_vg (rio_detail_ptr->chassis_num);
478 if (!opt_rio_ptr) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100479 opt_rio_ptr = kzalloc(sizeof(struct opt_rio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!opt_rio_ptr)
481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
483 opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
484 opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
485 opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
486 list_add (&opt_rio_ptr->opt_rio_list, &opt_vg_head);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 opt_rio_ptr->first_slot_num = min (opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
489 opt_rio_ptr->middle_num = max (opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 print_opt_vg ();
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700493 return 0;
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496/*
akpm@linux-foundation.orga8d2dbd2008-08-22 13:28:17 -0700497 * reorganizing linked list of expansion box
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499static struct opt_rio_lo *search_opt_lo (u8 chassis_num)
500{
501 struct opt_rio_lo *ptr;
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700502 list_for_each_entry(ptr, &opt_lo_head, opt_rio_lo_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (ptr->chassis_num == chassis_num)
504 return ptr;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return NULL;
507}
508
509static int combine_wpg_for_expansion (void)
510{
511 struct opt_rio_lo *opt_rio_lo_ptr = NULL;
512 struct rio_detail *rio_detail_ptr = NULL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700513
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700514 list_for_each_entry(rio_detail_ptr, &rio_lo_head, rio_detail_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 opt_rio_lo_ptr = search_opt_lo (rio_detail_ptr->chassis_num);
516 if (!opt_rio_lo_ptr) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100517 opt_rio_lo_ptr = kzalloc(sizeof(struct opt_rio_lo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!opt_rio_lo_ptr)
519 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
521 opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
522 opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
523 opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
524 opt_rio_lo_ptr->pack_count = 1;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 list_add (&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 opt_rio_lo_ptr->first_slot_num = min (opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
529 opt_rio_lo_ptr->middle_num = max (opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
530 opt_rio_lo_ptr->pack_count = 2;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537/* Since we don't know the max slot number per each chassis, hence go
538 * through the list of all chassis to find out the range
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700539 * Arguments: slot_num, 1st slot number of the chassis we think we are on,
540 * var (0 = chassis, 1 = expansion box)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static int first_slot_num (u8 slot_num, u8 first_slot, u8 var)
543{
544 struct opt_rio *opt_vg_ptr = NULL;
545 struct opt_rio_lo *opt_lo_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int rc = 0;
547
548 if (!var) {
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700549 list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700550 if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rc = -ENODEV;
552 break;
553 }
554 }
555 } else {
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700556 list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
558 rc = -ENODEV;
559 break;
560 }
561 }
562 }
563 return rc;
564}
565
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400566static struct opt_rio_lo *find_rxe_num (u8 slot_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 struct opt_rio_lo *opt_lo_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700570 list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 //check to see if this slot_num belongs to expansion box
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700572 if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_lo_ptr->first_slot_num, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return opt_lo_ptr;
574 }
575 return NULL;
576}
577
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400578static struct opt_rio *find_chassis_num (u8 slot_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct opt_rio *opt_vg_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700582 list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700583 //check to see if this slot_num belongs to chassis
584 if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num (slot_num, opt_vg_ptr->first_slot_num, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return opt_vg_ptr;
586 }
587 return NULL;
588}
589
590/* This routine will find out how many slots are in the chassis, so that
591 * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
592 */
593static u8 calculate_first_slot (u8 slot_num)
594{
595 u8 first_slot = 1;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400596 struct slot *slot_cur;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700597
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700598 list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (slot_cur->ctrl) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700600 if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 first_slot = slot_cur->ctrl->ending_slot_num;
602 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return first_slot + 1;
605
606}
Alex Chianga32615a2008-10-20 17:41:33 -0600607
608#define SLOT_NAME_SIZE 30
609
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400610static char *create_file_name (struct slot *slot_cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct opt_rio *opt_vg_ptr = NULL;
613 struct opt_rio_lo *opt_lo_ptr = NULL;
Alex Chianga32615a2008-10-20 17:41:33 -0600614 static char str[SLOT_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int which = 0; /* rxe = 1, chassis = 0 */
616 u8 number = 1; /* either chassis or rxe # */
617 u8 first_slot = 1;
618 u8 slot_num;
619 u8 flag = 0;
620
621 if (!slot_cur) {
622 err ("Structure passed is empty\n");
623 return NULL;
624 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 slot_num = slot_cur->number;
627
628 memset (str, 0, sizeof(str));
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (rio_table_ptr) {
631 if (rio_table_ptr->ver_num == 3) {
632 opt_vg_ptr = find_chassis_num (slot_num);
633 opt_lo_ptr = find_rxe_num (slot_num);
634 }
635 }
636 if (opt_vg_ptr) {
637 if (opt_lo_ptr) {
638 if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
639 number = opt_lo_ptr->chassis_num;
640 first_slot = opt_lo_ptr->first_slot_num;
641 which = 1; /* it is RXE */
642 } else {
643 first_slot = opt_vg_ptr->first_slot_num;
644 number = opt_vg_ptr->chassis_num;
645 which = 0;
646 }
647 } else {
648 first_slot = opt_vg_ptr->first_slot_num;
649 number = opt_vg_ptr->chassis_num;
650 which = 0;
651 }
652 ++flag;
653 } else if (opt_lo_ptr) {
654 number = opt_lo_ptr->chassis_num;
655 first_slot = opt_lo_ptr->first_slot_num;
656 which = 1;
657 ++flag;
658 } else if (rio_table_ptr) {
659 if (rio_table_ptr->ver_num == 3) {
660 /* if both NULL and we DO have correct RIO table in BIOS */
661 return NULL;
662 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!flag) {
665 if (slot_cur->ctrl->ctlr_type == 4) {
666 first_slot = calculate_first_slot (slot_num);
667 which = 1;
668 } else {
669 which = 0;
670 }
671 }
672
673 sprintf(str, "%s%dslot%d",
674 which == 0 ? "chassis" : "rxe",
675 number, slot_num - first_slot + 1);
676 return str;
677}
678
679static int fillslotinfo(struct hotplug_slot *hotplug_slot)
680{
681 struct slot *slot;
682 int rc = 0;
683
684 if (!hotplug_slot || !hotplug_slot->private)
685 return -EINVAL;
686
687 slot = hotplug_slot->private;
688 rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
689 if (rc)
690 return rc;
691
692 // power - enabled:1 not:0
693 hotplug_slot->info->power_status = SLOT_POWER(slot->status);
694
695 // attention - off:0, on:1, blinking:2
696 hotplug_slot->info->attention_status = SLOT_ATTN(slot->status, slot->ext_status);
697
698 // latch - open:1 closed:0
699 hotplug_slot->info->latch_status = SLOT_LATCH(slot->status);
700
701 // pci board - present:1 not:0
702 if (SLOT_PRESENT (slot->status))
703 hotplug_slot->info->adapter_status = 1;
704 else
705 hotplug_slot->info->adapter_status = 0;
706/*
707 if (slot->bus_on->supported_bus_mode
708 && (slot->bus_on->supported_speed == BUS_SPEED_66))
709 hotplug_slot->info->max_bus_speed_status = BUS_SPEED_66PCIX;
710 else
711 hotplug_slot->info->max_bus_speed_status = slot->bus_on->supported_speed;
712*/
713
714 return rc;
715}
716
717static void release_slot(struct hotplug_slot *hotplug_slot)
718{
719 struct slot *slot;
720
721 if (!hotplug_slot || !hotplug_slot->private)
722 return;
723
724 slot = hotplug_slot->private;
725 kfree(slot->hotplug_slot->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 kfree(slot->hotplug_slot);
727 slot->ctrl = NULL;
728 slot->bus_on = NULL;
729
730 /* we don't want to actually remove the resources, since free_resources will do just that */
731 ibmphp_unconfigure_card(&slot, -1);
732
733 kfree (slot);
734}
735
736static struct pci_driver ibmphp_driver;
737
738/*
739 * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
740 * each hpc from physical address to a list of hot plug controllers based on
741 * hpc descriptors.
742 */
743static int __init ebda_rsrc_controller (void)
744{
745 u16 addr, addr_slot, addr_bus;
746 u8 ctlr_id, temp, bus_index;
747 u16 ctlr, slot, bus;
748 u16 slot_num, bus_num, index;
749 struct hotplug_slot *hp_slot_ptr;
750 struct controller *hpc_ptr;
751 struct ebda_hpc_bus *bus_ptr;
752 struct ebda_hpc_slot *slot_ptr;
753 struct bus_info *bus_info_ptr1, *bus_info_ptr2;
754 int rc;
755 struct slot *tmp_slot;
Alex Chianga32615a2008-10-20 17:41:33 -0600756 char name[SLOT_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 addr = hpc_list_ptr->phys_addr;
759 for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
760 bus_index = 1;
761 ctlr_id = readb (io_mem + addr);
762 addr += 1;
763 slot_num = readb (io_mem + addr);
764
765 addr += 1;
766 addr_slot = addr; /* offset of slot structure */
767 addr += (slot_num * 4);
768
769 bus_num = readb (io_mem + addr);
770
771 addr += 1;
772 addr_bus = addr; /* offset of bus */
773 addr += (bus_num * 9); /* offset of ctlr_type */
774 temp = readb (io_mem + addr);
775
776 addr += 1;
777 /* init hpc structure */
778 hpc_ptr = alloc_ebda_hpc (slot_num, bus_num);
779 if (!hpc_ptr ) {
780 rc = -ENOMEM;
781 goto error_no_hpc;
782 }
783 hpc_ptr->ctlr_id = ctlr_id;
784 hpc_ptr->ctlr_relative_id = ctlr;
785 hpc_ptr->slot_count = slot_num;
786 hpc_ptr->bus_count = bus_num;
Masanari Iida367fa982012-07-23 22:39:51 +0900787 debug ("now enter ctlr data structure ---\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 debug ("ctlr id: %x\n", ctlr_id);
789 debug ("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
790 debug ("count of slots controlled by this ctlr: %x\n", slot_num);
791 debug ("count of buses controlled by this ctlr: %x\n", bus_num);
792
793 /* init slot structure, fetch slot, bus, cap... */
794 slot_ptr = hpc_ptr->slots;
795 for (slot = 0; slot < slot_num; slot++) {
796 slot_ptr->slot_num = readb (io_mem + addr_slot);
797 slot_ptr->slot_bus_num = readb (io_mem + addr_slot + slot_num);
798 slot_ptr->ctl_index = readb (io_mem + addr_slot + 2*slot_num);
799 slot_ptr->slot_cap = readb (io_mem + addr_slot + 3*slot_num);
800
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700801 // create bus_info lined list --- if only one slot per bus: slot_min = slot_max
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 bus_info_ptr2 = ibmphp_find_same_bus_num (slot_ptr->slot_bus_num);
804 if (!bus_info_ptr2) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100805 bus_info_ptr1 = kzalloc(sizeof(struct bus_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!bus_info_ptr1) {
807 rc = -ENOMEM;
808 goto error_no_hp_slot;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 bus_info_ptr1->slot_min = slot_ptr->slot_num;
811 bus_info_ptr1->slot_max = slot_ptr->slot_num;
812 bus_info_ptr1->slot_count += 1;
813 bus_info_ptr1->busno = slot_ptr->slot_bus_num;
814 bus_info_ptr1->index = bus_index++;
815 bus_info_ptr1->current_speed = 0xff;
816 bus_info_ptr1->current_bus_mode = 0xff;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 list_add_tail (&bus_info_ptr1->bus_info_list, &bus_info_head);
821
822 } else {
823 bus_info_ptr2->slot_min = min (bus_info_ptr2->slot_min, slot_ptr->slot_num);
824 bus_info_ptr2->slot_max = max (bus_info_ptr2->slot_max, slot_ptr->slot_num);
825 bus_info_ptr2->slot_count += 1;
826
827 }
828
829 // end of creating the bus_info linked list
830
831 slot_ptr++;
832 addr_slot += 1;
833 }
834
835 /* init bus structure */
836 bus_ptr = hpc_ptr->buses;
837 for (bus = 0; bus < bus_num; bus++) {
838 bus_ptr->bus_num = readb (io_mem + addr_bus + bus);
839 bus_ptr->slots_at_33_conv = readb (io_mem + addr_bus + bus_num + 8 * bus);
840 bus_ptr->slots_at_66_conv = readb (io_mem + addr_bus + bus_num + 8 * bus + 1);
841
842 bus_ptr->slots_at_66_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 2);
843
844 bus_ptr->slots_at_100_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 3);
845
846 bus_ptr->slots_at_133_pcix = readb (io_mem + addr_bus + bus_num + 8 * bus + 4);
847
848 bus_info_ptr2 = ibmphp_find_same_bus_num (bus_ptr->bus_num);
849 if (bus_info_ptr2) {
850 bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
851 bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
852 bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
853 bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700854 bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 bus_ptr++;
857 }
858
859 hpc_ptr->ctlr_type = temp;
860
861 switch (hpc_ptr->ctlr_type) {
862 case 1:
863 hpc_ptr->u.pci_ctlr.bus = readb (io_mem + addr);
864 hpc_ptr->u.pci_ctlr.dev_fun = readb (io_mem + addr + 1);
865 hpc_ptr->irq = readb (io_mem + addr + 2);
866 addr += 3;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700867 debug ("ctrl bus = %x, ctlr devfun = %x, irq = %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 hpc_ptr->u.pci_ctlr.bus,
869 hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
870 break;
871
872 case 0:
873 hpc_ptr->u.isa_ctlr.io_start = readw (io_mem + addr);
874 hpc_ptr->u.isa_ctlr.io_end = readw (io_mem + addr + 2);
875 if (!request_region (hpc_ptr->u.isa_ctlr.io_start,
876 (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
877 "ibmphp")) {
878 rc = -ENODEV;
879 goto error_no_hp_slot;
880 }
881 hpc_ptr->irq = readb (io_mem + addr + 4);
882 addr += 5;
883 break;
884
885 case 2:
886 case 4:
887 hpc_ptr->u.wpeg_ctlr.wpegbbar = readl (io_mem + addr);
888 hpc_ptr->u.wpeg_ctlr.i2c_addr = readb (io_mem + addr + 4);
889 hpc_ptr->irq = readb (io_mem + addr + 5);
890 addr += 6;
891 break;
892 default:
893 rc = -ENODEV;
894 goto error_no_hp_slot;
895 }
896
897 //reorganize chassis' linked list
898 combine_wpg_for_chassis ();
899 combine_wpg_for_expansion ();
900 hpc_ptr->revision = 0xff;
901 hpc_ptr->options = 0xff;
902 hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
903 hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;
904
905 // register slots with hpc core as well as create linked list of ibm slot
906 for (index = 0; index < hpc_ptr->slot_count; index++) {
907
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100908 hp_slot_ptr = kzalloc(sizeof(*hp_slot_ptr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!hp_slot_ptr) {
910 rc = -ENOMEM;
911 goto error_no_hp_slot;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100914 hp_slot_ptr->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!hp_slot_ptr->info) {
916 rc = -ENOMEM;
917 goto error_no_hp_info;
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100920 tmp_slot = kzalloc(sizeof(*tmp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!tmp_slot) {
922 rc = -ENOMEM;
923 goto error_no_slot;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Kristen Accardidc6712d2006-03-14 16:24:47 -0800926 tmp_slot->flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
929 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
930 tmp_slot->supported_speed = 3;
931 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
932 tmp_slot->supported_speed = 2;
933 else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
934 tmp_slot->supported_speed = 1;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
937 tmp_slot->supported_bus_mode = 1;
938 else
939 tmp_slot->supported_bus_mode = 0;
940
941
942 tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;
943
944 bus_info_ptr1 = ibmphp_find_same_bus_num (hpc_ptr->slots[index].slot_bus_num);
945 if (!bus_info_ptr1) {
Jesper Juhlb91aac22008-03-08 02:16:07 +0100946 kfree(tmp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 rc = -ENODEV;
948 goto error;
949 }
950 tmp_slot->bus_on = bus_info_ptr1;
951 bus_info_ptr1 = NULL;
952 tmp_slot->ctrl = hpc_ptr;
953
954 tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
955 tmp_slot->number = hpc_ptr->slots[index].slot_num;
956 tmp_slot->hotplug_slot = hp_slot_ptr;
957
958 hp_slot_ptr->private = tmp_slot;
959 hp_slot_ptr->release = release_slot;
960
961 rc = fillslotinfo(hp_slot_ptr);
962 if (rc)
963 goto error;
964
965 rc = ibmphp_init_devno ((struct slot **) &hp_slot_ptr->private);
966 if (rc)
967 goto error;
968 hp_slot_ptr->ops = &ibmphp_hotplug_slot_ops;
969
970 // end of registering ibm slot with hotplug core
971
972 list_add (& ((struct slot *)(hp_slot_ptr->private))->ibm_slot_list, &ibmphp_slot_head);
973 }
974
975 print_bus_info ();
976 list_add (&hpc_ptr->ebda_hpc_list, &ebda_hpc_head );
977
978 } /* each hpc */
979
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -0700980 list_for_each_entry(tmp_slot, &ibmphp_slot_head, ibm_slot_list) {
Alex Chianga32615a2008-10-20 17:41:33 -0600981 snprintf(name, SLOT_NAME_SIZE, "%s", create_file_name(tmp_slot));
Alex Chiangf46753c2008-06-10 15:28:50 -0600982 pci_hp_register(tmp_slot->hotplug_slot,
Alex Chianga32615a2008-10-20 17:41:33 -0600983 pci_find_bus(0, tmp_slot->bus), tmp_slot->device, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 print_ebda_hpc ();
987 print_ibm_slot ();
988 return 0;
989
990error:
991 kfree (hp_slot_ptr->private);
992error_no_slot:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 kfree (hp_slot_ptr->info);
994error_no_hp_info:
995 kfree (hp_slot_ptr);
996error_no_hp_slot:
997 free_ebda_hpc (hpc_ptr);
998error_no_hpc:
999 iounmap (io_mem);
1000 return rc;
1001}
1002
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * map info (bus, devfun, start addr, end addr..) of i/o, memory,
1005 * pfm from the physical addr to a list of resource.
1006 */
1007static int __init ebda_rsrc_rsrc (void)
1008{
1009 u16 addr;
1010 short rsrc;
1011 u8 type, rsrc_type;
1012 struct ebda_pci_rsrc *rsrc_ptr;
1013
1014 addr = rsrc_list_ptr->phys_addr;
1015 debug ("now entering rsrc land\n");
1016 debug ("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
1017
1018 for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
1019 type = readb (io_mem + addr);
1020
1021 addr += 1;
1022 rsrc_type = type & EBDA_RSRC_TYPE_MASK;
1023
1024 if (rsrc_type == EBDA_IO_RSRC_TYPE) {
1025 rsrc_ptr = alloc_ebda_pci_rsrc ();
1026 if (!rsrc_ptr) {
1027 iounmap (io_mem);
1028 return -ENOMEM;
1029 }
1030 rsrc_ptr->rsrc_type = type;
1031
1032 rsrc_ptr->bus_num = readb (io_mem + addr);
1033 rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1034 rsrc_ptr->start_addr = readw (io_mem + addr + 2);
1035 rsrc_ptr->end_addr = readw (io_mem + addr + 4);
1036 addr += 6;
1037
1038 debug ("rsrc from io type ----\n");
1039 debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
1040 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1041
1042 list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1043 }
1044
1045 if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
1046 rsrc_ptr = alloc_ebda_pci_rsrc ();
1047 if (!rsrc_ptr ) {
1048 iounmap (io_mem);
1049 return -ENOMEM;
1050 }
1051 rsrc_ptr->rsrc_type = type;
1052
1053 rsrc_ptr->bus_num = readb (io_mem + addr);
1054 rsrc_ptr->dev_fun = readb (io_mem + addr + 1);
1055 rsrc_ptr->start_addr = readl (io_mem + addr + 2);
1056 rsrc_ptr->end_addr = readl (io_mem + addr + 6);
1057 addr += 10;
1058
1059 debug ("rsrc from mem or pfm ---\n");
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001060 debug ("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);
1062
1063 list_add (&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
1064 }
1065 }
1066 kfree (rsrc_list_ptr);
1067 rsrc_list_ptr = NULL;
1068 print_ebda_pci_rsrc ();
1069 return 0;
1070}
1071
1072u16 ibmphp_get_total_controllers (void)
1073{
1074 return hpc_list_ptr->num_ctlrs;
1075}
1076
1077struct slot *ibmphp_get_slot_from_physical_num (u8 physical_num)
1078{
1079 struct slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -07001081 list_for_each_entry(slot, &ibmphp_slot_head, ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (slot->number == physical_num)
1083 return slot;
1084 }
1085 return NULL;
1086}
1087
1088/* To find:
1089 * - the smallest slot number
1090 * - the largest slot number
1091 * - the total number of the slots based on each bus
1092 * (if only one slot per bus slot_min = slot_max )
1093 */
1094struct bus_info *ibmphp_find_same_bus_num (u32 num)
1095{
1096 struct bus_info *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -07001098 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001099 if (ptr->busno == num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return ptr;
1101 }
1102 return NULL;
1103}
1104
1105/* Finding relative bus number, in order to map corresponding
1106 * bus register
1107 */
1108int ibmphp_get_bus_index (u8 num)
1109{
1110 struct bus_info *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -07001112 list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001113 if (ptr->busno == num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return ptr->index;
1115 }
1116 return -ENODEV;
1117}
1118
1119void ibmphp_free_bus_info_queue (void)
1120{
1121 struct bus_info *bus_info;
1122 struct list_head *list;
1123 struct list_head *next;
1124
1125 list_for_each_safe (list, next, &bus_info_head ) {
1126 bus_info = list_entry (list, struct bus_info, bus_info_list);
1127 kfree (bus_info);
1128 }
1129}
1130
1131void ibmphp_free_ebda_hpc_queue (void)
1132{
1133 struct controller *controller = NULL;
1134 struct list_head *list;
1135 struct list_head *next;
1136 int pci_flag = 0;
1137
1138 list_for_each_safe (list, next, &ebda_hpc_head) {
1139 controller = list_entry (list, struct controller, ebda_hpc_list);
1140 if (controller->ctlr_type == 0)
1141 release_region (controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
1142 else if ((controller->ctlr_type == 1) && (!pci_flag)) {
1143 ++pci_flag;
1144 pci_unregister_driver (&ibmphp_driver);
1145 }
1146 free_ebda_hpc (controller);
1147 }
1148}
1149
1150void ibmphp_free_ebda_pci_rsrc_queue (void)
1151{
1152 struct ebda_pci_rsrc *resource;
1153 struct list_head *list;
1154 struct list_head *next;
1155
1156 list_for_each_safe (list, next, &ibmphp_ebda_pci_rsrc_head) {
1157 resource = list_entry (list, struct ebda_pci_rsrc, ebda_pci_rsrc_list);
1158 kfree (resource);
1159 resource = NULL;
1160 }
1161}
1162
1163static struct pci_device_id id_table[] = {
1164 {
1165 .vendor = PCI_VENDOR_ID_IBM,
1166 .device = HPC_DEVICE_ID,
1167 .subvendor = PCI_VENDOR_ID_IBM,
1168 .subdevice = HPC_SUBSYSTEM_ID,
1169 .class = ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
1170 }, {}
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173MODULE_DEVICE_TABLE(pci, id_table);
1174
1175static int ibmphp_probe (struct pci_dev *, const struct pci_device_id *);
1176static struct pci_driver ibmphp_driver = {
1177 .name = "ibmphp",
1178 .id_table = id_table,
1179 .probe = ibmphp_probe,
1180};
1181
1182int ibmphp_register_pci (void)
1183{
1184 struct controller *ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int rc = 0;
1186
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -07001187 list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (ctrl->ctlr_type == 1) {
1189 rc = pci_register_driver(&ibmphp_driver);
1190 break;
1191 }
1192 }
1193 return rc;
1194}
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001195static int ibmphp_probe (struct pci_dev *dev, const struct pci_device_id *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct controller *ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 debug ("inside ibmphp_probe\n");
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001200
akpm@linux-foundation.org2fd39aa2008-08-22 13:30:14 -07001201 list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (ctrl->ctlr_type == 1) {
1203 if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
1204 ctrl->ctrl_dev = dev;
1205 debug ("found device!!!\n");
1206 debug ("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
1207 return 0;
1208 }
1209 }
1210 }
1211 return -ENODEV;
1212}