ibmphp_ebda.c 34.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * IBM Hot Plug Controller Driver
 *
 * Written By: Tong Yu, IBM Corporation
 *
 * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2001-2003 IBM Corp.
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to <gregkh@us.ibm.com>
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/list.h>
#include <linux/init.h>
#include "ibmphp.h"

/*
 * POST builds data blocks(in this data block definition, a char-1
 * byte, short(or word)-2 byte, long(dword)-4 byte) in the Extended
 * BIOS Data Area which describe the configuration of the hot-plug
 * controllers and resources used by the PCI Hot-Plug devices.
 *
 * This file walks EBDA, maps data block from physical addr,
 * reconstruct linked lists about all system resource(MEM, PFM, IO)
 * already assigned by POST, as well as linked lists about hot plug
 * controllers (ctlr#, slot#, bus&slot features...)
 */

/* Global lists */
B
Bogicevic Sasa 已提交
52 53
LIST_HEAD(ibmphp_ebda_pci_rsrc_head);
LIST_HEAD(ibmphp_slot_head);
L
Linus Torvalds 已提交
54 55 56 57 58

/* Local variables */
static struct ebda_hpc_list *hpc_list_ptr;
static struct ebda_rsrc_list *rsrc_list_ptr;
static struct rio_table_hdr *rio_table_ptr = NULL;
B
Bogicevic Sasa 已提交
59 60 61 62 63 64
static LIST_HEAD(ebda_hpc_head);
static LIST_HEAD(bus_info_head);
static LIST_HEAD(rio_vg_head);
static LIST_HEAD(rio_lo_head);
static LIST_HEAD(opt_vg_head);
static LIST_HEAD(opt_lo_head);
L
Linus Torvalds 已提交
65 66 67
static void __iomem *io_mem;

/* Local functions */
B
Bogicevic Sasa 已提交
68 69 70
static int ebda_rsrc_controller(void);
static int ebda_rsrc_rsrc(void);
static int ebda_rio_table(void);
L
Linus Torvalds 已提交
71

B
Bogicevic Sasa 已提交
72
static struct ebda_hpc_list * __init alloc_ebda_hpc_list(void)
L
Linus Torvalds 已提交
73
{
74
	return kzalloc(sizeof(struct ebda_hpc_list), GFP_KERNEL);
L
Linus Torvalds 已提交
75 76
}

B
Bogicevic Sasa 已提交
77
static struct controller *alloc_ebda_hpc(u32 slot_count, u32 bus_count)
L
Linus Torvalds 已提交
78 79 80 81 82
{
	struct controller *controller;
	struct ebda_hpc_slot *slots;
	struct ebda_hpc_bus *buses;

83
	controller = kzalloc(sizeof(struct controller), GFP_KERNEL);
L
Linus Torvalds 已提交
84 85 86
	if (!controller)
		goto error;

87
	slots = kcalloc(slot_count, sizeof(struct ebda_hpc_slot), GFP_KERNEL);
L
Linus Torvalds 已提交
88 89 90 91
	if (!slots)
		goto error_contr;
	controller->slots = slots;

92
	buses = kcalloc(bus_count, sizeof(struct ebda_hpc_bus), GFP_KERNEL);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
	if (!buses)
		goto error_slots;
	controller->buses = buses;

	return controller;
error_slots:
	kfree(controller->slots);
error_contr:
	kfree(controller);
error:
	return NULL;
}

B
Bogicevic Sasa 已提交
106
static void free_ebda_hpc(struct controller *controller)
L
Linus Torvalds 已提交
107
{
B
Bogicevic Sasa 已提交
108 109 110
	kfree(controller->slots);
	kfree(controller->buses);
	kfree(controller);
L
Linus Torvalds 已提交
111 112
}

B
Bogicevic Sasa 已提交
113
static struct ebda_rsrc_list * __init alloc_ebda_rsrc_list(void)
L
Linus Torvalds 已提交
114
{
115
	return kzalloc(sizeof(struct ebda_rsrc_list), GFP_KERNEL);
L
Linus Torvalds 已提交
116 117
}

B
Bogicevic Sasa 已提交
118
static struct ebda_pci_rsrc *alloc_ebda_pci_rsrc(void)
L
Linus Torvalds 已提交
119
{
120
	return kzalloc(sizeof(struct ebda_pci_rsrc), GFP_KERNEL);
L
Linus Torvalds 已提交
121 122
}

B
Bogicevic Sasa 已提交
123
static void __init print_bus_info(void)
L
Linus Torvalds 已提交
124 125
{
	struct bus_info *ptr;
126

127
	list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
B
Bogicevic Sasa 已提交
128 129 130 131 132 133 134 135 136 137 138 139
		debug("%s - slot_min = %x\n", __func__, ptr->slot_min);
		debug("%s - slot_max = %x\n", __func__, ptr->slot_max);
		debug("%s - slot_count = %x\n", __func__, ptr->slot_count);
		debug("%s - bus# = %x\n", __func__, ptr->busno);
		debug("%s - current_speed = %x\n", __func__, ptr->current_speed);
		debug("%s - controller_id = %x\n", __func__, ptr->controller_id);

		debug("%s - slots_at_33_conv = %x\n", __func__, ptr->slots_at_33_conv);
		debug("%s - slots_at_66_conv = %x\n", __func__, ptr->slots_at_66_conv);
		debug("%s - slots_at_66_pcix = %x\n", __func__, ptr->slots_at_66_pcix);
		debug("%s - slots_at_100_pcix = %x\n", __func__, ptr->slots_at_100_pcix);
		debug("%s - slots_at_133_pcix = %x\n", __func__, ptr->slots_at_133_pcix);
L
Linus Torvalds 已提交
140 141 142 143

	}
}

B
Bogicevic Sasa 已提交
144
static void print_lo_info(void)
L
Linus Torvalds 已提交
145 146
{
	struct rio_detail *ptr;
B
Bogicevic Sasa 已提交
147
	debug("print_lo_info ----\n");
148
	list_for_each_entry(ptr, &rio_lo_head, rio_detail_list) {
B
Bogicevic Sasa 已提交
149 150 151 152 153 154
		debug("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
		debug("%s - rio_type = %x\n", __func__, ptr->rio_type);
		debug("%s - owner_id = %x\n", __func__, ptr->owner_id);
		debug("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
		debug("%s - wpindex = %x\n", __func__, ptr->wpindex);
		debug("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
L
Linus Torvalds 已提交
155 156 157 158

	}
}

B
Bogicevic Sasa 已提交
159
static void print_vg_info(void)
L
Linus Torvalds 已提交
160 161
{
	struct rio_detail *ptr;
B
Bogicevic Sasa 已提交
162
	debug("%s ---\n", __func__);
163
	list_for_each_entry(ptr, &rio_vg_head, rio_detail_list) {
B
Bogicevic Sasa 已提交
164 165 166 167 168 169
		debug("%s - rio_node_id = %x\n", __func__, ptr->rio_node_id);
		debug("%s - rio_type = %x\n", __func__, ptr->rio_type);
		debug("%s - owner_id = %x\n", __func__, ptr->owner_id);
		debug("%s - first_slot_num = %x\n", __func__, ptr->first_slot_num);
		debug("%s - wpindex = %x\n", __func__, ptr->wpindex);
		debug("%s - chassis_num = %x\n", __func__, ptr->chassis_num);
L
Linus Torvalds 已提交
170 171 172 173

	}
}

B
Bogicevic Sasa 已提交
174
static void __init print_ebda_pci_rsrc(void)
L
Linus Torvalds 已提交
175 176 177
{
	struct ebda_pci_rsrc *ptr;

178
	list_for_each_entry(ptr, &ibmphp_ebda_pci_rsrc_head, ebda_pci_rsrc_list) {
B
Bogicevic Sasa 已提交
179 180
		debug("%s - rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
			__func__, ptr->rsrc_type, ptr->bus_num, ptr->dev_fun, ptr->start_addr, ptr->end_addr);
L
Linus Torvalds 已提交
181 182 183
	}
}

B
Bogicevic Sasa 已提交
184
static void __init print_ibm_slot(void)
L
Linus Torvalds 已提交
185 186 187
{
	struct slot *ptr;

188
	list_for_each_entry(ptr, &ibmphp_slot_head, ibm_slot_list) {
B
Bogicevic Sasa 已提交
189
		debug("%s - slot_number: %x\n", __func__, ptr->number);
L
Linus Torvalds 已提交
190 191 192
	}
}

B
Bogicevic Sasa 已提交
193
static void __init print_opt_vg(void)
L
Linus Torvalds 已提交
194 195
{
	struct opt_rio *ptr;
B
Bogicevic Sasa 已提交
196
	debug("%s ---\n", __func__);
197
	list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
B
Bogicevic Sasa 已提交
198 199 200 201
		debug("%s - rio_type %x\n", __func__, ptr->rio_type);
		debug("%s - chassis_num: %x\n", __func__, ptr->chassis_num);
		debug("%s - first_slot_num: %x\n", __func__, ptr->first_slot_num);
		debug("%s - middle_num: %x\n", __func__, ptr->middle_num);
L
Linus Torvalds 已提交
202 203 204
	}
}

B
Bogicevic Sasa 已提交
205
static void __init print_ebda_hpc(void)
L
Linus Torvalds 已提交
206 207 208 209
{
	struct controller *hpc_ptr;
	u16 index;

210
	list_for_each_entry(hpc_ptr, &ebda_hpc_head, ebda_hpc_list) {
L
Linus Torvalds 已提交
211
		for (index = 0; index < hpc_ptr->slot_count; index++) {
B
Bogicevic Sasa 已提交
212 213 214 215
			debug("%s - physical slot#: %x\n", __func__, hpc_ptr->slots[index].slot_num);
			debug("%s - pci bus# of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_bus_num);
			debug("%s - index into ctlr addr: %x\n", __func__, hpc_ptr->slots[index].ctl_index);
			debug("%s - cap of the slot: %x\n", __func__, hpc_ptr->slots[index].slot_cap);
L
Linus Torvalds 已提交
216 217
		}

218
		for (index = 0; index < hpc_ptr->bus_count; index++)
B
Bogicevic Sasa 已提交
219
			debug("%s - bus# of each bus controlled by this ctlr: %x\n", __func__, hpc_ptr->buses[index].bus_num);
L
Linus Torvalds 已提交
220

B
Bogicevic Sasa 已提交
221
		debug("%s - type of hpc: %x\n", __func__, hpc_ptr->ctlr_type);
L
Linus Torvalds 已提交
222 223
		switch (hpc_ptr->ctlr_type) {
		case 1:
B
Bogicevic Sasa 已提交
224 225 226
			debug("%s - bus: %x\n", __func__, hpc_ptr->u.pci_ctlr.bus);
			debug("%s - dev_fun: %x\n", __func__, hpc_ptr->u.pci_ctlr.dev_fun);
			debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
L
Linus Torvalds 已提交
227 228 229
			break;

		case 0:
B
Bogicevic Sasa 已提交
230 231 232
			debug("%s - io_start: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_start);
			debug("%s - io_end: %x\n", __func__, hpc_ptr->u.isa_ctlr.io_end);
			debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
L
Linus Torvalds 已提交
233 234 235 236
			break;

		case 2:
		case 4:
B
Bogicevic Sasa 已提交
237 238 239
			debug("%s - wpegbbar: %lx\n", __func__, hpc_ptr->u.wpeg_ctlr.wpegbbar);
			debug("%s - i2c_addr: %x\n", __func__, hpc_ptr->u.wpeg_ctlr.i2c_addr);
			debug("%s - irq: %x\n", __func__, hpc_ptr->irq);
L
Linus Torvalds 已提交
240 241 242 243 244
			break;
		}
	}
}

B
Bogicevic Sasa 已提交
245
int __init ibmphp_access_ebda(void)
L
Linus Torvalds 已提交
246
{
247
	u8 format, num_ctlrs, rio_complete, hs_complete, ebda_sz;
L
Linus Torvalds 已提交
248 249 250 251 252 253 254
	u16 ebda_seg, num_entries, next_offset, offset, blk_id, sub_addr, re, rc_id, re_id, base;
	int rc = 0;


	rio_complete = 0;
	hs_complete = 0;

B
Bogicevic Sasa 已提交
255 256
	io_mem = ioremap((0x40 << 4) + 0x0e, 2);
	if (!io_mem)
L
Linus Torvalds 已提交
257
		return -ENOMEM;
B
Bogicevic Sasa 已提交
258 259 260
	ebda_seg = readw(io_mem);
	iounmap(io_mem);
	debug("returned ebda segment: %x\n", ebda_seg);
261

262
	io_mem = ioremap(ebda_seg<<4, 1);
263 264
	if (!io_mem)
		return -ENOMEM;
265 266 267 268 269 270 271
	ebda_sz = readb(io_mem);
	iounmap(io_mem);
	debug("ebda size: %d(KiB)\n", ebda_sz);
	if (ebda_sz == 0)
		return -ENOMEM;

	io_mem = ioremap(ebda_seg<<4, (ebda_sz * 1024));
B
Bogicevic Sasa 已提交
272
	if (!io_mem)
L
Linus Torvalds 已提交
273 274 275 276 277
		return -ENOMEM;
	next_offset = 0x180;

	for (;;) {
		offset = next_offset;
278 279 280 281 282 283

		/* Make sure what we read is still in the mapped section */
		if (WARN(offset > (ebda_sz * 1024 - 4),
			 "ibmphp_ebda: next read is beyond ebda_sz\n"))
			break;

B
Bogicevic Sasa 已提交
284
		next_offset = readw(io_mem + offset);	/* offset of next blk */
L
Linus Torvalds 已提交
285 286 287 288

		offset += 2;
		if (next_offset == 0)	/* 0 indicate it's last blk */
			break;
B
Bogicevic Sasa 已提交
289
		blk_id = readw(io_mem + offset);	/* this blk id */
L
Linus Torvalds 已提交
290 291 292 293 294 295 296

		offset += 2;
		/* check if it is hot swap block or rio block */
		if (blk_id != 0x4853 && blk_id != 0x4752)
			continue;
		/* found hs table */
		if (blk_id == 0x4853) {
B
Bogicevic Sasa 已提交
297 298 299
			debug("now enter hot swap block---\n");
			debug("hot blk id: %x\n", blk_id);
			format = readb(io_mem + offset);
L
Linus Torvalds 已提交
300 301 302 303

			offset += 1;
			if (format != 4)
				goto error_nodev;
B
Bogicevic Sasa 已提交
304
			debug("hot blk format: %x\n", format);
L
Linus Torvalds 已提交
305 306 307 308
			/* hot swap sub blk */
			base = offset;

			sub_addr = base;
B
Bogicevic Sasa 已提交
309
			re = readw(io_mem + sub_addr);	/* next sub blk */
L
Linus Torvalds 已提交
310 311

			sub_addr += 2;
B
Bogicevic Sasa 已提交
312
			rc_id = readw(io_mem + sub_addr);	/* sub blk id */
L
Linus Torvalds 已提交
313 314 315 316 317

			sub_addr += 2;
			if (rc_id != 0x5243)
				goto error_nodev;
			/* rc sub blk signature  */
B
Bogicevic Sasa 已提交
318
			num_ctlrs = readb(io_mem + sub_addr);
L
Linus Torvalds 已提交
319 320

			sub_addr += 1;
B
Bogicevic Sasa 已提交
321
			hpc_list_ptr = alloc_ebda_hpc_list();
L
Linus Torvalds 已提交
322 323 324 325 326 327 328
			if (!hpc_list_ptr) {
				rc = -ENOMEM;
				goto out;
			}
			hpc_list_ptr->format = format;
			hpc_list_ptr->num_ctlrs = num_ctlrs;
			hpc_list_ptr->phys_addr = sub_addr;	/*  offset of RSRC_CONTROLLER blk */
B
Bogicevic Sasa 已提交
329 330 331 332
			debug("info about hpc descriptor---\n");
			debug("hot blk format: %x\n", format);
			debug("num of controller: %x\n", num_ctlrs);
			debug("offset of hpc data structure entries: %x\n ", sub_addr);
L
Linus Torvalds 已提交
333 334 335

			sub_addr = base + re;	/* re sub blk */
			/* FIXME: rc is never used/checked */
B
Bogicevic Sasa 已提交
336
			rc = readw(io_mem + sub_addr);	/* next sub blk */
L
Linus Torvalds 已提交
337 338

			sub_addr += 2;
B
Bogicevic Sasa 已提交
339
			re_id = readw(io_mem + sub_addr);	/* sub blk id */
L
Linus Torvalds 已提交
340 341 342 343 344 345

			sub_addr += 2;
			if (re_id != 0x5245)
				goto error_nodev;

			/* signature of re */
B
Bogicevic Sasa 已提交
346
			num_entries = readw(io_mem + sub_addr);
L
Linus Torvalds 已提交
347 348

			sub_addr += 2;	/* offset of RSRC_ENTRIES blk */
B
Bogicevic Sasa 已提交
349 350
			rsrc_list_ptr = alloc_ebda_rsrc_list();
			if (!rsrc_list_ptr) {
L
Linus Torvalds 已提交
351 352 353 354 355 356 357
				rc = -ENOMEM;
				goto out;
			}
			rsrc_list_ptr->format = format;
			rsrc_list_ptr->num_entries = num_entries;
			rsrc_list_ptr->phys_addr = sub_addr;

B
Bogicevic Sasa 已提交
358 359 360 361
			debug("info about rsrc descriptor---\n");
			debug("format: %x\n", format);
			debug("num of rsrc: %x\n", num_entries);
			debug("offset of rsrc data structure entries: %x\n ", sub_addr);
L
Linus Torvalds 已提交
362 363 364 365

			hs_complete = 1;
		} else {
		/* found rio table, blk_id == 0x4752 */
B
Bogicevic Sasa 已提交
366 367
			debug("now enter io table ---\n");
			debug("rio blk id: %x\n", blk_id);
L
Linus Torvalds 已提交
368

369
			rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL);
370 371 372 373
			if (!rio_table_ptr) {
				rc = -ENOMEM;
				goto out;
			}
B
Bogicevic Sasa 已提交
374 375 376 377
			rio_table_ptr->ver_num = readb(io_mem + offset);
			rio_table_ptr->scal_count = readb(io_mem + offset + 1);
			rio_table_ptr->riodev_count = readb(io_mem + offset + 2);
			rio_table_ptr->offset = offset + 3 ;
378

L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392
			debug("info about rio table hdr ---\n");
			debug("ver_num: %x\nscal_count: %x\nriodev_count: %x\noffset of rio table: %x\n ",
				rio_table_ptr->ver_num, rio_table_ptr->scal_count,
				rio_table_ptr->riodev_count, rio_table_ptr->offset);

			rio_complete = 1;
		}
	}

	if (!hs_complete && !rio_complete)
		goto error_nodev;

	if (rio_table_ptr) {
		if (rio_complete && rio_table_ptr->ver_num == 3) {
B
Bogicevic Sasa 已提交
393
			rc = ebda_rio_table();
L
Linus Torvalds 已提交
394 395 396 397
			if (rc)
				goto out;
		}
	}
B
Bogicevic Sasa 已提交
398
	rc = ebda_rsrc_controller();
L
Linus Torvalds 已提交
399 400 401
	if (rc)
		goto out;

B
Bogicevic Sasa 已提交
402
	rc = ebda_rsrc_rsrc();
L
Linus Torvalds 已提交
403 404 405 406
	goto out;
error_nodev:
	rc = -ENODEV;
out:
B
Bogicevic Sasa 已提交
407
	iounmap(io_mem);
L
Linus Torvalds 已提交
408 409 410 411 412 413
	return rc;
}

/*
 * map info of scalability details and rio details from physical address
 */
B
Bogicevic Sasa 已提交
414
static int __init ebda_rio_table(void)
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424
{
	u16 offset;
	u8 i;
	struct rio_detail *rio_detail_ptr;

	offset = rio_table_ptr->offset;
	offset += 12 * rio_table_ptr->scal_count;

	// we do concern about rio details
	for (i = 0; i < rio_table_ptr->riodev_count; i++) {
425
		rio_detail_ptr = kzalloc(sizeof(struct rio_detail), GFP_KERNEL);
L
Linus Torvalds 已提交
426 427
		if (!rio_detail_ptr)
			return -ENOMEM;
B
Bogicevic Sasa 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440
		rio_detail_ptr->rio_node_id = readb(io_mem + offset);
		rio_detail_ptr->bbar = readl(io_mem + offset + 1);
		rio_detail_ptr->rio_type = readb(io_mem + offset + 5);
		rio_detail_ptr->owner_id = readb(io_mem + offset + 6);
		rio_detail_ptr->port0_node_connect = readb(io_mem + offset + 7);
		rio_detail_ptr->port0_port_connect = readb(io_mem + offset + 8);
		rio_detail_ptr->port1_node_connect = readb(io_mem + offset + 9);
		rio_detail_ptr->port1_port_connect = readb(io_mem + offset + 10);
		rio_detail_ptr->first_slot_num = readb(io_mem + offset + 11);
		rio_detail_ptr->status = readb(io_mem + offset + 12);
		rio_detail_ptr->wpindex = readb(io_mem + offset + 13);
		rio_detail_ptr->chassis_num = readb(io_mem + offset + 14);
//		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);
L
Linus Torvalds 已提交
441
		//create linked list of chassis
442
		if (rio_detail_ptr->rio_type == 4 || rio_detail_ptr->rio_type == 5)
B
Bogicevic Sasa 已提交
443
			list_add(&rio_detail_ptr->rio_detail_list, &rio_vg_head);
444 445
		//create linked list of expansion box
		else if (rio_detail_ptr->rio_type == 6 || rio_detail_ptr->rio_type == 7)
B
Bogicevic Sasa 已提交
446
			list_add(&rio_detail_ptr->rio_detail_list, &rio_lo_head);
447
		else
L
Linus Torvalds 已提交
448
			// not in my concern
B
Bogicevic Sasa 已提交
449
			kfree(rio_detail_ptr);
L
Linus Torvalds 已提交
450 451
		offset += 15;
	}
B
Bogicevic Sasa 已提交
452 453
	print_lo_info();
	print_vg_info();
L
Linus Torvalds 已提交
454 455 456 457
	return 0;
}

/*
458
 * reorganizing linked list of chassis
L
Linus Torvalds 已提交
459
 */
B
Bogicevic Sasa 已提交
460
static struct opt_rio *search_opt_vg(u8 chassis_num)
L
Linus Torvalds 已提交
461 462
{
	struct opt_rio *ptr;
463
	list_for_each_entry(ptr, &opt_vg_head, opt_rio_list) {
L
Linus Torvalds 已提交
464 465
		if (ptr->chassis_num == chassis_num)
			return ptr;
466
	}
L
Linus Torvalds 已提交
467 468 469
	return NULL;
}

B
Bogicevic Sasa 已提交
470
static int __init combine_wpg_for_chassis(void)
L
Linus Torvalds 已提交
471 472 473
{
	struct opt_rio *opt_rio_ptr = NULL;
	struct rio_detail *rio_detail_ptr = NULL;
474

475
	list_for_each_entry(rio_detail_ptr, &rio_vg_head, rio_detail_list) {
B
Bogicevic Sasa 已提交
476
		opt_rio_ptr = search_opt_vg(rio_detail_ptr->chassis_num);
L
Linus Torvalds 已提交
477
		if (!opt_rio_ptr) {
478
			opt_rio_ptr = kzalloc(sizeof(struct opt_rio), GFP_KERNEL);
L
Linus Torvalds 已提交
479 480 481 482 483 484
			if (!opt_rio_ptr)
				return -ENOMEM;
			opt_rio_ptr->rio_type = rio_detail_ptr->rio_type;
			opt_rio_ptr->chassis_num = rio_detail_ptr->chassis_num;
			opt_rio_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
			opt_rio_ptr->middle_num = rio_detail_ptr->first_slot_num;
B
Bogicevic Sasa 已提交
485
			list_add(&opt_rio_ptr->opt_rio_list, &opt_vg_head);
486
		} else {
B
Bogicevic Sasa 已提交
487 488
			opt_rio_ptr->first_slot_num = min(opt_rio_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
			opt_rio_ptr->middle_num = max(opt_rio_ptr->middle_num, rio_detail_ptr->first_slot_num);
489
		}
L
Linus Torvalds 已提交
490
	}
B
Bogicevic Sasa 已提交
491
	print_opt_vg();
492 493
	return 0;
}
L
Linus Torvalds 已提交
494 495

/*
496
 * reorganizing linked list of expansion box
L
Linus Torvalds 已提交
497
 */
B
Bogicevic Sasa 已提交
498
static struct opt_rio_lo *search_opt_lo(u8 chassis_num)
L
Linus Torvalds 已提交
499 500
{
	struct opt_rio_lo *ptr;
501
	list_for_each_entry(ptr, &opt_lo_head, opt_rio_lo_list) {
L
Linus Torvalds 已提交
502 503
		if (ptr->chassis_num == chassis_num)
			return ptr;
504
	}
L
Linus Torvalds 已提交
505 506 507
	return NULL;
}

B
Bogicevic Sasa 已提交
508
static int combine_wpg_for_expansion(void)
L
Linus Torvalds 已提交
509 510 511
{
	struct opt_rio_lo *opt_rio_lo_ptr = NULL;
	struct rio_detail *rio_detail_ptr = NULL;
512

513
	list_for_each_entry(rio_detail_ptr, &rio_lo_head, rio_detail_list) {
B
Bogicevic Sasa 已提交
514
		opt_rio_lo_ptr = search_opt_lo(rio_detail_ptr->chassis_num);
L
Linus Torvalds 已提交
515
		if (!opt_rio_lo_ptr) {
516
			opt_rio_lo_ptr = kzalloc(sizeof(struct opt_rio_lo), GFP_KERNEL);
L
Linus Torvalds 已提交
517 518 519 520 521 522 523
			if (!opt_rio_lo_ptr)
				return -ENOMEM;
			opt_rio_lo_ptr->rio_type = rio_detail_ptr->rio_type;
			opt_rio_lo_ptr->chassis_num = rio_detail_ptr->chassis_num;
			opt_rio_lo_ptr->first_slot_num = rio_detail_ptr->first_slot_num;
			opt_rio_lo_ptr->middle_num = rio_detail_ptr->first_slot_num;
			opt_rio_lo_ptr->pack_count = 1;
524

B
Bogicevic Sasa 已提交
525
			list_add(&opt_rio_lo_ptr->opt_rio_lo_list, &opt_lo_head);
526
		} else {
B
Bogicevic Sasa 已提交
527 528
			opt_rio_lo_ptr->first_slot_num = min(opt_rio_lo_ptr->first_slot_num, rio_detail_ptr->first_slot_num);
			opt_rio_lo_ptr->middle_num = max(opt_rio_lo_ptr->middle_num, rio_detail_ptr->first_slot_num);
L
Linus Torvalds 已提交
529
			opt_rio_lo_ptr->pack_count = 2;
530
		}
L
Linus Torvalds 已提交
531
	}
532
	return 0;
L
Linus Torvalds 已提交
533
}
534

L
Linus Torvalds 已提交
535 536 537

/* Since we don't know the max slot number per each chassis, hence go
 * through the list of all chassis to find out the range
538 539
 * Arguments: slot_num, 1st slot number of the chassis we think we are on,
 * var (0 = chassis, 1 = expansion box)
L
Linus Torvalds 已提交
540
 */
B
Bogicevic Sasa 已提交
541
static int first_slot_num(u8 slot_num, u8 first_slot, u8 var)
L
Linus Torvalds 已提交
542 543 544 545 546 547
{
	struct opt_rio *opt_vg_ptr = NULL;
	struct opt_rio_lo *opt_lo_ptr = NULL;
	int rc = 0;

	if (!var) {
548
		list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
549
			if ((first_slot < opt_vg_ptr->first_slot_num) && (slot_num >= opt_vg_ptr->first_slot_num)) {
L
Linus Torvalds 已提交
550 551 552 553 554
				rc = -ENODEV;
				break;
			}
		}
	} else {
555
		list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564
			if ((first_slot < opt_lo_ptr->first_slot_num) && (slot_num >= opt_lo_ptr->first_slot_num)) {
				rc = -ENODEV;
				break;
			}
		}
	}
	return rc;
}

B
Bogicevic Sasa 已提交
565
static struct opt_rio_lo *find_rxe_num(u8 slot_num)
L
Linus Torvalds 已提交
566 567 568
{
	struct opt_rio_lo *opt_lo_ptr;

569
	list_for_each_entry(opt_lo_ptr, &opt_lo_head, opt_rio_lo_list) {
L
Linus Torvalds 已提交
570
		//check to see if this slot_num belongs to expansion box
B
Bogicevic Sasa 已提交
571
		if ((slot_num >= opt_lo_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_lo_ptr->first_slot_num, 1)))
L
Linus Torvalds 已提交
572 573 574 575 576
			return opt_lo_ptr;
	}
	return NULL;
}

B
Bogicevic Sasa 已提交
577
static struct opt_rio *find_chassis_num(u8 slot_num)
L
Linus Torvalds 已提交
578 579 580
{
	struct opt_rio *opt_vg_ptr;

581
	list_for_each_entry(opt_vg_ptr, &opt_vg_head, opt_rio_list) {
582
		//check to see if this slot_num belongs to chassis
B
Bogicevic Sasa 已提交
583
		if ((slot_num >= opt_vg_ptr->first_slot_num) && (!first_slot_num(slot_num, opt_vg_ptr->first_slot_num, 0)))
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591
			return opt_vg_ptr;
	}
	return NULL;
}

/* This routine will find out how many slots are in the chassis, so that
 * the slot numbers for rxe100 would start from 1, and not from 7, or 6 etc
 */
B
Bogicevic Sasa 已提交
592
static u8 calculate_first_slot(u8 slot_num)
L
Linus Torvalds 已提交
593 594
{
	u8 first_slot = 1;
R
Ryan Desfosses 已提交
595
	struct slot *slot_cur;
596

597
	list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
L
Linus Torvalds 已提交
598
		if (slot_cur->ctrl) {
599
			if ((slot_cur->ctrl->ctlr_type != 4) && (slot_cur->ctrl->ending_slot_num > first_slot) && (slot_num > slot_cur->ctrl->ending_slot_num))
L
Linus Torvalds 已提交
600 601
				first_slot = slot_cur->ctrl->ending_slot_num;
		}
602
	}
L
Linus Torvalds 已提交
603 604 605
	return first_slot + 1;

}
606 607 608

#define SLOT_NAME_SIZE 30

B
Bogicevic Sasa 已提交
609
static char *create_file_name(struct slot *slot_cur)
L
Linus Torvalds 已提交
610 611 612
{
	struct opt_rio *opt_vg_ptr = NULL;
	struct opt_rio_lo *opt_lo_ptr = NULL;
613
	static char str[SLOT_NAME_SIZE];
L
Linus Torvalds 已提交
614 615 616 617 618 619 620
	int which = 0; /* rxe = 1, chassis = 0 */
	u8 number = 1; /* either chassis or rxe # */
	u8 first_slot = 1;
	u8 slot_num;
	u8 flag = 0;

	if (!slot_cur) {
B
Bogicevic Sasa 已提交
621
		err("Structure passed is empty\n");
L
Linus Torvalds 已提交
622 623
		return NULL;
	}
624

L
Linus Torvalds 已提交
625 626
	slot_num = slot_cur->number;

B
Bogicevic Sasa 已提交
627
	memset(str, 0, sizeof(str));
628

L
Linus Torvalds 已提交
629 630
	if (rio_table_ptr) {
		if (rio_table_ptr->ver_num == 3) {
B
Bogicevic Sasa 已提交
631 632
			opt_vg_ptr = find_chassis_num(slot_num);
			opt_lo_ptr = find_rxe_num(slot_num);
L
Linus Torvalds 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		}
	}
	if (opt_vg_ptr) {
		if (opt_lo_ptr) {
			if ((slot_num - opt_vg_ptr->first_slot_num) > (slot_num - opt_lo_ptr->first_slot_num)) {
				number = opt_lo_ptr->chassis_num;
				first_slot = opt_lo_ptr->first_slot_num;
				which = 1; /* it is RXE */
			} else {
				first_slot = opt_vg_ptr->first_slot_num;
				number = opt_vg_ptr->chassis_num;
				which = 0;
			}
		} else {
			first_slot = opt_vg_ptr->first_slot_num;
			number = opt_vg_ptr->chassis_num;
			which = 0;
		}
		++flag;
	} else if (opt_lo_ptr) {
		number = opt_lo_ptr->chassis_num;
		first_slot = opt_lo_ptr->first_slot_num;
		which = 1;
		++flag;
	} else if (rio_table_ptr) {
		if (rio_table_ptr->ver_num == 3) {
			/* if both NULL and we DO have correct RIO table in BIOS */
			return NULL;
		}
662
	}
L
Linus Torvalds 已提交
663 664
	if (!flag) {
		if (slot_cur->ctrl->ctlr_type == 4) {
B
Bogicevic Sasa 已提交
665
			first_slot = calculate_first_slot(slot_num);
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
			which = 1;
		} else {
			which = 0;
		}
	}

	sprintf(str, "%s%dslot%d",
		which == 0 ? "chassis" : "rxe",
		number, slot_num - first_slot + 1);
	return str;
}

static int fillslotinfo(struct hotplug_slot *hotplug_slot)
{
	struct slot *slot;
	int rc = 0;

	if (!hotplug_slot || !hotplug_slot->private)
		return -EINVAL;

	slot = hotplug_slot->private;
	rc = ibmphp_hpc_readslot(slot, READ_ALLSTAT, NULL);
	if (rc)
		return rc;

	// power - enabled:1  not:0
	hotplug_slot->info->power_status = SLOT_POWER(slot->status);

	// attention - off:0, on:1, blinking:2
	hotplug_slot->info->attention_status = SLOT_ATTN(slot->status, slot->ext_status);

	// latch - open:1 closed:0
	hotplug_slot->info->latch_status = SLOT_LATCH(slot->status);

	// pci board - present:1 not:0
B
Bogicevic Sasa 已提交
701
	if (SLOT_PRESENT(slot->status))
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		hotplug_slot->info->adapter_status = 1;
	else
		hotplug_slot->info->adapter_status = 0;
/*
	if (slot->bus_on->supported_bus_mode
		&& (slot->bus_on->supported_speed == BUS_SPEED_66))
		hotplug_slot->info->max_bus_speed_status = BUS_SPEED_66PCIX;
	else
		hotplug_slot->info->max_bus_speed_status = slot->bus_on->supported_speed;
*/

	return rc;
}

static void release_slot(struct hotplug_slot *hotplug_slot)
{
	struct slot *slot;

	if (!hotplug_slot || !hotplug_slot->private)
		return;

	slot = hotplug_slot->private;
	kfree(slot->hotplug_slot->info);
	kfree(slot->hotplug_slot);
	slot->ctrl = NULL;
	slot->bus_on = NULL;

	/* we don't want to actually remove the resources, since free_resources will do just that */
	ibmphp_unconfigure_card(&slot, -1);

B
Bogicevic Sasa 已提交
732
	kfree(slot);
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741
}

static struct pci_driver ibmphp_driver;

/*
 * map info (ctlr-id, slot count, slot#.. bus count, bus#, ctlr type...) of
 * each hpc from physical address to a list of hot plug controllers based on
 * hpc descriptors.
 */
B
Bogicevic Sasa 已提交
742
static int __init ebda_rsrc_controller(void)
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751 752 753 754
{
	u16 addr, addr_slot, addr_bus;
	u8 ctlr_id, temp, bus_index;
	u16 ctlr, slot, bus;
	u16 slot_num, bus_num, index;
	struct hotplug_slot *hp_slot_ptr;
	struct controller *hpc_ptr;
	struct ebda_hpc_bus *bus_ptr;
	struct ebda_hpc_slot *slot_ptr;
	struct bus_info *bus_info_ptr1, *bus_info_ptr2;
	int rc;
	struct slot *tmp_slot;
755
	char name[SLOT_NAME_SIZE];
L
Linus Torvalds 已提交
756 757 758 759

	addr = hpc_list_ptr->phys_addr;
	for (ctlr = 0; ctlr < hpc_list_ptr->num_ctlrs; ctlr++) {
		bus_index = 1;
B
Bogicevic Sasa 已提交
760
		ctlr_id = readb(io_mem + addr);
L
Linus Torvalds 已提交
761
		addr += 1;
B
Bogicevic Sasa 已提交
762
		slot_num = readb(io_mem + addr);
L
Linus Torvalds 已提交
763 764 765 766 767

		addr += 1;
		addr_slot = addr;	/* offset of slot structure */
		addr += (slot_num * 4);

B
Bogicevic Sasa 已提交
768
		bus_num = readb(io_mem + addr);
L
Linus Torvalds 已提交
769 770 771 772

		addr += 1;
		addr_bus = addr;	/* offset of bus */
		addr += (bus_num * 9);	/* offset of ctlr_type */
B
Bogicevic Sasa 已提交
773
		temp = readb(io_mem + addr);
L
Linus Torvalds 已提交
774 775 776

		addr += 1;
		/* init hpc structure */
B
Bogicevic Sasa 已提交
777 778
		hpc_ptr = alloc_ebda_hpc(slot_num, bus_num);
		if (!hpc_ptr) {
L
Linus Torvalds 已提交
779 780 781 782 783 784 785
			rc = -ENOMEM;
			goto error_no_hpc;
		}
		hpc_ptr->ctlr_id = ctlr_id;
		hpc_ptr->ctlr_relative_id = ctlr;
		hpc_ptr->slot_count = slot_num;
		hpc_ptr->bus_count = bus_num;
B
Bogicevic Sasa 已提交
786 787 788 789 790
		debug("now enter ctlr data structure ---\n");
		debug("ctlr id: %x\n", ctlr_id);
		debug("ctlr_relative_id: %x\n", hpc_ptr->ctlr_relative_id);
		debug("count of slots controlled by this ctlr: %x\n", slot_num);
		debug("count of buses controlled by this ctlr: %x\n", bus_num);
L
Linus Torvalds 已提交
791 792 793 794

		/* init slot structure, fetch slot, bus, cap... */
		slot_ptr = hpc_ptr->slots;
		for (slot = 0; slot < slot_num; slot++) {
B
Bogicevic Sasa 已提交
795 796 797 798
			slot_ptr->slot_num = readb(io_mem + addr_slot);
			slot_ptr->slot_bus_num = readb(io_mem + addr_slot + slot_num);
			slot_ptr->ctl_index = readb(io_mem + addr_slot + 2*slot_num);
			slot_ptr->slot_cap = readb(io_mem + addr_slot + 3*slot_num);
L
Linus Torvalds 已提交
799

800
			// create bus_info lined list --- if only one slot per bus: slot_min = slot_max
L
Linus Torvalds 已提交
801

B
Bogicevic Sasa 已提交
802
			bus_info_ptr2 = ibmphp_find_same_bus_num(slot_ptr->slot_bus_num);
L
Linus Torvalds 已提交
803
			if (!bus_info_ptr2) {
804
				bus_info_ptr1 = kzalloc(sizeof(struct bus_info), GFP_KERNEL);
L
Linus Torvalds 已提交
805 806 807 808 809 810 811 812 813 814 815
				if (!bus_info_ptr1) {
					rc = -ENOMEM;
					goto error_no_hp_slot;
				}
				bus_info_ptr1->slot_min = slot_ptr->slot_num;
				bus_info_ptr1->slot_max = slot_ptr->slot_num;
				bus_info_ptr1->slot_count += 1;
				bus_info_ptr1->busno = slot_ptr->slot_bus_num;
				bus_info_ptr1->index = bus_index++;
				bus_info_ptr1->current_speed = 0xff;
				bus_info_ptr1->current_bus_mode = 0xff;
816

L
Linus Torvalds 已提交
817
				bus_info_ptr1->controller_id = hpc_ptr->ctlr_id;
818

B
Bogicevic Sasa 已提交
819
				list_add_tail(&bus_info_ptr1->bus_info_list, &bus_info_head);
L
Linus Torvalds 已提交
820 821

			} else {
B
Bogicevic Sasa 已提交
822 823
				bus_info_ptr2->slot_min = min(bus_info_ptr2->slot_min, slot_ptr->slot_num);
				bus_info_ptr2->slot_max = max(bus_info_ptr2->slot_max, slot_ptr->slot_num);
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836
				bus_info_ptr2->slot_count += 1;

			}

			// end of creating the bus_info linked list

			slot_ptr++;
			addr_slot += 1;
		}

		/* init bus structure */
		bus_ptr = hpc_ptr->buses;
		for (bus = 0; bus < bus_num; bus++) {
B
Bogicevic Sasa 已提交
837 838 839
			bus_ptr->bus_num = readb(io_mem + addr_bus + bus);
			bus_ptr->slots_at_33_conv = readb(io_mem + addr_bus + bus_num + 8 * bus);
			bus_ptr->slots_at_66_conv = readb(io_mem + addr_bus + bus_num + 8 * bus + 1);
L
Linus Torvalds 已提交
840

B
Bogicevic Sasa 已提交
841
			bus_ptr->slots_at_66_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 2);
L
Linus Torvalds 已提交
842

B
Bogicevic Sasa 已提交
843
			bus_ptr->slots_at_100_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 3);
L
Linus Torvalds 已提交
844

B
Bogicevic Sasa 已提交
845
			bus_ptr->slots_at_133_pcix = readb(io_mem + addr_bus + bus_num + 8 * bus + 4);
L
Linus Torvalds 已提交
846

B
Bogicevic Sasa 已提交
847
			bus_info_ptr2 = ibmphp_find_same_bus_num(bus_ptr->bus_num);
L
Linus Torvalds 已提交
848 849 850 851 852
			if (bus_info_ptr2) {
				bus_info_ptr2->slots_at_33_conv = bus_ptr->slots_at_33_conv;
				bus_info_ptr2->slots_at_66_conv = bus_ptr->slots_at_66_conv;
				bus_info_ptr2->slots_at_66_pcix = bus_ptr->slots_at_66_pcix;
				bus_info_ptr2->slots_at_100_pcix = bus_ptr->slots_at_100_pcix;
853
				bus_info_ptr2->slots_at_133_pcix = bus_ptr->slots_at_133_pcix;
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861
			}
			bus_ptr++;
		}

		hpc_ptr->ctlr_type = temp;

		switch (hpc_ptr->ctlr_type) {
			case 1:
B
Bogicevic Sasa 已提交
862 863 864
				hpc_ptr->u.pci_ctlr.bus = readb(io_mem + addr);
				hpc_ptr->u.pci_ctlr.dev_fun = readb(io_mem + addr + 1);
				hpc_ptr->irq = readb(io_mem + addr + 2);
L
Linus Torvalds 已提交
865
				addr += 3;
B
Bogicevic Sasa 已提交
866
				debug("ctrl bus = %x, ctlr devfun = %x, irq = %x\n",
L
Linus Torvalds 已提交
867 868 869 870 871
					hpc_ptr->u.pci_ctlr.bus,
					hpc_ptr->u.pci_ctlr.dev_fun, hpc_ptr->irq);
				break;

			case 0:
B
Bogicevic Sasa 已提交
872 873 874
				hpc_ptr->u.isa_ctlr.io_start = readw(io_mem + addr);
				hpc_ptr->u.isa_ctlr.io_end = readw(io_mem + addr + 2);
				if (!request_region(hpc_ptr->u.isa_ctlr.io_start,
L
Linus Torvalds 已提交
875 876 877 878 879
						     (hpc_ptr->u.isa_ctlr.io_end - hpc_ptr->u.isa_ctlr.io_start + 1),
						     "ibmphp")) {
					rc = -ENODEV;
					goto error_no_hp_slot;
				}
B
Bogicevic Sasa 已提交
880
				hpc_ptr->irq = readb(io_mem + addr + 4);
L
Linus Torvalds 已提交
881 882 883 884 885
				addr += 5;
				break;

			case 2:
			case 4:
B
Bogicevic Sasa 已提交
886 887 888
				hpc_ptr->u.wpeg_ctlr.wpegbbar = readl(io_mem + addr);
				hpc_ptr->u.wpeg_ctlr.i2c_addr = readb(io_mem + addr + 4);
				hpc_ptr->irq = readb(io_mem + addr + 5);
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896
				addr += 6;
				break;
			default:
				rc = -ENODEV;
				goto error_no_hp_slot;
		}

		//reorganize chassis' linked list
B
Bogicevic Sasa 已提交
897 898
		combine_wpg_for_chassis();
		combine_wpg_for_expansion();
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906
		hpc_ptr->revision = 0xff;
		hpc_ptr->options = 0xff;
		hpc_ptr->starting_slot_num = hpc_ptr->slots[0].slot_num;
		hpc_ptr->ending_slot_num = hpc_ptr->slots[slot_num-1].slot_num;

		// register slots with hpc core as well as create linked list of ibm slot
		for (index = 0; index < hpc_ptr->slot_count; index++) {

907
			hp_slot_ptr = kzalloc(sizeof(*hp_slot_ptr), GFP_KERNEL);
L
Linus Torvalds 已提交
908 909 910 911 912
			if (!hp_slot_ptr) {
				rc = -ENOMEM;
				goto error_no_hp_slot;
			}

913
			hp_slot_ptr->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
L
Linus Torvalds 已提交
914 915 916 917 918
			if (!hp_slot_ptr->info) {
				rc = -ENOMEM;
				goto error_no_hp_info;
			}

919
			tmp_slot = kzalloc(sizeof(*tmp_slot), GFP_KERNEL);
L
Linus Torvalds 已提交
920 921 922 923 924
			if (!tmp_slot) {
				rc = -ENOMEM;
				goto error_no_slot;
			}

925
			tmp_slot->flag = 1;
L
Linus Torvalds 已提交
926 927 928 929 930 931 932 933

			tmp_slot->capabilities = hpc_ptr->slots[index].slot_cap;
			if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_133_MAX) == EBDA_SLOT_133_MAX)
				tmp_slot->supported_speed =  3;
			else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_100_MAX) == EBDA_SLOT_100_MAX)
				tmp_slot->supported_speed =  2;
			else if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_66_MAX) == EBDA_SLOT_66_MAX)
				tmp_slot->supported_speed =  1;
934

L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942
			if ((hpc_ptr->slots[index].slot_cap & EBDA_SLOT_PCIX_CAP) == EBDA_SLOT_PCIX_CAP)
				tmp_slot->supported_bus_mode = 1;
			else
				tmp_slot->supported_bus_mode = 0;


			tmp_slot->bus = hpc_ptr->slots[index].slot_bus_num;

B
Bogicevic Sasa 已提交
943
			bus_info_ptr1 = ibmphp_find_same_bus_num(hpc_ptr->slots[index].slot_bus_num);
L
Linus Torvalds 已提交
944
			if (!bus_info_ptr1) {
945
				kfree(tmp_slot);
L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
				rc = -ENODEV;
				goto error;
			}
			tmp_slot->bus_on = bus_info_ptr1;
			bus_info_ptr1 = NULL;
			tmp_slot->ctrl = hpc_ptr;

			tmp_slot->ctlr_index = hpc_ptr->slots[index].ctl_index;
			tmp_slot->number = hpc_ptr->slots[index].slot_num;
			tmp_slot->hotplug_slot = hp_slot_ptr;

			hp_slot_ptr->private = tmp_slot;
			hp_slot_ptr->release = release_slot;

			rc = fillslotinfo(hp_slot_ptr);
			if (rc)
				goto error;

B
Bogicevic Sasa 已提交
964
			rc = ibmphp_init_devno((struct slot **) &hp_slot_ptr->private);
L
Linus Torvalds 已提交
965 966 967 968 969 970
			if (rc)
				goto error;
			hp_slot_ptr->ops = &ibmphp_hotplug_slot_ops;

			// end of registering ibm slot with hotplug core

B
Bogicevic Sasa 已提交
971
			list_add(&((struct slot *)(hp_slot_ptr->private))->ibm_slot_list, &ibmphp_slot_head);
L
Linus Torvalds 已提交
972 973
		}

B
Bogicevic Sasa 已提交
974 975
		print_bus_info();
		list_add(&hpc_ptr->ebda_hpc_list, &ebda_hpc_head);
L
Linus Torvalds 已提交
976 977 978

	}			/* each hpc  */

979
	list_for_each_entry(tmp_slot, &ibmphp_slot_head, ibm_slot_list) {
980
		snprintf(name, SLOT_NAME_SIZE, "%s", create_file_name(tmp_slot));
A
Alex Chiang 已提交
981
		pci_hp_register(tmp_slot->hotplug_slot,
982
			pci_find_bus(0, tmp_slot->bus), tmp_slot->device, name);
L
Linus Torvalds 已提交
983 984
	}

B
Bogicevic Sasa 已提交
985 986
	print_ebda_hpc();
	print_ibm_slot();
L
Linus Torvalds 已提交
987 988 989
	return 0;

error:
B
Bogicevic Sasa 已提交
990
	kfree(hp_slot_ptr->private);
L
Linus Torvalds 已提交
991
error_no_slot:
B
Bogicevic Sasa 已提交
992
	kfree(hp_slot_ptr->info);
L
Linus Torvalds 已提交
993
error_no_hp_info:
B
Bogicevic Sasa 已提交
994
	kfree(hp_slot_ptr);
L
Linus Torvalds 已提交
995
error_no_hp_slot:
B
Bogicevic Sasa 已提交
996
	free_ebda_hpc(hpc_ptr);
L
Linus Torvalds 已提交
997
error_no_hpc:
B
Bogicevic Sasa 已提交
998
	iounmap(io_mem);
L
Linus Torvalds 已提交
999 1000 1001
	return rc;
}

1002
/*
L
Linus Torvalds 已提交
1003 1004 1005
 * map info (bus, devfun, start addr, end addr..) of i/o, memory,
 * pfm from the physical addr to a list of resource.
 */
B
Bogicevic Sasa 已提交
1006
static int __init ebda_rsrc_rsrc(void)
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013
{
	u16 addr;
	short rsrc;
	u8 type, rsrc_type;
	struct ebda_pci_rsrc *rsrc_ptr;

	addr = rsrc_list_ptr->phys_addr;
B
Bogicevic Sasa 已提交
1014 1015
	debug("now entering rsrc land\n");
	debug("offset of rsrc: %x\n", rsrc_list_ptr->phys_addr);
L
Linus Torvalds 已提交
1016 1017

	for (rsrc = 0; rsrc < rsrc_list_ptr->num_entries; rsrc++) {
B
Bogicevic Sasa 已提交
1018
		type = readb(io_mem + addr);
L
Linus Torvalds 已提交
1019 1020 1021 1022 1023

		addr += 1;
		rsrc_type = type & EBDA_RSRC_TYPE_MASK;

		if (rsrc_type == EBDA_IO_RSRC_TYPE) {
B
Bogicevic Sasa 已提交
1024
			rsrc_ptr = alloc_ebda_pci_rsrc();
L
Linus Torvalds 已提交
1025
			if (!rsrc_ptr) {
B
Bogicevic Sasa 已提交
1026
				iounmap(io_mem);
L
Linus Torvalds 已提交
1027 1028 1029 1030
				return -ENOMEM;
			}
			rsrc_ptr->rsrc_type = type;

B
Bogicevic Sasa 已提交
1031 1032 1033 1034
			rsrc_ptr->bus_num = readb(io_mem + addr);
			rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
			rsrc_ptr->start_addr = readw(io_mem + addr + 2);
			rsrc_ptr->end_addr = readw(io_mem + addr + 4);
L
Linus Torvalds 已提交
1035 1036
			addr += 6;

B
Bogicevic Sasa 已提交
1037 1038
			debug("rsrc from io type ----\n");
			debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
L
Linus Torvalds 已提交
1039 1040
				rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);

B
Bogicevic Sasa 已提交
1041
			list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
L
Linus Torvalds 已提交
1042 1043 1044
		}

		if (rsrc_type == EBDA_MEM_RSRC_TYPE || rsrc_type == EBDA_PFM_RSRC_TYPE) {
B
Bogicevic Sasa 已提交
1045 1046 1047
			rsrc_ptr = alloc_ebda_pci_rsrc();
			if (!rsrc_ptr) {
				iounmap(io_mem);
L
Linus Torvalds 已提交
1048 1049 1050 1051
				return -ENOMEM;
			}
			rsrc_ptr->rsrc_type = type;

B
Bogicevic Sasa 已提交
1052 1053 1054 1055
			rsrc_ptr->bus_num = readb(io_mem + addr);
			rsrc_ptr->dev_fun = readb(io_mem + addr + 1);
			rsrc_ptr->start_addr = readl(io_mem + addr + 2);
			rsrc_ptr->end_addr = readl(io_mem + addr + 6);
L
Linus Torvalds 已提交
1056 1057
			addr += 10;

B
Bogicevic Sasa 已提交
1058 1059
			debug("rsrc from mem or pfm ---\n");
			debug("rsrc type: %x bus#: %x dev_func: %x start addr: %x end addr: %x\n",
L
Linus Torvalds 已提交
1060 1061
				rsrc_ptr->rsrc_type, rsrc_ptr->bus_num, rsrc_ptr->dev_fun, rsrc_ptr->start_addr, rsrc_ptr->end_addr);

B
Bogicevic Sasa 已提交
1062
			list_add(&rsrc_ptr->ebda_pci_rsrc_list, &ibmphp_ebda_pci_rsrc_head);
L
Linus Torvalds 已提交
1063 1064
		}
	}
B
Bogicevic Sasa 已提交
1065
	kfree(rsrc_list_ptr);
L
Linus Torvalds 已提交
1066
	rsrc_list_ptr = NULL;
B
Bogicevic Sasa 已提交
1067
	print_ebda_pci_rsrc();
L
Linus Torvalds 已提交
1068 1069 1070
	return 0;
}

B
Bogicevic Sasa 已提交
1071
u16 ibmphp_get_total_controllers(void)
L
Linus Torvalds 已提交
1072 1073 1074 1075
{
	return hpc_list_ptr->num_ctlrs;
}

B
Bogicevic Sasa 已提交
1076
struct slot *ibmphp_get_slot_from_physical_num(u8 physical_num)
L
Linus Torvalds 已提交
1077 1078 1079
{
	struct slot *slot;

1080
	list_for_each_entry(slot, &ibmphp_slot_head, ibm_slot_list) {
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
		if (slot->number == physical_num)
			return slot;
	}
	return NULL;
}

/* To find:
 *	- the smallest slot number
 *	- the largest slot number
 *	- the total number of the slots based on each bus
 *	  (if only one slot per bus slot_min = slot_max )
 */
B
Bogicevic Sasa 已提交
1093
struct bus_info *ibmphp_find_same_bus_num(u32 num)
L
Linus Torvalds 已提交
1094 1095 1096
{
	struct bus_info *ptr;

1097
	list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1098
		if (ptr->busno == num)
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106
			 return ptr;
	}
	return NULL;
}

/*  Finding relative bus number, in order to map corresponding
 *  bus register
 */
B
Bogicevic Sasa 已提交
1107
int ibmphp_get_bus_index(u8 num)
L
Linus Torvalds 已提交
1108 1109 1110
{
	struct bus_info *ptr;

1111
	list_for_each_entry(ptr, &bus_info_head, bus_info_list) {
1112
		if (ptr->busno == num)
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117
			return ptr->index;
	}
	return -ENODEV;
}

B
Bogicevic Sasa 已提交
1118
void ibmphp_free_bus_info_queue(void)
L
Linus Torvalds 已提交
1119
{
1120
	struct bus_info *bus_info, *next;
L
Linus Torvalds 已提交
1121

1122 1123
	list_for_each_entry_safe(bus_info, next, &bus_info_head,
				 bus_info_list) {
L
Linus Torvalds 已提交
1124 1125 1126 1127
		kfree (bus_info);
	}
}

B
Bogicevic Sasa 已提交
1128
void ibmphp_free_ebda_hpc_queue(void)
L
Linus Torvalds 已提交
1129
{
1130
	struct controller *controller = NULL, *next;
L
Linus Torvalds 已提交
1131 1132
	int pci_flag = 0;

1133 1134
	list_for_each_entry_safe(controller, next, &ebda_hpc_head,
				 ebda_hpc_list) {
L
Linus Torvalds 已提交
1135
		if (controller->ctlr_type == 0)
B
Bogicevic Sasa 已提交
1136
			release_region(controller->u.isa_ctlr.io_start, (controller->u.isa_ctlr.io_end - controller->u.isa_ctlr.io_start + 1));
L
Linus Torvalds 已提交
1137 1138
		else if ((controller->ctlr_type == 1) && (!pci_flag)) {
			++pci_flag;
B
Bogicevic Sasa 已提交
1139
			pci_unregister_driver(&ibmphp_driver);
L
Linus Torvalds 已提交
1140
		}
B
Bogicevic Sasa 已提交
1141
		free_ebda_hpc(controller);
L
Linus Torvalds 已提交
1142 1143 1144
	}
}

B
Bogicevic Sasa 已提交
1145
void ibmphp_free_ebda_pci_rsrc_queue(void)
L
Linus Torvalds 已提交
1146
{
1147
	struct ebda_pci_rsrc *resource, *next;
L
Linus Torvalds 已提交
1148

1149 1150
	list_for_each_entry_safe(resource, next, &ibmphp_ebda_pci_rsrc_head,
				 ebda_pci_rsrc_list) {
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		kfree (resource);
		resource = NULL;
	}
}

static struct pci_device_id id_table[] = {
	{
		.vendor		= PCI_VENDOR_ID_IBM,
		.device		= HPC_DEVICE_ID,
		.subvendor	= PCI_VENDOR_ID_IBM,
		.subdevice	= HPC_SUBSYSTEM_ID,
		.class		= ((PCI_CLASS_SYSTEM_PCI_HOTPLUG << 8) | 0x00),
	}, {}
1164
};
L
Linus Torvalds 已提交
1165 1166 1167

MODULE_DEVICE_TABLE(pci, id_table);

B
Bogicevic Sasa 已提交
1168
static int ibmphp_probe(struct pci_dev *, const struct pci_device_id *);
L
Linus Torvalds 已提交
1169 1170 1171 1172 1173 1174
static struct pci_driver ibmphp_driver = {
	.name		= "ibmphp",
	.id_table	= id_table,
	.probe		= ibmphp_probe,
};

B
Bogicevic Sasa 已提交
1175
int ibmphp_register_pci(void)
L
Linus Torvalds 已提交
1176 1177 1178 1179
{
	struct controller *ctrl;
	int rc = 0;

1180
	list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187
		if (ctrl->ctlr_type == 1) {
			rc = pci_register_driver(&ibmphp_driver);
			break;
		}
	}
	return rc;
}
B
Bogicevic Sasa 已提交
1188
static int ibmphp_probe(struct pci_dev *dev, const struct pci_device_id *ids)
L
Linus Torvalds 已提交
1189 1190 1191
{
	struct controller *ctrl;

B
Bogicevic Sasa 已提交
1192
	debug("inside ibmphp_probe\n");
1193

1194
	list_for_each_entry(ctrl, &ebda_hpc_head, ebda_hpc_list) {
L
Linus Torvalds 已提交
1195 1196 1197
		if (ctrl->ctlr_type == 1) {
			if ((dev->devfn == ctrl->u.pci_ctlr.dev_fun) && (dev->bus->number == ctrl->u.pci_ctlr.bus)) {
				ctrl->ctrl_dev = dev;
B
Bogicevic Sasa 已提交
1198 1199
				debug("found device!!!\n");
				debug("dev->device = %x, dev->subsystem_device = %x\n", dev->device, dev->subsystem_device);
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205
				return 0;
			}
		}
	}
	return -ENODEV;
}