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