cpqphp_core.c 35.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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
/*
 * Compaq Hot Plug Controller Driver
 *
 * Copyright (C) 1995,2001 Compaq Computer Corporation
 * Copyright (C) 2001 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (C) 2001 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 <greg@kroah.com>
 *
 * Jan 12, 2003 -	Added 66/100/133MHz PCI-X support,
28
 *			Torben Mathiasen <torben.mathiasen@hp.com>
Linus Torvalds's avatar
Linus Torvalds committed
29
30
31
32
33
34
35
36
37
38
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/pci.h>
39
#include <linux/pci_hotplug.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <linux/init.h>
#include <linux/interrupt.h>

#include <asm/uaccess.h>

#include "cpqphp.h"
#include "cpqphp_nvram.h"


/* Global variables */
int cpqhp_debug;
int cpqhp_legacy_mode;
struct controller *cpqhp_ctrl_list;	/* = NULL */
struct pci_func *cpqhp_slot_list[256];
54
struct irq_routing_table *cpqhp_routing_table;
Linus Torvalds's avatar
Linus Torvalds committed
55
56
57
58
59
60
61

/* local variables */
static void __iomem *smbios_table;
static void __iomem *smbios_start;
static void __iomem *cpqhp_rom_start;
static int power_mode;
static int debug;
62
static int initialized;
Linus Torvalds's avatar
Linus Torvalds committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

#define DRIVER_VERSION	"0.9.8"
#define DRIVER_AUTHOR	"Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>"
#define DRIVER_DESC	"Compaq Hot Plug PCI Controller Driver"

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

module_param(power_mode, bool, 0644);
MODULE_PARM_DESC(power_mode, "Power mode enabled or not");

module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "Debugging mode enabled or not");

#define CPQHPC_MODULE_MINOR 208

static inline int is_slot64bit(struct slot *slot)
{
	return (readb(slot->p_sm_slot + SMBIOS_SLOT_WIDTH) == 0x06) ? 1 : 0;
}

static inline int is_slot66mhz(struct slot *slot)
{
	return (readb(slot->p_sm_slot + SMBIOS_SLOT_TYPE) == 0x0E) ? 1 : 0;
}

/**
 * detect_SMBIOS_pointer - find the System Management BIOS Table in mem region.
 * @begin: begin pointer for region to be scanned.
 * @end: end pointer for region to be scanned.
 *
Randy Dunlap's avatar
Randy Dunlap committed
95
 * Returns pointer to the head of the SMBIOS tables (or %NULL).
Linus Torvalds's avatar
Linus Torvalds committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 */
static void __iomem * detect_SMBIOS_pointer(void __iomem *begin, void __iomem *end)
{
	void __iomem *fp;
	void __iomem *endp;
	u8 temp1, temp2, temp3, temp4;
	int status = 0;

	endp = (end - sizeof(u32) + 1);

	for (fp = begin; fp <= endp; fp += 16) {
		temp1 = readb(fp);
		temp2 = readb(fp+1);
		temp3 = readb(fp+2);
		temp4 = readb(fp+3);
		if (temp1 == '_' &&
		    temp2 == 'S' &&
		    temp3 == 'M' &&
		    temp4 == '_') {
			status = 1;
			break;
		}
	}
119

Linus Torvalds's avatar
Linus Torvalds committed
120
121
122
123
124
125
126
127
128
129
	if (!status)
		fp = NULL;

	dbg("Discovered SMBIOS Entry point at %p\n", fp);

	return fp;
}

/**
 * init_SERR - Initializes the per slot SERR generation.
Randy Dunlap's avatar
Randy Dunlap committed
130
 * @ctrl: controller to use
Linus Torvalds's avatar
Linus Torvalds committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 *
 * For unexpected switch opens
 */
static int init_SERR(struct controller * ctrl)
{
	u32 tempdword;
	u32 number_of_slots;
	u8 physical_slot;

	if (!ctrl)
		return 1;

	tempdword = ctrl->first_slot;

	number_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0F;
146
	/* Loop through slots */
Linus Torvalds's avatar
Linus Torvalds committed
147
148
149
150
151
152
153
154
155
156
	while (number_of_slots) {
		physical_slot = tempdword;
		writeb(0, ctrl->hpc_reg + SLOT_SERR);
		tempdword++;
		number_of_slots--;
	}

	return 0;
}

157
static int init_cpqhp_routing_table(void)
Linus Torvalds's avatar
Linus Torvalds committed
158
159
160
{
	int len;

161
162
	cpqhp_routing_table = pcibios_get_irq_routing_table();
	if (cpqhp_routing_table == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
163
164
		return -ENOMEM;

165
	len = cpqhp_routing_table_length();
Linus Torvalds's avatar
Linus Torvalds committed
166
	if (len == 0) {
167
168
		kfree(cpqhp_routing_table);
		cpqhp_routing_table = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
169
170
171
		return -1;
	}

172
173
174
175
176
177
178
179
180
181
182
	return 0;
}

/* nice debugging output */
static void pci_print_IRQ_route(void)
{
	int len;
	int loop;
	u8 tbus, tdevice, tslot;

	len = cpqhp_routing_table_length();
Linus Torvalds's avatar
Linus Torvalds committed
183

184
	dbg("bus dev func slot\n");
Linus Torvalds's avatar
Linus Torvalds committed
185
	for (loop = 0; loop < len; ++loop) {
186
187
188
		tbus = cpqhp_routing_table->slots[loop].bus;
		tdevice = cpqhp_routing_table->slots[loop].devfn;
		tslot = cpqhp_routing_table->slots[loop].slot;
Linus Torvalds's avatar
Linus Torvalds committed
189
190
191
		dbg("%d %d %d %d\n", tbus, tdevice >> 3, tdevice & 0x7, tslot);

	}
192
	return;
Linus Torvalds's avatar
Linus Torvalds committed
193
194
195
196
197
}


/**
 * get_subsequent_smbios_entry: get the next entry from bios table.
Randy Dunlap's avatar
Randy Dunlap committed
198
199
 * @smbios_start: where to start in the SMBIOS table
 * @smbios_table: location of the SMBIOS table
Linus Torvalds's avatar
Linus Torvalds committed
200
201
 * @curr: %NULL or pointer to previously returned structure
 *
Randy Dunlap's avatar
Randy Dunlap committed
202
203
204
205
206
 * Gets the first entry if previous == NULL;
 * otherwise, returns the next entry.
 * Uses global SMBIOS Table pointer.
 *
 * Returns a pointer to an SMBIOS structure or NULL if none found.
Linus Torvalds's avatar
Linus Torvalds committed
207
208
209
210
211
212
213
214
215
216
217
 */
static void __iomem *get_subsequent_smbios_entry(void __iomem *smbios_start,
						void __iomem *smbios_table,
						void __iomem *curr)
{
	u8 bail = 0;
	u8 previous_byte = 1;
	void __iomem *p_temp;
	void __iomem *p_max;

	if (!smbios_table || !curr)
218
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
219

220
	/* set p_max to the end of the table */
Linus Torvalds's avatar
Linus Torvalds committed
221
222
223
224
225
226
227
228
	p_max = smbios_start + readw(smbios_table + ST_LENGTH);

	p_temp = curr;
	p_temp += readb(curr + SMBIOS_GENERIC_LENGTH);

	while ((p_temp < p_max) && !bail) {
		/* Look for the double NULL terminator
		 * The first condition is the previous byte
229
230
		 * and the second is the curr
		 */
231
		if (!previous_byte && !(readb(p_temp)))
Linus Torvalds's avatar
Linus Torvalds committed
232
233
234
235
236
237
			bail = 1;

		previous_byte = readb(p_temp);
		p_temp++;
	}

238
	if (p_temp < p_max)
Linus Torvalds's avatar
Linus Torvalds committed
239
		return p_temp;
240
	else
Linus Torvalds's avatar
Linus Torvalds committed
241
242
243
244
245
		return NULL;
}


/**
Randy Dunlap's avatar
Randy Dunlap committed
246
247
248
249
 * get_SMBIOS_entry - return the requested SMBIOS entry or %NULL
 * @smbios_start: where to start in the SMBIOS table
 * @smbios_table: location of the SMBIOS table
 * @type: SMBIOS structure type to be returned
Linus Torvalds's avatar
Linus Torvalds committed
250
251
 * @previous: %NULL or pointer to previously returned structure
 *
Randy Dunlap's avatar
Randy Dunlap committed
252
 * Gets the first entry of the specified type if previous == %NULL;
Linus Torvalds's avatar
Linus Torvalds committed
253
 * Otherwise, returns the next entry of the given type.
Randy Dunlap's avatar
Randy Dunlap committed
254
255
 * Uses global SMBIOS Table pointer.
 * Uses get_subsequent_smbios_entry.
Linus Torvalds's avatar
Linus Torvalds committed
256
 *
Randy Dunlap's avatar
Randy Dunlap committed
257
 * Returns a pointer to an SMBIOS structure or %NULL if none found.
Linus Torvalds's avatar
Linus Torvalds committed
258
259
260
261
262
263
264
265
266
 */
static void __iomem *get_SMBIOS_entry(void __iomem *smbios_start,
					void __iomem *smbios_table,
					u8 type,
					void __iomem *previous)
{
	if (!smbios_table)
		return NULL;

267
	if (!previous)
Linus Torvalds's avatar
Linus Torvalds committed
268
		previous = smbios_start;
269
	else
Linus Torvalds's avatar
Linus Torvalds committed
270
271
272
		previous = get_subsequent_smbios_entry(smbios_start,
					smbios_table, previous);

273
274
	while (previous)
		if (readb(previous + SMBIOS_GENERIC_TYPE) != type)
Linus Torvalds's avatar
Linus Torvalds committed
275
276
			previous = get_subsequent_smbios_entry(smbios_start,
						smbios_table, previous);
277
		else
Linus Torvalds's avatar
Linus Torvalds committed
278
279
280
281
282
283
284
285
286
			break;

	return previous;
}

static void release_slot(struct hotplug_slot *hotplug_slot)
{
	struct slot *slot = hotplug_slot->private;

287
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

	kfree(slot->hotplug_slot->info);
	kfree(slot->hotplug_slot);
	kfree(slot);
}

static int ctrl_slot_cleanup (struct controller * ctrl)
{
	struct slot *old_slot, *next_slot;

	old_slot = ctrl->slot;
	ctrl->slot = NULL;

	while (old_slot) {
		/* memory will be freed by the release_slot callback */
		next_slot = old_slot->next;
		pci_hp_deregister (old_slot->hotplug_slot);
		old_slot = next_slot;
	}

308
309
	cpqhp_remove_debugfs_files(ctrl);

310
	/* Free IRQ associated with hot plug device */
Linus Torvalds's avatar
Linus Torvalds committed
311
	free_irq(ctrl->interrupt, ctrl);
312
	/* Unmap the memory */
Linus Torvalds's avatar
Linus Torvalds committed
313
	iounmap(ctrl->hpc_reg);
314
	/* Finally reclaim PCI mem */
Linus Torvalds's avatar
Linus Torvalds committed
315
316
317
	release_mem_region(pci_resource_start(ctrl->pci_dev, 0),
			   pci_resource_len(ctrl->pci_dev, 0));

318
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
319
320
321
}


322
323
324
325
326
327
328
329
330
331
332
/**
 * get_slot_mapping - determine logical slot mapping for PCI device
 *
 * Won't work for more than one PCI-PCI bridge in a slot.
 *
 * @bus_num - bus number of PCI device
 * @dev_num - device number of PCI device
 * @slot - Pointer to u8 where slot number will	be returned
 *
 * Output:	SUCCESS or FAILURE
 */
Linus Torvalds's avatar
Linus Torvalds committed
333
334
335
336
337
338
339
340
341
static int
get_slot_mapping(struct pci_bus *bus, u8 bus_num, u8 dev_num, u8 *slot)
{
	u32 work;
	long len;
	long loop;

	u8 tbus, tdevice, tslot, bridgeSlot;

342
	dbg("%s: %p, %d, %d, %p\n", __func__, bus, bus_num, dev_num, slot);
Linus Torvalds's avatar
Linus Torvalds committed
343
344
345

	bridgeSlot = 0xFF;

346
	len = cpqhp_routing_table_length();
Linus Torvalds's avatar
Linus Torvalds committed
347
	for (loop = 0; loop < len; ++loop) {
348
349
350
		tbus = cpqhp_routing_table->slots[loop].bus;
		tdevice = cpqhp_routing_table->slots[loop].devfn >> 3;
		tslot = cpqhp_routing_table->slots[loop].slot;
Linus Torvalds's avatar
Linus Torvalds committed
351
352
353
354
355
356

		if ((tbus == bus_num) && (tdevice == dev_num)) {
			*slot = tslot;
			return 0;
		} else {
			/* Did not get a match on the target PCI device. Check
357
358
359
360
361
362
363
364
			 * if the current IRQ table entry is a PCI-to-PCI
			 * bridge device.  If so, and it's secondary bus
			 * matches the bus number for the target device, I need
			 * to save the bridge's slot number.  If I can not find
			 * an entry for the target device, I will have to
			 * assume it's on the other side of the bridge, and
			 * assign it the bridge's slot.
			 */
Linus Torvalds's avatar
Linus Torvalds committed
365
366
			bus->number = tbus;
			pci_bus_read_config_dword(bus, PCI_DEVFN(tdevice, 0),
367
						PCI_CLASS_REVISION, &work);
Linus Torvalds's avatar
Linus Torvalds committed
368
369
370
371
372
373

			if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
				pci_bus_read_config_dword(bus,
							PCI_DEVFN(tdevice, 0),
							PCI_PRIMARY_BUS, &work);
				// See if bridge's secondary bus matches target bus.
374
				if (((work >> 8) & 0x000000FF) == (long) bus_num)
Linus Torvalds's avatar
Linus Torvalds committed
375
376
377
378
379
380
					bridgeSlot = tslot;
			}
		}

	}

381
382
383
384
385
	/* If we got here, we didn't find an entry in the IRQ mapping table for
	 * the target PCI device.  If we did determine that the target device
	 * is on the other side of a PCI-to-PCI bridge, return the slot number
	 * for the bridge.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
386
387
388
389
	if (bridgeSlot != 0xFF) {
		*slot = bridgeSlot;
		return 0;
	}
390
	/* Couldn't find an entry in the routing table for this PCI device */
Linus Torvalds's avatar
Linus Torvalds committed
391
392
393
394
395
396
	return -1;
}


/**
 * cpqhp_set_attention_status - Turns the Amber LED for a slot on or off
Randy Dunlap's avatar
Randy Dunlap committed
397
398
399
 * @ctrl: struct controller to use
 * @func: PCI device/function info
 * @status: LED control flag: 1 = LED on, 0 = LED off
Linus Torvalds's avatar
Linus Torvalds committed
400
401
402
403
404
405
406
407
 */
static int
cpqhp_set_attention_status(struct controller *ctrl, struct pci_func *func,
				u32 status)
{
	u8 hp_slot;

	if (func == NULL)
408
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
409
410
411

	hp_slot = func->device - ctrl->slot_device_offset;

412
	/* Wait for exclusive access to hardware */
413
	mutex_lock(&ctrl->crit_sect);
Linus Torvalds's avatar
Linus Torvalds committed
414

415
	if (status == 1)
Linus Torvalds's avatar
Linus Torvalds committed
416
		amber_LED_on (ctrl, hp_slot);
417
	else if (status == 0)
Linus Torvalds's avatar
Linus Torvalds committed
418
		amber_LED_off (ctrl, hp_slot);
419
	else {
420
		/* Done with exclusive hardware access */
421
		mutex_unlock(&ctrl->crit_sect);
422
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
423
424
425
426
	}

	set_SOGO(ctrl);

427
	/* Wait for SOBS to be unset */
Linus Torvalds's avatar
Linus Torvalds committed
428
429
	wait_for_ctrl_irq (ctrl);

430
	/* Done with exclusive hardware access */
431
	mutex_unlock(&ctrl->crit_sect);
Linus Torvalds's avatar
Linus Torvalds committed
432

433
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
434
435
436
437
438
}


/**
 * set_attention_status - Turns the Amber LED for a slot on or off
Randy Dunlap's avatar
Randy Dunlap committed
439
440
 * @hotplug_slot: slot to change LED on
 * @status: LED control flag
Linus Torvalds's avatar
Linus Torvalds committed
441
442
443
444
445
446
447
448
449
450
451
 */
static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
{
	struct pci_func *slot_func;
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;
	u8 bus;
	u8 devfn;
	u8 device;
	u8 function;

452
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

	if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
		return -ENODEV;

	device = devfn >> 3;
	function = devfn & 0x7;
	dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);

	slot_func = cpqhp_slot_find(bus, device, function);
	if (!slot_func)
		return -ENODEV;

	return cpqhp_set_attention_status(ctrl, slot_func, status);
}


static int process_SI(struct hotplug_slot *hotplug_slot)
{
	struct pci_func *slot_func;
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;
	u8 bus;
	u8 devfn;
	u8 device;
	u8 function;

479
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
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
505
506
507
508
509
510

	if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
		return -ENODEV;

	device = devfn >> 3;
	function = devfn & 0x7;
	dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);

	slot_func = cpqhp_slot_find(bus, device, function);
	if (!slot_func)
		return -ENODEV;

	slot_func->bus = bus;
	slot_func->device = device;
	slot_func->function = function;
	slot_func->configured = 0;
	dbg("board_added(%p, %p)\n", slot_func, ctrl);
	return cpqhp_process_SI(ctrl, slot_func);
}


static int process_SS(struct hotplug_slot *hotplug_slot)
{
	struct pci_func *slot_func;
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;
	u8 bus;
	u8 devfn;
	u8 device;
	u8 function;

511
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
512
513
514
515
516
517
518
519
520
521
522
523

	if (cpqhp_get_bus_dev(ctrl, &bus, &devfn, slot->number) == -1)
		return -ENODEV;

	device = devfn >> 3;
	function = devfn & 0x7;
	dbg("bus, dev, fn = %d, %d, %d\n", bus, device, function);

	slot_func = cpqhp_slot_find(bus, device, function);
	if (!slot_func)
		return -ENODEV;

524
	dbg("In %s, slot_func = %p, ctrl = %p\n", __func__, slot_func, ctrl);
Linus Torvalds's avatar
Linus Torvalds committed
525
526
527
528
529
530
531
532
533
	return cpqhp_process_SS(ctrl, slot_func);
}


static int hardware_test(struct hotplug_slot *hotplug_slot, u32 value)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;

534
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
535

536
	return cpqhp_hardware_test(ctrl, value);
Linus Torvalds's avatar
Linus Torvalds committed
537
538
539
540
541
542
543
544
}


static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;

545
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
546
547
548
549
550
551
552
553
554

	*value = get_slot_enabled(ctrl, slot);
	return 0;
}

static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;
555

556
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
560
561
562
563
564
565
566

	*value = cpq_get_attention_status(ctrl, slot);
	return 0;
}

static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;

567
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
568
569
570
571
572
573
574
575
576
577
578

	*value = cpq_get_latch_status(ctrl, slot);

	return 0;
}

static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;

579
	dbg("%s - physical_slot = %s\n", __func__, slot_name(slot));
Linus Torvalds's avatar
Linus Torvalds committed
580
581
582
583
584
585

	*value = get_presence_status(ctrl, slot);

	return 0;
}

586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
static struct hotplug_slot_ops cpqphp_hotplug_slot_ops = {
	.set_attention_status =	set_attention_status,
	.enable_slot =		process_SI,
	.disable_slot =		process_SS,
	.hardware_test =	hardware_test,
	.get_power_status =	get_power_status,
	.get_attention_status =	get_attention_status,
	.get_latch_status =	get_latch_status,
	.get_adapter_status =	get_adapter_status,
};

#define SLOT_NAME_SIZE 10

static int ctrl_slot_setup(struct controller *ctrl,
			void __iomem *smbios_start,
			void __iomem *smbios_table)
{
	struct slot *slot;
	struct hotplug_slot *hotplug_slot;
	struct hotplug_slot_info *hotplug_slot_info;
606
	struct pci_bus *bus = ctrl->pci_bus;
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
	u8 number_of_slots;
	u8 slot_device;
	u8 slot_number;
	u8 ctrl_slot;
	u32 tempdword;
	char name[SLOT_NAME_SIZE];
	void __iomem *slot_entry= NULL;
	int result = -ENOMEM;

	dbg("%s\n", __func__);

	tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);

	number_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0F;
	slot_device = readb(ctrl->hpc_reg + SLOT_MASK) >> 4;
	slot_number = ctrl->first_slot;

	while (number_of_slots) {
		slot = kzalloc(sizeof(*slot), GFP_KERNEL);
		if (!slot)
			goto error;

		slot->hotplug_slot = kzalloc(sizeof(*(slot->hotplug_slot)),
						GFP_KERNEL);
		if (!slot->hotplug_slot)
			goto error_slot;
		hotplug_slot = slot->hotplug_slot;

635
		hotplug_slot->info = kzalloc(sizeof(*(hotplug_slot->info)),
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
662
663
664
665
666
667
668
669
670
671
							GFP_KERNEL);
		if (!hotplug_slot->info)
			goto error_hpslot;
		hotplug_slot_info = hotplug_slot->info;

		slot->ctrl = ctrl;
		slot->bus = ctrl->bus;
		slot->device = slot_device;
		slot->number = slot_number;
		dbg("slot->number = %u\n", slot->number);

		slot_entry = get_SMBIOS_entry(smbios_start, smbios_table, 9,
					slot_entry);

		while (slot_entry && (readw(slot_entry + SMBIOS_SLOT_NUMBER) !=
				slot->number)) {
			slot_entry = get_SMBIOS_entry(smbios_start,
						smbios_table, 9, slot_entry);
		}

		slot->p_sm_slot = slot_entry;

		init_timer(&slot->task_event);
		slot->task_event.expires = jiffies + 5 * HZ;
		slot->task_event.function = cpqhp_pushbutton_thread;

		/*FIXME: these capabilities aren't used but if they are
		 *	 they need to be correctly implemented
		 */
		slot->capabilities |= PCISLOT_REPLACE_SUPPORTED;
		slot->capabilities |= PCISLOT_INTERLOCK_SUPPORTED;

		if (is_slot64bit(slot))
			slot->capabilities |= PCISLOT_64_BIT_SUPPORTED;
		if (is_slot66mhz(slot))
			slot->capabilities |= PCISLOT_66_MHZ_SUPPORTED;
672
		if (bus->cur_bus_speed == PCI_SPEED_66MHz)
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
701
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
			slot->capabilities |= PCISLOT_66_MHZ_OPERATION;

		ctrl_slot =
			slot_device - (readb(ctrl->hpc_reg + SLOT_MASK) >> 4);

		/* Check presence */
		slot->capabilities |=
			((((~tempdword) >> 23) |
			 ((~tempdword) >> 15)) >> ctrl_slot) & 0x02;
		/* Check the switch state */
		slot->capabilities |=
			((~tempdword & 0xFF) >> ctrl_slot) & 0x01;
		/* Check the slot enable */
		slot->capabilities |=
			((read_slot_enable(ctrl) << 2) >> ctrl_slot) & 0x04;

		/* register this slot with the hotplug pci core */
		hotplug_slot->release = &release_slot;
		hotplug_slot->private = slot;
		snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
		hotplug_slot->ops = &cpqphp_hotplug_slot_ops;

		hotplug_slot_info->power_status = get_slot_enabled(ctrl, slot);
		hotplug_slot_info->attention_status =
			cpq_get_attention_status(ctrl, slot);
		hotplug_slot_info->latch_status =
			cpq_get_latch_status(ctrl, slot);
		hotplug_slot_info->adapter_status =
			get_presence_status(ctrl, slot);

		dbg("registering bus %d, dev %d, number %d, "
				"ctrl->slot_device_offset %d, slot %d\n",
				slot->bus, slot->device,
				slot->number, ctrl->slot_device_offset,
				slot_number);
		result = pci_hp_register(hotplug_slot,
					 ctrl->pci_dev->bus,
					 slot->device,
					 name);
		if (result) {
			err("pci_hp_register failed with error %d\n", result);
			goto error_info;
		}

		slot->next = ctrl->slot;
		ctrl->slot = slot;

		number_of_slots--;
		slot_device++;
		slot_number++;
	}

	return 0;
error_info:
	kfree(hotplug_slot_info);
error_hpslot:
	kfree(hotplug_slot);
error_slot:
	kfree(slot);
error:
	return result;
}

static int one_time_init(void)
{
	int loop;
	int retval = 0;

	if (initialized)
		return 0;

	power_mode = 0;

746
	retval = init_cpqhp_routing_table();
747
748
749
	if (retval)
		goto error;

750
751
752
	if (cpqhp_debug)
		pci_print_IRQ_route();

753
754
755
756
757
758
759
	dbg("Initialize + Start the notification mechanism \n");

	retval = cpqhp_event_start_thread();
	if (retval)
		goto error;

	dbg("Initialize slot lists\n");
760
	for (loop = 0; loop < 256; loop++)
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
		cpqhp_slot_list[loop] = NULL;

	/* FIXME: We also need to hook the NMI handler eventually.
	 * this also needs to be worked with Christoph
	 * register_NMI_handler();
	 */
	/* Map rom address */
	cpqhp_rom_start = ioremap(ROM_PHY_ADDR, ROM_PHY_LEN);
	if (!cpqhp_rom_start) {
		err ("Could not ioremap memory region for ROM\n");
		retval = -EIO;
		goto error;
	}

	/* Now, map the int15 entry point if we are on compaq specific
	 * hardware
	 */
	compaq_nvram_init(cpqhp_rom_start);

	/* Map smbios table entry point structure */
	smbios_table = detect_SMBIOS_pointer(cpqhp_rom_start,
					cpqhp_rom_start + ROM_PHY_LEN);
	if (!smbios_table) {
		err ("Could not find the SMBIOS pointer in memory\n");
		retval = -EIO;
		goto error_rom_start;
	}

	smbios_start = ioremap(readl(smbios_table + ST_ADDRESS),
					readw(smbios_table + ST_LENGTH));
	if (!smbios_start) {
		err ("Could not ioremap memory region taken from SMBIOS values\n");
		retval = -EIO;
		goto error_smbios_start;
	}

	initialized = 1;

	return retval;

error_smbios_start:
	iounmap(smbios_start);
error_rom_start:
	iounmap(cpqhp_rom_start);
error:
	return retval;
}

Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
812
813
814
815
816
817
818
819
820
821
static int cpqhpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	u8 num_of_slots = 0;
	u8 hp_slot = 0;
	u8 device;
	u8 bus_cap;
	u16 temp_word;
	u16 vendor_id;
	u16 subsystem_vid;
	u16 subsystem_deviceid;
	u32 rc;
	struct controller *ctrl;
	struct pci_func *func;
822
	struct pci_bus *bus;
823
824
825
826
827
828
829
830
	int err;

	err = pci_enable_device(pdev);
	if (err) {
		printk(KERN_ERR MY_NAME ": cannot enable PCI device %s (%d)\n",
			pci_name(pdev), err);
		return err;
	}
831

832
	bus = pdev->subordinate;
833
834
835
836
837
838
	if (!bus) {
		dev_notice(&pdev->dev, "the device is not a bridge, "
				"skipping\n");
		rc = -ENODEV;
		goto err_disable_device;
	}
Linus Torvalds's avatar
Linus Torvalds committed
839

840
841
842
	/* Need to read VID early b/c it's used to differentiate CPQ and INTC
	 * discovery
	 */
Linus Torvalds's avatar
Linus Torvalds committed
843
844
845
	rc = pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor_id);
	if (rc || ((vendor_id != PCI_VENDOR_ID_COMPAQ) && (vendor_id != PCI_VENDOR_ID_INTEL))) {
		err(msg_HPC_non_compaq_or_intel);
846
847
		rc = -ENODEV;
		goto err_disable_device;
Linus Torvalds's avatar
Linus Torvalds committed
848
849
850
	}
	dbg("Vendor ID: %x\n", vendor_id);

851
852
	dbg("revision: %d\n", pdev->revision);
	if ((vendor_id == PCI_VENDOR_ID_COMPAQ) && (!pdev->revision)) {
Linus Torvalds's avatar
Linus Torvalds committed
853
		err(msg_HPC_rev_error);
854
855
		rc = -ENODEV;
		goto err_disable_device;
Linus Torvalds's avatar
Linus Torvalds committed
856
857
	}

Thomas Weber's avatar
Thomas Weber committed
858
	/* Check for the proper subsystem ID's
859
	 * Intel uses a different SSID programming model than Compaq.
Linus Torvalds's avatar
Linus Torvalds committed
860
861
862
	 * For Intel, each SSID bit identifies a PHP capability.
	 * Also Intel HPC's may have RID=0.
	 */
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
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
	if ((pdev->revision <= 2) && (vendor_id != PCI_VENDOR_ID_INTEL)) {
		err(msg_HPC_not_supported);
		return -ENODEV;
	}

	/* TODO: This code can be made to support non-Compaq or Intel
	 * subsystem IDs
	 */
	rc = pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vid);
	if (rc) {
		err("%s : pci_read_config_word failed\n", __func__);
		goto err_disable_device;
	}
	dbg("Subsystem Vendor ID: %x\n", subsystem_vid);
	if ((subsystem_vid != PCI_VENDOR_ID_COMPAQ) && (subsystem_vid != PCI_VENDOR_ID_INTEL)) {
		err(msg_HPC_non_compaq_or_intel);
		rc = -ENODEV;
		goto err_disable_device;
	}

	ctrl = kzalloc(sizeof(struct controller), GFP_KERNEL);
	if (!ctrl) {
		err("%s : out of memory\n", __func__);
		rc = -ENOMEM;
		goto err_disable_device;
	}

	rc = pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &subsystem_deviceid);
	if (rc) {
		err("%s : pci_read_config_word failed\n", __func__);
		goto err_free_ctrl;
	}

	info("Hot Plug Subsystem Device ID: %x\n", subsystem_deviceid);

	/* Set Vendor ID, so it can be accessed later from other
	 * functions
	 */
	ctrl->vendor_id = vendor_id;

	switch (subsystem_vid) {
	case PCI_VENDOR_ID_COMPAQ:
		if (pdev->revision >= 0x13) { /* CIOBX */
			ctrl->push_flag = 1;
			ctrl->slot_switch_type = 1;
			ctrl->push_button = 1;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 1;
			ctrl->pcix_speed_capability = 1;
			pci_read_config_byte(pdev, 0x41, &bus_cap);
			if (bus_cap & 0x80) {
				dbg("bus max supports 133MHz PCI-X\n");
916
				bus->max_bus_speed = PCI_SPEED_133MHz_PCIX;
917
918
919
920
				break;
			}
			if (bus_cap & 0x40) {
				dbg("bus max supports 100MHz PCI-X\n");
921
				bus->max_bus_speed = PCI_SPEED_100MHz_PCIX;
922
923
924
925
				break;
			}
			if (bus_cap & 20) {
				dbg("bus max supports 66MHz PCI-X\n");
926
				bus->max_bus_speed = PCI_SPEED_66MHz_PCIX;
927
928
929
930
				break;
			}
			if (bus_cap & 10) {
				dbg("bus max supports 66MHz PCI\n");
931
				bus->max_bus_speed = PCI_SPEED_66MHz;
932
933
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
934

935
			break;
Linus Torvalds's avatar
Linus Torvalds committed
936
937
		}

938
939
940
941
		switch (subsystem_deviceid) {
		case PCI_SUB_HPC_ID:
			/* Original 6500/7000 implementation */
			ctrl->slot_switch_type = 1;
942
			bus->max_bus_speed = PCI_SPEED_33MHz;
943
944
945
946
947
948
949
950
951
952
			ctrl->push_button = 0;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 0;
			ctrl->pcix_speed_capability = 0;
			break;
		case PCI_SUB_HPC_ID2:
			/* First Pushbutton implementation */
			ctrl->push_flag = 1;
			ctrl->slot_switch_type = 1;
953
			bus->max_bus_speed = PCI_SPEED_33MHz;
954
955
956
957
958
959
960
961
962
			ctrl->push_button = 1;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 0;
			ctrl->pcix_speed_capability = 0;
			break;
		case PCI_SUB_HPC_ID_INTC:
			/* Third party (6500/7000) */
			ctrl->slot_switch_type = 1;
963
			bus->max_bus_speed = PCI_SPEED_33MHz;
964
965
966
967
968
969
970
971
972
973
			ctrl->push_button = 0;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 0;
			ctrl->pcix_speed_capability = 0;
			break;
		case PCI_SUB_HPC_ID3:
			/* First 66 Mhz implementation */
			ctrl->push_flag = 1;
			ctrl->slot_switch_type = 1;
974
			bus->max_bus_speed = PCI_SPEED_66MHz;
975
976
977
978
979
980
981
982
983
984
			ctrl->push_button = 1;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 0;
			ctrl->pcix_speed_capability = 0;
			break;
		case PCI_SUB_HPC_ID4:
			/* First PCI-X implementation, 100MHz */
			ctrl->push_flag = 1;
			ctrl->slot_switch_type = 1;
985
			bus->max_bus_speed = PCI_SPEED_100MHz_PCIX;
986
987
988
989
990
991
992
993
994
			ctrl->push_button = 1;
			ctrl->pci_config_space = 1;
			ctrl->defeature_PHP = 1;
			ctrl->pcix_support = 1;
			ctrl->pcix_speed_capability = 0;
			break;
		default:
			err(msg_HPC_not_supported);
			rc = -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
995
996
			goto err_free_ctrl;
		}
997
		break;
Linus Torvalds's avatar
Linus Torvalds committed
998

999
1000
	case PCI_VENDOR_ID_INTEL:
		/* Check for speed capability (0=33, 1=66) */