pci.c 16.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
J
Jan Glauber 已提交
2 3 4 5 6 7 8 9
/*
 * Copyright IBM Corp. 2012
 *
 * Author(s):
 *   Jan Glauber <jang@linux.vnet.ibm.com>
 *
 * The System z PCI code is a rewrite from a prototype by
 * the following people (Kudoz!):
10 11 12 13 14 15 16
 *   Alexander Schmidt
 *   Christoph Raisch
 *   Hannes Hering
 *   Hoang-Nam Nguyen
 *   Jan-Bernd Themann
 *   Stefan Roscher
 *   Thomas Klein
J
Jan Glauber 已提交
17 18
 */

G
Gerald Schaefer 已提交
19 20
#define KMSG_COMPONENT "zpci"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
J
Jan Glauber 已提交
21 22 23 24 25 26 27 28 29

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/delay.h>
#include <linux/seq_file.h>
#include <linux/pci.h>

30 31
#include <asm/isc.h>
#include <asm/airq.h>
J
Jan Glauber 已提交
32 33
#include <asm/facility.h>
#include <asm/pci_insn.h>
J
Jan Glauber 已提交
34
#include <asm/pci_clp.h>
J
Jan Glauber 已提交
35
#include <asm/pci_dma.h>
J
Jan Glauber 已提交
36 37

/* list of all detected zpci devices */
38
static LIST_HEAD(zpci_list);
39
static DEFINE_SPINLOCK(zpci_list_lock);
J
Jan Glauber 已提交
40

41 42
static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
static DEFINE_SPINLOCK(zpci_domain_lock);
43

S
Sebastian Ott 已提交
44
#define ZPCI_IOMAP_ENTRIES						\
45
	min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2),	\
S
Sebastian Ott 已提交
46 47
	    ZPCI_IOMAP_MAX_ENTRIES)

J
Jan Glauber 已提交
48
static DEFINE_SPINLOCK(zpci_iomap_lock);
S
Sebastian Ott 已提交
49
static unsigned long *zpci_iomap_bitmap;
J
Jan Glauber 已提交
50 51 52
struct zpci_iomap_entry *zpci_iomap_start;
EXPORT_SYMBOL_GPL(zpci_iomap_start);

53 54
static struct kmem_cache *zdev_fmb_cache;

J
Jan Glauber 已提交
55 56 57 58
struct zpci_dev *get_zdev_by_fid(u32 fid)
{
	struct zpci_dev *tmp, *zdev = NULL;

59
	spin_lock(&zpci_list_lock);
J
Jan Glauber 已提交
60 61 62 63 64 65
	list_for_each_entry(tmp, &zpci_list, entry) {
		if (tmp->fid == fid) {
			zdev = tmp;
			break;
		}
	}
66
	spin_unlock(&zpci_list_lock);
J
Jan Glauber 已提交
67 68 69
	return zdev;
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void zpci_remove_reserved_devices(void)
{
	struct zpci_dev *tmp, *zdev;
	enum zpci_state state;
	LIST_HEAD(remove);

	spin_lock(&zpci_list_lock);
	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
		    !clp_get_state(zdev->fid, &state) &&
		    state == ZPCI_FN_STATE_RESERVED)
			list_move_tail(&zdev->entry, &remove);
	}
	spin_unlock(&zpci_list_lock);

	list_for_each_entry_safe(zdev, tmp, &remove, entry)
		zpci_remove_device(zdev);
}

J
Jan Glauber 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
{
	return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
}

int pci_domain_nr(struct pci_bus *bus)
{
	return ((struct zpci_dev *) bus->sysdata)->domain;
}
EXPORT_SYMBOL_GPL(pci_domain_nr);

int pci_proc_domain(struct pci_bus *bus)
{
	return pci_domain_nr(bus);
}
EXPORT_SYMBOL_GPL(pci_proc_domain);

J
Jan Glauber 已提交
106 107 108 109
/* Modify PCI: Register I/O address translation parameters */
int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
		       u64 base, u64 limit, u64 iota)
{
110 111 112
	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
	struct zpci_fib fib = {0};
	u8 status;
J
Jan Glauber 已提交
113 114

	WARN_ON_ONCE(iota & 0x3fff);
115 116 117 118
	fib.pba = base;
	fib.pal = limit;
	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
	return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
J
Jan Glauber 已提交
119 120 121 122 123
}

/* Modify PCI: Unregister I/O address translation parameters */
int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
{
124 125 126
	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
	struct zpci_fib fib = {0};
	u8 cc, status;
J
Jan Glauber 已提交
127

128 129 130 131
	cc = zpci_mod_fc(req, &fib, &status);
	if (cc == 3) /* Function already gone. */
		cc = 0;
	return cc ? -EIO : 0;
J
Jan Glauber 已提交
132 133
}

134 135 136
/* Modify PCI: Set PCI function measurement parameters */
int zpci_fmb_enable_device(struct zpci_dev *zdev)
{
137 138 139
	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
	struct zpci_fib fib = {0};
	u8 cc, status;
140

S
Sebastian Ott 已提交
141
	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
142 143
		return -EINVAL;

144
	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
145 146 147 148
	if (!zdev->fmb)
		return -ENOMEM;
	WARN_ON((u64) zdev->fmb & 0xf);

149 150 151 152 153
	/* reset software counters */
	atomic64_set(&zdev->allocated_pages, 0);
	atomic64_set(&zdev->mapped_pages, 0);
	atomic64_set(&zdev->unmapped_pages, 0);

154 155 156 157 158 159 160
	fib.fmb_addr = virt_to_phys(zdev->fmb);
	cc = zpci_mod_fc(req, &fib, &status);
	if (cc) {
		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
		zdev->fmb = NULL;
	}
	return cc ? -EIO : 0;
161 162 163 164 165
}

/* Modify PCI: Disable PCI function measurement */
int zpci_fmb_disable_device(struct zpci_dev *zdev)
{
166 167 168
	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
	struct zpci_fib fib = {0};
	u8 cc, status;
169 170 171 172 173

	if (!zdev->fmb)
		return -EINVAL;

	/* Function measurement is disabled if fmb address is zero */
174 175 176
	cc = zpci_mod_fc(req, &fib, &status);
	if (cc == 3) /* Function already gone. */
		cc = 0;
177

178 179 180 181 182
	if (!cc) {
		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
		zdev->fmb = NULL;
	}
	return cc ? -EIO : 0;
183 184
}

J
Jan Glauber 已提交
185 186 187 188 189 190
static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
{
	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
	u64 data;
	int rc;

191
	rc = zpci_load(&data, req, offset);
192
	if (!rc) {
193 194
		data = le64_to_cpu((__force __le64) data);
		data >>= (8 - len) * 8;
J
Jan Glauber 已提交
195
		*val = (u32) data;
196
	} else
J
Jan Glauber 已提交
197 198 199 200 201 202 203 204 205 206
		*val = 0xffffffff;
	return rc;
}

static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
{
	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
	u64 data = val;
	int rc;

207 208
	data <<= (8 - len) * 8;
	data = (__force u64) cpu_to_le64(data);
209
	rc = zpci_store(data, req, offset);
J
Jan Glauber 已提交
210 211 212 213 214 215 216 217 218 219
	return rc;
}

resource_size_t pcibios_align_resource(void *data, const struct resource *res,
				       resource_size_t size,
				       resource_size_t align)
{
	return 0;
}

220 221 222 223 224 225
/* combine single writes by using store-block insn */
void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
{
       zpci_memcpy_toio(to, from, count);
}

J
Jan Glauber 已提交
226
/* Create a virtual mapping cookie for a PCI BAR */
M
Michael S. Tsirkin 已提交
227 228 229 230
void __iomem *pci_iomap_range(struct pci_dev *pdev,
			      int bar,
			      unsigned long offset,
			      unsigned long max)
J
Jan Glauber 已提交
231
{
S
Sebastian Ott 已提交
232
	struct zpci_dev *zdev =	to_zpci(pdev);
J
Jan Glauber 已提交
233 234
	int idx;

S
Sebastian Ott 已提交
235
	if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
J
Jan Glauber 已提交
236 237 238 239
		return NULL;

	idx = zdev->bars[bar].map_idx;
	spin_lock(&zpci_iomap_lock);
M
Michael S. Tsirkin 已提交
240
	/* Detect overrun */
241 242 243
	WARN_ON(!++zpci_iomap_start[idx].count);
	zpci_iomap_start[idx].fh = zdev->fh;
	zpci_iomap_start[idx].bar = bar;
J
Jan Glauber 已提交
244 245
	spin_unlock(&zpci_iomap_lock);

246
	return (void __iomem *) ZPCI_ADDR(idx) + offset;
J
Jan Glauber 已提交
247
}
248
EXPORT_SYMBOL(pci_iomap_range);
M
Michael S. Tsirkin 已提交
249 250 251 252 253 254

void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
{
	return pci_iomap_range(dev, bar, 0, maxlen);
}
EXPORT_SYMBOL(pci_iomap);
J
Jan Glauber 已提交
255 256 257

void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
{
258
	unsigned int idx = ZPCI_IDX(addr);
J
Jan Glauber 已提交
259 260

	spin_lock(&zpci_iomap_lock);
M
Michael S. Tsirkin 已提交
261
	/* Detect underrun */
262
	WARN_ON(!zpci_iomap_start[idx].count);
M
Michael S. Tsirkin 已提交
263 264 265 266
	if (!--zpci_iomap_start[idx].count) {
		zpci_iomap_start[idx].fh = 0;
		zpci_iomap_start[idx].bar = 0;
	}
J
Jan Glauber 已提交
267 268
	spin_unlock(&zpci_iomap_lock);
}
269
EXPORT_SYMBOL(pci_iounmap);
J
Jan Glauber 已提交
270 271 272 273 274

static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
		    int size, u32 *val)
{
	struct zpci_dev *zdev = get_zdev_by_bus(bus);
275
	int ret;
J
Jan Glauber 已提交
276 277

	if (!zdev || devfn != ZPCI_DEVFN)
278 279 280 281 282
		ret = -ENODEV;
	else
		ret = zpci_cfg_load(zdev, where, val, size);

	return ret;
J
Jan Glauber 已提交
283 284 285 286 287 288
}

static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
		     int size, u32 val)
{
	struct zpci_dev *zdev = get_zdev_by_bus(bus);
289
	int ret;
J
Jan Glauber 已提交
290 291

	if (!zdev || devfn != ZPCI_DEVFN)
292 293 294 295 296
		ret = -ENODEV;
	else
		ret = zpci_cfg_store(zdev, where, val, size);

	return ret;
J
Jan Glauber 已提交
297 298 299 300 301 302 303
}

static struct pci_ops pci_root_ops = {
	.read = pci_read,
	.write = pci_write,
};

S
Sebastian Ott 已提交
304 305 306 307 308 309 310 311 312
#ifdef CONFIG_PCI_IOV
static struct resource iov_res = {
	.name	= "PCI IOV res",
	.start	= 0,
	.end	= -1,
	.flags	= IORESOURCE_MEM,
};
#endif

313
static void zpci_map_resources(struct pci_dev *pdev)
J
Jan Glauber 已提交
314 315 316 317 318 319 320 321
{
	resource_size_t len;
	int i;

	for (i = 0; i < PCI_BAR_COUNT; i++) {
		len = pci_resource_len(pdev, i);
		if (!len)
			continue;
322 323
		pdev->resource[i].start =
			(resource_size_t __force) pci_iomap(pdev, i, 0);
J
Jan Glauber 已提交
324 325
		pdev->resource[i].end = pdev->resource[i].start + len - 1;
	}
S
Sebastian Ott 已提交
326 327 328 329 330 331 332 333 334 335 336

#ifdef CONFIG_PCI_IOV
	i = PCI_IOV_RESOURCES;

	for (; i < PCI_SRIOV_NUM_BARS + PCI_IOV_RESOURCES; i++) {
		len = pci_resource_len(pdev, i);
		if (!len)
			continue;
		pdev->resource[i].parent = &iov_res;
	}
#endif
337 338
}

339
static void zpci_unmap_resources(struct pci_dev *pdev)
340 341 342 343 344 345 346 347
{
	resource_size_t len;
	int i;

	for (i = 0; i < PCI_BAR_COUNT; i++) {
		len = pci_resource_len(pdev, i);
		if (!len)
			continue;
348 349
		pci_iounmap(pdev, (void __iomem __force *)
			    pdev->resource[i].start);
350 351
	}
}
J
Jan Glauber 已提交
352 353 354

static int zpci_alloc_iomap(struct zpci_dev *zdev)
{
S
Sebastian Ott 已提交
355
	unsigned long entry;
J
Jan Glauber 已提交
356 357

	spin_lock(&zpci_iomap_lock);
S
Sebastian Ott 已提交
358 359
	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
	if (entry == ZPCI_IOMAP_ENTRIES) {
J
Jan Glauber 已提交
360 361 362
		spin_unlock(&zpci_iomap_lock);
		return -ENOSPC;
	}
S
Sebastian Ott 已提交
363
	set_bit(entry, zpci_iomap_bitmap);
J
Jan Glauber 已提交
364 365 366 367 368 369 370 371
	spin_unlock(&zpci_iomap_lock);
	return entry;
}

static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
{
	spin_lock(&zpci_iomap_lock);
	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
S
Sebastian Ott 已提交
372
	clear_bit(entry, zpci_iomap_bitmap);
J
Jan Glauber 已提交
373 374 375
	spin_unlock(&zpci_iomap_lock);
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
				    unsigned long size, unsigned long flags)
{
	struct resource *r;

	r = kzalloc(sizeof(*r), GFP_KERNEL);
	if (!r)
		return NULL;

	r->start = start;
	r->end = r->start + size - 1;
	r->flags = flags;
	r->name = zdev->res_name;

	if (request_resource(&iomem_resource, r)) {
		kfree(r);
		return NULL;
	}
	return r;
}

static int zpci_setup_bus_resources(struct zpci_dev *zdev,
				    struct list_head *resources)
{
	unsigned long addr, size, flags;
	struct resource *res;
	int i, entry;

	snprintf(zdev->res_name, sizeof(zdev->res_name),
		 "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);

	for (i = 0; i < PCI_BAR_COUNT; i++) {
		if (!zdev->bars[i].size)
			continue;
		entry = zpci_alloc_iomap(zdev);
		if (entry < 0)
			return entry;
		zdev->bars[i].map_idx = entry;

		/* only MMIO is supported */
		flags = IORESOURCE_MEM;
		if (zdev->bars[i].val & 8)
			flags |= IORESOURCE_PREFETCH;
		if (zdev->bars[i].val & 4)
			flags |= IORESOURCE_MEM_64;

422
		addr = ZPCI_ADDR(entry);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		size = 1UL << zdev->bars[i].size;

		res = __alloc_res(zdev, addr, size, flags);
		if (!res) {
			zpci_free_iomap(zdev, entry);
			return -ENOMEM;
		}
		zdev->bars[i].res = res;
		pci_add_resource(resources, res);
	}

	return 0;
}

static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
{
	int i;

	for (i = 0; i < PCI_BAR_COUNT; i++) {
442
		if (!zdev->bars[i].size || !zdev->bars[i].res)
443 444 445 446 447 448 449 450
			continue;

		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
		release_resource(zdev->bars[i].res);
		kfree(zdev->bars[i].res);
	}
}

451 452
int pcibios_add_device(struct pci_dev *pdev)
{
S
Sebastian Ott 已提交
453 454 455
	struct resource *res;
	int i;

S
Sebastian Ott 已提交
456 457 458
	if (pdev->is_physfn)
		pdev->no_vf_scan = 1;

459
	pdev->dev.groups = zpci_attr_groups;
460
	pdev->dev.dma_ops = &s390_pci_dma_ops;
461
	zpci_map_resources(pdev);
S
Sebastian Ott 已提交
462 463 464 465 466 467 468 469 470 471 472

	for (i = 0; i < PCI_BAR_COUNT; i++) {
		res = &pdev->resource[i];
		if (res->parent || !res->flags)
			continue;
		pci_claim_resource(pdev, i);
	}

	return 0;
}

473 474 475 476 477
void pcibios_release_device(struct pci_dev *pdev)
{
	zpci_unmap_resources(pdev);
}

S
Sebastian Ott 已提交
478 479
int pcibios_enable_device(struct pci_dev *pdev, int mask)
{
S
Sebastian Ott 已提交
480
	struct zpci_dev *zdev = to_zpci(pdev);
481

482
	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
483 484
	zpci_fmb_enable_device(zdev);

485
	return pci_enable_resources(pdev, mask);
486 487
}

S
Sebastian Ott 已提交
488
void pcibios_disable_device(struct pci_dev *pdev)
489
{
S
Sebastian Ott 已提交
490
	struct zpci_dev *zdev = to_zpci(pdev);
491 492 493 494 495

	zpci_fmb_disable_device(zdev);
	zpci_debug_exit_device(zdev);
}

496 497 498
#ifdef CONFIG_HIBERNATE_CALLBACKS
static int zpci_restore(struct device *dev)
{
499
	struct pci_dev *pdev = to_pci_dev(dev);
S
Sebastian Ott 已提交
500
	struct zpci_dev *zdev = to_zpci(pdev);
501 502 503 504 505 506 507 508 509
	int ret = 0;

	if (zdev->state != ZPCI_FN_STATE_ONLINE)
		goto out;

	ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
	if (ret)
		goto out;

510
	zpci_map_resources(pdev);
511
	zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
512 513 514 515 516 517 518 519
			   (u64) zdev->dma_table);

out:
	return ret;
}

static int zpci_freeze(struct device *dev)
{
520
	struct pci_dev *pdev = to_pci_dev(dev);
S
Sebastian Ott 已提交
521
	struct zpci_dev *zdev = to_zpci(pdev);
522 523 524 525 526

	if (zdev->state != ZPCI_FN_STATE_ONLINE)
		return 0;

	zpci_unregister_ioat(zdev, 0);
527
	zpci_unmap_resources(pdev);
528 529 530 531 532 533 534 535 536 537 538
	return clp_disable_fh(zdev);
}

struct dev_pm_ops pcibios_pm_ops = {
	.thaw_noirq = zpci_restore,
	.freeze_noirq = zpci_freeze,
	.restore_noirq = zpci_restore,
	.poweroff_noirq = zpci_freeze,
};
#endif /* CONFIG_HIBERNATE_CALLBACKS */

J
Jan Glauber 已提交
539 540
static int zpci_alloc_domain(struct zpci_dev *zdev)
{
541 542
	if (zpci_unique_uid) {
		zdev->domain = (u16) zdev->uid;
543 544 545 546 547 548 549 550 551 552
		if (zdev->domain >= ZPCI_NR_DEVICES)
			return 0;

		spin_lock(&zpci_domain_lock);
		if (test_bit(zdev->domain, zpci_domain)) {
			spin_unlock(&zpci_domain_lock);
			return -EEXIST;
		}
		set_bit(zdev->domain, zpci_domain);
		spin_unlock(&zpci_domain_lock);
553 554 555
		return 0;
	}

J
Jan Glauber 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568
	spin_lock(&zpci_domain_lock);
	zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
	if (zdev->domain == ZPCI_NR_DEVICES) {
		spin_unlock(&zpci_domain_lock);
		return -ENOSPC;
	}
	set_bit(zdev->domain, zpci_domain);
	spin_unlock(&zpci_domain_lock);
	return 0;
}

static void zpci_free_domain(struct zpci_dev *zdev)
{
569
	if (zdev->domain >= ZPCI_NR_DEVICES)
570 571
		return;

J
Jan Glauber 已提交
572 573 574 575 576
	spin_lock(&zpci_domain_lock);
	clear_bit(zdev->domain, zpci_domain);
	spin_unlock(&zpci_domain_lock);
}

577 578 579 580 581 582
void pcibios_remove_bus(struct pci_bus *bus)
{
	struct zpci_dev *zdev = get_zdev_by_bus(bus);

	zpci_exit_slot(zdev);
	zpci_cleanup_bus_resources(zdev);
583
	zpci_destroy_iommu(zdev);
584 585 586 587 588 589
	zpci_free_domain(zdev);

	spin_lock(&zpci_list_lock);
	list_del(&zdev->entry);
	spin_unlock(&zpci_list_lock);

590
	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
591 592 593 594 595 596 597 598 599 600
	kfree(zdev);
}

static int zpci_scan_bus(struct zpci_dev *zdev)
{
	LIST_HEAD(resources);
	int ret;

	ret = zpci_setup_bus_resources(zdev, &resources);
	if (ret)
601
		goto error;
602 603 604 605

	zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
				      zdev, &resources);
	if (!zdev->bus) {
606 607
		ret = -EIO;
		goto error;
608 609
	}
	zdev->bus->max_bus_speed = zdev->max_bus_speed;
610
	pci_bus_add_devices(zdev->bus);
611
	return 0;
612 613 614 615 616

error:
	zpci_cleanup_bus_resources(zdev);
	pci_free_resource_list(&resources);
	return ret;
617 618
}

J
Jan Glauber 已提交
619 620 621 622 623 624 625
int zpci_enable_device(struct zpci_dev *zdev)
{
	int rc;

	rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
	if (rc)
		goto out;
J
Jan Glauber 已提交
626 627 628 629

	rc = zpci_dma_init_device(zdev);
	if (rc)
		goto out_dma;
S
Sebastian Ott 已提交
630 631

	zdev->state = ZPCI_FN_STATE_ONLINE;
J
Jan Glauber 已提交
632
	return 0;
J
Jan Glauber 已提交
633 634 635

out_dma:
	clp_disable_fh(zdev);
J
Jan Glauber 已提交
636 637 638 639 640
out:
	return rc;
}
EXPORT_SYMBOL_GPL(zpci_enable_device);

641 642 643 644 645 646 647
int zpci_disable_device(struct zpci_dev *zdev)
{
	zpci_dma_exit_device(zdev);
	return clp_disable_fh(zdev);
}
EXPORT_SYMBOL_GPL(zpci_disable_device);

J
Jan Glauber 已提交
648 649 650 651 652 653 654 655
int zpci_create_device(struct zpci_dev *zdev)
{
	int rc;

	rc = zpci_alloc_domain(zdev);
	if (rc)
		goto out;

656 657 658 659
	rc = zpci_init_iommu(zdev);
	if (rc)
		goto out_free;

660
	mutex_init(&zdev->lock);
S
Sebastian Ott 已提交
661 662 663
	if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
		rc = zpci_enable_device(zdev);
		if (rc)
664
			goto out_destroy_iommu;
S
Sebastian Ott 已提交
665 666
	}
	rc = zpci_scan_bus(zdev);
J
Jan Glauber 已提交
667
	if (rc)
S
Sebastian Ott 已提交
668
		goto out_disable;
J
Jan Glauber 已提交
669

670
	spin_lock(&zpci_list_lock);
J
Jan Glauber 已提交
671
	list_add_tail(&zdev->entry, &zpci_list);
672
	spin_unlock(&zpci_list_lock);
J
Jan Glauber 已提交
673

674 675
	zpci_init_slot(zdev);

J
Jan Glauber 已提交
676 677
	return 0;

S
Sebastian Ott 已提交
678 679 680
out_disable:
	if (zdev->state == ZPCI_FN_STATE_ONLINE)
		zpci_disable_device(zdev);
681 682
out_destroy_iommu:
	zpci_destroy_iommu(zdev);
S
Sebastian Ott 已提交
683
out_free:
J
Jan Glauber 已提交
684 685 686 687 688
	zpci_free_domain(zdev);
out:
	return rc;
}

S
Sebastian Ott 已提交
689 690 691 692 693 694 695 696 697
void zpci_remove_device(struct zpci_dev *zdev)
{
	if (!zdev->bus)
		return;

	pci_stop_root_bus(zdev->bus);
	pci_remove_root_bus(zdev->bus);
}

698 699 700 701 702 703 704 705 706
int zpci_report_error(struct pci_dev *pdev,
		      struct zpci_report_error_header *report)
{
	struct zpci_dev *zdev = to_zpci(pdev);

	return sclp_pci_report(report, zdev->fh, zdev->fid);
}
EXPORT_SYMBOL(zpci_report_error);

J
Jan Glauber 已提交
707 708
static int zpci_mem_init(void)
{
709 710 711
	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));

712
	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
713
					   __alignof__(struct zpci_fmb), 0, NULL);
714
	if (!zdev_fmb_cache)
S
Sebastian Ott 已提交
715
		goto error_fmb;
716

S
Sebastian Ott 已提交
717 718
	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
				   sizeof(*zpci_iomap_start), GFP_KERNEL);
J
Jan Glauber 已提交
719
	if (!zpci_iomap_start)
720
		goto error_iomap;
J
Jan Glauber 已提交
721

S
Sebastian Ott 已提交
722 723 724 725 726 727 728 729
	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
	if (!zpci_iomap_bitmap)
		goto error_iomap_bitmap;

	return 0;
error_iomap_bitmap:
	kfree(zpci_iomap_start);
730
error_iomap:
731
	kmem_cache_destroy(zdev_fmb_cache);
S
Sebastian Ott 已提交
732
error_fmb:
J
Jan Glauber 已提交
733 734 735 736 737
	return -ENOMEM;
}

static void zpci_mem_exit(void)
{
S
Sebastian Ott 已提交
738
	kfree(zpci_iomap_bitmap);
J
Jan Glauber 已提交
739
	kfree(zpci_iomap_start);
740
	kmem_cache_destroy(zdev_fmb_cache);
J
Jan Glauber 已提交
741 742
}

743
static unsigned int s390_pci_probe __initdata = 1;
744
static unsigned int s390_pci_initialized;
J
Jan Glauber 已提交
745 746 747

char * __init pcibios_setup(char *str)
{
S
Sebastian Ott 已提交
748 749
	if (!strcmp(str, "off")) {
		s390_pci_probe = 0;
J
Jan Glauber 已提交
750 751 752 753 754
		return NULL;
	}
	return str;
}

755 756 757 758 759
bool zpci_is_enabled(void)
{
	return s390_pci_initialized;
}

J
Jan Glauber 已提交
760 761 762 763
static int __init pci_base_init(void)
{
	int rc;

764
	if (!s390_pci_probe)
J
Jan Glauber 已提交
765 766
		return 0;

767
	if (!test_facility(69) || !test_facility(71))
J
Jan Glauber 已提交
768 769
		return 0;

770 771
	rc = zpci_debug_init();
	if (rc)
772
		goto out;
773

J
Jan Glauber 已提交
774 775 776 777
	rc = zpci_mem_init();
	if (rc)
		goto out_mem;

778 779 780 781
	rc = zpci_irq_init();
	if (rc)
		goto out_irq;

J
Jan Glauber 已提交
782 783 784 785
	rc = zpci_dma_init();
	if (rc)
		goto out_dma;

S
Sebastian Ott 已提交
786
	rc = clp_scan_pci_devices();
J
Jan Glauber 已提交
787 788 789
	if (rc)
		goto out_find;

790
	s390_pci_initialized = 1;
J
Jan Glauber 已提交
791 792
	return 0;

J
Jan Glauber 已提交
793
out_find:
J
Jan Glauber 已提交
794 795
	zpci_dma_exit();
out_dma:
796 797
	zpci_irq_exit();
out_irq:
J
Jan Glauber 已提交
798 799
	zpci_mem_exit();
out_mem:
800
	zpci_debug_exit();
801
out:
J
Jan Glauber 已提交
802 803
	return rc;
}
804
subsys_initcall_sync(pci_base_init);
805 806 807

void zpci_rescan(void)
{
808 809
	if (zpci_is_enabled())
		clp_rescan_pci_devices_simple();
810
}