1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets 4 * 5 * The initial developer of the original code is David A. Hinds 6 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds 7 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 8 * 9 * (C) 1999 David A. Hinds 10 */ 11 12 #include <linux/module.h> 13 #include <linux/moduleparam.h> 14 #include <linux/init.h> 15 #include <linux/interrupt.h> 16 #include <linux/kernel.h> 17 #include <linux/errno.h> 18 #include <linux/types.h> 19 #include <linux/slab.h> 20 #include <linux/ioport.h> 21 #include <linux/timer.h> 22 #include <linux/pci.h> 23 #include <linux/device.h> 24 #include <linux/io.h> 25 26 #include <asm/irq.h> 27 28 #include <pcmcia/ss.h> 29 #include <pcmcia/cistpl.h> 30 #include "cs_internal.h" 31 32 /* moved to rsrc_mgr.c 33 MODULE_AUTHOR("David A. Hinds, Dominik Brodowski"); 34 MODULE_LICENSE("GPL"); 35 */ 36 37 /* Parameters that can be set with 'insmod' */ 38 39 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444) 40 41 INT_MODULE_PARM(probe_mem, 1); /* memory probe? */ 42 #ifdef CONFIG_PCMCIA_PROBE 43 INT_MODULE_PARM(probe_io, 1); /* IO port probe? */ 44 INT_MODULE_PARM(mem_limit, 0x10000); 45 #endif 46 47 /* for io_db and mem_db */ 48 struct resource_map { 49 u_long base, num; 50 struct resource_map *next; 51 }; 52 53 struct socket_data { 54 struct resource_map mem_db; 55 struct resource_map mem_db_valid; 56 struct resource_map io_db; 57 }; 58 59 #define MEM_PROBE_LOW (1 << 0) 60 #define MEM_PROBE_HIGH (1 << 1) 61 62 /* Action field */ 63 #define REMOVE_MANAGED_RESOURCE 1 64 #define ADD_MANAGED_RESOURCE 2 65 66 /*====================================================================== 67 68 Linux resource management extensions 69 70 ======================================================================*/ 71 72 static struct resource * 73 claim_region(struct pcmcia_socket *s, resource_size_t base, 74 resource_size_t size, int type, char *name) 75 { 76 struct resource *res, *parent; 77 78 parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource; 79 res = pcmcia_make_resource(base, size, type | IORESOURCE_BUSY, name); 80 81 if (res) { 82 #ifdef CONFIG_PCI 83 if (s && s->cb_dev) 84 parent = pci_find_parent_resource(s->cb_dev, res); 85 #endif 86 if (!parent || request_resource(parent, res)) { 87 kfree(res); 88 res = NULL; 89 } 90 } 91 return res; 92 } 93 94 static void free_region(struct resource *res) 95 { 96 if (res) { 97 release_resource(res); 98 kfree(res); 99 } 100 } 101 102 /*====================================================================== 103 104 These manage the internal databases of available resources. 105 106 ======================================================================*/ 107 108 static int add_interval(struct resource_map *map, u_long base, u_long num) 109 { 110 struct resource_map *p, *q; 111 112 for (p = map; ; p = p->next) { 113 if ((p != map) && (p->base+p->num >= base)) { 114 p->num = max(num + base - p->base, p->num); 115 return 0; 116 } 117 if ((p->next == map) || (p->next->base > base+num-1)) 118 break; 119 } 120 q = kmalloc(sizeof(struct resource_map), GFP_KERNEL); 121 if (!q) { 122 printk(KERN_WARNING "out of memory to update resources\n"); 123 return -ENOMEM; 124 } 125 q->base = base; q->num = num; 126 q->next = p->next; p->next = q; 127 return 0; 128 } 129 130 /*====================================================================*/ 131 132 static int sub_interval(struct resource_map *map, u_long base, u_long num) 133 { 134 struct resource_map *p, *q; 135 136 for (p = map; ; p = q) { 137 q = p->next; 138 if (q == map) 139 break; 140 if ((q->base+q->num > base) && (base+num > q->base)) { 141 if (q->base >= base) { 142 if (q->base+q->num <= base+num) { 143 /* Delete whole block */ 144 p->next = q->next; 145 kfree(q); 146 /* don't advance the pointer yet */ 147 q = p; 148 } else { 149 /* Cut off bit from the front */ 150 q->num = q->base + q->num - base - num; 151 q->base = base + num; 152 } 153 } else if (q->base+q->num <= base+num) { 154 /* Cut off bit from the end */ 155 q->num = base - q->base; 156 } else { 157 /* Split the block into two pieces */ 158 p = kmalloc(sizeof(struct resource_map), 159 GFP_KERNEL); 160 if (!p) { 161 printk(KERN_WARNING "out of memory to update resources\n"); 162 return -ENOMEM; 163 } 164 p->base = base+num; 165 p->num = q->base+q->num - p->base; 166 q->num = base - q->base; 167 p->next = q->next ; q->next = p; 168 } 169 } 170 } 171 return 0; 172 } 173 174 /*====================================================================== 175 176 These routines examine a region of IO or memory addresses to 177 determine what ranges might be genuinely available. 178 179 ======================================================================*/ 180 181 #ifdef CONFIG_PCMCIA_PROBE 182 static void do_io_probe(struct pcmcia_socket *s, unsigned int base, 183 unsigned int num) 184 { 185 struct resource *res; 186 struct socket_data *s_data = s->resource_data; 187 unsigned int i, j, bad; 188 int any; 189 u_char *b, hole, most; 190 191 dev_info(&s->dev, "cs: IO port probe %#x-%#x:", base, base+num-1); 192 193 /* First, what does a floating port look like? */ 194 b = kzalloc(256, GFP_KERNEL); 195 if (!b) { 196 pr_cont("\n"); 197 dev_err(&s->dev, "do_io_probe: unable to kmalloc 256 bytes\n"); 198 return; 199 } 200 for (i = base, most = 0; i < base+num; i += 8) { 201 res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); 202 if (!res) 203 continue; 204 hole = inb(i); 205 for (j = 1; j < 8; j++) 206 if (inb(i+j) != hole) 207 break; 208 free_region(res); 209 if ((j == 8) && (++b[hole] > b[most])) 210 most = hole; 211 if (b[most] == 127) 212 break; 213 } 214 kfree(b); 215 216 bad = any = 0; 217 for (i = base; i < base+num; i += 8) { 218 res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); 219 if (!res) { 220 if (!any) 221 pr_cont(" excluding"); 222 if (!bad) 223 bad = any = i; 224 continue; 225 } 226 for (j = 0; j < 8; j++) 227 if (inb(i+j) != most) 228 break; 229 free_region(res); 230 if (j < 8) { 231 if (!any) 232 pr_cont(" excluding"); 233 if (!bad) 234 bad = any = i; 235 } else { 236 if (bad) { 237 sub_interval(&s_data->io_db, bad, i-bad); 238 pr_cont(" %#x-%#x", bad, i-1); 239 bad = 0; 240 } 241 } 242 } 243 if (bad) { 244 if ((num > 16) && (bad == base) && (i == base+num)) { 245 sub_interval(&s_data->io_db, bad, i-bad); 246 pr_cont(" nothing: probe failed.\n"); 247 return; 248 } else { 249 sub_interval(&s_data->io_db, bad, i-bad); 250 pr_cont(" %#x-%#x", bad, i-1); 251 } 252 } 253 254 pr_cont("%s\n", !any ? " clean" : ""); 255 } 256 #endif 257 258 /*======================================================================*/ 259 260 /* 261 * readable() - iomem validation function for cards with a valid CIS 262 */ 263 static int readable(struct pcmcia_socket *s, struct resource *res, 264 unsigned int *count) 265 { 266 int ret = -EINVAL; 267 268 if (s->fake_cis) { 269 dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n"); 270 return 0; 271 } 272 273 s->cis_mem.res = res; 274 s->cis_virt = ioremap(res->start, s->map_size); 275 if (s->cis_virt) { 276 mutex_unlock(&s->ops_mutex); 277 /* as we're only called from pcmcia.c, we're safe */ 278 if (s->callback->validate) 279 ret = s->callback->validate(s, count); 280 /* invalidate mapping */ 281 mutex_lock(&s->ops_mutex); 282 iounmap(s->cis_virt); 283 s->cis_virt = NULL; 284 } 285 s->cis_mem.res = NULL; 286 if ((ret) || (*count == 0)) 287 return -EINVAL; 288 return 0; 289 } 290 291 /* 292 * checksum() - iomem validation function for simple memory cards 293 */ 294 static int checksum(struct pcmcia_socket *s, struct resource *res, 295 unsigned int *value) 296 { 297 pccard_mem_map map; 298 int i, a = 0, b = -1, d; 299 void __iomem *virt; 300 301 virt = ioremap(res->start, s->map_size); 302 if (virt) { 303 map.map = 0; 304 map.flags = MAP_ACTIVE; 305 map.speed = 0; 306 map.res = res; 307 map.card_start = 0; 308 s->ops->set_mem_map(s, &map); 309 310 /* Don't bother checking every word... */ 311 for (i = 0; i < s->map_size; i += 44) { 312 d = readl(virt+i); 313 a += d; 314 b &= d; 315 } 316 317 map.flags = 0; 318 s->ops->set_mem_map(s, &map); 319 320 iounmap(virt); 321 } 322 323 if (b == -1) 324 return -EINVAL; 325 326 *value = a; 327 328 return 0; 329 } 330 331 /** 332 * do_validate_mem() - low level validate a memory region for PCMCIA use 333 * @s: PCMCIA socket to validate 334 * @base: start address of resource to check 335 * @size: size of resource to check 336 * @validate: validation function to use 337 * 338 * do_validate_mem() splits up the memory region which is to be checked 339 * into two parts. Both are passed to the @validate() function. If 340 * @validate() returns non-zero, or the value parameter to @validate() 341 * is zero, or the value parameter is different between both calls, 342 * the check fails, and -EINVAL is returned. Else, 0 is returned. 343 */ 344 static int do_validate_mem(struct pcmcia_socket *s, 345 unsigned long base, unsigned long size, 346 int (*validate)(struct pcmcia_socket *s, 347 struct resource *res, 348 unsigned int *value)) 349 { 350 struct socket_data *s_data = s->resource_data; 351 struct resource *res1, *res2; 352 unsigned int info1 = 1, info2 = 1; 353 int ret = -EINVAL; 354 355 res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe"); 356 res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM, 357 "PCMCIA memprobe"); 358 359 if (res1 && res2) { 360 ret = 0; 361 if (validate) { 362 ret = validate(s, res1, &info1); 363 ret += validate(s, res2, &info2); 364 } 365 } 366 367 dev_dbg(&s->dev, "cs: memory probe 0x%06lx-0x%06lx: %pr %pr %u %u %u", 368 base, base+size-1, res1, res2, ret, info1, info2); 369 370 free_region(res2); 371 free_region(res1); 372 373 if ((ret) || (info1 != info2) || (info1 == 0)) 374 return -EINVAL; 375 376 if (validate && !s->fake_cis) { 377 /* move it to the validated data set */ 378 ret = add_interval(&s_data->mem_db_valid, base, size); 379 if (ret) 380 return ret; 381 sub_interval(&s_data->mem_db, base, size); 382 } 383 384 return 0; 385 } 386 387 388 /** 389 * do_mem_probe() - validate a memory region for PCMCIA use 390 * @s: PCMCIA socket to validate 391 * @base: start address of resource to check 392 * @num: size of resource to check 393 * @validate: validation function to use 394 * @fallback: validation function to use if validate fails 395 * 396 * do_mem_probe() checks a memory region for use by the PCMCIA subsystem. 397 * To do so, the area is split up into sensible parts, and then passed 398 * into the @validate() function. Only if @validate() and @fallback() fail, 399 * the area is marked as unavailable for use by the PCMCIA subsystem. The 400 * function returns the size of the usable memory area. 401 */ 402 static int do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num, 403 int (*validate)(struct pcmcia_socket *s, 404 struct resource *res, 405 unsigned int *value), 406 int (*fallback)(struct pcmcia_socket *s, 407 struct resource *res, 408 unsigned int *value)) 409 { 410 struct socket_data *s_data = s->resource_data; 411 u_long i, j, bad, fail, step; 412 413 dev_info(&s->dev, "cs: memory probe 0x%06lx-0x%06lx:", 414 base, base+num-1); 415 bad = fail = 0; 416 step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff); 417 /* don't allow too large steps */ 418 if (step > 0x800000) 419 step = 0x800000; 420 /* cis_readable wants to map 2x map_size */ 421 if (step < 2 * s->map_size) 422 step = 2 * s->map_size; 423 for (i = j = base; i < base+num; i = j + step) { 424 if (!fail) { 425 for (j = i; j < base+num; j += step) { 426 if (!do_validate_mem(s, j, step, validate)) 427 break; 428 } 429 fail = ((i == base) && (j == base+num)); 430 } 431 if ((fail) && (fallback)) { 432 for (j = i; j < base+num; j += step) 433 if (!do_validate_mem(s, j, step, fallback)) 434 break; 435 } 436 if (i != j) { 437 if (!bad) 438 pr_cont(" excluding"); 439 pr_cont(" %#05lx-%#05lx", i, j-1); 440 sub_interval(&s_data->mem_db, i, j-i); 441 bad += j-i; 442 } 443 } 444 pr_cont("%s\n", !bad ? " clean" : ""); 445 return num - bad; 446 } 447 448 449 #ifdef CONFIG_PCMCIA_PROBE 450 451 /** 452 * inv_probe() - top-to-bottom search for one usuable high memory area 453 * @s: PCMCIA socket to validate 454 * @m: resource_map to check 455 */ 456 static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s) 457 { 458 struct socket_data *s_data = s->resource_data; 459 u_long ok; 460 if (m == &s_data->mem_db) 461 return 0; 462 ok = inv_probe(m->next, s); 463 if (ok) { 464 if (m->base >= 0x100000) 465 sub_interval(&s_data->mem_db, m->base, m->num); 466 return ok; 467 } 468 if (m->base < 0x100000) 469 return 0; 470 return do_mem_probe(s, m->base, m->num, readable, checksum); 471 } 472 473 /** 474 * validate_mem() - memory probe function 475 * @s: PCMCIA socket to validate 476 * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH 477 * 478 * The memory probe. If the memory list includes a 64K-aligned block 479 * below 1MB, we probe in 64K chunks, and as soon as we accumulate at 480 * least mem_limit free space, we quit. Returns 0 on usuable ports. 481 */ 482 static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) 483 { 484 struct resource_map *m, mm; 485 static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 }; 486 unsigned long b, i, ok = 0; 487 struct socket_data *s_data = s->resource_data; 488 489 /* We do up to four passes through the list */ 490 if (probe_mask & MEM_PROBE_HIGH) { 491 if (inv_probe(s_data->mem_db.next, s) > 0) 492 return 0; 493 if (s_data->mem_db_valid.next != &s_data->mem_db_valid) 494 return 0; 495 dev_notice(&s->dev, 496 "cs: warning: no high memory space available!\n"); 497 return -ENODEV; 498 } 499 500 for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { 501 mm = *m; 502 /* Only probe < 1 MB */ 503 if (mm.base >= 0x100000) 504 continue; 505 if ((mm.base | mm.num) & 0xffff) { 506 ok += do_mem_probe(s, mm.base, mm.num, readable, 507 checksum); 508 continue; 509 } 510 /* Special probe for 64K-aligned block */ 511 for (i = 0; i < 4; i++) { 512 b = order[i] << 12; 513 if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) { 514 if (ok >= mem_limit) 515 sub_interval(&s_data->mem_db, b, 0x10000); 516 else 517 ok += do_mem_probe(s, b, 0x10000, 518 readable, checksum); 519 } 520 } 521 } 522 523 if (ok > 0) 524 return 0; 525 526 return -ENODEV; 527 } 528 529 #else /* CONFIG_PCMCIA_PROBE */ 530 531 /** 532 * validate_mem() - memory probe function 533 * @s: PCMCIA socket to validate 534 * @probe_mask: ignored 535 * 536 * Returns 0 on usuable ports. 537 */ 538 static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask) 539 { 540 struct resource_map *m, mm; 541 struct socket_data *s_data = s->resource_data; 542 unsigned long ok = 0; 543 544 for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) { 545 mm = *m; 546 ok += do_mem_probe(s, mm.base, mm.num, readable, checksum); 547 } 548 if (ok > 0) 549 return 0; 550 return -ENODEV; 551 } 552 553 #endif /* CONFIG_PCMCIA_PROBE */ 554 555 556 /** 557 * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use 558 * @s: PCMCIA socket to validate 559 * 560 * This is tricky... when we set up CIS memory, we try to validate 561 * the memory window space allocations. 562 * 563 * Locking note: Must be called with skt_mutex held! 564 */ 565 static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s) 566 { 567 struct socket_data *s_data = s->resource_data; 568 unsigned int probe_mask = MEM_PROBE_LOW; 569 int ret; 570 571 if (!probe_mem || !(s->state & SOCKET_PRESENT)) 572 return 0; 573 574 if (s->features & SS_CAP_PAGE_REGS) 575 probe_mask = MEM_PROBE_HIGH; 576 577 ret = validate_mem(s, probe_mask); 578 579 if (s_data->mem_db_valid.next != &s_data->mem_db_valid) 580 return 0; 581 582 return ret; 583 } 584 585 struct pcmcia_align_data { 586 unsigned long mask; 587 unsigned long offset; 588 struct resource_map *map; 589 }; 590 591 static resource_size_t pcmcia_common_align(struct pcmcia_align_data *align_data, 592 resource_size_t start) 593 { 594 resource_size_t ret; 595 /* 596 * Ensure that we have the correct start address 597 */ 598 ret = (start & ~align_data->mask) + align_data->offset; 599 if (ret < start) 600 ret += align_data->mask + 1; 601 return ret; 602 } 603 604 static resource_size_t 605 pcmcia_align(void *align_data, const struct resource *res, 606 resource_size_t size, resource_size_t align) 607 { 608 struct pcmcia_align_data *data = align_data; 609 struct resource_map *m; 610 resource_size_t start; 611 612 start = pcmcia_common_align(data, res->start); 613 614 for (m = data->map->next; m != data->map; m = m->next) { 615 unsigned long map_start = m->base; 616 unsigned long map_end = m->base + m->num - 1; 617 618 /* 619 * If the lower resources are not available, try aligning 620 * to this entry of the resource database to see if it'll 621 * fit here. 622 */ 623 if (start < map_start) 624 start = pcmcia_common_align(data, map_start); 625 626 /* 627 * If we're above the area which was passed in, there's 628 * no point proceeding. 629 */ 630 if (start >= res->end) 631 break; 632 633 if ((start + size - 1) <= map_end) 634 break; 635 } 636 637 /* 638 * If we failed to find something suitable, ensure we fail. 639 */ 640 if (m == data->map) 641 start = res->end; 642 643 return start; 644 } 645 646 /* 647 * Adjust an existing IO region allocation, but making sure that we don't 648 * encroach outside the resources which the user supplied. 649 */ 650 static int __nonstatic_adjust_io_region(struct pcmcia_socket *s, 651 unsigned long r_start, 652 unsigned long r_end) 653 { 654 struct resource_map *m; 655 struct socket_data *s_data = s->resource_data; 656 int ret = -ENOMEM; 657 658 for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) { 659 unsigned long start = m->base; 660 unsigned long end = m->base + m->num - 1; 661 662 if (start > r_start || r_end > end) 663 continue; 664 665 ret = 0; 666 } 667 668 return ret; 669 } 670 671 /*====================================================================== 672 673 These find ranges of I/O ports or memory addresses that are not 674 currently allocated by other devices. 675 676 The 'align' field should reflect the number of bits of address 677 that need to be preserved from the initial value of *base. It 678 should be a power of two, greater than or equal to 'num'. A value 679 of 0 means that all bits of *base are significant. *base should 680 also be strictly less than 'align'. 681 682 ======================================================================*/ 683 684 static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s, 685 unsigned long base, int num, 686 unsigned long align) 687 { 688 struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO, 689 dev_name(&s->dev)); 690 struct socket_data *s_data = s->resource_data; 691 struct pcmcia_align_data data; 692 unsigned long min = base; 693 int ret; 694 695 if (!res) 696 return NULL; 697 698 data.mask = align - 1; 699 data.offset = base & data.mask; 700 data.map = &s_data->io_db; 701 702 #ifdef CONFIG_PCI 703 if (s->cb_dev) { 704 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, 705 min, 0, pcmcia_align, &data); 706 } else 707 #endif 708 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, 709 1, pcmcia_align, &data); 710 711 if (ret != 0) { 712 kfree(res); 713 res = NULL; 714 } 715 return res; 716 } 717 718 static int nonstatic_find_io(struct pcmcia_socket *s, unsigned int attr, 719 unsigned int *base, unsigned int num, 720 unsigned int align, struct resource **parent) 721 { 722 int i, ret = 0; 723 724 /* Check for an already-allocated window that must conflict with 725 * what was asked for. It is a hack because it does not catch all 726 * potential conflicts, just the most obvious ones. 727 */ 728 for (i = 0; i < MAX_IO_WIN; i++) { 729 if (!s->io[i].res) 730 continue; 731 732 if (!*base) 733 continue; 734 735 if ((s->io[i].res->start & (align-1)) == *base) 736 return -EBUSY; 737 } 738 739 for (i = 0; i < MAX_IO_WIN; i++) { 740 struct resource *res = s->io[i].res; 741 unsigned int try; 742 743 if (res && (res->flags & IORESOURCE_BITS) != 744 (attr & IORESOURCE_BITS)) 745 continue; 746 747 if (!res) { 748 if (align == 0) 749 align = 0x10000; 750 751 res = s->io[i].res = __nonstatic_find_io_region(s, 752 *base, num, 753 align); 754 if (!res) 755 return -EINVAL; 756 757 *base = res->start; 758 s->io[i].res->flags = 759 ((res->flags & ~IORESOURCE_BITS) | 760 (attr & IORESOURCE_BITS)); 761 s->io[i].InUse = num; 762 *parent = res; 763 return 0; 764 } 765 766 /* Try to extend top of window */ 767 try = res->end + 1; 768 if ((*base == 0) || (*base == try)) { 769 ret = __nonstatic_adjust_io_region(s, res->start, 770 res->end + num); 771 if (!ret) { 772 ret = adjust_resource(s->io[i].res, res->start, 773 resource_size(res) + num); 774 if (ret) 775 continue; 776 *base = try; 777 s->io[i].InUse += num; 778 *parent = res; 779 return 0; 780 } 781 } 782 783 /* Try to extend bottom of window */ 784 try = res->start - num; 785 if ((*base == 0) || (*base == try)) { 786 ret = __nonstatic_adjust_io_region(s, 787 res->start - num, 788 res->end); 789 if (!ret) { 790 ret = adjust_resource(s->io[i].res, 791 res->start - num, 792 resource_size(res) + num); 793 if (ret) 794 continue; 795 *base = try; 796 s->io[i].InUse += num; 797 *parent = res; 798 return 0; 799 } 800 } 801 } 802 803 return -EINVAL; 804 } 805 806 807 static struct resource *nonstatic_find_mem_region(u_long base, u_long num, 808 u_long align, int low, struct pcmcia_socket *s) 809 { 810 struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM, 811 dev_name(&s->dev)); 812 struct socket_data *s_data = s->resource_data; 813 struct pcmcia_align_data data; 814 unsigned long min, max; 815 int ret, i, j; 816 817 if (!res) 818 return NULL; 819 820 low = low || !(s->features & SS_CAP_PAGE_REGS); 821 822 data.mask = align - 1; 823 data.offset = base & data.mask; 824 825 for (i = 0; i < 2; i++) { 826 data.map = &s_data->mem_db_valid; 827 if (low) { 828 max = 0x100000UL; 829 min = base < max ? base : 0; 830 } else { 831 max = ~0UL; 832 min = 0x100000UL + base; 833 } 834 835 for (j = 0; j < 2; j++) { 836 #ifdef CONFIG_PCI 837 if (s->cb_dev) { 838 ret = pci_bus_alloc_resource(s->cb_dev->bus, 839 res, num, 1, min, 0, 840 pcmcia_align, &data); 841 } else 842 #endif 843 { 844 ret = allocate_resource(&iomem_resource, 845 res, num, min, max, 1, 846 pcmcia_align, &data); 847 } 848 if (ret == 0) 849 break; 850 data.map = &s_data->mem_db; 851 } 852 if (ret == 0 || low) 853 break; 854 low = 1; 855 } 856 857 if (ret != 0) { 858 kfree(res); 859 res = NULL; 860 } 861 return res; 862 } 863 864 865 static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end) 866 { 867 struct socket_data *data = s->resource_data; 868 unsigned long size = end - start + 1; 869 int ret = 0; 870 871 if (end < start) 872 return -EINVAL; 873 874 switch (action) { 875 case ADD_MANAGED_RESOURCE: 876 ret = add_interval(&data->mem_db, start, size); 877 if (!ret) 878 do_mem_probe(s, start, size, NULL, NULL); 879 break; 880 case REMOVE_MANAGED_RESOURCE: 881 ret = sub_interval(&data->mem_db, start, size); 882 break; 883 default: 884 ret = -EINVAL; 885 } 886 887 return ret; 888 } 889 890 891 static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end) 892 { 893 struct socket_data *data = s->resource_data; 894 unsigned long size; 895 int ret = 0; 896 897 #if defined(CONFIG_X86) 898 /* on x86, avoid anything < 0x100 for it is often used for 899 * legacy platform devices */ 900 if (start < 0x100) 901 start = 0x100; 902 #endif 903 904 size = end - start + 1; 905 906 if (end < start) 907 return -EINVAL; 908 909 if (end > IO_SPACE_LIMIT) 910 return -EINVAL; 911 912 switch (action) { 913 case ADD_MANAGED_RESOURCE: 914 if (add_interval(&data->io_db, start, size) != 0) { 915 ret = -EBUSY; 916 break; 917 } 918 #ifdef CONFIG_PCMCIA_PROBE 919 if (probe_io) 920 do_io_probe(s, start, size); 921 #endif 922 break; 923 case REMOVE_MANAGED_RESOURCE: 924 sub_interval(&data->io_db, start, size); 925 break; 926 default: 927 ret = -EINVAL; 928 break; 929 } 930 931 return ret; 932 } 933 934 935 #ifdef CONFIG_PCI 936 static int nonstatic_autoadd_resources(struct pcmcia_socket *s) 937 { 938 struct resource *res; 939 int i, done = 0; 940 941 if (!s->cb_dev || !s->cb_dev->bus) 942 return -ENODEV; 943 944 #if defined(CONFIG_X86) 945 /* If this is the root bus, the risk of hitting some strange 946 * system devices is too high: If a driver isn't loaded, the 947 * resources are not claimed; even if a driver is loaded, it 948 * may not request all resources or even the wrong one. We 949 * can neither trust the rest of the kernel nor ACPI/PNP and 950 * CRS parsing to get it right. Therefore, use several 951 * safeguards: 952 * 953 * - Do not auto-add resources if the CardBus bridge is on 954 * the PCI root bus 955 * 956 * - Avoid any I/O ports < 0x100. 957 * 958 * - On PCI-PCI bridges, only use resources which are set up 959 * exclusively for the secondary PCI bus: the risk of hitting 960 * system devices is quite low, as they usually aren't 961 * connected to the secondary PCI bus. 962 */ 963 if (s->cb_dev->bus->number == 0) 964 return -EINVAL; 965 966 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { 967 res = s->cb_dev->bus->resource[i]; 968 #else 969 pci_bus_for_each_resource(s->cb_dev->bus, res, i) { 970 #endif 971 if (!res) 972 continue; 973 974 if (res->flags & IORESOURCE_IO) { 975 /* safeguard against the root resource, where the 976 * risk of hitting any other device would be too 977 * high */ 978 if (res == &ioport_resource) 979 continue; 980 981 dev_info(&s->cb_dev->dev, 982 "pcmcia: parent PCI bridge window: %pR\n", 983 res); 984 if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end)) 985 done |= IORESOURCE_IO; 986 987 } 988 989 if (res->flags & IORESOURCE_MEM) { 990 /* safeguard against the root resource, where the 991 * risk of hitting any other device would be too 992 * high */ 993 if (res == &iomem_resource) 994 continue; 995 996 dev_info(&s->cb_dev->dev, 997 "pcmcia: parent PCI bridge window: %pR\n", 998 res); 999 if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end)) 1000 done |= IORESOURCE_MEM; 1001 } 1002 } 1003 1004 /* if we got at least one of IO, and one of MEM, we can be glad and 1005 * activate the PCMCIA subsystem */ 1006 if (done == (IORESOURCE_MEM | IORESOURCE_IO)) 1007 s->resource_setup_done = 1; 1008 1009 return 0; 1010 } 1011 1012 #else 1013 1014 static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s) 1015 { 1016 return -ENODEV; 1017 } 1018 1019 #endif 1020 1021 1022 static int nonstatic_init(struct pcmcia_socket *s) 1023 { 1024 struct socket_data *data; 1025 1026 data = kzalloc(sizeof(struct socket_data), GFP_KERNEL); 1027 if (!data) 1028 return -ENOMEM; 1029 1030 data->mem_db.next = &data->mem_db; 1031 data->mem_db_valid.next = &data->mem_db_valid; 1032 data->io_db.next = &data->io_db; 1033 1034 s->resource_data = (void *) data; 1035 1036 nonstatic_autoadd_resources(s); 1037 1038 return 0; 1039 } 1040 1041 static void nonstatic_release_resource_db(struct pcmcia_socket *s) 1042 { 1043 struct socket_data *data = s->resource_data; 1044 struct resource_map *p, *q; 1045 1046 for (p = data->mem_db_valid.next; p != &data->mem_db_valid; p = q) { 1047 q = p->next; 1048 kfree(p); 1049 } 1050 for (p = data->mem_db.next; p != &data->mem_db; p = q) { 1051 q = p->next; 1052 kfree(p); 1053 } 1054 for (p = data->io_db.next; p != &data->io_db; p = q) { 1055 q = p->next; 1056 kfree(p); 1057 } 1058 1059 kfree(data); 1060 } 1061 1062 1063 struct pccard_resource_ops pccard_nonstatic_ops = { 1064 .validate_mem = pcmcia_nonstatic_validate_mem, 1065 .find_io = nonstatic_find_io, 1066 .find_mem = nonstatic_find_mem_region, 1067 .init = nonstatic_init, 1068 .exit = nonstatic_release_resource_db, 1069 }; 1070 EXPORT_SYMBOL(pccard_nonstatic_ops); 1071 1072 1073 /* sysfs interface to the resource database */ 1074 1075 static ssize_t show_io_db(struct device *dev, 1076 struct device_attribute *attr, char *buf) 1077 { 1078 struct pcmcia_socket *s = dev_get_drvdata(dev); 1079 struct socket_data *data; 1080 struct resource_map *p; 1081 ssize_t ret = 0; 1082 1083 mutex_lock(&s->ops_mutex); 1084 data = s->resource_data; 1085 1086 for (p = data->io_db.next; p != &data->io_db; p = p->next) { 1087 if (ret > (PAGE_SIZE - 10)) 1088 continue; 1089 ret += sysfs_emit_at(buf, ret, 1090 "0x%08lx - 0x%08lx\n", 1091 ((unsigned long) p->base), 1092 ((unsigned long) p->base + p->num - 1)); 1093 } 1094 1095 mutex_unlock(&s->ops_mutex); 1096 return ret; 1097 } 1098 1099 static ssize_t store_io_db(struct device *dev, 1100 struct device_attribute *attr, 1101 const char *buf, size_t count) 1102 { 1103 struct pcmcia_socket *s = dev_get_drvdata(dev); 1104 unsigned long start_addr, end_addr; 1105 unsigned int add = ADD_MANAGED_RESOURCE; 1106 ssize_t ret = 0; 1107 1108 ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); 1109 if (ret != 2) { 1110 ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); 1111 add = REMOVE_MANAGED_RESOURCE; 1112 if (ret != 2) { 1113 ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr, 1114 &end_addr); 1115 add = ADD_MANAGED_RESOURCE; 1116 if (ret != 2) 1117 return -EINVAL; 1118 } 1119 } 1120 if (end_addr < start_addr) 1121 return -EINVAL; 1122 1123 mutex_lock(&s->ops_mutex); 1124 ret = adjust_io(s, add, start_addr, end_addr); 1125 mutex_unlock(&s->ops_mutex); 1126 1127 return ret ? ret : count; 1128 } 1129 static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db); 1130 1131 static ssize_t show_mem_db(struct device *dev, 1132 struct device_attribute *attr, char *buf) 1133 { 1134 struct pcmcia_socket *s = dev_get_drvdata(dev); 1135 struct socket_data *data; 1136 struct resource_map *p; 1137 ssize_t ret = 0; 1138 1139 mutex_lock(&s->ops_mutex); 1140 data = s->resource_data; 1141 1142 for (p = data->mem_db_valid.next; p != &data->mem_db_valid; 1143 p = p->next) { 1144 if (ret > (PAGE_SIZE - 10)) 1145 continue; 1146 ret += sysfs_emit_at(buf, ret, 1147 "0x%08lx - 0x%08lx\n", 1148 ((unsigned long) p->base), 1149 ((unsigned long) p->base + p->num - 1)); 1150 } 1151 1152 for (p = data->mem_db.next; p != &data->mem_db; p = p->next) { 1153 if (ret > (PAGE_SIZE - 10)) 1154 continue; 1155 ret += sysfs_emit_at(buf, ret, 1156 "0x%08lx - 0x%08lx\n", 1157 ((unsigned long) p->base), 1158 ((unsigned long) p->base + p->num - 1)); 1159 } 1160 1161 mutex_unlock(&s->ops_mutex); 1162 return ret; 1163 } 1164 1165 static ssize_t store_mem_db(struct device *dev, 1166 struct device_attribute *attr, 1167 const char *buf, size_t count) 1168 { 1169 struct pcmcia_socket *s = dev_get_drvdata(dev); 1170 unsigned long start_addr, end_addr; 1171 unsigned int add = ADD_MANAGED_RESOURCE; 1172 ssize_t ret = 0; 1173 1174 ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr); 1175 if (ret != 2) { 1176 ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr); 1177 add = REMOVE_MANAGED_RESOURCE; 1178 if (ret != 2) { 1179 ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr, 1180 &end_addr); 1181 add = ADD_MANAGED_RESOURCE; 1182 if (ret != 2) 1183 return -EINVAL; 1184 } 1185 } 1186 if (end_addr < start_addr) 1187 return -EINVAL; 1188 1189 mutex_lock(&s->ops_mutex); 1190 ret = adjust_memory(s, add, start_addr, end_addr); 1191 mutex_unlock(&s->ops_mutex); 1192 1193 return ret ? ret : count; 1194 } 1195 static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db); 1196 1197 static struct attribute *pccard_rsrc_attributes[] = { 1198 &dev_attr_available_resources_io.attr, 1199 &dev_attr_available_resources_mem.attr, 1200 NULL, 1201 }; 1202 1203 static const struct attribute_group rsrc_attributes = { 1204 .attrs = pccard_rsrc_attributes, 1205 }; 1206 1207 static int pccard_sysfs_add_rsrc(struct device *dev) 1208 { 1209 struct pcmcia_socket *s = dev_get_drvdata(dev); 1210 1211 if (s->resource_ops != &pccard_nonstatic_ops) 1212 return 0; 1213 return sysfs_create_group(&dev->kobj, &rsrc_attributes); 1214 } 1215 1216 static void pccard_sysfs_remove_rsrc(struct device *dev) 1217 { 1218 struct pcmcia_socket *s = dev_get_drvdata(dev); 1219 1220 if (s->resource_ops != &pccard_nonstatic_ops) 1221 return; 1222 sysfs_remove_group(&dev->kobj, &rsrc_attributes); 1223 } 1224 1225 static struct class_interface pccard_rsrc_interface __refdata = { 1226 .class = &pcmcia_socket_class, 1227 .add_dev = &pccard_sysfs_add_rsrc, 1228 .remove_dev = &pccard_sysfs_remove_rsrc, 1229 }; 1230 1231 static int __init nonstatic_sysfs_init(void) 1232 { 1233 return class_interface_register(&pccard_rsrc_interface); 1234 } 1235 1236 static void __exit nonstatic_sysfs_exit(void) 1237 { 1238 class_interface_unregister(&pccard_rsrc_interface); 1239 } 1240 1241 module_init(nonstatic_sysfs_init); 1242 module_exit(nonstatic_sysfs_exit); 1243