ibmphp_core.c 34.0 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
/*
 * IBM Hot Plug Controller Driver
 *
 * Written By: Chuck Cole, Jyoti Shah, Tong Yu, Irene Zubarev, 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/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include "../pci.h"
38
#include <asm/pci_x86.h>		/* for struct irq_routing_table */
39
#include <asm/io_apic.h>
L
Linus Torvalds 已提交
40 41
#include "ibmphp.h"

B
Bogicevic Sasa 已提交
42 43 44 45 46
#define attn_on(sl)  ibmphp_hpc_writeslot(sl, HPC_SLOT_ATTNON)
#define attn_off(sl) ibmphp_hpc_writeslot(sl, HPC_SLOT_ATTNOFF)
#define attn_LED_blink(sl) ibmphp_hpc_writeslot(sl, HPC_SLOT_BLINKLED)
#define get_ctrl_revision(sl, rev) ibmphp_hpc_readslot(sl, READ_REVLEVEL, rev)
#define get_hpc_options(sl, opt) ibmphp_hpc_readslot(sl, READ_HPCOPTIONS, opt)
L
Linus Torvalds 已提交
47 48 49 50 51 52

#define DRIVER_VERSION	"0.6"
#define DRIVER_DESC	"IBM Hot Plug PCI Controller Driver"

int ibmphp_debug;

53
static bool debug;
L
Linus Torvalds 已提交
54
module_param(debug, bool, S_IRUGO | S_IWUSR);
B
Bogicevic Sasa 已提交
55 56 57
MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
58 59 60 61

struct pci_bus *ibmphp_pci_bus;
static int max_slots;

62
static int irqs[16];    /* PIC mode IRQs we're using so far (in case MPS
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74
			 * tables don't provide default info for empty slots */

static int init_flag;

/*
static int get_max_adapter_speed_1 (struct hotplug_slot *, u8 *, u8);

static inline int get_max_adapter_speed (struct hotplug_slot *hs, u8 *value)
{
	return get_max_adapter_speed_1 (hs, value, 1);
}
*/
75
static inline int get_cur_bus_info(struct slot **sl)
L
Linus Torvalds 已提交
76 77
{
	int rc = 1;
R
Ryan Desfosses 已提交
78
	struct slot *slot_cur = *sl;
L
Linus Torvalds 已提交
79 80

	debug("options = %x\n", slot_cur->ctrl->options);
81
	debug("revision = %x\n", slot_cur->ctrl->revision);
L
Linus Torvalds 已提交
82

83
	if (READ_BUS_STATUS(slot_cur->ctrl))
L
Linus Torvalds 已提交
84
		rc = ibmphp_hpc_readslot(slot_cur, READ_BUSSTATUS, NULL);
85 86

	if (rc)
L
Linus Torvalds 已提交
87
		return rc;
88

L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99
	slot_cur->bus_on->current_speed = CURRENT_BUS_SPEED(slot_cur->busstatus);
	if (READ_BUS_MODE(slot_cur->ctrl))
		slot_cur->bus_on->current_bus_mode =
				CURRENT_BUS_MODE(slot_cur->busstatus);
	else
		slot_cur->bus_on->current_bus_mode = 0xFF;

	debug("busstatus = %x, bus_speed = %x, bus_mode = %x\n",
			slot_cur->busstatus,
			slot_cur->bus_on->current_speed,
			slot_cur->bus_on->current_bus_mode);
100

L
Linus Torvalds 已提交
101 102 103 104 105 106 107
	*sl = slot_cur;
	return 0;
}

static inline int slot_update(struct slot **sl)
{
	int rc;
108 109
	rc = ibmphp_hpc_readslot(*sl, READ_ALLSTAT, NULL);
	if (rc)
L
Linus Torvalds 已提交
110 111 112 113 114 115
		return rc;
	if (!init_flag)
		rc = get_cur_bus_info(sl);
	return rc;
}

B
Bogicevic Sasa 已提交
116
static int __init get_max_slots(void)
L
Linus Torvalds 已提交
117
{
R
Ryan Desfosses 已提交
118
	struct slot *slot_cur;
L
Linus Torvalds 已提交
119 120
	u8 slot_count = 0;

121
	list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		/* sometimes the hot-pluggable slots start with 4 (not always from 1) */
		slot_count = max(slot_count, slot_cur->number);
	}
	return slot_count;
}

/* This routine will put the correct slot->device information per slot.  It's
 * called from initialization of the slot structures. It will also assign
 * interrupt numbers per each slot.
 * Parameters: struct slot
 * Returns 0 or errors
 */
int ibmphp_init_devno(struct slot **cur_slot)
{
	struct irq_routing_table *rtable;
	int len;
	int loop;
	int i;

	rtable = pcibios_get_irq_routing_table();
	if (!rtable) {
		err("no BIOS routing table...\n");
		return -ENOMEM;
	}

	len = (rtable->size - sizeof(struct irq_routing_table)) /
			sizeof(struct irq_info);

150 151
	if (!len) {
		kfree(rtable);
L
Linus Torvalds 已提交
152
		return -1;
153
	}
L
Linus Torvalds 已提交
154
	for (loop = 0; loop < len; loop++) {
155 156
		if ((*cur_slot)->number == rtable->slots[loop].slot &&
		    (*cur_slot)->bus == rtable->slots[loop].bus) {
L
Linus Torvalds 已提交
157 158 159
			(*cur_slot)->device = PCI_SLOT(rtable->slots[loop].devfn);
			for (i = 0; i < 4; i++)
				(*cur_slot)->irq[i] = IO_APIC_get_PCI_irq_vector((int) (*cur_slot)->bus,
160
						(int) (*cur_slot)->device, i);
161 162 163 164 165 166 167 168 169 170

			debug("(*cur_slot)->irq[0] = %x\n",
					(*cur_slot)->irq[0]);
			debug("(*cur_slot)->irq[1] = %x\n",
					(*cur_slot)->irq[1]);
			debug("(*cur_slot)->irq[2] = %x\n",
					(*cur_slot)->irq[2]);
			debug("(*cur_slot)->irq[3] = %x\n",
					(*cur_slot)->irq[3]);

171
			debug("rtable->exclusive_irqs = %x\n",
L
Linus Torvalds 已提交
172
					rtable->exclusive_irqs);
173
			debug("rtable->slots[loop].irq[0].bitmap = %x\n",
L
Linus Torvalds 已提交
174
					rtable->slots[loop].irq[0].bitmap);
175
			debug("rtable->slots[loop].irq[1].bitmap = %x\n",
L
Linus Torvalds 已提交
176
					rtable->slots[loop].irq[1].bitmap);
177
			debug("rtable->slots[loop].irq[2].bitmap = %x\n",
L
Linus Torvalds 已提交
178
					rtable->slots[loop].irq[2].bitmap);
179
			debug("rtable->slots[loop].irq[3].bitmap = %x\n",
L
Linus Torvalds 已提交
180 181
					rtable->slots[loop].irq[3].bitmap);

182
			debug("rtable->slots[loop].irq[0].link = %x\n",
L
Linus Torvalds 已提交
183
					rtable->slots[loop].irq[0].link);
184
			debug("rtable->slots[loop].irq[1].link = %x\n",
L
Linus Torvalds 已提交
185
					rtable->slots[loop].irq[1].link);
186
			debug("rtable->slots[loop].irq[2].link = %x\n",
L
Linus Torvalds 已提交
187
					rtable->slots[loop].irq[2].link);
188
			debug("rtable->slots[loop].irq[3].link = %x\n",
L
Linus Torvalds 已提交
189
					rtable->slots[loop].irq[3].link);
190 191 192
			debug("end of init_devno\n");
			kfree(rtable);
			return 0;
L
Linus Torvalds 已提交
193 194 195
		}
	}

196
	kfree(rtable);
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	return -1;
}

static inline int power_on(struct slot *slot_cur)
{
	u8 cmd = HPC_SLOT_ON;
	int retval;

	retval = ibmphp_hpc_writeslot(slot_cur, cmd);
	if (retval) {
		err("power on failed\n");
		return retval;
	}
	if (CTLR_RESULT(slot_cur->ctrl->status)) {
		err("command not completed successfully in power_on\n");
		return -EIO;
	}
	msleep(3000);	/* For ServeRAID cards, and some 66 PCI */
	return 0;
}

static inline int power_off(struct slot *slot_cur)
{
	u8 cmd = HPC_SLOT_OFF;
	int retval;

	retval = ibmphp_hpc_writeslot(slot_cur, cmd);
	if (retval) {
		err("power off failed\n");
		return retval;
	}
	if (CTLR_RESULT(slot_cur->ctrl->status)) {
		err("command not completed successfully in power_off\n");
		retval = -EIO;
	}
	return retval;
}

static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
{
	int rc = 0;
	struct slot *pslot;
239
	u8 cmd = 0x00;     /* avoid compiler warning */
L
Linus Torvalds 已提交
240 241 242 243

	debug("set_attention_status - Entry hotplug_slot[%lx] value[%x]\n",
			(ulong) hotplug_slot, value);
	ibmphp_lock_operations();
244

L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	if (hotplug_slot) {
		switch (value) {
		case HPC_SLOT_ATTN_OFF:
			cmd = HPC_SLOT_ATTNOFF;
			break;
		case HPC_SLOT_ATTN_ON:
			cmd = HPC_SLOT_ATTNON;
			break;
		case HPC_SLOT_ATTN_BLINK:
			cmd = HPC_SLOT_BLINKLED;
			break;
		default:
			rc = -ENODEV;
			err("set_attention_status - Error : invalid input [%x]\n",
					value);
			break;
		}
		if (rc == 0) {
			pslot = hotplug_slot->private;
			if (pslot)
				rc = ibmphp_hpc_writeslot(pslot, cmd);
			else
				rc = -ENODEV;
		}
270
	} else
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278
		rc = -ENODEV;

	ibmphp_unlock_operations();

	debug("set_attention_status - Exit rc[%d]\n", rc);
	return rc;
}

R
Ryan Desfosses 已提交
279
static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
280 281 282 283 284 285 286
{
	int rc = -ENODEV;
	struct slot *pslot;
	struct slot myslot;

	debug("get_attention_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
					(ulong) hotplug_slot, (ulong) value);
287

L
Linus Torvalds 已提交
288
	ibmphp_lock_operations();
289
	if (hotplug_slot) {
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		pslot = hotplug_slot->private;
		if (pslot) {
			memcpy(&myslot, pslot, sizeof(struct slot));
			rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
						&(myslot.status));
			if (!rc)
				rc = ibmphp_hpc_readslot(pslot,
						READ_EXTSLOTSTATUS,
						&(myslot.ext_status));
			if (!rc)
				*value = SLOT_ATTN(myslot.status,
						myslot.ext_status);
		}
	}

	ibmphp_unlock_operations();
	debug("get_attention_status - Exit rc[%d] value[%x]\n", rc, *value);
	return rc;
}

R
Ryan Desfosses 已提交
310
static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318
{
	int rc = -ENODEV;
	struct slot *pslot;
	struct slot myslot;

	debug("get_latch_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
					(ulong) hotplug_slot, (ulong) value);
	ibmphp_lock_operations();
319
	if (hotplug_slot) {
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		pslot = hotplug_slot->private;
		if (pslot) {
			memcpy(&myslot, pslot, sizeof(struct slot));
			rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
						&(myslot.status));
			if (!rc)
				*value = SLOT_LATCH(myslot.status);
		}
	}

	ibmphp_unlock_operations();
	debug("get_latch_status - Exit rc[%d] rc[%x] value[%x]\n",
			rc, rc, *value);
	return rc;
}


R
Ryan Desfosses 已提交
337
static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345
{
	int rc = -ENODEV;
	struct slot *pslot;
	struct slot myslot;

	debug("get_power_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
					(ulong) hotplug_slot, (ulong) value);
	ibmphp_lock_operations();
346
	if (hotplug_slot) {
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		pslot = hotplug_slot->private;
		if (pslot) {
			memcpy(&myslot, pslot, sizeof(struct slot));
			rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
						&(myslot.status));
			if (!rc)
				*value = SLOT_PWRGD(myslot.status);
		}
	}

	ibmphp_unlock_operations();
	debug("get_power_status - Exit rc[%d] rc[%x] value[%x]\n",
			rc, rc, *value);
	return rc;
}

R
Ryan Desfosses 已提交
363
static int get_adapter_present(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372
{
	int rc = -ENODEV;
	struct slot *pslot;
	u8 present;
	struct slot myslot;

	debug("get_adapter_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
					(ulong) hotplug_slot, (ulong) value);
	ibmphp_lock_operations();
373
	if (hotplug_slot) {
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		pslot = hotplug_slot->private;
		if (pslot) {
			memcpy(&myslot, pslot, sizeof(struct slot));
			rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
						&(myslot.status));
			if (!rc) {
				present = SLOT_PRESENT(myslot.status);
				if (present == HPC_SLOT_EMPTY)
					*value = 0;
				else
					*value = 1;
			}
		}
	}

	ibmphp_unlock_operations();
	debug("get_adapter_present - Exit rc[%d] value[%x]\n", rc, *value);
	return rc;
}

394
static int get_max_bus_speed(struct slot *slot)
L
Linus Torvalds 已提交
395
{
396
	int rc;
L
Linus Torvalds 已提交
397
	u8 mode = 0;
398 399
	enum pci_bus_speed speed;
	struct pci_bus *bus = slot->hotplug_slot->pci_slot->bus;
L
Linus Torvalds 已提交
400

401
	debug("%s - Entry slot[%p]\n", __func__, slot);
L
Linus Torvalds 已提交
402 403

	ibmphp_lock_operations();
404
	mode = slot->supported_bus_mode;
405
	speed = slot->supported_speed;
L
Linus Torvalds 已提交
406 407
	ibmphp_unlock_operations();

408 409 410 411
	switch (speed) {
	case BUS_SPEED_33:
		break;
	case BUS_SPEED_66:
412
		if (mode == BUS_MODE_PCIX)
413 414 415 416 417 418 419 420 421
			speed += 0x01;
		break;
	case BUS_SPEED_100:
	case BUS_SPEED_133:
		speed += 0x01;
		break;
	default:
		/* Note (will need to change): there would be soon 256, 512 also */
		rc = -ENODEV;
L
Linus Torvalds 已提交
422 423
	}

424 425 426 427
	if (!rc)
		bus->max_bus_speed = speed;

	debug("%s - Exit rc[%d] speed[%x]\n", __func__, rc, speed);
L
Linus Torvalds 已提交
428 429 430 431
	return rc;
}

/*
R
Ryan Desfosses 已提交
432
static int get_max_adapter_speed_1(struct hotplug_slot *hotplug_slot, u8 *value, u8 flag)
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
{
	int rc = -ENODEV;
	struct slot *pslot;
	struct slot myslot;

	debug("get_max_adapter_speed_1 - Entry hotplug_slot[%lx] pvalue[%lx]\n",
						(ulong)hotplug_slot, (ulong) value);

	if (flag)
		ibmphp_lock_operations();

	if (hotplug_slot && value) {
		pslot = hotplug_slot->private;
		if (pslot) {
			memcpy(&myslot, pslot, sizeof(struct slot));
			rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
						&(myslot.status));

			if (!(SLOT_LATCH (myslot.status)) &&
					(SLOT_PRESENT (myslot.status))) {
				rc = ibmphp_hpc_readslot(pslot,
						READ_EXTSLOTSTATUS,
						&(myslot.ext_status));
				if (!rc)
					*value = SLOT_SPEED(myslot.ext_status);
			} else
				*value = MAX_ADAPTER_NONE;
B
Bogicevic Sasa 已提交
460
		}
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469
	}

	if (flag)
		ibmphp_unlock_operations();

	debug("get_max_adapter_speed_1 - Exit rc[%d] value[%x]\n", rc, *value);
	return rc;
}

R
Ryan Desfosses 已提交
470
static int get_bus_name(struct hotplug_slot *hotplug_slot, char *value)
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
{
	int rc = -ENODEV;
	struct slot *pslot = NULL;

	debug("get_bus_name - Entry hotplug_slot[%lx]\n", (ulong)hotplug_slot);

	ibmphp_lock_operations();

	if (hotplug_slot) {
		pslot = hotplug_slot->private;
		if (pslot) {
			rc = 0;
			snprintf(value, 100, "Bus %x", pslot->bus);
		}
	} else
		rc = -ENODEV;

	ibmphp_unlock_operations();
	debug("get_bus_name - Exit rc[%d] value[%x]\n", rc, *value);
	return rc;
}
*/

/****************************************************************************
 * This routine will initialize the ops data structure used in the validate
 * function. It will also power off empty slots that are powered on since BIOS
 * leaves those on, albeit disconnected
 ****************************************************************************/
static int __init init_ops(void)
{
	struct slot *slot_cur;
	int retval;
	int rc;

505
	list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
L
Linus Torvalds 已提交
506 507
		debug("BEFORE GETTING SLOT STATUS, slot # %x\n",
							slot_cur->number);
508
		if (slot_cur->ctrl->revision == 0xFF)
L
Linus Torvalds 已提交
509 510 511 512
			if (get_ctrl_revision(slot_cur,
						&slot_cur->ctrl->revision))
				return -1;

513 514
		if (slot_cur->bus_on->current_speed == 0xFF)
			if (get_cur_bus_info(&slot_cur))
L
Linus Torvalds 已提交
515
				return -1;
516
		get_max_bus_speed(slot_cur);
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

		if (slot_cur->ctrl->options == 0xFF)
			if (get_hpc_options(slot_cur, &slot_cur->ctrl->options))
				return -1;

		retval = slot_update(&slot_cur);
		if (retval)
			return retval;

		debug("status = %x\n", slot_cur->status);
		debug("ext_status = %x\n", slot_cur->ext_status);
		debug("SLOT_POWER = %x\n", SLOT_POWER(slot_cur->status));
		debug("SLOT_PRESENT = %x\n", SLOT_PRESENT(slot_cur->status));
		debug("SLOT_LATCH = %x\n", SLOT_LATCH(slot_cur->status));

532 533
		if ((SLOT_PWRGD(slot_cur->status)) &&
		    !(SLOT_PRESENT(slot_cur->status)) &&
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		    !(SLOT_LATCH(slot_cur->status))) {
			debug("BEFORE POWER OFF COMMAND\n");
				rc = power_off(slot_cur);
				if (rc)
					return rc;

	/*		retval = slot_update(&slot_cur);
	 *		if (retval)
	 *			return retval;
	 *		ibmphp_update_slot_info(slot_cur);
	 */
		}
	}
	init_flag = 0;
	return 0;
}

/* This operation will check whether the slot is within the bounds and
 * the operation is valid to perform on that slot
 * Parameters: slot, operation
 * Returns: 0 or error codes
 */
static int validate(struct slot *slot_cur, int opn)
{
	int number;
	int retval;

	if (!slot_cur)
		return -ENODEV;
	number = slot_cur->number;
	if ((number > max_slots) || (number < 0))
		return -EBADSLT;
	debug("slot_number in validate is %d\n", slot_cur->number);

	retval = slot_update(&slot_cur);
	if (retval)
		return retval;

	switch (opn) {
		case ENABLE:
574 575
			if (!(SLOT_PWRGD(slot_cur->status)) &&
			     (SLOT_PRESENT(slot_cur->status)) &&
L
Linus Torvalds 已提交
576 577 578 579
			     !(SLOT_LATCH(slot_cur->status)))
				return 0;
			break;
		case DISABLE:
580
			if ((SLOT_PWRGD(slot_cur->status)) &&
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
			    (SLOT_PRESENT(slot_cur->status)) &&
			    !(SLOT_LATCH(slot_cur->status)))
				return 0;
			break;
		default:
			break;
	}
	err("validate failed....\n");
	return -EINVAL;
}

/****************************************************************************
 * This routine is for updating the data structures in the hotplug core
 * Parameters: struct slot
 * Returns: 0 or error
 ****************************************************************************/
int ibmphp_update_slot_info(struct slot *slot_cur)
{
	struct hotplug_slot_info *info;
600
	struct pci_bus *bus = slot_cur->hotplug_slot->pci_slot->bus;
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609
	int rc;
	u8 bus_speed;
	u8 mode;

	info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
	if (!info) {
		err("out of system memory\n");
		return -ENOMEM;
	}
610

L
Linus Torvalds 已提交
611 612 613 614
	info->power_status = SLOT_PWRGD(slot_cur->status);
	info->attention_status = SLOT_ATTN(slot_cur->status,
						slot_cur->ext_status);
	info->latch_status = SLOT_LATCH(slot_cur->status);
B
Bogicevic Sasa 已提交
615 616
	if (!SLOT_PRESENT(slot_cur->status)) {
		info->adapter_status = 0;
L
Linus Torvalds 已提交
617 618
/*		info->max_adapter_speed_status = MAX_ADAPTER_NONE; */
	} else {
B
Bogicevic Sasa 已提交
619
		info->adapter_status = 1;
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630
/*		get_max_adapter_speed_1(slot_cur->hotplug_slot,
					&info->max_adapter_speed_status, 0); */
	}

	bus_speed = slot_cur->bus_on->current_speed;
	mode = slot_cur->bus_on->current_bus_mode;

	switch (bus_speed) {
		case BUS_SPEED_33:
			break;
		case BUS_SPEED_66:
631
			if (mode == BUS_MODE_PCIX)
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645
				bus_speed += 0x01;
			else if (mode == BUS_MODE_PCI)
				;
			else
				bus_speed = PCI_SPEED_UNKNOWN;
			break;
		case BUS_SPEED_100:
		case BUS_SPEED_133:
			bus_speed += 0x01;
			break;
		default:
			bus_speed = PCI_SPEED_UNKNOWN;
	}

646
	bus->cur_bus_speed = bus_speed;
647 648
	// To do: bus_names

L
Linus Torvalds 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
	rc = pci_hp_change_slot_info(slot_cur->hotplug_slot, info);
	kfree(info);
	return rc;
}


/******************************************************************************
 * This function will return the pci_func, given bus and devfunc, or NULL.  It
 * is called from visit routines
 ******************************************************************************/

static struct pci_func *ibm_slot_find(u8 busno, u8 device, u8 function)
{
	struct pci_func *func_cur;
	struct slot *slot_cur;
664
	list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
		if (slot_cur->func) {
			func_cur = slot_cur->func;
			while (func_cur) {
				if ((func_cur->busno == busno) &&
						(func_cur->device == device) &&
						(func_cur->function == function))
					return func_cur;
				func_cur = func_cur->next;
			}
		}
	}
	return NULL;
}

/*************************************************************
 * This routine frees up memory used by struct slot, including
 * the pointers to pci_func, bus, hotplug_slot, controller,
 * and deregistering from the hotplug core
 *************************************************************/
static void free_slots(void)
{
686
	struct slot *slot_cur, *next;
L
Linus Torvalds 已提交
687

688
	debug("%s -- enter\n", __func__);
L
Linus Torvalds 已提交
689

690 691
	list_for_each_entry_safe(slot_cur, next, &ibmphp_slot_head,
				 ibm_slot_list) {
L
Linus Torvalds 已提交
692 693
		pci_hp_deregister(slot_cur->hotplug_slot);
	}
694
	debug("%s -- exit\n", __func__);
L
Linus Torvalds 已提交
695 696 697 698 699 700 701
}

static void ibm_unconfigure_device(struct pci_func *func)
{
	struct pci_dev *temp;
	u8 j;

702
	debug("inside %s\n", __func__);
L
Linus Torvalds 已提交
703 704 705 706
	debug("func->device = %x, func->function = %x\n",
					func->device, func->function);
	debug("func->device << 3 | 0x0  = %x\n", func->device << 3 | 0x0);

707 708
	pci_lock_rescan_remove();

L
Linus Torvalds 已提交
709
	for (j = 0; j < 0x08; j++) {
710 711
		temp = pci_get_bus_and_slot(func->busno, (func->device << 3) | j);
		if (temp) {
712
			pci_stop_and_remove_bus_device(temp);
713 714
			pci_dev_put(temp);
		}
L
Linus Torvalds 已提交
715
	}
716

717
	pci_dev_put(func->dev);
718 719

	pci_unlock_rescan_remove();
L
Linus Torvalds 已提交
720 721 722
}

/*
723 724
 * The following function is to fix kernel bug regarding
 * getting bus entries, here we manually add those primary
L
Linus Torvalds 已提交
725 726 727 728
 * bus entries to kernel bus structure whenever apply
 */
static u8 bus_structure_fixup(u8 busno)
{
729
	struct pci_bus *bus, *b;
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737
	struct pci_dev *dev;
	u16 l;

	if (pci_find_bus(0, busno) || !(ibmphp_find_same_bus_num(busno)))
		return 1;

	bus = kmalloc(sizeof(*bus), GFP_KERNEL);
	if (!bus) {
738
		err("%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
739 740 741 742 743
		return 1;
	}
	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev) {
		kfree(bus);
744
		err("%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753
		return 1;
	}

	bus->number = busno;
	bus->ops = ibmphp_pci_bus->ops;
	dev->bus = bus;
	for (dev->devfn = 0; dev->devfn < 256; dev->devfn += 8) {
		if (!pci_read_config_word(dev, PCI_VENDOR_ID, &l) &&
					(l != 0x0000) && (l != 0xffff)) {
M
Masanari Iida 已提交
754
			debug("%s - Inside bus_structure_fixup()\n",
755
							__func__);
756 757 758 759 760
			b = pci_scan_bus(busno, ibmphp_pci_bus->ops, NULL);
			if (!b)
				continue;

			pci_bus_add_devices(b);
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
			break;
		}
	}

	kfree(dev);
	kfree(bus);

	return 0;
}

static int ibm_configure_device(struct pci_func *func)
{
	struct pci_bus *child;
	int num;
	int flag = 0;	/* this is to make sure we don't double scan the bus,
					for bridged devices primarily */

778 779
	pci_lock_rescan_remove();

L
Linus Torvalds 已提交
780 781 782
	if (!(bus_structure_fixup(func->busno)))
		flag = 1;
	if (func->dev == NULL)
783
		func->dev = pci_get_bus_and_slot(func->busno,
L
Linus Torvalds 已提交
784 785 786 787 788
				PCI_DEVFN(func->device, func->function));

	if (func->dev == NULL) {
		struct pci_bus *bus = pci_find_bus(0, func->busno);
		if (!bus)
789
			goto out;
L
Linus Torvalds 已提交
790 791 792 793 794 795

		num = pci_scan_slot(bus,
				PCI_DEVFN(func->device, func->function));
		if (num)
			pci_bus_add_devices(bus);

796
		func->dev = pci_get_bus_and_slot(func->busno,
L
Linus Torvalds 已提交
797 798 799
				PCI_DEVFN(func->device, func->function));
		if (func->dev == NULL) {
			err("ERROR... : pci_dev still NULL\n");
800
			goto out;
L
Linus Torvalds 已提交
801 802 803
		}
	}
	if (!(flag) && (func->dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
804 805 806 807
		pci_hp_add_bridge(func->dev);
		child = func->dev->subordinate;
		if (child)
			pci_bus_add_devices(child);
L
Linus Torvalds 已提交
808 809
	}

810 811
 out:
	pci_unlock_rescan_remove();
L
Linus Torvalds 已提交
812 813 814 815
	return 0;
}

/*******************************************************
816
 * Returns whether the bus is empty or not
L
Linus Torvalds 已提交
817
 *******************************************************/
R
Ryan Desfosses 已提交
818
static int is_bus_empty(struct slot *slot_cur)
L
Linus Torvalds 已提交
819 820
{
	int rc;
R
Ryan Desfosses 已提交
821
	struct slot *tmp_slot;
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	u8 i = slot_cur->bus_on->slot_min;

	while (i <= slot_cur->bus_on->slot_max) {
		if (i == slot_cur->number) {
			i++;
			continue;
		}
		tmp_slot = ibmphp_get_slot_from_physical_num(i);
		if (!tmp_slot)
			return 0;
		rc = slot_update(&tmp_slot);
		if (rc)
			return 0;
		if (SLOT_PRESENT(tmp_slot->status) &&
					SLOT_PWRGD(tmp_slot->status))
			return 0;
		i++;
	}
	return 1;
}

/***********************************************************
844
 * If the HPC permits and the bus currently empty, tries to set the
L
Linus Torvalds 已提交
845 846 847 848
 * bus speed and mode at the maximum card and bus capability
 * Parameters: slot
 * Returns: bus is set (0) or error code
 ***********************************************************/
R
Ryan Desfosses 已提交
849
static int set_bus(struct slot *slot_cur)
L
Linus Torvalds 已提交
850 851 852 853 854
{
	int rc;
	u8 speed;
	u8 cmd = 0x0;
	int retval;
855
	static const struct pci_device_id ciobx[] = {
L
Linus Torvalds 已提交
856
		{ PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 0x0101) },
B
Bogicevic Sasa 已提交
857
		{ },
858
	};
L
Linus Torvalds 已提交
859

860
	debug("%s - entry slot # %d\n", __func__, slot_cur->number);
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	if (SET_BUS_STATUS(slot_cur->ctrl) && is_bus_empty(slot_cur)) {
		rc = slot_update(&slot_cur);
		if (rc)
			return rc;
		speed = SLOT_SPEED(slot_cur->ext_status);
		debug("ext_status = %x, speed = %x\n", slot_cur->ext_status, speed);
		switch (speed) {
		case HPC_SLOT_SPEED_33:
			cmd = HPC_BUS_33CONVMODE;
			break;
		case HPC_SLOT_SPEED_66:
			if (SLOT_PCIX(slot_cur->ext_status)) {
				if ((slot_cur->supported_speed >= BUS_SPEED_66) &&
						(slot_cur->supported_bus_mode == BUS_MODE_PCIX))
					cmd = HPC_BUS_66PCIXMODE;
				else if (!SLOT_BUS_MODE(slot_cur->ext_status))
					/* if max slot/bus capability is 66 pci
					and there's no bus mode mismatch, then
879
					the adapter supports 66 pci */
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
					cmd = HPC_BUS_66CONVMODE;
				else
					cmd = HPC_BUS_33CONVMODE;
			} else {
				if (slot_cur->supported_speed >= BUS_SPEED_66)
					cmd = HPC_BUS_66CONVMODE;
				else
					cmd = HPC_BUS_33CONVMODE;
			}
			break;
		case HPC_SLOT_SPEED_133:
			switch (slot_cur->supported_speed) {
			case BUS_SPEED_33:
				cmd = HPC_BUS_33CONVMODE;
				break;
			case BUS_SPEED_66:
				if (slot_cur->supported_bus_mode == BUS_MODE_PCIX)
					cmd = HPC_BUS_66PCIXMODE;
				else
					cmd = HPC_BUS_66CONVMODE;
				break;
			case BUS_SPEED_100:
				cmd = HPC_BUS_100PCIXMODE;
				break;
			case BUS_SPEED_133:
				/* This is to take care of the bug in CIOBX chip */
				if (pci_dev_present(ciobx))
					ibmphp_hpc_writeslot(slot_cur,
							HPC_BUS_100PCIXMODE);
				cmd = HPC_BUS_133PCIXMODE;
				break;
			default:
				err("Wrong bus speed\n");
				return -ENODEV;
			}
			break;
		default:
			err("wrong slot speed\n");
			return -ENODEV;
		}
		debug("setting bus speed for slot %d, cmd %x\n",
						slot_cur->number, cmd);
		retval = ibmphp_hpc_writeslot(slot_cur, cmd);
		if (retval) {
			err("setting bus speed failed\n");
			return retval;
		}
		if (CTLR_RESULT(slot_cur->ctrl->status)) {
			err("command not completed successfully in set_bus\n");
			return -EIO;
		}
	}
932
	/* This is for x440, once Brandon fixes the firmware,
L
Linus Torvalds 已提交
933 934
	will not need this delay */
	msleep(1000);
935
	debug("%s -Exit\n", __func__);
L
Linus Torvalds 已提交
936 937 938 939
	return 0;
}

/* This routine checks the bus limitations that the slot is on from the BIOS.
940
 * This is used in deciding whether or not to power up the slot.
L
Linus Torvalds 已提交
941
 * (electrical/spec limitations. For example, >1 133 MHz or >2 66 PCI cards on
942
 * same bus)
L
Linus Torvalds 已提交
943 944 945 946 947 948
 * Parameters: slot
 * Returns: 0 = no limitations, -EINVAL = exceeded limitations on the bus
 */
static int check_limitations(struct slot *slot_cur)
{
	u8 i;
R
Ryan Desfosses 已提交
949
	struct slot *tmp_slot;
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	u8 count = 0;
	u8 limitation = 0;

	for (i = slot_cur->bus_on->slot_min; i <= slot_cur->bus_on->slot_max; i++) {
		tmp_slot = ibmphp_get_slot_from_physical_num(i);
		if (!tmp_slot)
			return -ENODEV;
		if ((SLOT_PWRGD(tmp_slot->status)) &&
					!(SLOT_CONNECT(tmp_slot->status)))
			count++;
	}
	get_cur_bus_info(&slot_cur);
	switch (slot_cur->bus_on->current_speed) {
	case BUS_SPEED_33:
		limitation = slot_cur->bus_on->slots_at_33_conv;
		break;
	case BUS_SPEED_66:
		if (slot_cur->bus_on->current_bus_mode == BUS_MODE_PCIX)
			limitation = slot_cur->bus_on->slots_at_66_pcix;
		else
			limitation = slot_cur->bus_on->slots_at_66_conv;
		break;
	case BUS_SPEED_100:
		limitation = slot_cur->bus_on->slots_at_100_pcix;
		break;
	case BUS_SPEED_133:
		limitation = slot_cur->bus_on->slots_at_133_pcix;
		break;
	}

	if ((count + 1) > limitation)
		return -EINVAL;
	return 0;
}

static inline void print_card_capability(struct slot *slot_cur)
{
	info("capability of the card is ");
988
	if ((slot_cur->ext_status & CARD_INFO) == PCIX133)
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		info("   133 MHz PCI-X\n");
	else if ((slot_cur->ext_status & CARD_INFO) == PCIX66)
		info("    66 MHz PCI-X\n");
	else if ((slot_cur->ext_status & CARD_INFO) == PCI66)
		info("    66 MHz PCI\n");
	else
		info("    33 MHz PCI\n");

}

/* This routine will power on the slot, configure the device(s) and find the
 * drivers for them.
 * Parameters: hotplug_slot
 * Returns: 0 or failure codes
 */
static int enable_slot(struct hotplug_slot *hs)
{
	int rc, i, rcpr;
	struct slot *slot_cur;
	u8 function;
	struct pci_func *tmp_func;

	ibmphp_lock_operations();

	debug("ENABLING SLOT........\n");
	slot_cur = hs->private;

1016 1017
	rc = validate(slot_cur, ENABLE);
	if (rc) {
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022
		err("validate function failed\n");
		goto error_nopower;
	}

	attn_LED_blink(slot_cur);
1023

L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	rc = set_bus(slot_cur);
	if (rc) {
		err("was not able to set the bus\n");
		goto error_nopower;
	}

	/*-----------------debugging------------------------------*/
	get_cur_bus_info(&slot_cur);
	debug("the current bus speed right after set_bus = %x\n",
					slot_cur->bus_on->current_speed);
	/*----------------------------------------------------------*/

	rc = check_limitations(slot_cur);
	if (rc) {
		err("Adding this card exceeds the limitations of this bus.\n");
1039
		err("(i.e., >1 133MHz cards running on same bus, or >2 66 PCI cards running on same bus.\n");
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		err("Try hot-adding into another bus\n");
		rc = -EINVAL;
		goto error_nopower;
	}

	rc = power_on(slot_cur);

	if (rc) {
		err("something wrong when powering up... please see below for details\n");
		/* need to turn off before on, otherwise, blinking overwrites */
		attn_off(slot_cur);
		attn_on(slot_cur);
		if (slot_update(&slot_cur)) {
			attn_off(slot_cur);
			attn_on(slot_cur);
			rc = -ENODEV;
			goto exit;
		}
		/* Check to see the error of why it failed */
		if ((SLOT_POWER(slot_cur->status)) &&
					!(SLOT_PWRGD(slot_cur->status)))
			err("power fault occurred trying to power up\n");
		else if (SLOT_BUS_SPEED(slot_cur->status)) {
1063
			err("bus speed mismatch occurred.  please check current bus speed and card capability\n");
L
Linus Torvalds 已提交
1064 1065
			print_card_capability(slot_cur);
		} else if (SLOT_BUS_MODE(slot_cur->ext_status)) {
1066
			err("bus mode mismatch occurred.  please check current bus mode and card capability\n");
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
			print_card_capability(slot_cur);
		}
		ibmphp_update_slot_info(slot_cur);
		goto exit;
	}
	debug("after power_on\n");
	/*-----------------------debugging---------------------------*/
	get_cur_bus_info(&slot_cur);
	debug("the current bus speed right after power_on = %x\n",
					slot_cur->bus_on->current_speed);
	/*----------------------------------------------------------*/

	rc = slot_update(&slot_cur);
	if (rc)
		goto error_power;
1082

L
Linus Torvalds 已提交
1083 1084 1085 1086 1087 1088
	rc = -EINVAL;
	if (SLOT_POWER(slot_cur->status) && !(SLOT_PWRGD(slot_cur->status))) {
		err("power fault occurred trying to power up...\n");
		goto error_power;
	}
	if (SLOT_POWER(slot_cur->status) && (SLOT_BUS_SPEED(slot_cur->status))) {
1089
		err("bus speed mismatch occurred.  please check current bus speed and card capability\n");
L
Linus Torvalds 已提交
1090 1091
		print_card_capability(slot_cur);
		goto error_power;
1092
	}
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099
	/* Don't think this case will happen after above checks...
	 * but just in case, for paranoia sake */
	if (!(SLOT_POWER(slot_cur->status))) {
		err("power on failed...\n");
		goto error_power;
	}

1100
	slot_cur->func = kzalloc(sizeof(struct pci_func), GFP_KERNEL);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	if (!slot_cur->func) {
		/* We cannot do update_slot_info here, since no memory for
		 * kmalloc n.e.ways, and update_slot_info allocates some */
		err("out of system memory\n");
		rc = -ENOMEM;
		goto error_power;
	}
	slot_cur->func->busno = slot_cur->bus;
	slot_cur->func->device = slot_cur->device;
	for (i = 0; i < 4; i++)
		slot_cur->func->irq[i] = slot_cur->irq[i];

	debug("b4 configure_card, slot_cur->bus = %x, slot_cur->device = %x\n",
					slot_cur->bus, slot_cur->device);

	if (ibmphp_configure_card(slot_cur->func, slot_cur->number)) {
		err("configure_card was unsuccessful...\n");
		/* true because don't need to actually deallocate resources,
		 * just remove references */
		ibmphp_unconfigure_card(&slot_cur, 1);
		debug("after unconfigure_card\n");
		slot_cur->func = NULL;
		rc = -ENOMEM;
		goto error_power;
	}

	function = 0x00;
	do {
		tmp_func = ibm_slot_find(slot_cur->bus, slot_cur->func->device,
							function++);
		if (tmp_func && !(tmp_func->dev))
			ibm_configure_device(tmp_func);
	} while (tmp_func);

	attn_off(slot_cur);
	if (slot_update(&slot_cur)) {
		rc = -EFAULT;
		goto exit;
	}
	ibmphp_print_test();
	rc = ibmphp_update_slot_info(slot_cur);
exit:
1143
	ibmphp_unlock_operations();
L
Linus Torvalds 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	return rc;

error_nopower:
	attn_off(slot_cur);	/* need to turn off if was blinking b4 */
	attn_on(slot_cur);
error_cont:
	rcpr = slot_update(&slot_cur);
	if (rcpr) {
		rc = rcpr;
		goto exit;
	}
	ibmphp_update_slot_info(slot_cur);
	goto exit;

error_power:
	attn_off(slot_cur);	/* need to turn off if was blinking b4 */
	attn_on(slot_cur);
	rcpr = power_off(slot_cur);
	if (rcpr) {
		rc = rcpr;
		goto exit;
	}
	goto error_cont;
}

/**************************************************************
* HOT REMOVING ADAPTER CARD                                   *
* INPUT: POINTER TO THE HOTPLUG SLOT STRUCTURE                *
* OUTPUT: SUCCESS 0 ; FAILURE: UNCONFIGURE , VALIDATE         *
B
Bogicevic Sasa 已提交
1173
*		DISABLE POWER ,                               *
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178
**************************************************************/
static int ibmphp_disable_slot(struct hotplug_slot *hotplug_slot)
{
	struct slot *slot = hotplug_slot->private;
	int rc;
1179

L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	ibmphp_lock_operations();
	rc = ibmphp_do_disable_slot(slot);
	ibmphp_unlock_operations();
	return rc;
}

int ibmphp_do_disable_slot(struct slot *slot_cur)
{
	int rc;
	u8 flag;

1191 1192
	debug("DISABLING SLOT...\n");

1193
	if ((slot_cur == NULL) || (slot_cur->ctrl == NULL))
L
Linus Torvalds 已提交
1194
		return -ENODEV;
1195

L
Linus Torvalds 已提交
1196
	flag = slot_cur->flag;
1197
	slot_cur->flag = 1;
L
Linus Torvalds 已提交
1198

1199
	if (flag == 1) {
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207
		rc = validate(slot_cur, DISABLE);
			/* checking if powered off already & valid slot # */
		if (rc)
			goto error;
	}
	attn_LED_blink(slot_cur);

	if (slot_cur->func == NULL) {
1208
		/* We need this for functions that were there on bootup */
1209
		slot_cur->func = kzalloc(sizeof(struct pci_func), GFP_KERNEL);
L
Linus Torvalds 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		if (!slot_cur->func) {
			err("out of system memory\n");
			rc = -ENOMEM;
			goto error;
		}
		slot_cur->func->busno = slot_cur->bus;
		slot_cur->func->device = slot_cur->device;
	}

	ibm_unconfigure_device(slot_cur->func);
1220 1221 1222 1223 1224 1225 1226

	/*
	 * If we got here from latch suddenly opening on operating card or
	 * a power fault, there's no power to the card, so cannot
	 * read from it to determine what resources it occupied.  This operation
	 * is forbidden anyhow.  The best we can do is remove it from kernel
	 * lists at least */
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

	if (!flag) {
		attn_off(slot_cur);
		return 0;
	}

	rc = ibmphp_unconfigure_card(&slot_cur, 0);
	slot_cur->func = NULL;
	debug("in disable_slot. after unconfigure_card\n");
	if (rc) {
		err("could not unconfigure card.\n");
		goto error;
	}

	rc = ibmphp_hpc_writeslot(slot_cur, HPC_SLOT_OFF);
	if (rc)
		goto error;

	attn_off(slot_cur);
	rc = slot_update(&slot_cur);
	if (rc)
		goto exit;

	rc = ibmphp_update_slot_info(slot_cur);
	ibmphp_print_test();
exit:
	return rc;

error:
	/*  Need to turn off if was blinking b4 */
	attn_off(slot_cur);
	attn_on(slot_cur);
	if (slot_update(&slot_cur)) {
		rc = -EFAULT;
		goto exit;
	}
1263
	if (flag)
L
Linus Torvalds 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
		ibmphp_update_slot_info(slot_cur);
	goto exit;
}

struct hotplug_slot_ops ibmphp_hotplug_slot_ops = {
	.set_attention_status =		set_attention_status,
	.enable_slot =			enable_slot,
	.disable_slot =			ibmphp_disable_slot,
	.hardware_test =		NULL,
	.get_power_status =		get_power_status,
	.get_attention_status =		get_attention_status,
	.get_latch_status =		get_latch_status,
	.get_adapter_status =		get_adapter_present,
/*	.get_max_adapter_speed =	get_max_adapter_speed,
	.get_bus_name_status =		get_bus_name,
*/
};

static void ibmphp_unload(void)
{
	free_slots();
	debug("after slots\n");
	ibmphp_free_resources();
	debug("after resources\n");
	ibmphp_free_bus_info_queue();
	debug("after bus info\n");
	ibmphp_free_ebda_hpc_queue();
	debug("after ebda hpc\n");
	ibmphp_free_ebda_pci_rsrc_queue();
	debug("after ebda pci rsrc\n");
	kfree(ibmphp_pci_bus);
}

static int __init ibmphp_init(void)
{
	struct pci_bus *bus;
	int i = 0;
	int rc = 0;

	init_flag = 1;

	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");

	ibmphp_pci_bus = kmalloc(sizeof(*ibmphp_pci_bus), GFP_KERNEL);
	if (!ibmphp_pci_bus) {
		err("out of memory\n");
		rc = -ENOMEM;
		goto exit;
	}

	bus = pci_find_bus(0, 0);
	if (!bus) {
		err("Can't find the root pci bus, can not continue\n");
		rc = -ENODEV;
		goto error;
	}
	memcpy(ibmphp_pci_bus, bus, sizeof(*ibmphp_pci_bus));

	ibmphp_debug = debug;

	ibmphp_hpc_initvars();

	for (i = 0; i < 16; i++)
		irqs[i] = 0;

1329 1330
	rc = ibmphp_access_ebda();
	if (rc)
L
Linus Torvalds 已提交
1331 1332 1333
		goto error;
	debug("after ibmphp_access_ebda()\n");

1334 1335
	rc = ibmphp_rsrc_init();
	if (rc)
L
Linus Torvalds 已提交
1336 1337 1338 1339
		goto error;
	debug("AFTER Resource & EBDA INITIALIZATIONS\n");

	max_slots = get_max_slots();
1340

1341 1342
	rc = ibmphp_register_pci();
	if (rc)
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347 1348 1349 1350
		goto error;

	if (init_ops()) {
		rc = -ENODEV;
		goto error;
	}

	ibmphp_print_test();
1351 1352
	rc = ibmphp_hpc_start_poll_thread();
	if (rc)
L
Linus Torvalds 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
		goto error;

exit:
	return rc;

error:
	ibmphp_unload();
	goto exit;
}

static void __exit ibmphp_exit(void)
{
	ibmphp_hpc_stop_poll_thread();
	debug("after polling\n");
	ibmphp_unload();
	debug("done\n");
}

module_init(ibmphp_init);
1372
module_exit(ibmphp_exit);