rsrc_nonstatic.c 29.6 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
/*
 * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * The initial developer of the original code is David A. Hinds
 * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
 * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
 *
 * (C) 1999		David A. Hinds
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/timer.h>
#include <linux/pci.h>
#include <linux/device.h>
D
Dominik Brodowski 已提交
27
#include <linux/io.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34

#include <asm/irq.h>

#include <pcmcia/ss.h>
#include <pcmcia/cistpl.h>
#include "cs_internal.h"

35
/* moved to rsrc_mgr.c
L
Linus Torvalds 已提交
36 37
MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
MODULE_LICENSE("GPL");
38
*/
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/* Parameters that can be set with 'insmod' */

#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)

INT_MODULE_PARM(probe_mem,	1);		/* memory probe? */
#ifdef CONFIG_PCMCIA_PROBE
INT_MODULE_PARM(probe_io,	1);		/* IO port probe? */
INT_MODULE_PARM(mem_limit,	0x10000);
#endif

/* for io_db and mem_db */
struct resource_map {
	u_long			base, num;
	struct resource_map	*next;
};

struct socket_data {
	struct resource_map		mem_db;
58
	struct resource_map		mem_db_valid;
L
Linus Torvalds 已提交
59 60 61 62 63 64
	struct resource_map		io_db;
};

#define MEM_PROBE_LOW	(1 << 0)
#define MEM_PROBE_HIGH	(1 << 1)

D
Dominik Brodowski 已提交
65 66 67
/* Action field */
#define REMOVE_MANAGED_RESOURCE		1
#define ADD_MANAGED_RESOURCE		2
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75

/*======================================================================

    Linux resource management extensions

======================================================================*/

static struct resource *
76 77
claim_region(struct pcmcia_socket *s, resource_size_t base,
		resource_size_t size, int type, char *name)
L
Linus Torvalds 已提交
78 79 80 81
{
	struct resource *res, *parent;

	parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource;
82
	res = pcmcia_make_resource(base, size, type | IORESOURCE_BUSY, name);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	if (res) {
#ifdef CONFIG_PCI
		if (s && s->cb_dev)
			parent = pci_find_parent_resource(s->cb_dev, res);
#endif
		if (!parent || request_resource(parent, res)) {
			kfree(res);
			res = NULL;
		}
	}
	return res;
}

static void free_region(struct resource *res)
{
	if (res) {
		release_resource(res);
		kfree(res);
	}
}

/*======================================================================

    These manage the internal databases of available resources.

======================================================================*/

static int add_interval(struct resource_map *map, u_long base, u_long num)
{
113
	struct resource_map *p, *q;
L
Linus Torvalds 已提交
114

115
	for (p = map; ; p = p->next) {
116 117 118 119
		if ((p != map) && (p->base+p->num >= base)) {
			p->num = max(num + base - p->base, p->num);
			return 0;
		}
120 121 122 123 124 125 126 127 128 129 130
		if ((p->next == map) || (p->next->base > base+num-1))
			break;
	}
	q = kmalloc(sizeof(struct resource_map), GFP_KERNEL);
	if (!q) {
		printk(KERN_WARNING "out of memory to update resources\n");
		return -ENOMEM;
	}
	q->base = base; q->num = num;
	q->next = p->next; p->next = q;
	return 0;
L
Linus Torvalds 已提交
131 132 133 134 135 136
}

/*====================================================================*/

static int sub_interval(struct resource_map *map, u_long base, u_long num)
{
D
Dominik Brodowski 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	struct resource_map *p, *q;

	for (p = map; ; p = q) {
		q = p->next;
		if (q == map)
			break;
		if ((q->base+q->num > base) && (base+num > q->base)) {
			if (q->base >= base) {
				if (q->base+q->num <= base+num) {
					/* Delete whole block */
					p->next = q->next;
					kfree(q);
					/* don't advance the pointer yet */
					q = p;
				} else {
					/* Cut off bit from the front */
					q->num = q->base + q->num - base - num;
					q->base = base + num;
				}
			} else if (q->base+q->num <= base+num) {
				/* Cut off bit from the end */
				q->num = base - q->base;
			} else {
				/* Split the block into two pieces */
				p = kmalloc(sizeof(struct resource_map),
					GFP_KERNEL);
				if (!p) {
					printk(KERN_WARNING "out of memory to update resources\n");
					return -ENOMEM;
				}
				p->base = base+num;
				p->num = q->base+q->num - p->base;
				q->num = base - q->base;
				p->next = q->next ; q->next = p;
			}
172
		}
D
Dominik Brodowski 已提交
173 174
	}
	return 0;
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184
}

/*======================================================================

    These routines examine a region of IO or memory addresses to
    determine what ranges might be genuinely available.

======================================================================*/

#ifdef CONFIG_PCMCIA_PROBE
185 186
static void do_io_probe(struct pcmcia_socket *s, unsigned int base,
			unsigned int num)
L
Linus Torvalds 已提交
187
{
D
Dominik Brodowski 已提交
188 189 190 191 192 193
	struct resource *res;
	struct socket_data *s_data = s->resource_data;
	unsigned int i, j, bad;
	int any;
	u_char *b, hole, most;

194
	dev_info(&s->dev, "cs: IO port probe %#x-%#x:", base, base+num-1);
D
Dominik Brodowski 已提交
195 196 197 198

	/* First, what does a floating port look like? */
	b = kzalloc(256, GFP_KERNEL);
	if (!b) {
199 200
		pr_cont("\n");
		dev_err(&s->dev, "do_io_probe: unable to kmalloc 256 bytes\n");
D
Dominik Brodowski 已提交
201 202 203
		return;
	}
	for (i = base, most = 0; i < base+num; i += 8) {
204
		res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
D
Dominik Brodowski 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		if (!res)
			continue;
		hole = inb(i);
		for (j = 1; j < 8; j++)
			if (inb(i+j) != hole)
				break;
		free_region(res);
		if ((j == 8) && (++b[hole] > b[most]))
			most = hole;
		if (b[most] == 127)
			break;
	}
	kfree(b);

	bad = any = 0;
	for (i = base; i < base+num; i += 8) {
221 222 223
		res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
		if (!res) {
			if (!any)
224
				pr_cont(" excluding");
225 226
			if (!bad)
				bad = any = i;
D
Dominik Brodowski 已提交
227
			continue;
228
		}
D
Dominik Brodowski 已提交
229 230 231 232 233 234
		for (j = 0; j < 8; j++)
			if (inb(i+j) != most)
				break;
		free_region(res);
		if (j < 8) {
			if (!any)
235
				pr_cont(" excluding");
D
Dominik Brodowski 已提交
236 237 238 239 240
			if (!bad)
				bad = any = i;
		} else {
			if (bad) {
				sub_interval(&s_data->io_db, bad, i-bad);
241
				pr_cont(" %#x-%#x", bad, i-1);
D
Dominik Brodowski 已提交
242 243 244 245 246 247
				bad = 0;
			}
		}
	}
	if (bad) {
		if ((num > 16) && (bad == base) && (i == base+num)) {
248
			sub_interval(&s_data->io_db, bad, i-bad);
249
			pr_cont(" nothing: probe failed.\n");
D
Dominik Brodowski 已提交
250 251 252
			return;
		} else {
			sub_interval(&s_data->io_db, bad, i-bad);
253
			pr_cont(" %#x-%#x", bad, i-1);
D
Dominik Brodowski 已提交
254 255 256
		}
	}

257
	pr_cont("%s\n", !any ? " clean" : "");
L
Linus Torvalds 已提交
258 259 260
}
#endif

261
/*======================================================================*/
L
Linus Torvalds 已提交
262

263 264 265
/**
 * readable() - iomem validation function for cards with a valid CIS
 */
266 267
static int readable(struct pcmcia_socket *s, struct resource *res,
		    unsigned int *count)
L
Linus Torvalds 已提交
268
{
269
	int ret = -EINVAL;
L
Linus Torvalds 已提交
270

271 272 273 274
	if (s->fake_cis) {
		dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n");
		return 0;
	}
L
Linus Torvalds 已提交
275 276 277 278

	s->cis_mem.res = res;
	s->cis_virt = ioremap(res->start, s->map_size);
	if (s->cis_virt) {
279
		mutex_unlock(&s->ops_mutex);
280 281 282
		/* as we're only called from pcmcia.c, we're safe */
		if (s->callback->validate)
			ret = s->callback->validate(s, count);
283
		/* invalidate mapping */
284
		mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
285 286 287 288
		iounmap(s->cis_virt);
		s->cis_virt = NULL;
	}
	s->cis_mem.res = NULL;
289 290 291
	if ((ret) || (*count == 0))
		return -EINVAL;
	return 0;
L
Linus Torvalds 已提交
292 293
}

294 295 296 297 298
/**
 * checksum() - iomem validation function for simple memory cards
 */
static int checksum(struct pcmcia_socket *s, struct resource *res,
		    unsigned int *value)
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
	pccard_mem_map map;
	int i, a = 0, b = -1, d;
	void __iomem *virt;

	virt = ioremap(res->start, s->map_size);
	if (virt) {
		map.map = 0;
		map.flags = MAP_ACTIVE;
		map.speed = 0;
		map.res = res;
		map.card_start = 0;
		s->ops->set_mem_map(s, &map);

		/* Don't bother checking every word... */
		for (i = 0; i < s->map_size; i += 44) {
			d = readl(virt+i);
			a += d;
			b &= d;
		}

		map.flags = 0;
		s->ops->set_mem_map(s, &map);

		iounmap(virt);
	}

326 327
	if (b == -1)
		return -EINVAL;
L
Linus Torvalds 已提交
328

329
	*value = a;
L
Linus Torvalds 已提交
330

331
	return 0;
L
Linus Torvalds 已提交
332 333
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/**
 * do_validate_mem() - low level validate a memory region for PCMCIA use
 * @s:		PCMCIA socket to validate
 * @base:	start address of resource to check
 * @size:	size of resource to check
 * @validate:	validation function to use
 *
 * do_validate_mem() splits up the memory region which is to be checked
 * into two parts. Both are passed to the @validate() function. If
 * @validate() returns non-zero, or the value parameter to @validate()
 * is zero, or the value parameter is different between both calls,
 * the check fails, and -EINVAL is returned. Else, 0 is returned.
 */
static int do_validate_mem(struct pcmcia_socket *s,
			   unsigned long base, unsigned long size,
			   int validate (struct pcmcia_socket *s,
					 struct resource *res,
					 unsigned int *value))
L
Linus Torvalds 已提交
352
{
353
	struct socket_data *s_data = s->resource_data;
L
Linus Torvalds 已提交
354
	struct resource *res1, *res2;
355 356
	unsigned int info1 = 1, info2 = 1;
	int ret = -EINVAL;
L
Linus Torvalds 已提交
357

D
Dominik Brodowski 已提交
358 359 360
	res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe");
	res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM,
			"PCMCIA memprobe");
L
Linus Torvalds 已提交
361 362

	if (res1 && res2) {
363 364 365 366 367
		ret = 0;
		if (validate) {
			ret = validate(s, res1, &info1);
			ret += validate(s, res2, &info2);
		}
L
Linus Torvalds 已提交
368 369
	}

370 371
	dev_dbg(&s->dev, "cs: memory probe 0x%06lx-0x%06lx: %p %p %u %u %u",
		base, base+size-1, res1, res2, ret, info1, info2);
L
Linus Torvalds 已提交
372

373 374 375
	free_region(res2);
	free_region(res1);

376 377
	if ((ret) || (info1 != info2) || (info1 == 0))
		return -EINVAL;
L
Linus Torvalds 已提交
378

379 380 381 382
	if (validate && !s->fake_cis) {
		/* move it to the validated data set */
		add_interval(&s_data->mem_db_valid, base, size);
		sub_interval(&s_data->mem_db, base, size);
L
Linus Torvalds 已提交
383 384
	}

385
	return 0;
L
Linus Torvalds 已提交
386 387 388
}


389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
/**
 * do_mem_probe() - validate a memory region for PCMCIA use
 * @s:		PCMCIA socket to validate
 * @base:	start address of resource to check
 * @num:	size of resource to check
 * @validate:	validation function to use
 * @fallback:	validation function to use if validate fails
 *
 * do_mem_probe() checks a memory region for use by the PCMCIA subsystem.
 * To do so, the area is split up into sensible parts, and then passed
 * into the @validate() function. Only if @validate() and @fallback() fail,
 * the area is marked as unavaibale for use by the PCMCIA subsystem. The
 * function returns the size of the usable memory area.
 */
static int do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num,
			int validate (struct pcmcia_socket *s,
				      struct resource *res,
				      unsigned int *value),
			int fallback (struct pcmcia_socket *s,
				      struct resource *res,
				      unsigned int *value))
L
Linus Torvalds 已提交
410
{
D
Dominik Brodowski 已提交
411 412 413
	struct socket_data *s_data = s->resource_data;
	u_long i, j, bad, fail, step;

414 415
	dev_info(&s->dev, "cs: memory probe 0x%06lx-0x%06lx:",
		 base, base+num-1);
D
Dominik Brodowski 已提交
416 417 418 419 420 421 422 423 424 425 426
	bad = fail = 0;
	step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
	/* don't allow too large steps */
	if (step > 0x800000)
		step = 0x800000;
	/* cis_readable wants to map 2x map_size */
	if (step < 2 * s->map_size)
		step = 2 * s->map_size;
	for (i = j = base; i < base+num; i = j + step) {
		if (!fail) {
			for (j = i; j < base+num; j += step) {
427
				if (!do_validate_mem(s, j, step, validate))
D
Dominik Brodowski 已提交
428 429 430 431
					break;
			}
			fail = ((i == base) && (j == base+num));
		}
432 433 434
		if ((fail) && (fallback)) {
			for (j = i; j < base+num; j += step)
				if (!do_validate_mem(s, j, step, fallback))
D
Dominik Brodowski 已提交
435 436 437 438
					break;
		}
		if (i != j) {
			if (!bad)
439 440
				pr_cont(" excluding");
			pr_cont(" %#05lx-%#05lx", i, j-1);
D
Dominik Brodowski 已提交
441 442 443 444
			sub_interval(&s_data->mem_db, i, j-i);
			bad += j-i;
		}
	}
445
	pr_cont("%s\n", !bad ? " clean" : "");
D
Dominik Brodowski 已提交
446
	return num - bad;
L
Linus Torvalds 已提交
447 448
}

449

L
Linus Torvalds 已提交
450 451
#ifdef CONFIG_PCMCIA_PROBE

452 453 454 455 456
/**
 * inv_probe() - top-to-bottom search for one usuable high memory area
 * @s:		PCMCIA socket to validate
 * @m:		resource_map to check
 */
L
Linus Torvalds 已提交
457 458
static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s)
{
459 460 461 462 463 464 465 466 467 468 469 470
	struct socket_data *s_data = s->resource_data;
	u_long ok;
	if (m == &s_data->mem_db)
		return 0;
	ok = inv_probe(m->next, s);
	if (ok) {
		if (m->base >= 0x100000)
			sub_interval(&s_data->mem_db, m->base, m->num);
		return ok;
	}
	if (m->base < 0x100000)
		return 0;
471
	return do_mem_probe(s, m->base, m->num, readable, checksum);
L
Linus Torvalds 已提交
472 473
}

474 475 476 477 478 479 480 481 482
/**
 * validate_mem() - memory probe function
 * @s:		PCMCIA socket to validate
 * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH
 *
 * The memory probe.  If the memory list includes a 64K-aligned block
 * below 1MB, we probe in 64K chunks, and as soon as we accumulate at
 * least mem_limit free space, we quit. Returns 0 on usuable ports.
 */
483
static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
L
Linus Torvalds 已提交
484
{
485 486 487 488
	struct resource_map *m, mm;
	static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
	unsigned long b, i, ok = 0;
	struct socket_data *s_data = s->resource_data;
L
Linus Torvalds 已提交
489

490 491 492 493
	/* We do up to four passes through the list */
	if (probe_mask & MEM_PROBE_HIGH) {
		if (inv_probe(s_data->mem_db.next, s) > 0)
			return 0;
494 495
		if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
			return 0;
496
		dev_notice(&s->dev,
497
			   "cs: warning: no high memory space available!\n");
498
		return -ENODEV;
L
Linus Torvalds 已提交
499
	}
500 501 502 503 504 505 506

	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
		mm = *m;
		/* Only probe < 1 MB */
		if (mm.base >= 0x100000)
			continue;
		if ((mm.base | mm.num) & 0xffff) {
507 508
			ok += do_mem_probe(s, mm.base, mm.num, readable,
					   checksum);
509 510 511 512 513 514 515 516 517
			continue;
		}
		/* Special probe for 64K-aligned block */
		for (i = 0; i < 4; i++) {
			b = order[i] << 12;
			if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) {
				if (ok >= mem_limit)
					sub_interval(&s_data->mem_db, b, 0x10000);
				else
518 519
					ok += do_mem_probe(s, b, 0x10000,
							   readable, checksum);
520 521
			}
		}
L
Linus Torvalds 已提交
522
	}
523 524 525 526 527

	if (ok > 0)
		return 0;

	return -ENODEV;
L
Linus Torvalds 已提交
528 529 530 531
}

#else /* CONFIG_PCMCIA_PROBE */

532 533 534 535 536 537 538
/**
 * validate_mem() - memory probe function
 * @s:		PCMCIA socket to validate
 * @probe_mask: ignored
 *
 * Returns 0 on usuable ports.
 */
A
Andrew Morton 已提交
539
static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
L
Linus Torvalds 已提交
540 541 542
{
	struct resource_map *m, mm;
	struct socket_data *s_data = s->resource_data;
A
Andrew Morton 已提交
543
	unsigned long ok = 0;
L
Linus Torvalds 已提交
544 545 546

	for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
		mm = *m;
547
		ok += do_mem_probe(s, mm.base, mm.num, readable, checksum);
L
Linus Torvalds 已提交
548
	}
A
Andrew Morton 已提交
549 550 551
	if (ok > 0)
		return 0;
	return -ENODEV;
L
Linus Torvalds 已提交
552 553 554 555 556
}

#endif /* CONFIG_PCMCIA_PROBE */


557 558 559 560 561 562 563
/**
 * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use
 * @s:		PCMCIA socket to validate
 *
 * This is tricky... when we set up CIS memory, we try to validate
 * the memory window space allocations.
 *
564
 * Locking note: Must be called with skt_mutex held!
L
Linus Torvalds 已提交
565
 */
566
static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s)
L
Linus Torvalds 已提交
567 568
{
	struct socket_data *s_data = s->resource_data;
569
	unsigned int probe_mask = MEM_PROBE_LOW;
570
	int ret;
571

572
	if (!probe_mem || !(s->state & SOCKET_PRESENT))
573
		return 0;
L
Linus Torvalds 已提交
574

575 576
	if (s->features & SS_CAP_PAGE_REGS)
		probe_mask = MEM_PROBE_HIGH;
L
Linus Torvalds 已提交
577

578
	ret = validate_mem(s, probe_mask);
L
Linus Torvalds 已提交
579

580 581
	if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
		return 0;
L
Linus Torvalds 已提交
582

583
	return ret;
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591
}

struct pcmcia_align_data {
	unsigned long	mask;
	unsigned long	offset;
	struct resource_map	*map;
};

D
Dominik Brodowski 已提交
592 593
static resource_size_t pcmcia_common_align(struct pcmcia_align_data *align_data,
					resource_size_t start)
L
Linus Torvalds 已提交
594
{
D
Dominik Brodowski 已提交
595
	resource_size_t ret;
L
Linus Torvalds 已提交
596 597 598
	/*
	 * Ensure that we have the correct start address
	 */
D
Dominik Brodowski 已提交
599 600 601 602
	ret = (start & ~align_data->mask) + align_data->offset;
	if (ret < start)
		ret += align_data->mask + 1;
	return ret;
L
Linus Torvalds 已提交
603 604
}

605
static resource_size_t
606 607
pcmcia_align(void *align_data, const struct resource *res,
	resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
608 609 610
{
	struct pcmcia_align_data *data = align_data;
	struct resource_map *m;
611
	resource_size_t start;
L
Linus Torvalds 已提交
612

D
Dominik Brodowski 已提交
613
	start = pcmcia_common_align(data, res->start);
L
Linus Torvalds 已提交
614 615

	for (m = data->map->next; m != data->map; m = m->next) {
D
Dominik Brodowski 已提交
616 617
		unsigned long map_start = m->base;
		unsigned long map_end = m->base + m->num - 1;
L
Linus Torvalds 已提交
618 619 620 621 622 623

		/*
		 * If the lower resources are not available, try aligning
		 * to this entry of the resource database to see if it'll
		 * fit here.
		 */
D
Dominik Brodowski 已提交
624 625
		if (start < map_start)
			start = pcmcia_common_align(data, map_start);
L
Linus Torvalds 已提交
626 627 628 629 630

		/*
		 * If we're above the area which was passed in, there's
		 * no point proceeding.
		 */
D
Dominik Brodowski 已提交
631
		if (start >= res->end)
L
Linus Torvalds 已提交
632 633
			break;

D
Dominik Brodowski 已提交
634
		if ((start + size - 1) <= map_end)
L
Linus Torvalds 已提交
635 636 637 638 639 640 641
			break;
	}

	/*
	 * If we failed to find something suitable, ensure we fail.
	 */
	if (m == data->map)
642 643 644
		start = res->end;

	return start;
L
Linus Torvalds 已提交
645 646 647 648 649 650
}

/*
 * Adjust an existing IO region allocation, but making sure that we don't
 * encroach outside the resources which the user supplied.
 */
651 652 653
static int __nonstatic_adjust_io_region(struct pcmcia_socket *s,
					unsigned long r_start,
					unsigned long r_end)
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665
{
	struct resource_map *m;
	struct socket_data *s_data = s->resource_data;
	int ret = -ENOMEM;

	for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) {
		unsigned long start = m->base;
		unsigned long end = m->base + m->num - 1;

		if (start > r_start || r_end > end)
			continue;

666
		ret = 0;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	}

	return ret;
}

/*======================================================================

    These find ranges of I/O ports or memory addresses that are not
    currently allocated by other devices.

    The 'align' field should reflect the number of bits of address
    that need to be preserved from the initial value of *base.  It
    should be a power of two, greater than or equal to 'num'.  A value
    of 0 means that all bits of *base are significant.  *base should
    also be strictly less than 'align'.

======================================================================*/

685 686 687
static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s,
						unsigned long base, int num,
						unsigned long align)
L
Linus Torvalds 已提交
688
{
689 690
	struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
						dev_name(&s->dev));
L
Linus Torvalds 已提交
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
	struct socket_data *s_data = s->resource_data;
	struct pcmcia_align_data data;
	unsigned long min = base;
	int ret;

	data.mask = align - 1;
	data.offset = base & data.mask;
	data.map = &s_data->io_db;

#ifdef CONFIG_PCI
	if (s->cb_dev) {
		ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
					     min, 0, pcmcia_align, &data);
	} else
#endif
		ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
					1, pcmcia_align, &data);

	if (ret != 0) {
		kfree(res);
		res = NULL;
	}
	return res;
}

716 717
static int nonstatic_find_io(struct pcmcia_socket *s, unsigned int attr,
			unsigned int *base, unsigned int num,
718
			unsigned int align, struct resource **parent)
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 746 747 748 749 750 751 752 753 754 755 756 757 758 759
{
	int i, ret = 0;

	/* Check for an already-allocated window that must conflict with
	 * what was asked for.  It is a hack because it does not catch all
	 * potential conflicts, just the most obvious ones.
	 */
	for (i = 0; i < MAX_IO_WIN; i++) {
		if (!s->io[i].res)
			continue;

		if (!*base)
			continue;

		if ((s->io[i].res->start & (align-1)) == *base)
			return -EBUSY;
	}

	for (i = 0; i < MAX_IO_WIN; i++) {
		struct resource *res = s->io[i].res;
		unsigned int try;

		if (res && (res->flags & IORESOURCE_BITS) !=
			(attr & IORESOURCE_BITS))
			continue;

		if (!res) {
			if (align == 0)
				align = 0x10000;

			res = s->io[i].res = __nonstatic_find_io_region(s,
								*base, num,
								align);
			if (!res)
				return -EINVAL;

			*base = res->start;
			s->io[i].res->flags =
				((res->flags & ~IORESOURCE_BITS) |
					(attr & IORESOURCE_BITS));
			s->io[i].InUse = num;
760
			*parent = res;
761 762 763 764 765 766 767 768 769 770
			return 0;
		}

		/* Try to extend top of window */
		try = res->end + 1;
		if ((*base == 0) || (*base == try)) {
			ret =  __nonstatic_adjust_io_region(s, res->start,
							res->end + num);
			if (!ret) {
				ret = adjust_resource(s->io[i].res, res->start,
771
						      resource_size(res) + num);
772 773 774 775
				if (ret)
					continue;
				*base = try;
				s->io[i].InUse += num;
776
				*parent = res;
777 778 779 780 781 782 783 784 785 786 787 788
				return 0;
			}
		}

		/* Try to extend bottom of window */
		try = res->start - num;
		if ((*base == 0) || (*base == try)) {
			ret =  __nonstatic_adjust_io_region(s,
							res->start - num,
							res->end);
			if (!ret) {
				ret = adjust_resource(s->io[i].res,
789 790
						      res->start - num,
						      resource_size(res) + num);
791 792 793 794
				if (ret)
					continue;
				*base = try;
				s->io[i].InUse += num;
795
				*parent = res;
796 797 798 799 800 801 802 803 804
				return 0;
			}
		}
	}

	return -EINVAL;
}


D
Dominik Brodowski 已提交
805
static struct resource *nonstatic_find_mem_region(u_long base, u_long num,
D
Dominik Brodowski 已提交
806
		u_long align, int low, struct pcmcia_socket *s)
L
Linus Torvalds 已提交
807
{
808 809
	struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM,
						dev_name(&s->dev));
L
Linus Torvalds 已提交
810 811 812
	struct socket_data *s_data = s->resource_data;
	struct pcmcia_align_data data;
	unsigned long min, max;
813
	int ret, i, j;
L
Linus Torvalds 已提交
814 815 816 817 818 819 820

	low = low || !(s->features & SS_CAP_PAGE_REGS);

	data.mask = align - 1;
	data.offset = base & data.mask;

	for (i = 0; i < 2; i++) {
821
		data.map = &s_data->mem_db_valid;
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829
		if (low) {
			max = 0x100000UL;
			min = base < max ? base : 0;
		} else {
			max = ~0UL;
			min = 0x100000UL + base;
		}

830
		for (j = 0; j < 2; j++) {
L
Linus Torvalds 已提交
831
#ifdef CONFIG_PCI
832 833 834 835 836
			if (s->cb_dev) {
				ret = pci_bus_alloc_resource(s->cb_dev->bus,
							res, num, 1, min, 0,
							pcmcia_align, &data);
			} else
L
Linus Torvalds 已提交
837
#endif
838 839 840 841 842 843 844 845 846
			{
				ret = allocate_resource(&iomem_resource,
							res, num, min, max, 1,
							pcmcia_align, &data);
			}
			if (ret == 0)
				break;
			data.map = &s_data->mem_db;
		}
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859
		if (ret == 0 || low)
			break;
		low = 1;
	}

	if (ret != 0) {
		kfree(res);
		res = NULL;
	}
	return res;
}


860
static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
L
Linus Torvalds 已提交
861 862
{
	struct socket_data *data = s->resource_data;
863 864
	unsigned long size = end - start + 1;
	int ret = 0;
L
Linus Torvalds 已提交
865

866
	if (end < start)
867
		return -EINVAL;
L
Linus Torvalds 已提交
868

869
	switch (action) {
L
Linus Torvalds 已提交
870
	case ADD_MANAGED_RESOURCE:
871
		ret = add_interval(&data->mem_db, start, size);
872 873
		if (!ret)
			do_mem_probe(s, start, size, NULL, NULL);
L
Linus Torvalds 已提交
874 875
		break;
	case REMOVE_MANAGED_RESOURCE:
876
		ret = sub_interval(&data->mem_db, start, size);
L
Linus Torvalds 已提交
877 878
		break;
	default:
879
		ret = -EINVAL;
L
Linus Torvalds 已提交
880 881 882 883 884 885
	}

	return ret;
}


886
static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
L
Linus Torvalds 已提交
887 888
{
	struct socket_data *data = s->resource_data;
889
	unsigned long size;
890
	int ret = 0;
L
Linus Torvalds 已提交
891

892 893 894 895 896 897 898
#if defined(CONFIG_X86)
	/* on x86, avoid anything < 0x100 for it is often used for
	 * legacy platform devices */
	if (start < 0x100)
		start = 0x100;
#endif

899 900
	size = end - start + 1;

901
	if (end < start)
902 903 904 905
		return -EINVAL;

	if (end > IO_SPACE_LIMIT)
		return -EINVAL;
L
Linus Torvalds 已提交
906

907
	switch (action) {
L
Linus Torvalds 已提交
908
	case ADD_MANAGED_RESOURCE:
909 910
		if (add_interval(&data->io_db, start, size) != 0) {
			ret = -EBUSY;
L
Linus Torvalds 已提交
911 912 913 914
			break;
		}
#ifdef CONFIG_PCMCIA_PROBE
		if (probe_io)
915
			do_io_probe(s, start, size);
L
Linus Torvalds 已提交
916 917 918
#endif
		break;
	case REMOVE_MANAGED_RESOURCE:
919
		sub_interval(&data->io_db, start, size);
L
Linus Torvalds 已提交
920 921
		break;
	default:
922
		ret = -EINVAL;
L
Linus Torvalds 已提交
923 924 925 926 927 928 929
		break;
	}

	return ret;
}


930 931 932 933 934 935 936 937 938
#ifdef CONFIG_PCI
static int nonstatic_autoadd_resources(struct pcmcia_socket *s)
{
	struct resource *res;
	int i, done = 0;

	if (!s->cb_dev || !s->cb_dev->bus)
		return -ENODEV;

B
Brian Gerst 已提交
939
#if defined(CONFIG_X86)
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	/* If this is the root bus, the risk of hitting some strange
	 * system devices is too high: If a driver isn't loaded, the
	 * resources are not claimed; even if a driver is loaded, it
	 * may not request all resources or even the wrong one. We
	 * can neither trust the rest of the kernel nor ACPI/PNP and
	 * CRS parsing to get it right. Therefore, use several
	 * safeguards:
	 *
	 * - Do not auto-add resources if the CardBus bridge is on
	 *   the PCI root bus
	 *
	 * - Avoid any I/O ports < 0x100.
	 *
	 * - On PCI-PCI bridges, only use resources which are set up
	 *   exclusively for the secondary PCI bus: the risk of hitting
	 *   system devices is quite low, as they usually aren't
	 *   connected to the secondary PCI bus.
957 958 959 960
	 */
	if (s->cb_dev->bus->number == 0)
		return -EINVAL;

961 962 963
	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
		res = s->cb_dev->bus->resource[i];
#else
964
	pci_bus_for_each_resource(s->cb_dev->bus, res, i) {
965
#endif
966 967 968 969
		if (!res)
			continue;

		if (res->flags & IORESOURCE_IO) {
970 971 972
			/* safeguard against the root resource, where the
			 * risk of hitting any other device would be too
			 * high */
973 974
			if (res == &ioport_resource)
				continue;
975

976 977 978
			dev_info(&s->cb_dev->dev,
				 "pcmcia: parent PCI bridge window: %pR\n",
				 res);
979 980 981 982 983 984
			if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end))
				done |= IORESOURCE_IO;

		}

		if (res->flags & IORESOURCE_MEM) {
985 986 987
			/* safeguard against the root resource, where the
			 * risk of hitting any other device would be too
			 * high */
988 989
			if (res == &iomem_resource)
				continue;
990

991 992 993
			dev_info(&s->cb_dev->dev,
				 "pcmcia: parent PCI bridge window: %pR\n",
				 res);
994 995 996 997 998 999 1000
			if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end))
				done |= IORESOURCE_MEM;
		}
	}

	/* if we got at least one of IO, and one of MEM, we can be glad and
	 * activate the PCMCIA subsystem */
1001
	if (done == (IORESOURCE_MEM | IORESOURCE_IO))
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		s->resource_setup_done = 1;

	return 0;
}

#else

static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s)
{
	return -ENODEV;
}

#endif


L
Linus Torvalds 已提交
1017 1018 1019 1020
static int nonstatic_init(struct pcmcia_socket *s)
{
	struct socket_data *data;

1021
	data = kzalloc(sizeof(struct socket_data), GFP_KERNEL);
L
Linus Torvalds 已提交
1022 1023 1024 1025
	if (!data)
		return -ENOMEM;

	data->mem_db.next = &data->mem_db;
1026
	data->mem_db_valid.next = &data->mem_db_valid;
L
Linus Torvalds 已提交
1027 1028 1029 1030
	data->io_db.next = &data->io_db;

	s->resource_data = (void *) data;

1031 1032
	nonstatic_autoadd_resources(s);

L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040
	return 0;
}

static void nonstatic_release_resource_db(struct pcmcia_socket *s)
{
	struct socket_data *data = s->resource_data;
	struct resource_map *p, *q;

1041 1042 1043 1044
	for (p = data->mem_db_valid.next; p != &data->mem_db_valid; p = q) {
		q = p->next;
		kfree(p);
	}
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	for (p = data->mem_db.next; p != &data->mem_db; p = q) {
		q = p->next;
		kfree(p);
	}
	for (p = data->io_db.next; p != &data->io_db; p = q) {
		q = p->next;
		kfree(p);
	}
}


struct pccard_resource_ops pccard_nonstatic_ops = {
	.validate_mem = pcmcia_nonstatic_validate_mem,
1058
	.find_io = nonstatic_find_io,
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067
	.find_mem = nonstatic_find_mem_region,
	.init = nonstatic_init,
	.exit = nonstatic_release_resource_db,
};
EXPORT_SYMBOL(pccard_nonstatic_ops);


/* sysfs interface to the resource database */

1068 1069
static ssize_t show_io_db(struct device *dev,
			  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1070
{
1071
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1072 1073 1074 1075
	struct socket_data *data;
	struct resource_map *p;
	ssize_t ret = 0;

1076
	mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081
	data = s->resource_data;

	for (p = data->io_db.next; p != &data->io_db; p = p->next) {
		if (ret > (PAGE_SIZE - 10))
			continue;
D
Dominik Brodowski 已提交
1082 1083 1084 1085
		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
				"0x%08lx - 0x%08lx\n",
				((unsigned long) p->base),
				((unsigned long) p->base + p->num - 1));
L
Linus Torvalds 已提交
1086 1087
	}

1088
	mutex_unlock(&s->ops_mutex);
D
Dominik Brodowski 已提交
1089
	return ret;
L
Linus Torvalds 已提交
1090 1091
}

1092 1093 1094
static ssize_t store_io_db(struct device *dev,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
1095
{
1096
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1097
	unsigned long start_addr, end_addr;
1098
	unsigned int add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1099 1100
	ssize_t ret = 0;

D
Dominik Brodowski 已提交
1101
	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
L
Linus Torvalds 已提交
1102
	if (ret != 2) {
D
Dominik Brodowski 已提交
1103
		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
1104
		add = REMOVE_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1105
		if (ret != 2) {
D
Dominik Brodowski 已提交
1106 1107
			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
				&end_addr);
1108
			add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1109 1110 1111 1112
			if (ret != 2)
				return -EINVAL;
		}
	}
1113
	if (end_addr < start_addr)
L
Linus Torvalds 已提交
1114 1115
		return -EINVAL;

1116
	mutex_lock(&s->ops_mutex);
1117
	ret = adjust_io(s, add, start_addr, end_addr);
1118
	mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
1119 1120 1121

	return ret ? ret : count;
}
1122
static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db);
L
Linus Torvalds 已提交
1123

1124 1125
static ssize_t show_mem_db(struct device *dev,
			   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1126
{
1127
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1128 1129 1130 1131
	struct socket_data *data;
	struct resource_map *p;
	ssize_t ret = 0;

1132
	mutex_lock(&s->ops_mutex);
L
Linus Torvalds 已提交
1133 1134
	data = s->resource_data;

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	for (p = data->mem_db_valid.next; p != &data->mem_db_valid;
	     p = p->next) {
		if (ret > (PAGE_SIZE - 10))
			continue;
		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
				"0x%08lx - 0x%08lx\n",
				((unsigned long) p->base),
				((unsigned long) p->base + p->num - 1));
	}

L
Linus Torvalds 已提交
1145 1146 1147
	for (p = data->mem_db.next; p != &data->mem_db; p = p->next) {
		if (ret > (PAGE_SIZE - 10))
			continue;
D
Dominik Brodowski 已提交
1148 1149 1150 1151
		ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
				"0x%08lx - 0x%08lx\n",
				((unsigned long) p->base),
				((unsigned long) p->base + p->num - 1));
L
Linus Torvalds 已提交
1152 1153
	}

1154
	mutex_unlock(&s->ops_mutex);
D
Dominik Brodowski 已提交
1155
	return ret;
L
Linus Torvalds 已提交
1156 1157
}

1158 1159 1160
static ssize_t store_mem_db(struct device *dev,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
1161
{
1162
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1163
	unsigned long start_addr, end_addr;
1164
	unsigned int add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1165 1166
	ssize_t ret = 0;

D
Dominik Brodowski 已提交
1167
	ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
L
Linus Torvalds 已提交
1168
	if (ret != 2) {
D
Dominik Brodowski 已提交
1169
		ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
1170
		add = REMOVE_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1171
		if (ret != 2) {
D
Dominik Brodowski 已提交
1172 1173
			ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
				&end_addr);
1174
			add = ADD_MANAGED_RESOURCE;
L
Linus Torvalds 已提交
1175 1176 1177 1178
			if (ret != 2)
				return -EINVAL;
		}
	}
1179
	if (end_addr < start_addr)
L
Linus Torvalds 已提交
1180 1181
		return -EINVAL;

1182
	mutex_lock(&s->ops_mutex);
1183
	ret = adjust_memory(s, add, start_addr, end_addr);
1184
	mutex_unlock(&s->ops_mutex);
L
Linus Torvalds 已提交
1185 1186 1187

	return ret ? ret : count;
}
1188
static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db);
L
Linus Torvalds 已提交
1189

1190 1191 1192
static struct attribute *pccard_rsrc_attributes[] = {
	&dev_attr_available_resources_io.attr,
	&dev_attr_available_resources_mem.attr,
L
Linus Torvalds 已提交
1193 1194 1195
	NULL,
};

1196 1197 1198 1199
static const struct attribute_group rsrc_attributes = {
	.attrs = pccard_rsrc_attributes,
};

B
Bill Pemberton 已提交
1200
static int pccard_sysfs_add_rsrc(struct device *dev,
1201
					   struct class_interface *class_intf)
L
Linus Torvalds 已提交
1202
{
1203
	struct pcmcia_socket *s = dev_get_drvdata(dev);
1204

L
Linus Torvalds 已提交
1205 1206
	if (s->resource_ops != &pccard_nonstatic_ops)
		return 0;
1207
	return sysfs_create_group(&dev->kobj, &rsrc_attributes);
L
Linus Torvalds 已提交
1208 1209
}

B
Bill Pemberton 已提交
1210
static void pccard_sysfs_remove_rsrc(struct device *dev,
1211
					       struct class_interface *class_intf)
L
Linus Torvalds 已提交
1212
{
1213
	struct pcmcia_socket *s = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1214 1215 1216

	if (s->resource_ops != &pccard_nonstatic_ops)
		return;
1217
	sysfs_remove_group(&dev->kobj, &rsrc_attributes);
L
Linus Torvalds 已提交
1218 1219
}

1220
static struct class_interface pccard_rsrc_interface __refdata = {
L
Linus Torvalds 已提交
1221
	.class = &pcmcia_socket_class,
1222
	.add_dev = &pccard_sysfs_add_rsrc,
B
Bill Pemberton 已提交
1223
	.remove_dev = &pccard_sysfs_remove_rsrc,
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
};

static int __init nonstatic_sysfs_init(void)
{
	return class_interface_register(&pccard_rsrc_interface);
}

static void __exit nonstatic_sysfs_exit(void)
{
	class_interface_unregister(&pccard_rsrc_interface);
}

module_init(nonstatic_sysfs_init);
module_exit(nonstatic_sysfs_exit);