cpci_hotplug_pci.c 7.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * CompactPCI Hot Plug Driver PCI functions
 *
4
 * Copyright (C) 2002,2005 by SOMA Networks, Inc.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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 <scottm@somanetworks.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
29
#include <linux/pci_hotplug.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39
#include <linux/proc_fs.h>
#include "../pci.h"
#include "cpci_hotplug.h"

#define MY_NAME	"cpci_hotplug"

extern int cpci_debug;

#define dbg(format, arg...)					\
	do {							\
40
		if (cpci_debug)					\
L
Linus Torvalds 已提交
41 42
			printk (KERN_DEBUG "%s: " format "\n",	\
				MY_NAME , ## arg); 		\
43
	} while (0)
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
#define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
#define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)


u8 cpci_get_attention_status(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
57
	if (!hs_cap)
L
Linus Torvalds 已提交
58 59
		return 0;

60
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
61 62
				     slot->devfn,
				     hs_cap + 2,
63
				     &hs_csr))
L
Linus Torvalds 已提交
64
		return 0;
65

L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76
	return hs_csr & 0x0008 ? 1 : 0;
}

int cpci_set_attention_status(struct slot* slot, int status)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
77
	if (!hs_cap)
L
Linus Torvalds 已提交
78
		return 0;
79
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
80 81
				     slot->devfn,
				     hs_cap + 2,
82
				     &hs_csr))
L
Linus Torvalds 已提交
83
		return 0;
84
	if (status)
L
Linus Torvalds 已提交
85
		hs_csr |= HS_CSR_LOO;
86
	else
L
Linus Torvalds 已提交
87
		hs_csr &= ~HS_CSR_LOO;
88
	if (pci_bus_write_config_word(slot->bus,
L
Linus Torvalds 已提交
89 90
				      slot->devfn,
				      hs_cap + 2,
91
				      hs_csr))
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103
		return 0;
	return 1;
}

u16 cpci_get_hs_csr(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
104
	if (!hs_cap)
L
Linus Torvalds 已提交
105
		return 0xFFFF;
106
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
107 108
				     slot->devfn,
				     hs_cap + 2,
109
				     &hs_csr))
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
		return 0xFFFF;
	return hs_csr;
}

int cpci_check_and_clear_ins(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;
	int ins = 0;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
123
	if (!hs_cap)
L
Linus Torvalds 已提交
124
		return 0;
125
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
126 127
				     slot->devfn,
				     hs_cap + 2,
128
				     &hs_csr))
L
Linus Torvalds 已提交
129
		return 0;
130
	if (hs_csr & HS_CSR_INS) {
L
Linus Torvalds 已提交
131
		/* Clear INS (by setting it) */
132
		if (pci_bus_write_config_word(slot->bus,
L
Linus Torvalds 已提交
133 134
					      slot->devfn,
					      hs_cap + 2,
135
					      hs_csr))
L
Linus Torvalds 已提交
136
			ins = 0;
137 138
		else
			ins = 1;
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151
	}
	return ins;
}

int cpci_check_ext(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;
	int ext = 0;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
152
	if (!hs_cap)
L
Linus Torvalds 已提交
153
		return 0;
154
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
155 156
				     slot->devfn,
				     hs_cap + 2,
157
				     &hs_csr))
L
Linus Torvalds 已提交
158
		return 0;
159
	if (hs_csr & HS_CSR_EXT)
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171
		ext = 1;
	return ext;
}

int cpci_clear_ext(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
172
	if (!hs_cap)
L
Linus Torvalds 已提交
173
		return -ENODEV;
174
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
175 176
				     slot->devfn,
				     hs_cap + 2,
177
				     &hs_csr))
L
Linus Torvalds 已提交
178
		return -ENODEV;
179
	if (hs_csr & HS_CSR_EXT) {
L
Linus Torvalds 已提交
180
		/* Clear EXT (by setting it) */
181
		if (pci_bus_write_config_word(slot->bus,
L
Linus Torvalds 已提交
182 183
					      slot->devfn,
					      hs_cap + 2,
184
					      hs_csr))
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
			return -ENODEV;
	}
	return 0;
}

int cpci_led_on(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
198
	if (!hs_cap)
L
Linus Torvalds 已提交
199
		return -ENODEV;
200
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
201 202
				     slot->devfn,
				     hs_cap + 2,
203
				     &hs_csr))
L
Linus Torvalds 已提交
204
		return -ENODEV;
205
	if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
L
Linus Torvalds 已提交
206
		hs_csr |= HS_CSR_LOO;
207
		if (pci_bus_write_config_word(slot->bus,
L
Linus Torvalds 已提交
208 209 210 211
					      slot->devfn,
					      hs_cap + 2,
					      hs_csr)) {
			err("Could not set LOO for slot %s",
212
			    hotplug_slot_name(slot->hotplug_slot));
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
			return -ENODEV;
		}
	}
	return 0;
}

int cpci_led_off(struct slot* slot)
{
	int hs_cap;
	u16 hs_csr;

	hs_cap = pci_bus_find_capability(slot->bus,
					 slot->devfn,
					 PCI_CAP_ID_CHSWP);
227
	if (!hs_cap)
L
Linus Torvalds 已提交
228
		return -ENODEV;
229
	if (pci_bus_read_config_word(slot->bus,
L
Linus Torvalds 已提交
230 231
				     slot->devfn,
				     hs_cap + 2,
232
				     &hs_csr))
L
Linus Torvalds 已提交
233
		return -ENODEV;
234
	if (hs_csr & HS_CSR_LOO) {
L
Linus Torvalds 已提交
235
		hs_csr &= ~HS_CSR_LOO;
236
		if (pci_bus_write_config_word(slot->bus,
L
Linus Torvalds 已提交
237 238 239 240
					      slot->devfn,
					      hs_cap + 2,
					      hs_csr)) {
			err("Could not clear LOO for slot %s",
241
			    hotplug_slot_name(slot->hotplug_slot));
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252
			return -ENODEV;
		}
	}
	return 0;
}


/*
 * Device configuration functions
 */

253
int __ref cpci_configure_slot(struct slot *slot)
L
Linus Torvalds 已提交
254
{
255 256
	struct pci_bus *parent;
	int fn;
L
Linus Torvalds 已提交
257

258
	dbg("%s - enter", __func__);
L
Linus Torvalds 已提交
259

260
	if (slot->dev == NULL) {
L
Linus Torvalds 已提交
261 262
		dbg("pci_dev null, finding %02x:%02x:%x",
		    slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
263
		slot->dev = pci_get_slot(slot->bus, slot->devfn);
L
Linus Torvalds 已提交
264 265 266
	}

	/* Still NULL? Well then scan for it! */
267
	if (slot->dev == NULL) {
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275
		int n;
		dbg("pci_dev still null");

		/*
		 * This will generate pci_dev structures for all functions, but
		 * we will only call this case when lookup fails.
		 */
		n = pci_scan_slot(slot->bus, slot->devfn);
276
		dbg("%s: pci_scan_slot returned %d", __func__, n);
277 278
		slot->dev = pci_get_slot(slot->bus, slot->devfn);
		if (slot->dev == NULL) {
L
Linus Torvalds 已提交
279
			err("Could not find PCI device for slot %02x", slot->number);
280
			return -ENODEV;
L
Linus Torvalds 已提交
281 282
		}
	}
283 284 285 286 287 288 289 290 291 292 293 294
	parent = slot->dev->bus;

	for (fn = 0; fn < 8; fn++) {
		struct pci_dev *dev;

		dev = pci_get_slot(parent, PCI_DEVFN(PCI_SLOT(slot->devfn), fn));
		if (!dev)
			continue;
		if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
		    (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)) {
			/* Find an unused bus number for the new bridge */
			struct pci_bus *child;
295 296
			unsigned char busnr, start = parent->busn_res.start;
			unsigned char end = parent->busn_res.end;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

			for (busnr = start; busnr <= end; busnr++) {
				if (!pci_find_bus(pci_domain_nr(parent),
						  busnr))
					break;
			}
			if (busnr >= end) {
				err("No free bus for hot-added bridge\n");
				pci_dev_put(dev);
				continue;
			}
			child = pci_add_new_bus(parent, dev, busnr);
			if (!child) {
				err("Cannot add new bus for %s\n",
				    pci_name(dev));
				pci_dev_put(dev);
				continue;
			}
315
			child->busn_res.end = pci_do_scan_bus(child);
316 317 318
			pci_bus_size_bridges(child);
		}
		pci_dev_put(dev);
L
Linus Torvalds 已提交
319 320
	}

321 322 323
	pci_bus_assign_resources(parent);
	pci_bus_add_devices(parent);
	pci_enable_bridges(parent);
S
Scott Murray 已提交
324

325
	dbg("%s - exit", __func__);
S
Scott Murray 已提交
326
	return 0;
L
Linus Torvalds 已提交
327 328 329 330 331 332 333
}

int cpci_unconfigure_slot(struct slot* slot)
{
	int i;
	struct pci_dev *dev;

334
	dbg("%s - enter", __func__);
335
	if (!slot->dev) {
L
Linus Torvalds 已提交
336 337 338 339 340
		err("No device for slot %02x\n", slot->number);
		return -ENODEV;
	}

	for (i = 0; i < 8; i++) {
341
		dev = pci_get_slot(slot->bus,
L
Linus Torvalds 已提交
342
				    PCI_DEVFN(PCI_SLOT(slot->devfn), i));
343
		if (dev) {
344
			pci_stop_and_remove_bus_device(dev);
345
			pci_dev_put(dev);
L
Linus Torvalds 已提交
346 347
		}
	}
348 349 350
	pci_dev_put(slot->dev);
	slot->dev = NULL;

351
	dbg("%s - exit", __func__);
S
Scott Murray 已提交
352
	return 0;
L
Linus Torvalds 已提交
353
}